Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
0d3f636af3 | |||
a64d47c02e | |||
e6279c00f1 | |||
8d59494a75 |
4727
package-lock.json
generated
4727
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
16
package.json
16
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@pushrocks/webrequest",
|
"name": "@pushrocks/webrequest",
|
||||||
"version": "3.0.7",
|
"version": "3.0.9",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "securely request from browsers",
|
"description": "securely request from browsers",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
@ -14,21 +14,21 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.63",
|
"@gitzone/tsbuild": "^2.1.63",
|
||||||
"@gitzone/tsbundle": "^2.0.4",
|
"@gitzone/tsbundle": "^2.0.6",
|
||||||
"@gitzone/tstest": "^1.0.71",
|
"@gitzone/tstest": "^1.0.72",
|
||||||
"@pushrocks/smartexpress": "^4.0.5",
|
"@pushrocks/smartexpress": "^4.0.5",
|
||||||
"@pushrocks/tapbundle": "^5.0.3",
|
"@pushrocks/tapbundle": "^5.0.4",
|
||||||
"@types/node": "^17.0.36",
|
"@types/node": "^18.6.2",
|
||||||
"tslint": "^6.1.3",
|
"tslint": "^6.1.3",
|
||||||
"tslint-config-prettier": "^1.18.0"
|
"tslint-config-prettier": "^1.18.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartdelay": "^2.0.13",
|
"@pushrocks/smartdelay": "^2.0.13",
|
||||||
"@pushrocks/smartenv": "^5.0.2",
|
"@pushrocks/smartenv": "^5.0.2",
|
||||||
"@pushrocks/smartjson": "^4.0.6",
|
"@pushrocks/smartjson": "^5.0.1",
|
||||||
"@pushrocks/smartpromise": "^3.1.7",
|
"@pushrocks/smartpromise": "^3.1.7",
|
||||||
"@pushrocks/webstore": "^2.0.3",
|
"@pushrocks/webstore": "^2.0.4",
|
||||||
"node-fetch": "^3.2.4"
|
"node-fetch": "^3.2.9"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"ts/**/*",
|
"ts/**/*",
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@pushrocks/webrequest',
|
name: '@pushrocks/webrequest',
|
||||||
version: '3.0.7',
|
version: '3.0.9',
|
||||||
description: 'securely request from browsers'
|
description: 'securely request from browsers'
|
||||||
}
|
}
|
||||||
|
26
ts/index.ts
26
ts/index.ts
@ -1,5 +1,9 @@
|
|||||||
import * as plugins from './webrequest.plugins.js';
|
import * as plugins from './webrequest.plugins.js';
|
||||||
|
|
||||||
|
export interface IWebrequestContructorOptions {
|
||||||
|
logging?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* web request
|
* web request
|
||||||
*/
|
*/
|
||||||
@ -26,7 +30,13 @@ export class WebRequest {
|
|||||||
storeName: 'webrequest',
|
storeName: 'webrequest',
|
||||||
});
|
});
|
||||||
|
|
||||||
constructor() {
|
public options: IWebrequestContructorOptions;
|
||||||
|
|
||||||
|
constructor(public optionsArg: IWebrequestContructorOptions = {}) {
|
||||||
|
this.options = {
|
||||||
|
logging: true,
|
||||||
|
...optionsArg
|
||||||
|
}
|
||||||
WebRequest.loadNeededPolyfills();
|
WebRequest.loadNeededPolyfills();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,7 +112,7 @@ export class WebRequest {
|
|||||||
}
|
}
|
||||||
let cachedResponseDeferred = plugins.smartpromise.defer<Response>();
|
let cachedResponseDeferred = plugins.smartpromise.defer<Response>();
|
||||||
let cacheUsed = false;
|
let cacheUsed = false;
|
||||||
if (optionsArg.useCache && this.cacheStore.check(urlArg)) {
|
if (optionsArg.useCache && await this.cacheStore.check(urlArg)) {
|
||||||
const responseBuffer: ArrayBuffer = await this.cacheStore.get(urlArg);
|
const responseBuffer: ArrayBuffer = await this.cacheStore.get(urlArg);
|
||||||
cachedResponseDeferred.resolve(new Response(responseBuffer, {}));
|
cachedResponseDeferred.resolve(new Response(responseBuffer, {}));
|
||||||
} else {
|
} else {
|
||||||
@ -133,7 +143,7 @@ export class WebRequest {
|
|||||||
const buffer = await response.clone().arrayBuffer();
|
const buffer = await response.clone().arrayBuffer();
|
||||||
await this.cacheStore.set(urlArg, buffer);
|
await this.cacheStore.set(urlArg, buffer);
|
||||||
}
|
}
|
||||||
console.log(`${urlArg} answers with status: ${response.status}`);
|
this.log(`${urlArg} answers with status: ${response.status}`);
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -185,7 +195,7 @@ export class WebRequest {
|
|||||||
if (!urlToUse) {
|
if (!urlToUse) {
|
||||||
throw new Error('request failed permanently');
|
throw new Error('request failed permanently');
|
||||||
}
|
}
|
||||||
console.log(`Getting ${urlToUse} with method ${optionsArg.method}`);
|
this.log(`Getting ${urlToUse} with method ${optionsArg.method}`);
|
||||||
const response = await fetch(urlToUse, {
|
const response = await fetch(urlToUse, {
|
||||||
method: optionsArg.method,
|
method: optionsArg.method,
|
||||||
headers: {
|
headers: {
|
||||||
@ -194,7 +204,7 @@ export class WebRequest {
|
|||||||
},
|
},
|
||||||
body: optionsArg.body,
|
body: optionsArg.body,
|
||||||
});
|
});
|
||||||
console.log(`${urlToUse} answers with status: ${response.status}`);
|
this.log(`${urlToUse} answers with status: ${response.status}`);
|
||||||
|
|
||||||
if (response.status >= 200 && response.status < 300) {
|
if (response.status >= 200 && response.status < 300) {
|
||||||
return response;
|
return response;
|
||||||
@ -210,4 +220,10 @@ export class WebRequest {
|
|||||||
const finalResponse: Response = await doRequest(allUrls[usedUrlIndex]);
|
const finalResponse: Response = await doRequest(allUrls[usedUrlIndex]);
|
||||||
return finalResponse;
|
return finalResponse;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public log(logArg: string) {
|
||||||
|
if (this.options.logging) {
|
||||||
|
console.log(logArg);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
10
tsconfig.json
Normal file
10
tsconfig.json
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"experimentalDecorators": true,
|
||||||
|
"useDefineForClassFields": false,
|
||||||
|
"target": "ES2022",
|
||||||
|
"module": "ES2022",
|
||||||
|
"moduleResolution": "nodenext",
|
||||||
|
"esModuleInterop": true
|
||||||
|
}
|
||||||
|
}
|
17
tslint.json
17
tslint.json
@ -1,17 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": ["tslint:latest", "tslint-config-prettier"],
|
|
||||||
"rules": {
|
|
||||||
"semicolon": [true, "always"],
|
|
||||||
"no-console": false,
|
|
||||||
"ordered-imports": false,
|
|
||||||
"object-literal-sort-keys": false,
|
|
||||||
"member-ordering": {
|
|
||||||
"options":{
|
|
||||||
"order": [
|
|
||||||
"static-method"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"defaultSeverity": "warning"
|
|
||||||
}
|
|
Reference in New Issue
Block a user