@0xtld/tair-node
Version:
A Node.js package for Tair functionality with configuration, core, and helper modules.
78 lines (77 loc) • 3.09 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AxiosHelper = exports.AsyncHelper = void 0;
const tslib_1 = require("tslib");
const axios_1 = tslib_1.__importDefault(require("axios"));
const constant_1 = require("../config/constant");
const config_1 = require("../config");
class AsyncHelper {
static async retryOperation(operation, configs = {
maxAttempts: constant_1.CONSTANTS.RETRY_ATTEMPTS, delay: constant_1.CONSTANTS.RETRY_DELAY
}) {
const { maxAttempts, delay } = configs;
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
return await operation();
}
catch (error) {
if (attempt === maxAttempts)
throw error;
config_1.logger.warn(`Retry ${operation.name} failed attempt ${attempt} of ${maxAttempts}. Retrying.... in ${attempt} seconds`);
await new Promise(resolve => setTimeout(resolve, delay * attempt));
}
}
throw new Error(`Retry operation failed after ${maxAttempts} attempts`);
}
}
exports.AsyncHelper = AsyncHelper;
class AxiosHelper {
static headers(configs) {
let headers = {
accept: "*/*",
"accept-encoding": "gzip, deflate, br, zstd",
"accept-language": "en-US;q=0.6,en;q=0.5",
"content-type": "application/json",
"sec-ch-ua": '"Chromium";v="130", "Google Chrome";v="130", "Not?A_Brand";v="99""',
"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/130.0.0.0 Safari/537.36",
};
if (typeof configs === "object") {
headers = Object.assign(Object.assign({}, headers), configs);
}
else if (typeof configs === "string") {
headers["origin"] = `${configs}`;
headers["referer"] = `${configs}/`;
}
return headers;
}
static createInstance(axiosConfigs, configs = {
isCustomInterceptors: false,
origin: null,
}) {
const instance = axios_1.default.create(axiosConfigs);
const { isCustomInterceptors, origin } = configs;
if (!isCustomInterceptors) {
instance.interceptors.request.use((config) => {
if (origin) {
config.headers = AxiosHelper.headers(origin);
}
return config;
}, (error) => {
return Promise.reject(error);
});
instance.interceptors.response.use((response) => {
return response.data;
}, (error) => {
return Promise.reject(error);
});
}
// TODO: Add handle custom configurations here
return instance;
}
}
exports.AxiosHelper = AxiosHelper;