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 { MojangServerStatus } from "@/types/mojang";
|
||||||
import type { CachedPlayer } from "@/types/player";
|
import type { CachedPlayer } from "@/types/player";
|
||||||
import { Platform } from "@/types/server";
|
import { Platform } from "@/types/server";
|
||||||
@ -12,7 +12,7 @@ import { CachedJavaMinecraftServer } from "@/types/server/java-server";
|
|||||||
* @returns the promised player
|
* @returns the promised player
|
||||||
*/
|
*/
|
||||||
export const getPlayer = (query: string): Promise<CachedPlayer> =>
|
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.
|
* Get a Minecraft server by its platform and hostname.
|
||||||
@ -26,12 +26,12 @@ export const getMinecraftServer = (
|
|||||||
hostname: string
|
hostname: string
|
||||||
): Promise<CachedJavaMinecraftServer | CachedBedrockMinecraftServer> =>
|
): Promise<CachedJavaMinecraftServer | CachedBedrockMinecraftServer> =>
|
||||||
platform === "java"
|
platform === "java"
|
||||||
? makeWebRequest<CachedJavaMinecraftServer>(
|
? new WebRequest(
|
||||||
`/server/${platform}/${hostname}`
|
`/server/${platform}/${hostname}`
|
||||||
)
|
).execute<CachedJavaMinecraftServer>()
|
||||||
: makeWebRequest<CachedBedrockMinecraftServer>(
|
: new WebRequest(
|
||||||
`/server/${platform}/${hostname}`
|
`/server/${platform}/${hostname}`
|
||||||
);
|
).execute<CachedBedrockMinecraftServer>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if the server with the
|
* Check if the server with the
|
||||||
@ -41,7 +41,7 @@ export const getMinecraftServer = (
|
|||||||
* @returns the promised blocked status
|
* @returns the promised blocked status
|
||||||
*/
|
*/
|
||||||
export const isMojangBlocked = (hostname: string): Promise<boolean> =>
|
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.
|
* Get the status of Mojang servers.
|
||||||
@ -49,4 +49,4 @@ export const isMojangBlocked = (hostname: string): Promise<boolean> =>
|
|||||||
* @returns the promised status
|
* @returns the promised status
|
||||||
*/
|
*/
|
||||||
export const getMojangServerStatus = (): Promise<MojangServerStatus> =>
|
export const getMojangServerStatus = (): Promise<MojangServerStatus> =>
|
||||||
makeWebRequest<MojangServerStatus>("/mojang/status");
|
new WebRequest("/mojang/status").execute<MojangServerStatus>();
|
||||||
|
@ -4,13 +4,30 @@ const ENDPOINT = "https://mc.rainnny.club"; // The API endpoint to use
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Make a web request to the API.
|
* 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> =>
|
export class WebRequest {
|
||||||
|
/**
|
||||||
|
* The endpoint to make the request to.
|
||||||
|
*/
|
||||||
|
endpoint: string;
|
||||||
|
|
||||||
|
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) => {
|
new Promise(async (resolve, reject) => {
|
||||||
const response: Response = await fetch(`${ENDPOINT}/${endpoint}`); // Request the player
|
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();
|
const json: any = await response.json();
|
||||||
|
|
||||||
// Resolve the response
|
// Resolve the response
|
||||||
@ -19,4 +36,9 @@ export const makeWebRequest = <T>(endpoint: string): Promise<T> =>
|
|||||||
} else {
|
} else {
|
||||||
reject(json as ErrorResponse); // The request failed
|
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