Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
547b151ee2 | |||
9855311739 | |||
1453abcd5f | |||
99afb573cd | |||
94360ff1da | |||
3fd2075c26 | |||
71782672d7 | |||
9dec755ea5 |
7663
package-lock.json
generated
7663
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
22
package.json
22
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@pushrocks/webrequest",
|
||||
"version": "2.0.7",
|
||||
"version": "2.0.11",
|
||||
"private": false,
|
||||
"description": "securely request from browsers",
|
||||
"main": "dist_ts/index.js",
|
||||
@ -9,21 +9,24 @@
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"test": "(tstest test/ --web)",
|
||||
"build": "(tsbuild --web)",
|
||||
"build": "(tsbuild --web && tsbundle npm)",
|
||||
"format": "(gitzone format)"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@gitzone/tsbuild": "^2.1.24",
|
||||
"@gitzone/tstest": "^1.0.33",
|
||||
"@pushrocks/smartexpress": "^3.0.72",
|
||||
"@pushrocks/tapbundle": "^3.2.1",
|
||||
"@types/node": "^14.0.14",
|
||||
"node-fetch": "^2.6.0",
|
||||
"@gitzone/tsbundle": "^1.0.72",
|
||||
"@gitzone/tstest": "^1.0.38",
|
||||
"@pushrocks/smartexpress": "^3.0.73",
|
||||
"@pushrocks/tapbundle": "^3.2.7",
|
||||
"@types/node": "^14.0.19",
|
||||
"tslint": "^6.1.2",
|
||||
"tslint-config-prettier": "^1.18.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@pushrocks/smartdelay": "^2.0.9"
|
||||
"@pushrocks/smartdelay": "^2.0.9",
|
||||
"@pushrocks/smartenv": "^4.0.10",
|
||||
"@pushrocks/smartjson": "^4.0.5",
|
||||
"node-fetch": "^2.6.0"
|
||||
},
|
||||
"files": [
|
||||
"ts/**/*",
|
||||
@ -36,5 +39,8 @@
|
||||
"cli.js",
|
||||
"npmextra.json",
|
||||
"readme.md"
|
||||
],
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
]
|
||||
}
|
||||
|
@ -27,7 +27,6 @@ Platform support | [ or [contribute monthly](https://lossless.link/contribute). :)
|
||||
|
16
test/test.browser.ts
Normal file
16
test/test.browser.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { expect, tap } from '@pushrocks/tapbundle';
|
||||
import * as webrequest from '../ts/index';
|
||||
|
||||
tap.test('first test', async tools => {
|
||||
const done = tools.defer();
|
||||
const response = await new webrequest.WebRequest().request([
|
||||
'https://lossless.com'
|
||||
], {
|
||||
method: 'GET'
|
||||
}).catch(e => {
|
||||
done.resolve();
|
||||
});
|
||||
await done.promise;
|
||||
});
|
||||
|
||||
tap.start();
|
28
ts/index.ts
28
ts/index.ts
@ -6,11 +6,10 @@ import * as plugins from './webrequest.plugins';
|
||||
export class WebRequest {
|
||||
private static polyfillsLoaded = false;
|
||||
public static loadNeededPolyfills() {
|
||||
if (!this.polyfillsLoaded) {
|
||||
const smartenv = new plugins.smartenv.Smartenv();
|
||||
if (!smartenv.isBrowser && !this.polyfillsLoaded) {
|
||||
this.polyfillsLoaded = true;
|
||||
// tslint:disable-next-line: no-eval
|
||||
const loadFetchPolyfill = eval(`globalThis.fetch = require('node-fetch')`);
|
||||
loadFetchPolyfill();
|
||||
globalThis.fetch = smartenv.getSafeNodeModule('node-fetch');
|
||||
}
|
||||
}
|
||||
|
||||
@ -22,7 +21,9 @@ export class WebRequest {
|
||||
const response: Response = await this.request(urlArg, {
|
||||
method: 'GET'
|
||||
});
|
||||
return response.json();
|
||||
const responseText = await response.text();
|
||||
const responseResult = plugins.smartjson.parse(responseText);
|
||||
return responseResult;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -30,10 +31,12 @@ export class WebRequest {
|
||||
*/
|
||||
public async postJson(urlArg: string, requestBody?: any) {
|
||||
const response: Response = await this.request(urlArg, {
|
||||
body: JSON.stringify(requestBody),
|
||||
body: plugins.smartjson.stringify(requestBody),
|
||||
method: 'POST'
|
||||
});
|
||||
return response.json();
|
||||
const responseText = await response.text();
|
||||
const responseResult = plugins.smartjson.parse(responseText);
|
||||
return responseResult;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -41,10 +44,12 @@ export class WebRequest {
|
||||
*/
|
||||
public async putJson(urlArg: string, requestBody?: any) {
|
||||
const response: Response = await this.request(urlArg, {
|
||||
body: JSON.stringify(requestBody),
|
||||
body: plugins.smartjson.stringify(requestBody),
|
||||
method: 'PUT'
|
||||
});
|
||||
return response.json();
|
||||
const responseText = await response.text();
|
||||
const responseResult = plugins.smartjson.parse(responseText);
|
||||
return responseResult;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -54,7 +59,9 @@ export class WebRequest {
|
||||
const response: Response = await this.request(urlArg, {
|
||||
method: 'GET'
|
||||
});
|
||||
return response.json();
|
||||
const responseText = await response.text();
|
||||
const responseResult = plugins.smartjson.parse(responseText);
|
||||
return responseResult;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -123,7 +130,6 @@ export class WebRequest {
|
||||
};
|
||||
|
||||
const finalResponse: Response = await doRequest(allUrls[usedUrlIndex]);
|
||||
console.log(finalResponse);
|
||||
return finalResponse;
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
import * as smartdelay from '@pushrocks/smartdelay';
|
||||
import * as smartenv from '@pushrocks/smartenv';
|
||||
import * as smartjson from '@pushrocks/smartjson';
|
||||
|
||||
export { smartdelay };
|
||||
export { smartdelay, smartenv, smartjson };
|
||||
|
Reference in New Issue
Block a user