From b07d8c088800cf6f59e0c766a8d8a4cba7fa0f1b Mon Sep 17 00:00:00 2001 From: Rainnny7 Date: Mon, 15 Apr 2024 07:46:45 -0400 Subject: [PATCH] Add better support to web requests --- JS-SDK/src/lib/restfulmc.ts | 16 ++++++------ JS-SDK/src/lib/webRequest.ts | 50 ++++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 22 deletions(-) diff --git a/JS-SDK/src/lib/restfulmc.ts b/JS-SDK/src/lib/restfulmc.ts index 1b21582..d1e1913 100644 --- a/JS-SDK/src/lib/restfulmc.ts +++ b/JS-SDK/src/lib/restfulmc.ts @@ -1,4 +1,4 @@ -import { makeWebRequest } from "@/lib/webRequest"; +import { WebRequest } from "@/lib/webRequest"; import { MojangServerStatus } from "@/types/mojang"; import type { CachedPlayer } from "@/types/player"; import { Platform } from "@/types/server"; @@ -12,7 +12,7 @@ import { CachedJavaMinecraftServer } from "@/types/server/java-server"; * @returns the promised player */ export const getPlayer = (query: string): Promise => - makeWebRequest(`/player/${query}`); + new WebRequest(`/player/${query}`).execute(); /** * Get a Minecraft server by its platform and hostname. @@ -26,12 +26,12 @@ export const getMinecraftServer = ( hostname: string ): Promise => platform === "java" - ? makeWebRequest( + ? new WebRequest( `/server/${platform}/${hostname}` - ) - : makeWebRequest( + ).execute() + : new WebRequest( `/server/${platform}/${hostname}` - ); + ).execute(); /** * Check if the server with the @@ -41,7 +41,7 @@ export const getMinecraftServer = ( * @returns the promised blocked status */ export const isMojangBlocked = (hostname: string): Promise => - makeWebRequest(`/server/blocked/${hostname}`); + new WebRequest(`/server/blocked/${hostname}`).execute(); /** * Get the status of Mojang servers. @@ -49,4 +49,4 @@ export const isMojangBlocked = (hostname: string): Promise => * @returns the promised status */ export const getMojangServerStatus = (): Promise => - makeWebRequest("/mojang/status"); + new WebRequest("/mojang/status").execute(); diff --git a/JS-SDK/src/lib/webRequest.ts b/JS-SDK/src/lib/webRequest.ts index 7ff68c6..2ef2314 100644 --- a/JS-SDK/src/lib/webRequest.ts +++ b/JS-SDK/src/lib/webRequest.ts @@ -4,19 +4,41 @@ 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(); +export class WebRequest { + /** + * The endpoint to make the request to. + */ + endpoint: string; - // Resolve the response - if (response.ok) { - resolve(json as T); - } else { - reject(json as ErrorResponse); // The request failed - } - }); + constructor(endpoint: string) { + this.endpoint = endpoint; + } + + /** + * Execute this web request. + * + * @returns the promised response + * @template T the type of the response + */ + execute = (): Promise => + new Promise(async (resolve, reject) => { + const response: Response = await fetch(`${ENDPOINT}/${this.endpoint}`); // Request the player + const contentType: string | null = response.headers.get("Content-Type"); // Get the response content type + + // Parse as Json + if (contentType === "application/json") { + const json: any = await response.json(); + + // Resolve the response + if (response.ok) { + resolve(json as T); + } else { + reject(json as ErrorResponse); // The request failed + } + } else { + // Fallback to an array buffer + return (await response.arrayBuffer()) as T; + } + }); +}