pterowrap
Version:
A node.js wrapper for Pterodactyl API
59 lines (58 loc) • 2.26 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const axios_1 = __importDefault(require("axios"));
const Util_1 = __importDefault(require("../utils/Util"));
class BaseInstance {
constructor(url, api_key, instance_type) {
this.url = url;
this.api_key = api_key;
this.instance_type = instance_type;
if (!url)
throw new Error("Url is undefined!");
else if (!api_key)
throw new Error("Api Key is undefined!");
this.url = Util_1.default.formatURL(url);
this.api_key = api_key;
this.headers = {
Authorization: `Bearer ${this.api_key}`,
"Content-Type": "application/json",
Accept: "application/json",
};
}
call(options) {
const { endpoint, parameters, method, body } = this.formatOptions(options);
const params = Util_1.default.formatParams(parameters);
return new Promise(async (resolve, reject) => {
try {
const call = this.url + `/${this.instance_type}/` + endpoint + params;
let return_data = null;
const m = method === null || method === void 0 ? void 0 : method.toLowerCase();
const f = axios_1.default[m];
if (m === "get" || m === "delete")
return_data = (await f(call, { headers: this.headers })).data;
else
return_data = (await f(call, body, { headers: this.headers })).data;
resolve(return_data);
}
catch (err) {
reject(err.response.data.errors);
}
});
}
formatOptions(options) {
let { endpoint, parameters, method, body } = options;
if (!endpoint)
endpoint = "/";
if (!parameters)
parameters = {};
if (!method)
method = "GET";
if (!body)
body = {};
return { endpoint, parameters, method, body };
}
}
exports.default = BaseInstance;