Cleanup the lib
Some checks failed
Deploy Lib / docker (ubuntu-latest, 2.44.0) (push) Failing after 1m29s
Some checks failed
Deploy Lib / docker (ubuntu-latest, 2.44.0) (push) Failing after 1m29s
This commit is contained in:
parent
6e096554d8
commit
50b5c8ba4b
@ -1,10 +1,8 @@
|
|||||||
import { ErrorResponse } from "../types/generic";
|
|
||||||
import type { CachedPlayer } from "../types/player";
|
import type { CachedPlayer } from "../types/player";
|
||||||
import { Platform } from "../types/server";
|
import { Platform } from "../types/server";
|
||||||
import { CachedBedrockMinecraftServer } from "../types/server/bedrock-server";
|
import { CachedBedrockMinecraftServer } from "../types/server/bedrock-server";
|
||||||
import { CachedJavaMinecraftServer } from "../types/server/java-server";
|
import { CachedJavaMinecraftServer } from "../types/server/java-server";
|
||||||
|
import { makeWebRequest } from "./webRequest";
|
||||||
const ENDPOINT = "https://mc.rainnny.club"; // The API endpoint to use
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a player by their username or UUID.
|
* 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
|
* @param query the query to search for the player by
|
||||||
* @returns the promised player
|
* @returns the promised player
|
||||||
*/
|
*/
|
||||||
export const getPlayer = (query: string): Promise<CachedPlayer> => {
|
export const getPlayer = (query: string): Promise<CachedPlayer> =>
|
||||||
return new Promise(async (resolve, reject) => {
|
makeWebRequest<CachedPlayer>(`/player/${query}`);
|
||||||
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
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a Minecraft server by its platform and hostname.
|
* Get a Minecraft server by its platform and hostname.
|
||||||
@ -36,25 +23,14 @@ export const getPlayer = (query: string): Promise<CachedPlayer> => {
|
|||||||
export const getMinecraftServer = (
|
export const getMinecraftServer = (
|
||||||
platform: Platform,
|
platform: Platform,
|
||||||
hostname: string
|
hostname: string
|
||||||
): Promise<CachedJavaMinecraftServer | CachedBedrockMinecraftServer> => {
|
): Promise<CachedJavaMinecraftServer | CachedBedrockMinecraftServer> =>
|
||||||
return new Promise(async (resolve, reject) => {
|
platform === "java"
|
||||||
const response: Response = await fetch(
|
? makeWebRequest<CachedJavaMinecraftServer>(
|
||||||
`${ENDPOINT}/server/${platform}/${hostname}`
|
`/server/${platform}/${hostname}`
|
||||||
); // Request the server
|
)
|
||||||
const json: any = await response.json();
|
: makeWebRequest<CachedBedrockMinecraftServer>(
|
||||||
|
`/server/${platform}/${hostname}`
|
||||||
// Resolve the server
|
);
|
||||||
if (response.ok) {
|
|
||||||
resolve(
|
|
||||||
platform === "java"
|
|
||||||
? (json as CachedJavaMinecraftServer)
|
|
||||||
: (json as CachedBedrockMinecraftServer)
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
reject(json as ErrorResponse); // The request failed
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check if the server with the
|
* Check if the server with the
|
||||||
@ -63,37 +39,13 @@ export const getMinecraftServer = (
|
|||||||
* @param hostname the server hostname to check
|
* @param hostname the server hostname to check
|
||||||
* @returns the promised blocked status
|
* @returns the promised blocked status
|
||||||
*/
|
*/
|
||||||
export const isMojangBlocked = (hostname: string): Promise<boolean> => {
|
export const isMojangBlocked = (hostname: string): Promise<boolean> =>
|
||||||
return new Promise(async (resolve, reject) => {
|
makeWebRequest<boolean>(`/server/blocked/${hostname}`);
|
||||||
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
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the status of Mojang servers.
|
* Get the status of Mojang servers.
|
||||||
*
|
*
|
||||||
* @returns the promised status
|
* @returns the promised status
|
||||||
*/
|
*/
|
||||||
export const getMojangServerStatus = (): Promise<MojangServerStatus> => {
|
export const getMojangServerStatus = (): Promise<MojangServerStatus> =>
|
||||||
return new Promise(async (resolve, reject) => {
|
makeWebRequest<MojangServerStatus>("/mojang/status");
|
||||||
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
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
22
Lib/src/lib/webRequest.ts
Normal file
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
|
||||||
|
}
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user