Javascript-Library/src/lib/server.ts

47 lines
1.8 KiB
TypeScript
Raw Normal View History

2024-04-15 07:56:26 +00:00
import { API_ENDPOINT } from "..";
import WebRequest from "../common/WebRequest";
import { CachedMinecraftServer } from "../types/cache/cachedMinecraftServer";
import { BlockedStatus } from "../types/server/blockedStatus";
import { ServerPlatform } from "../types/server/platform";
const serverEndpoint = API_ENDPOINT + "/server/{platform}/{hostname}";
const serverIconEndpoint = API_ENDPOINT + "/server/icon/{hostname}";
const blockedServerEndpoint = API_ENDPOINT + "/server/blocked/{hostname}";
/**
* Gets information about a Minecraft server.
*
* @param platform the platform of server
* @param hostname the hostname of the server
* @param port the port of the server
* @returns the server information, or null if the server does not exist
*/
export function getServer(platform: ServerPlatform, hostname: string, port?: 25565): Promise<CachedMinecraftServer> {
const ip = port ? `${hostname}:${port}` : hostname;
return WebRequest.get<CachedMinecraftServer>(
serverEndpoint.replace("{platform}", platform).replace("{hostname}", ip)
);
}
/**
* Gets the icon of a Java Minecraft server.
*
* @param hostname the hostname of the server
* @param port the port of the server
* @returns the server icon, or null if the server does not have an icon
*/
export function getServerIcon(hostname: string, port?: 25565): Promise<Buffer> {
const ip = port ? `${hostname}:${port}` : hostname;
return WebRequest.get<Buffer>(serverIconEndpoint.replace("{hostname}", ip));
}
/**
* Gets the Mojang blocked status of a Minecraft server.
*
* @param hostname the hostname of the server
* @returns true if the server is blocked, false otherwise
*/
export function getBlockedStatus(hostname: string): Promise<BlockedStatus> {
return WebRequest.get<BlockedStatus>(blockedServerEndpoint.replace("{hostname}", hostname));
}