aboutsummaryrefslogtreecommitdiff
path: root/build/hypixelApi.js
diff options
context:
space:
mode:
Diffstat (limited to 'build/hypixelApi.js')
-rw-r--r--build/hypixelApi.js45
1 files changed, 18 insertions, 27 deletions
diff --git a/build/hypixelApi.js b/build/hypixelApi.js
index a35dd64..da1be16 100644
--- a/build/hypixelApi.js
+++ b/build/hypixelApi.js
@@ -1,33 +1,26 @@
-"use strict";
-var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
-};
-var _a, _b, _c;
-Object.defineProperty(exports, "__esModule", { value: true });
-exports.sendApiRequest = exports.getKeyUsage = exports.chooseApiKey = void 0;
/**
* Fetch the raw Hypixel API
*/
-const node_fetch_1 = __importDefault(require("node-fetch"));
-const util_1 = require("./util");
-const https_1 = require("https");
+import fetch from 'node-fetch';
+import { jsonToQuery, shuffle } from './util.js';
+import { Agent } from 'https';
if (!process.env.hypixel_keys)
// if there's no hypixel keys in env, run dotenv
- require('dotenv').config();
+ (await import('dotenv')).config();
// We need to create an agent to prevent memory leaks and to only do dns lookups once
-const httpsAgent = new https_1.Agent({
+const httpsAgent = new Agent({
keepAlive: true
});
/** This array should only ever contain one item because using multiple hypixel api keys isn't allowed :) */
-const apiKeys = (_c = (_b = (_a = process.env) === null || _a === void 0 ? void 0 : _a.hypixel_keys) === null || _b === void 0 ? void 0 : _b.split(' ')) !== null && _c !== void 0 ? _c : [];
+const apiKeys = process.env?.hypixel_keys?.split(' ') ?? [];
const apiKeyUsage = {};
const baseHypixelAPI = 'https://api.hypixel.net';
/** Choose the best current API key */
-function chooseApiKey() {
+export function chooseApiKey() {
// find the api key with the lowest amount of uses
let bestKeyUsage = null;
let bestKey = null;
- for (let key of util_1.shuffle(apiKeys.slice())) {
+ for (let key of shuffle(apiKeys.slice())) {
const keyUsage = apiKeyUsage[key];
// if the key has never been used before, use it
if (!keyUsage)
@@ -43,8 +36,7 @@ function chooseApiKey() {
}
return bestKey;
}
-exports.chooseApiKey = chooseApiKey;
-function getKeyUsage() {
+export function getKeyUsage() {
let keyLimit = 0;
let keyUsage = 0;
for (let key of Object.values(apiKeyUsage)) {
@@ -56,20 +48,18 @@ function getKeyUsage() {
usage: keyUsage
};
}
-exports.getKeyUsage = getKeyUsage;
/** Send an HTTP request to the Hypixel API */
-async function sendApiRequest({ path, key, args }) {
+export let sendApiRequest = async function sendApiRequest({ path, key, args }) {
// Send a raw http request to api.hypixel.net, and return the parsed json
- var _a, _b, _c;
if (key)
// If there's an api key, add it to the arguments
args.key = key;
// Construct a url from the base api url, path, and arguments
- const fetchUrl = baseHypixelAPI + '/' + path + '?' + util_1.jsonToQuery(args);
+ const fetchUrl = baseHypixelAPI + '/' + path + '?' + jsonToQuery(args);
let fetchResponse;
let fetchJsonParsed;
try {
- fetchResponse = await node_fetch_1.default(fetchUrl, { agent: () => httpsAgent });
+ fetchResponse = await fetch(fetchUrl, { agent: () => httpsAgent });
fetchJsonParsed = await fetchResponse.json();
}
catch {
@@ -85,9 +75,9 @@ async function sendApiRequest({ path, key, args }) {
if (fetchResponse.headers.get('ratelimit-limit'))
// remember how many uses it has
apiKeyUsage[key] = {
- remaining: parseInt((_a = fetchResponse.headers.get('ratelimit-remaining')) !== null && _a !== void 0 ? _a : '0'),
- limit: parseInt((_b = fetchResponse.headers.get('ratelimit-limit')) !== null && _b !== void 0 ? _b : '0'),
- reset: Date.now() + parseInt((_c = fetchResponse.headers.get('ratelimit-reset')) !== null && _c !== void 0 ? _c : '0') * 1000
+ remaining: parseInt(fetchResponse.headers.get('ratelimit-remaining') ?? '0'),
+ limit: parseInt(fetchResponse.headers.get('ratelimit-limit') ?? '0'),
+ reset: Date.now() + parseInt(fetchResponse.headers.get('ratelimit-reset') ?? '0') * 1000
};
if (fetchJsonParsed.throttle) {
if (apiKeyUsage[key])
@@ -97,5 +87,6 @@ async function sendApiRequest({ path, key, args }) {
return await sendApiRequest({ path, key, args });
}
return fetchJsonParsed;
-}
-exports.sendApiRequest = sendApiRequest;
+};
+// this is necessary for mocking in the tests because es6
+export function mockSendApiRequest($value) { sendApiRequest = $value; }