From 7f680dd0abbbbeab32c79dcd724f375e1351a016 Mon Sep 17 00:00:00 2001 From: cyclane Date: Sat, 3 Jul 2021 10:29:25 +0100 Subject: [PATCH] getAPIKeyInformation --- src/lib/hypixel.ts | 32 ++++++++++++++++++ src/lib/util.ts | 81 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 src/lib/hypixel.ts create mode 100644 src/lib/util.ts diff --git a/src/lib/hypixel.ts b/src/lib/hypixel.ts new file mode 100644 index 0000000..c351dc5 --- /dev/null +++ b/src/lib/hypixel.ts @@ -0,0 +1,32 @@ +import { authorize, requestWithDefaults } from "./util"; + +export type HypixelResponse = Promise< + { + _response: Response; + success: boolean; + } & T +>; + +export const ENDPOINT = "https://api.hypixel.net/"; +const request = requestWithDefaults(ENDPOINT); + +/** + * Get Hypixel API key information + * @param key API key + * @returns A response with the API key information + */ +export async function getAPIKeyInformation(key: string): HypixelResponse<{ + record?: { + key: string; + owner: string; + limit: number; + queriesInPastMin: number; + totalQueries: number; + }; +}> { + let response = await request("/key", authorize(key)); + return { + ...(await response.json()), + _response: response + }; +} diff --git a/src/lib/util.ts b/src/lib/util.ts new file mode 100644 index 0000000..07ea89c --- /dev/null +++ b/src/lib/util.ts @@ -0,0 +1,81 @@ +import { join } from "path"; + +/** + * Get a fetch function + * @returns A fetch function + */ +export function getFetch(): typeof fetch { + if (typeof fetch !== "function") { + return require("node-fetch"); + } + return fetch; +} + +export function requestWithDefaults( + baseURL: string, + defaultInit?: RequestInit +): (endpoint: string, init?: RequestInit) => Promise { + return async function ( + endpoint: string, + overrideInit?: RequestInit + ): Promise { + let response = await getFetch()( + join(baseURL, endpoint), + defaultInit ? deepMerge(defaultInit, overrideInit) : overrideInit + ); + if (response.ok) { + return response; + } + throw { + ...new Error("non-200 status code"), + response + }; + }; +} + +/** + * Generate RequestInit for Hypixel authorization + * @param key The API key + * @returns The RequestInit + */ +export function authorize(key: string): RequestInit { + return { + headers: { + "API-Key": key + } + }; +} + +/** + * Simple object check + * @param item item to check + * @returns Whether item is an object + */ +export function isObject(item: any): boolean { + return item && typeof item === "object" && !Array.isArray(item); +} + +/** + * Deep merge two objects + * @param target Target (base object) + * @param sources Objects to merge into target (in order) + * @returns Deep merged object + */ +export function deepMerge(target: T, ...sources: T[]): T { + if (!sources.length) return target; + const source = sources.shift(); + + // Two separate checks for source to make typescript chill + if (isObject(target) && isObject(source) && source) { + for (const key in source) { + if (isObject(source[key])) { + if (!target[key]) Object.assign(target, { [key]: {} }); + deepMerge(target[key], source[key]); + } else { + Object.assign(target, { [key]: source[key] }); + } + } + } + + return deepMerge(target, ...sources); +}