newmax-utils
Version:
Utils & Libs for Newmax Tech
115 lines (114 loc) • 4.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.FetchRequest = void 0;
const delay_1 = require("./delay");
class FetchRequest {
commonHeaders;
errorStatus;
constructor() {
this.commonHeaders = {
Accept: '*/*',
'Content-Type': 'application/json',
'Accept-Language': 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7',
'Sec-Ch-Ua': '"Google Chrome";v="113", "Chromium";v="113", "Not-A.Brand";v="24"',
'Sec-Ch-Ua-Mobile': '?0',
'Sec-Ch-Ua-Platform': '"Windows"',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36',
};
this.errorStatus = {};
}
fetchData = async ({ url, method = 'GET', body = {}, headersOptions = {}, options = {}, }) => {
let response;
let data;
let text;
let headers;
const reqParams = {
method,
headers: { ...this.commonHeaders, ...headersOptions },
redirect: 'manual',
...options,
};
if (['POST', 'UPDATE', 'PUT', 'PATCH'].includes(method)) {
reqParams.body = typeof body === 'string' ? body : JSON.stringify(body);
}
try {
response = await fetch(url, reqParams);
headers = response.headers;
}
catch (e) {
const error = e;
return {
error: { status: 505, message: error.code || error.cause?.code || 'UNKNOWN FETCH ERROR' },
};
}
try {
text = await response.text();
}
catch (e) {
text = 'Response is not valid to TEXT';
}
try {
data = JSON.parse(text);
}
catch (e) {
data = { message: 'Response is not valid to JSON' };
}
if (!response.ok || response.status === 204) {
return {
data,
text,
headers,
error: { status: response.status, message: response.statusText },
};
}
return { data, text, headers };
};
fetchErrorLoop = async (caller, params, company, errStatusBreakList = []) => {
errStatusBreakList = [...errStatusBreakList, 429, 500, 502, 503, 504, 505, 408];
this.errorStatus[company] = { code: 0, count: 0 };
let delayCount = 5;
let retryCount = 0;
do {
const { data, text, headers, error } = await this.fetchData(params);
if (error && error.status === 429 && headers) {
delayCount = Number(headers.get('x-ratelimit-retry')) || 15;
console.log('\x1b[34m%s\x1b[0m', `${caller}`, `delay ${delayCount} | ${company}`);
retryCount++;
if (retryCount > 30) {
return { data, text, headers, error };
}
await (0, delay_1.delay)(delayCount + 5);
continue;
}
else if (error &&
errStatusBreakList.includes(error.status) &&
this.errorStatus[company].count < 5) {
if (error.status !== 429)
delayCount = 5;
if (this.errorStatus[company].code === error.status) {
this.errorStatus[company].count++;
continue;
}
console.log('\x1b[31m%s\x1b[0m', `[FETCH ERROR: [${caller}]]:`, `${error.status} - ${error.message} | ${company}`);
this.errorStatus[company].code = error.status;
this.errorStatus[company].count = 1;
continue;
}
if (error && this.errorStatus[company].count >= 5) {
console.log('\x1b[31m%s\x1b[0m', `[STOP LOOP [${caller}]]:`, `${error.status} - ${error.message} | ${company}`);
return { data, text, headers, error };
}
this.errorStatus[company] = { code: 0, count: 0 };
retryCount = 0;
if (headers && Number(headers.get('x-ratelimit-remaining')) === 0) {
delayCount = Number(headers.get('x-ratelimit-retry')) || 15;
await (0, delay_1.delay)(delayCount + 5);
}
return { data, text, headers, error };
} while (true);
};
}
exports.FetchRequest = FetchRequest;