RESTfulMC/JS-SDK/src/lib/restfulmc.ts

88 lines
2.7 KiB
TypeScript
Raw Normal View History

2024-04-15 11:05:31 -04:00
import { WebRequest } from "@/lib/web-request";
2024-04-15 11:24:03 -04:00
import { MojangServerStatusResponse } from "@/types/mojang/server-status-response";
2024-04-15 11:05:31 -04:00
import { CachedPlayer } from "@/types/player/player";
import { SkinPart } from "@/types/player/skin-part";
2024-04-15 11:24:03 -04:00
import { CachedBedrockMinecraftServer } from "@/types/server/bedrock/server";
2024-04-15 09:12:41 -04:00
import { CachedJavaMinecraftServer } from "@/types/server/java-server";
2024-04-15 11:10:05 -04:00
import { ServerPlatform } from "@/types/server/platform";
2024-04-14 14:03:02 -04:00
2024-04-14 13:34:34 -04:00
/**
* Get a player by their username or UUID.
*
* @param query the query to search for the player by
* @returns the promised player
*/
2024-04-14 16:29:29 -04:00
export const getPlayer = (query: string): Promise<CachedPlayer> =>
2024-04-15 07:46:45 -04:00
new WebRequest(`/player/${query}`).execute<CachedPlayer>();
/**
* Get the part of a skin texture for
* a player by their username or UUID.
*
* @param part the part of the player's skin to get
* @param query the query to search for the player by
* @param extension the skin part image extension
* @param size the size of the skin part image
* @returns the promised skin part texture
*/
export const getSkinPart = (
part: SkinPart,
query: string,
extension: "png" | "jpg" = "png",
size: number = 128
): Promise<ArrayBuffer> =>
new WebRequest(
`/player/${part}/${query}.${extension}?size=${size}`
).execute<ArrayBuffer>();
/**
* Get a Minecraft server by its platform and hostname.
*
* @param platform the platform of the server
* @param hostname the hostname of the server
* @returns the promised server
*/
export const getMinecraftServer = (
2024-04-15 09:12:41 -04:00
platform: ServerPlatform,
hostname: string
2024-04-14 16:29:29 -04:00
): Promise<CachedJavaMinecraftServer | CachedBedrockMinecraftServer> =>
2024-04-15 09:12:41 -04:00
platform === ServerPlatform.JAVA
2024-04-15 07:46:45 -04:00
? new WebRequest(
2024-04-14 16:29:29 -04:00
`/server/${platform}/${hostname}`
2024-04-15 07:46:45 -04:00
).execute<CachedJavaMinecraftServer>()
: new WebRequest(
2024-04-14 16:29:29 -04:00
`/server/${platform}/${hostname}`
2024-04-15 07:46:45 -04:00
).execute<CachedBedrockMinecraftServer>();
2024-04-14 15:56:45 -04:00
/**
* Check if the server with the
* given hostname is blocked by Mojang.
*
* @param hostname the server hostname to check
* @returns the promised blocked status
*/
2024-04-14 16:29:29 -04:00
export const isMojangBlocked = (hostname: string): Promise<boolean> =>
2024-04-15 10:32:53 -04:00
new WebRequest(`/server/blocked/${hostname}`)
.execute<{
blocked: boolean;
}>()
.then((res) => res.blocked);
2024-04-14 16:06:42 -04:00
/**
* Get the icon of the Java Minecraft
* server with the given hostname.
*
* @param hostname the hostname of the server
* @returns the primised server icon
*/
export const getJavaServerFavicon = (hostname: string): Promise<ArrayBuffer> =>
new WebRequest(`/server/icon/${hostname}`).execute<ArrayBuffer>();
2024-04-14 16:06:42 -04:00
/**
* Get the status of Mojang servers.
*
* @returns the promised status
*/
2024-04-15 11:24:03 -04:00
export const getMojangServerStatus = (): Promise<MojangServerStatusResponse> =>
new WebRequest("/mojang/status").execute<MojangServerStatusResponse>();