@vermaysha/discord-webhook
Version:
Discord Webhook built using TypeScript which supports Browser and Node
51 lines (50 loc) • 1.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Request = exports.delay = void 0;
const tslib_1 = require("tslib");
const axios_1 = require("axios");
function delay(second) {
return new Promise((resolve) => {
setTimeout(resolve, second * 1000);
});
}
exports.delay = delay;
class Request {
constructor(client) {
this.client = client;
this.retries = 1;
}
send(method = 'GET', data) {
var _a, _b, _c;
return tslib_1.__awaiter(this, void 0, void 0, function* () {
try {
const request = yield this.client.request({
method,
data,
});
const result = request.data;
if (result.status === 429 && this.retries <= 60) {
this.retries++;
yield delay(parseInt((_c = (_b = (_a = request.headers) === null || _a === void 0 ? void 0 : _a['x-ratelimit-reset-after']) !== null && _b !== void 0 ? _b : result.retry_after) !== null && _c !== void 0 ? _c : 3, 10));
return this.send(method, data);
}
this.retries = 1;
return result;
}
catch (error) {
if (error instanceof axios_1.AxiosError) {
throw new Error(`Request Error: [${error.code}] - ${error.message}`);
}
else {
return {
retcode: -9999,
message: '',
data: null,
error: JSON.stringify(error),
};
}
}
});
}
}
exports.Request = Request;