Cleanup the lib
Some checks failed
Deploy Lib / docker (ubuntu-latest, 2.44.0) (push) Failing after 1m29s

This commit is contained in:
Braydon 2024-04-14 16:29:29 -04:00
parent 6e096554d8
commit 50b5c8ba4b
2 changed files with 37 additions and 63 deletions

@ -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<CachedPlayer> => {
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<CachedPlayer> =>
makeWebRequest<CachedPlayer>(`/player/${query}`);
/**
* Get a Minecraft server by its platform and hostname.
@ -36,25 +23,14 @@ export const getPlayer = (query: string): Promise<CachedPlayer> => {
export const getMinecraftServer = (
platform: Platform,
hostname: string
): Promise<CachedJavaMinecraftServer | CachedBedrockMinecraftServer> => {
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<CachedJavaMinecraftServer | CachedBedrockMinecraftServer> =>
platform === "java"
? makeWebRequest<CachedJavaMinecraftServer>(
`/server/${platform}/${hostname}`
)
: makeWebRequest<CachedBedrockMinecraftServer>(
`/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<boolean> => {
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<boolean> =>
makeWebRequest<boolean>(`/server/blocked/${hostname}`);
/**
* Get the status of Mojang servers.
*
* @returns the promised status
*/
export const getMojangServerStatus = (): Promise<MojangServerStatus> => {
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<MojangServerStatus> =>
makeWebRequest<MojangServerStatus>("/mojang/status");

22
Lib/src/lib/webRequest.ts Normal file

@ -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 = <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();
// Resolve the response
if (response.ok) {
resolve(json as T);
} else {
reject(json as ErrorResponse); // The request failed
}
});