BREAKING CHANGE(core): switch to support binary files in future versions
This commit is contained in:
53
ts/index.ts
53
ts/index.ts
@ -4,26 +4,46 @@ import * as plugins from './webrequest.plugins';
|
||||
* web request
|
||||
*/
|
||||
export class WebRequest {
|
||||
/**
|
||||
* gets json
|
||||
*/
|
||||
public async getJson(urlArg: string | string[], requestBody?) {
|
||||
const response = await this.request(urlArg, {
|
||||
public async getJson(urlArg: string | string[], requestBody?: any) {
|
||||
const response: Response = await this.request(urlArg, {
|
||||
body: requestBody,
|
||||
method: 'GET'
|
||||
});
|
||||
|
||||
return response.json();
|
||||
}
|
||||
|
||||
/**
|
||||
* postJson
|
||||
*/
|
||||
postJson() {}
|
||||
public async postJson(urlArg: string, requestBody?: any) {
|
||||
const response: Response = await this.request(urlArg, {
|
||||
body: requestBody,
|
||||
method: 'GET'
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
/**
|
||||
* put js
|
||||
*/
|
||||
putJson() {}
|
||||
public async putJson(urlArg: string, requestBody?: any) {
|
||||
const response: Response = await this.request(urlArg, {
|
||||
body: requestBody,
|
||||
method: 'GET'
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
/**
|
||||
* put js
|
||||
*/
|
||||
public async deleteJson(urlArg: string, requestBody?: any) {
|
||||
const response: Response = await this.request(urlArg, {
|
||||
body: requestBody,
|
||||
method: 'GET'
|
||||
});
|
||||
return response.json();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
@ -34,7 +54,7 @@ export class WebRequest {
|
||||
method: 'GET' | 'POST' | 'PUT' | 'DELETE';
|
||||
body?: any;
|
||||
}
|
||||
) {
|
||||
): Promise<Response> {
|
||||
let allUrls: string[];
|
||||
let usedUrlIndex = 0;
|
||||
|
||||
@ -47,22 +67,21 @@ export class WebRequest {
|
||||
|
||||
const requestHistory: string[] = []; // keep track of the request history
|
||||
|
||||
const doHistoryCheck = async ( // check history for a
|
||||
const doHistoryCheck = async (
|
||||
// check history for a
|
||||
historyEntryTypeArg: string
|
||||
) => {
|
||||
requestHistory.push(historyEntryTypeArg);
|
||||
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
|
||||
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
|
||||
}
|
||||
|
||||
let numOfHistoryType = 0;
|
||||
for (const entry of requestHistory) {
|
||||
if (entry === historyEntryTypeArg) numOfHistoryType++;
|
||||
}
|
||||
if (numOfHistoryType > (2 * allUrls.length * usedUrlIndex)) {
|
||||
if (numOfHistoryType > 2 * allUrls.length * usedUrlIndex) {
|
||||
usedUrlIndex++;
|
||||
}
|
||||
};
|
||||
@ -72,7 +91,7 @@ export class WebRequest {
|
||||
if (!urlToUse) {
|
||||
throw new Error('request failed permanently');
|
||||
}
|
||||
|
||||
|
||||
const response = await fetch(urlToUse, {
|
||||
method: optionsArg.method,
|
||||
headers: {
|
||||
@ -93,6 +112,6 @@ export class WebRequest {
|
||||
|
||||
const finalResponse: Response = await doRequest(urlArg[usedUrlIndex]);
|
||||
console.log(finalResponse);
|
||||
return JSON.parse(await finalResponse.text());
|
||||
return finalResponse;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,3 @@
|
||||
import * as smartdelay from '@pushrocks/smartdelay';
|
||||
|
||||
export {
|
||||
smartdelay
|
||||
}
|
||||
export { smartdelay };
|
||||
|
Reference in New Issue
Block a user