getAPIKeyInformation
This commit is contained in:
parent
7f0ec23201
commit
7f680dd0ab
|
@ -0,0 +1,32 @@
|
|||
import { authorize, requestWithDefaults } from "./util";
|
||||
|
||||
export type HypixelResponse<T> = 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
|
||||
};
|
||||
}
|
|
@ -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<Response> {
|
||||
return async function (
|
||||
endpoint: string,
|
||||
overrideInit?: RequestInit
|
||||
): Promise<Response> {
|
||||
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<T>(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);
|
||||
}
|
Loading…
Reference in New Issue