Add better support to web requests
This commit is contained in:
parent
cf345c3410
commit
b07d8c0888
@ -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<CachedPlayer> =>
|
||||
makeWebRequest<CachedPlayer>(`/player/${query}`);
|
||||
new WebRequest(`/player/${query}`).execute<CachedPlayer>();
|
||||
|
||||
/**
|
||||
* Get a Minecraft server by its platform and hostname.
|
||||
@ -26,12 +26,12 @@ export const getMinecraftServer = (
|
||||
hostname: string
|
||||
): Promise<CachedJavaMinecraftServer | CachedBedrockMinecraftServer> =>
|
||||
platform === "java"
|
||||
? makeWebRequest<CachedJavaMinecraftServer>(
|
||||
? new WebRequest(
|
||||
`/server/${platform}/${hostname}`
|
||||
)
|
||||
: makeWebRequest<CachedBedrockMinecraftServer>(
|
||||
).execute<CachedJavaMinecraftServer>()
|
||||
: new WebRequest(
|
||||
`/server/${platform}/${hostname}`
|
||||
);
|
||||
).execute<CachedBedrockMinecraftServer>();
|
||||
|
||||
/**
|
||||
* Check if the server with the
|
||||
@ -41,7 +41,7 @@ export const getMinecraftServer = (
|
||||
* @returns the promised blocked status
|
||||
*/
|
||||
export const isMojangBlocked = (hostname: string): Promise<boolean> =>
|
||||
makeWebRequest<boolean>(`/server/blocked/${hostname}`);
|
||||
new WebRequest(`/server/blocked/${hostname}`).execute<boolean>();
|
||||
|
||||
/**
|
||||
* Get the status of Mojang servers.
|
||||
@ -49,4 +49,4 @@ export const isMojangBlocked = (hostname: string): Promise<boolean> =>
|
||||
* @returns the promised status
|
||||
*/
|
||||
export const getMojangServerStatus = (): Promise<MojangServerStatus> =>
|
||||
makeWebRequest<MojangServerStatus>("/mojang/status");
|
||||
new WebRequest("/mojang/status").execute<MojangServerStatus>();
|
||||
|
@ -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 = <T>(endpoint: string): Promise<T> =>
|
||||
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 = <T>(): Promise<T> =>
|
||||
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;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user