UNPKG

dblstatistics.js

Version:

Official node module for interacting with the dblstats.com API

121 lines (120 loc) 4.33 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Client = void 0; const package_json_1 = __importDefault(require("../package.json")); const querystring_1 = __importDefault(require("querystring")); const node_fetch_1 = __importStar(require("node-fetch")); require("./typings"); /** * dblstats API Client */ class Client { /** * Creates a new DBL Stats Client intstance * @param token Your dblstats.com token */ constructor(token) { this.token = token; } async _request(method, path, body) { var _a; const headers = new node_fetch_1.Headers(); headers.set('Authorization', this.token); headers.set('User-Agent', `DBLStats.js / ${package_json_1.default.version}`); if (method !== 'GET') headers.set('Content-Type', 'application/json'); let url = `https://dblstatistics.com/api/${path}`; // @ts-ignore querystring typings are messed if (body && method === 'GET') url += `?${querystring_1.default.stringify(body)}`; const response = await node_fetch_1.default(url, { method, headers, body: body && method !== 'GET' ? JSON.stringify(body) : null }); let responseBody; if ((_a = response.headers.get('Content-Type')) === null || _a === void 0 ? void 0 : _a.startsWith('application/json')) { responseBody = await response.json(); } else { responseBody = await response.text(); } if (!response.ok) throw new Error(`DBLStats in request ${response.status} : ${response.statusText}`); return responseBody; } /** * Gets a bots current information * @param id ID of the bot * @returns Bot information */ async getBot(id) { if (!id) throw new Error('Missing ID'); return this._request('GET', `/bots/${id}`); } /** * Fetch previous bot data, each packet is an hour apart * @param id ID of the bot * @param limit Amount of responses (default = 500) * @returns An array of bot information an hour apart */ async getBotHistory(id, limit) { if (!id) throw new Error('Missing ID'); return this._request('GET', `/bots/${id}/previous`, { limit }).then(x => x.data); } /** * Get the top bots for a specific metric * @param by For which metric * @param limit Amount of responses * @returns Array of top bots for that metric */ async getTop(by, limit) { if (!by) throw new Error('Missing sortBy'); return this._request('GET', '/bots/top', { sortby: by, limit }); } /** * Get the bots that a user owns * @param id The User's ID * @return An array of bots the user is owner on */ async getUsersBots(id) { if (!id) throw new Error('Missing ID'); return this._request('GET', `/users/${id}/bots`); } /** * Get all auction tags * @returns Strings of tags, response.bot is bot tags and response.server is server tags */ async getTags() { return this._request('GET', '/auctions/tags'); } } exports.Client = Client; module.exports = Client; exports.default = Client;