UNPKG

blapi

Version:

BLAPI is a package to handle posting your discord stats to botlists. It's intended to be used with discord.js, though you can also manually post your stats.

46 lines 1.68 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.get = exports.post = void 0; const centra_1 = __importDefault(require("centra")); /** Custom post function based on centra */ async function post(apiPath, apiKey, sendObj, logger) { const postData = JSON.stringify(sendObj); try { const request = (0, centra_1.default)(apiPath, 'POST'); request.reqHeaders = { 'Content-Type': 'application/json', 'Content-Length': String(postData.length), Authorization: apiKey, }; const response = await request.body(postData).send(); logger.info(` posted to ${apiPath}`); logger.info(` statusCode: ${response.statusCode}`); logger.info(` headers: ${JSON.stringify(response.headers)}`); // it's text because text accepts both json and plain text, while json only supports json logger.info(` data: ${await response.text()}`); return response; } catch (e) { const error = e; logger.error(error.message); return { error }; } } exports.post = post; /** Custom get function based on centra */ async function get(url, logger) { try { const response = await (0, centra_1.default)(url, 'GET').send(); return response.json(); } catch (e) { const error = e; logger.error(error.message); throw new Error(`Request to ${url} failed with Errorcode ${error.name}`); } } exports.get = get; //# sourceMappingURL=requests.js.map