@evolvejs/core
Version:
An advanced Discord API wrapper with TS and JS support
193 lines (192 loc) • 8.02 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RestAPIHandler = void 0;
const node_fetch_1 = __importDefault(require("node-fetch"));
const __1 = require("../..");
const util_1 = require("util");
const DiscordRejection_1 = __importDefault(require("./DiscordRejection"));
const __2 = require("../..");
const AsyncronousQueue_1 = require("../../Utils/AsyncronousQueue");
/**
* RestAPIHandler
* A promise based queued rest api handler
* @param client Your EvolveClient
* @param endpoint The endpoint from which to fetch
*/
class RestAPIHandler {
constructor(client, endpoint) {
this._cooldown = 0;
Object.defineProperty(this, "_client", {
value: client,
enumerable: false,
writable: false,
configurable: false,
});
Object.defineProperty(this, "_endpoint", {
value: endpoint,
enumerable: false,
writable: false,
configurable: false,
});
Object.defineProperty(this, "_queue", {
value: new AsyncronousQueue_1.AsyncronousQueue(),
enumerable: false,
writable: false,
configurable: false,
});
}
get active() {
return this._queue.resolved;
}
get(id) {
return __awaiter(this, void 0, void 0, function* () {
let endpoint = this._endpoint;
if (id)
endpoint = endpoint.replace(":id", id);
if (!id && endpoint.includes(":id"))
throw this._client.transformer.error(`Id parameter is required as the ${this._endpoint} has a ':id' which needs to be replaced...`);
return this._fetch({ endpoint, method: "GET", json_params: undefined });
});
}
put(json, id) {
return __awaiter(this, void 0, void 0, function* () {
let endpoint = this._endpoint;
if (id)
endpoint = endpoint.replace(":id", id);
if (!id && endpoint.includes(":id"))
throw this._client.transformer.error(`Id parameter is required as the ${this._endpoint} has a ':id' which needs to be replaced...`);
return this._fetch({
endpoint,
method: "PUT",
json_params: typeof json == "object" ? JSON.stringify(json) : json,
});
});
}
delete(id) {
return __awaiter(this, void 0, void 0, function* () {
let endpoint = this._endpoint;
if (id)
endpoint = endpoint.replace(":id", id);
if (!id && endpoint.includes(":id"))
throw this._client.transformer.error(`Id parameter is required as the ${this._endpoint} has a ':id' which needs to be replaced...`);
return this._fetch({ endpoint, method: "DELETE", json_params: undefined });
});
}
post(json, id) {
return __awaiter(this, void 0, void 0, function* () {
let endpoint = this._endpoint;
if (id)
endpoint = endpoint.replace(":id", id);
if (!id && endpoint.includes(":id"))
throw this._client.transformer.error(`Id parameter is required as the ${this._endpoint} has a ':id' which needs to be replaced...`);
return this._fetch({
endpoint,
method: "POST",
json_params: typeof json == "object" ? JSON.stringify(json) : json,
});
});
}
patch(json, id) {
return __awaiter(this, void 0, void 0, function* () {
let endpoint = this._endpoint;
if (id)
endpoint = endpoint.replace(":id", id);
if (!id && endpoint.includes(":id"))
throw this._client.transformer.error(`Id parameter is required as the ${this._endpoint} has a ':id' which needs to be replaced...`);
return this._fetch({
endpoint,
method: "PATCH",
json_params: typeof json == "object" ? JSON.stringify(json) : json,
});
});
}
_fetch(options) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
yield this._queue.delay();
const whileExectued = RestAPIHandler.globalTimeout;
yield util_1.promisify(setTimeout)(whileExectued);
RestAPIHandler.globalTimeout -= whileExectued;
try {
yield util_1.promisify(setTimeout)(this._cooldown);
this._cooldown = 1;
const res = yield node_fetch_1.default(`${__1.CONSTANTS.Api}${options.endpoint}`, {
method: options.method,
headers: {
"Content-Type": "application/json",
Authorization: `Bot ${this._client.token}`,
},
body: options.json_params,
});
const json = yield res.json();
if (res.headers) {
const resetAfter = Number((_a = res.headers.get("x-ratelimit-reset-after")) !== null && _a !== void 0 ? _a : json.retry_after) * 1000;
if (this._cooldown !== 0) {
this._cooldown += resetAfter;
}
else
this._cooldown = resetAfter;
}
if (res.status === 429) {
if (json.global)
RestAPIHandler.globalTimeout += this._cooldown;
yield util_1.promisify(setTimeout)(this._cooldown);
return this._fetch(options);
}
if (!res.ok) {
const rejection = new DiscordRejection_1.default({
code: json.code,
msg: this._client.transformer.error(json.message),
http: res.status,
path: options.endpoint,
});
if (this._client.listenerCount(__2.EVENTS.API_ERROR) < 1) {
throw rejection;
}
else
throw this._client.emit(__2.EVENTS.API_ERROR, rejection);
}
return json;
}
catch (e) {
if (this._client.listenerCount(__2.EVENTS.API_ERROR) < 1)
throw this._client.transformer.error(e);
else
throw this._client.emit(__2.EVENTS.API_ERROR, e);
}
finally {
this._cooldown = 1;
this._queue.dequeue();
}
});
}
dequeueAll() {
try {
for (let i = 0; i < this._queue.notResolved; i++) {
try {
this._queue.dequeue();
}
catch (e) {
throw new Error(e);
}
}
}
catch (e) {
throw this._client.transformer.error(e);
}
}
}
exports.RestAPIHandler = RestAPIHandler;
RestAPIHandler.globalTimeout = 1;