From 50b5c8ba4b98a60a3d4fee4d76758a0a10801fc8 Mon Sep 17 00:00:00 2001 From: Rainnny7 Date: Sun, 14 Apr 2024 16:29:29 -0400 Subject: [PATCH] Cleanup the lib --- Lib/src/lib/restfulmc.ts | 78 ++++++++------------------------------- Lib/src/lib/webRequest.ts | 22 +++++++++++ 2 files changed, 37 insertions(+), 63 deletions(-) create mode 100644 Lib/src/lib/webRequest.ts diff --git a/Lib/src/lib/restfulmc.ts b/Lib/src/lib/restfulmc.ts index b349492..5ffba48 100644 --- a/Lib/src/lib/restfulmc.ts +++ b/Lib/src/lib/restfulmc.ts @@ -1,10 +1,8 @@ -import { ErrorResponse } from "../types/generic"; import type { CachedPlayer } from "../types/player"; import { Platform } from "../types/server"; import { CachedBedrockMinecraftServer } from "../types/server/bedrock-server"; import { CachedJavaMinecraftServer } from "../types/server/java-server"; - -const ENDPOINT = "https://mc.rainnny.club"; // The API endpoint to use +import { makeWebRequest } from "./webRequest"; /** * Get a player by their username or UUID. @@ -12,19 +10,8 @@ const ENDPOINT = "https://mc.rainnny.club"; // The API endpoint to use * @param query the query to search for the player by * @returns the promised player */ -export const getPlayer = (query: string): Promise => { - return new Promise(async (resolve, reject) => { - const response: Response = await fetch(`${ENDPOINT}/player/${query}`); // Request the player - const json: any = await response.json(); - - // Resolve the player - if (response.ok) { - resolve(json as CachedPlayer); - } else { - reject(json as ErrorResponse); // The request failed - } - }); -}; +export const getPlayer = (query: string): Promise => + makeWebRequest(`/player/${query}`); /** * Get a Minecraft server by its platform and hostname. @@ -36,25 +23,14 @@ export const getPlayer = (query: string): Promise => { export const getMinecraftServer = ( platform: Platform, hostname: string -): Promise => { - return new Promise(async (resolve, reject) => { - const response: Response = await fetch( - `${ENDPOINT}/server/${platform}/${hostname}` - ); // Request the server - const json: any = await response.json(); - - // Resolve the server - if (response.ok) { - resolve( - platform === "java" - ? (json as CachedJavaMinecraftServer) - : (json as CachedBedrockMinecraftServer) - ); - } else { - reject(json as ErrorResponse); // The request failed - } - }); -}; +): Promise => + platform === "java" + ? makeWebRequest( + `/server/${platform}/${hostname}` + ) + : makeWebRequest( + `/server/${platform}/${hostname}` + ); /** * Check if the server with the @@ -63,37 +39,13 @@ export const getMinecraftServer = ( * @param hostname the server hostname to check * @returns the promised blocked status */ -export const isMojangBlocked = (hostname: string): Promise => { - return new Promise(async (resolve, reject) => { - const response: Response = await fetch( - `${ENDPOINT}/server/blocked/${hostname}` - ); // Request the server's blocked status - const json: any = await response.json(); - - // Resolve the blocked status - if (response.ok) { - resolve(json.blocked as boolean); - } else { - reject(json as ErrorResponse); // The request failed - } - }); -}; +export const isMojangBlocked = (hostname: string): Promise => + makeWebRequest(`/server/blocked/${hostname}`); /** * Get the status of Mojang servers. * * @returns the promised status */ -export const getMojangServerStatus = (): Promise => { - return new Promise(async (resolve, reject) => { - const response: Response = await fetch(`${ENDPOINT}/mojang/status`); // Request the statuses - const json: any = await response.json(); - - // Resolve the blocked status - if (response.ok) { - resolve(json as MojangServerStatus); - } else { - reject(json as ErrorResponse); // The request failed - } - }); -}; +export const getMojangServerStatus = (): Promise => + makeWebRequest("/mojang/status"); diff --git a/Lib/src/lib/webRequest.ts b/Lib/src/lib/webRequest.ts new file mode 100644 index 0000000..fb186cb --- /dev/null +++ b/Lib/src/lib/webRequest.ts @@ -0,0 +1,22 @@ +import { ErrorResponse } from "../types/generic"; + +const ENDPOINT = "https://mc.rainnny.club"; // The API endpoint to use + +/** + * Make a web request to the API. + * + * @param url the endpoint to make the request to + * @returns the promised response + */ +export const makeWebRequest = (endpoint: string): Promise => + new Promise(async (resolve, reject) => { + const response: Response = await fetch(`${ENDPOINT}/${endpoint}`); // Request the player + const json: any = await response.json(); + + // Resolve the response + if (response.ok) { + resolve(json as T); + } else { + reject(json as ErrorResponse); // The request failed + } + });