webrequest/ts/index.ts

129 lines
3.3 KiB
TypeScript
Raw Normal View History

2018-11-30 16:12:48 +00:00
import * as plugins from './webrequest.plugins';
/**
* web request
*/
export class WebRequest {
2020-06-25 22:45:19 +00:00
private static polyfillsLoaded = false;
public static loadNeededPolyfills() {
2020-06-25 23:34:59 +00:00
const smartenv = new plugins.smartenv.Smartenv();
if (!smartenv.isBrowser && !this.polyfillsLoaded) {
2020-06-25 22:45:19 +00:00
this.polyfillsLoaded = true;
2020-06-25 23:34:59 +00:00
globalThis.fetch = smartenv.getSafeNodeModule('node-fetch');
2020-06-25 22:45:19 +00:00
}
}
constructor() {
WebRequest.loadNeededPolyfills();
}
2019-09-27 12:04:52 +00:00
public async getJson(urlArg: string | string[]) {
const response: Response = await this.request(urlArg, {
method: 'GET'
});
return response.json();
}
2018-11-30 16:12:48 +00:00
/**
* postJson
2018-11-30 16:12:48 +00:00
*/
public async postJson(urlArg: string, requestBody?: any) {
const response: Response = await this.request(urlArg, {
2019-09-27 19:20:06 +00:00
body: JSON.stringify(requestBody),
2019-09-27 12:04:52 +00:00
method: 'POST'
2019-06-04 13:23:47 +00:00
});
return response.json();
2019-06-04 13:23:47 +00:00
}
2018-11-30 16:12:48 +00:00
/**
* put js
2018-11-30 16:12:48 +00:00
*/
public async putJson(urlArg: string, requestBody?: any) {
const response: Response = await this.request(urlArg, {
2019-09-27 19:20:06 +00:00
body: JSON.stringify(requestBody),
2019-09-27 12:04:52 +00:00
method: 'PUT'
});
return response.json();
}
2018-11-30 16:12:48 +00:00
/**
* put js
*/
2019-09-27 12:04:52 +00:00
public async deleteJson(urlArg: string) {
const response: Response = await this.request(urlArg, {
method: 'GET'
});
return response.json();
}
2018-11-30 16:12:48 +00:00
/**
2019-09-26 20:33:15 +00:00
* a fault tolerant request
2018-11-30 16:12:48 +00:00
*/
2019-09-26 20:33:15 +00:00
public async request(
2018-11-30 16:12:48 +00:00
urlArg: string | string[],
optionsArg: {
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
2019-02-15 11:39:29 +00:00
body?: any;
2018-11-30 16:12:48 +00:00
}
): Promise<Response> {
2018-11-30 16:12:48 +00:00
let allUrls: string[];
let usedUrlIndex = 0;
// determine what we got
if (Array.isArray(urlArg)) {
allUrls = urlArg;
} else {
allUrls = [urlArg];
}
2018-12-04 16:35:40 +00:00
const requestHistory: string[] = []; // keep track of the request history
2018-11-30 16:12:48 +00:00
const doHistoryCheck = async (
// check history for a
2018-12-04 16:35:40 +00:00
historyEntryTypeArg: string
2018-11-30 16:12:48 +00:00
) => {
requestHistory.push(historyEntryTypeArg);
2018-12-04 16:35:40 +00:00
if (historyEntryTypeArg === '429') {
console.log('got 429, so waiting a little bit.');
await plugins.smartdelay.delayFor(Math.floor(Math.random() * (2000 - 1000 + 1)) + 1000); // wait between 1 and 10 seconds
2018-12-04 16:35:40 +00:00
}
2018-11-30 16:12:48 +00:00
let numOfHistoryType = 0;
for (const entry of requestHistory) {
if (entry === historyEntryTypeArg) numOfHistoryType++;
}
if (numOfHistoryType > 2 * allUrls.length * usedUrlIndex) {
2018-11-30 16:12:48 +00:00
usedUrlIndex++;
}
};
// lets go recursive
const doRequest = async (urlToUse: string) => {
if (!urlToUse) {
throw new Error('request failed permanently');
}
2019-09-27 19:20:06 +00:00
console.log(`Getting ${urlToUse} with method ${optionsArg.method}`);
2018-11-30 16:12:48 +00:00
const response = await fetch(urlToUse, {
method: optionsArg.method,
headers: {
'Content-Type': 'application/json'
2019-02-15 11:39:29 +00:00
},
body: optionsArg.body
2018-11-30 16:12:48 +00:00
});
2018-12-04 16:35:40 +00:00
console.log(`${urlToUse} answers with status: ${response.status}`);
2018-12-03 17:35:30 +00:00
if (response.status >= 200 && response.status < 300) {
2018-12-04 16:35:40 +00:00
return response;
} else {
await doHistoryCheck(response.status.toString());
2019-06-04 13:23:47 +00:00
// tslint:disable-next-line: no-return-await
2018-11-30 16:12:48 +00:00
return await doRequest(allUrls[usedUrlIndex]);
}
};
2019-09-27 18:16:28 +00:00
const finalResponse: Response = await doRequest(allUrls[usedUrlIndex]);
2019-06-04 13:23:47 +00:00
console.log(finalResponse);
return finalResponse;
2018-11-30 16:12:48 +00:00
}
}