From b9d79659c84d1df1ebd6076c3568ac59ab6cc6f2 Mon Sep 17 00:00:00 2001 From: Liam Date: Mon, 15 Apr 2024 08:56:26 +0100 Subject: [PATCH] cleanup --- src/common/WebRequest.ts | 34 +++++++++++--- src/index.ts | 27 ++--------- src/lib/mojang.ts | 14 ++++++ src/lib/player.ts | 39 ++++++++++++++++ src/lib/server.ts | 46 ++++++++++++++++++ src/server/mojang.ts | 25 ---------- src/server/player.ts | 74 ----------------------------- src/server/server.ts | 78 ------------------------------- src/types/server/blockedStatus.ts | 6 +++ test/mojang.ts | 4 +- test/player.ts | 10 ++-- test/server.ts | 12 +++-- 12 files changed, 150 insertions(+), 219 deletions(-) create mode 100644 src/lib/mojang.ts create mode 100644 src/lib/player.ts create mode 100644 src/lib/server.ts delete mode 100644 src/server/mojang.ts delete mode 100644 src/server/player.ts delete mode 100644 src/server/server.ts create mode 100644 src/types/server/blockedStatus.ts diff --git a/src/common/WebRequest.ts b/src/common/WebRequest.ts index 62ed65d..7b418f3 100644 --- a/src/common/WebRequest.ts +++ b/src/common/WebRequest.ts @@ -1,4 +1,5 @@ -import axios, { AxiosResponse } from "axios"; +import axios from "axios"; +import { Error } from "../types/error"; export default class WebRequest { /** @@ -7,12 +8,31 @@ export default class WebRequest { * @param url the url * @returns the response */ - public static get(url: string): Promise> { - return axios.get(url, { - validateStatus: () => true, // Don't throw errors - headers: { - "User-Agent": "McUtils-JS-Library/1.0", - }, + public static get(url: string): Promise { + return new Promise(async (resolve, reject) => { + const response = await axios.get(url, { + validateStatus: () => true, // Don't throw errors + headers: { + "User-Agent": "McUtils-JS-Library/1.0", + }, + }); + + const data = response.data; + + // Reject if the status code is not 200 + if (response.status !== 200) { + reject(data as Error); + return; + } + + // Resolve with a buffer if the content type is an image + if (response.headers["content-type"].includes("image/")) { + resolve(Buffer.from(data, "utf-8") as unknown as T); + return; + } + + // Resolve with the data + resolve(data as T); }); } } diff --git a/src/index.ts b/src/index.ts index 312ec78..2ecc5ff 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,24 +1,5 @@ -import MojangTools from "./server/mojang"; -import PlayerTools from "./server/player"; -import ServerTools from "./server/server"; +export const API_ENDPOINT = "https://api.mcutils.xyz"; -export class MinecraftUtils { - public static API_ENDPOINT = "https://api.mcutils.xyz"; - - /** - * The server instance. - */ - public server = new ServerTools(); - - /** - * The player instance. - */ - public player = new PlayerTools(); - - /** - * The Mojang instance. - */ - public mojang = new MojangTools(); -} - -export default new MinecraftUtils(); +export * from "./lib/mojang"; +export * from "./lib/player"; +export * from "./lib/server"; diff --git a/src/lib/mojang.ts b/src/lib/mojang.ts new file mode 100644 index 0000000..c305594 --- /dev/null +++ b/src/lib/mojang.ts @@ -0,0 +1,14 @@ +import { API_ENDPOINT } from ".."; +import WebRequest from "../common/WebRequest"; +import { CachedEndpointStatus } from "../types/cache/cachedEndpointStatus"; + +const endpointStatusEndpoint = API_ENDPOINT + "/mojang/status"; + +/** + * Gets the Mojang API status. + * + * @returns the Mojang API status + */ +export function getMojangEndpointStatus(): Promise { + return WebRequest.get(endpointStatusEndpoint); +} diff --git a/src/lib/player.ts b/src/lib/player.ts new file mode 100644 index 0000000..922f952 --- /dev/null +++ b/src/lib/player.ts @@ -0,0 +1,39 @@ +import { API_ENDPOINT } from ".."; +import WebRequest from "../common/WebRequest"; +import { CachedPlayer } from "../types/cache/cachedPlayer"; +import { CachedUsernameToUuid } from "../types/cache/cachedUsernameToUuid"; + +const playerEndpoint = API_ENDPOINT + "/player/{id}"; +const playerUsernameToUuidEndpoint = API_ENDPOINT + "/player/uuid/{id}"; +const playerSkinPartEndpoint = API_ENDPOINT + "/player/{part}/{id}"; + +/** + * Gets information about a Minecraft player. + * + * @param id the id of the player + * @returns the player information, or null if the player does not exist + */ +export function getPlayer(id: string): Promise { + return WebRequest.get(playerEndpoint.replace("{id}", id)); +} + +/** + * Gets the UUID of a Minecraft player. + * + * @param id the id of the player + * @returns the player's UUID, or null if the player does not exist + */ +export function getPlayerUuid(id: string): Promise { + return WebRequest.get(playerUsernameToUuidEndpoint.replace("{id}", id)); +} + +/** + * Gets a part of a Minecraft player's skin. + * + * @param part the part of the skin + * @param id the id of the player + * @returns the player's skin part, or null if the player does not exist + */ +export function getPlayerSkinPart(part: string, id: string): Promise { + return WebRequest.get(playerSkinPartEndpoint.replace("{part}", part).replace("{id}", id)); +} diff --git a/src/lib/server.ts b/src/lib/server.ts new file mode 100644 index 0000000..62781fa --- /dev/null +++ b/src/lib/server.ts @@ -0,0 +1,46 @@ +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 { + const ip = port ? `${hostname}:${port}` : hostname; + return WebRequest.get( + 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 { + const ip = port ? `${hostname}:${port}` : hostname; + return WebRequest.get(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 { + return WebRequest.get(blockedServerEndpoint.replace("{hostname}", hostname)); +} diff --git a/src/server/mojang.ts b/src/server/mojang.ts deleted file mode 100644 index 5d90108..0000000 --- a/src/server/mojang.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { MinecraftUtils } from ".."; -import WebRequest from "../common/WebRequest"; -import { CachedEndpointStatus } from "../types/cache/cachedEndpointStatus"; - -export default class MojangTools { - public endpointStatusEndpoint = MinecraftUtils.API_ENDPOINT + "/mojang/status"; - - /** - * Gets the Mojang API status. - * - * @returns the Mojang API status - */ - public getMojangEndpointStatus(): Promise { - return new Promise(async (resolve, reject) => { - const response = await WebRequest.get(this.endpointStatusEndpoint); - const data = response.data; - - if (response.status !== 200) { - reject(null); - return; - } - resolve(data); - }); - } -} diff --git a/src/server/player.ts b/src/server/player.ts deleted file mode 100644 index 779dbb0..0000000 --- a/src/server/player.ts +++ /dev/null @@ -1,74 +0,0 @@ -import { MinecraftUtils } from ".."; -import WebRequest from "../common/WebRequest"; -import { CachedPlayer } from "../types/cache/cachedPlayer"; -import { CachedUsernameToUuid } from "../types/cache/cachedUsernameToUuid"; - -export default class PlayerTools { - public playerEndpoint = MinecraftUtils.API_ENDPOINT + "/player/{id}"; - public playerUsernameToUuidEndpoint = MinecraftUtils.API_ENDPOINT + "/player/uuid/{id}"; - public playerSkinPartEndpoint = MinecraftUtils.API_ENDPOINT + "/player/{part}/{id}"; - - /** - * Gets information about a Minecraft player. - * - * @param id the id of the player - * @returns the player information, or null if the player does not exist - */ - public getPlayer(id: string): Promise { - return new Promise(async (resolve, reject) => { - const url = this.playerEndpoint.replace("{id}", id); - - const response = await WebRequest.get(url); - const data = response.data; - - if (response.status !== 200) { - reject(null); - return; - } - resolve(data); - }); - } - - /** - * Gets the UUID of a Minecraft player. - * - * @param id the id of the player - * @returns the player's UUID, or null if the player does not exist - */ - public getPlayerUuid(id: string): Promise { - return new Promise(async (resolve, reject) => { - const url = this.playerUsernameToUuidEndpoint.replace("{id}", id); - - const response = await WebRequest.get(url); - const data = response.data; - - if (response.status !== 200) { - reject(null); - return; - } - resolve(data); - }); - } - - /** - * Gets a part of a Minecraft player's skin. - * - * @param part the part of the skin - * @param id the id of the player - * @returns the player's skin part, or null if the player does not exist - */ - public getPlayerSkinPart(part: string, id: string): Promise { - return new Promise(async (resolve, reject) => { - const url = this.playerSkinPartEndpoint.replace("{part}", part).replace("{id}", id); - - const response = await WebRequest.get(url); - const data = response.data; - - if (response.status !== 200) { - reject(null); - return; - } - resolve(Buffer.from(data, "utf-8")); - }); - } -} diff --git a/src/server/server.ts b/src/server/server.ts deleted file mode 100644 index a6de390..0000000 --- a/src/server/server.ts +++ /dev/null @@ -1,78 +0,0 @@ -import { MinecraftUtils } from ".."; -import WebRequest from "../common/WebRequest"; -import { CachedMinecraftServer } from "../types/cache/cachedMinecraftServer"; -import { ServerPlatform } from "../types/server/platform"; - -export default class ServerTools { - public serverEndpoint = MinecraftUtils.API_ENDPOINT + "/server/{platform}/{hostname}"; - public serverIconEndpoint = MinecraftUtils.API_ENDPOINT + "/server/icon/{hostname}"; - public blockedServerEndpoint = MinecraftUtils.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 - */ - public getServer(platform: ServerPlatform, hostname: string, port?: 25565): Promise { - return new Promise(async (resolve, reject) => { - const ip = port ? `${hostname}:${port}` : hostname; - const url = this.serverEndpoint.replace("{platform}", platform).replace("{hostname}", ip); - - const response = await WebRequest.get(url); - const data = response.data; - - if (response.status !== 200) { - reject(null); - return; - } - resolve(data); - }); - } - - /** - * 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 - */ - public getServerIcon(hostname: string, port?: 25565): Promise { - return new Promise(async (resolve, reject) => { - const ip = port ? `${hostname}:${port}` : hostname; - const url = this.serverIconEndpoint.replace("{hostname}", ip); - - const response = await WebRequest.get(url); - const data = response.data; - - if (response.status !== 200) { - reject(null); - return; - } - resolve(Buffer.from(data, "utf-8")); - }); - } - - /** - * 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 - */ - public getBlockedStatus(hostname: string): Promise { - return new Promise(async (resolve, reject) => { - const url = this.blockedServerEndpoint.replace("{hostname}", hostname); - - const response = await WebRequest.get(url); - const data = response.data; - - if (response.status !== 200) { - reject(null); - return; - } - resolve(data.blocked); - }); - } -} diff --git a/src/types/server/blockedStatus.ts b/src/types/server/blockedStatus.ts new file mode 100644 index 0000000..29ad079 --- /dev/null +++ b/src/types/server/blockedStatus.ts @@ -0,0 +1,6 @@ +export type BlockedStatus = { + /** + * The mojang blocked status for the server. + */ + blocked: boolean; +}; diff --git a/test/mojang.ts b/test/mojang.ts index 7000eda..f60fe3e 100644 --- a/test/mojang.ts +++ b/test/mojang.ts @@ -1,7 +1,7 @@ -import mcUtils from "../src/index"; +import { getMojangEndpointStatus } from "../dist"; test("ensureMojangEndpointStatusLookupSuccess", async () => { - const response = await mcUtils.mojang.getMojangEndpointStatus(); + const response = await getMojangEndpointStatus(); expect(response).toHaveProperty("endpoints"); }); diff --git a/test/player.ts b/test/player.ts index 00a5da7..9414c5a 100644 --- a/test/player.ts +++ b/test/player.ts @@ -1,7 +1,7 @@ -import mcUtils from "../src/index"; +import { getPlayer, getPlayerSkinPart, getPlayerUuid } from "../dist/index"; test("ensureGetPlayerLookupSuccess", async () => { - const response = await mcUtils.player.getPlayer("Notch"); + const response = await getPlayer("Notch"); const { player } = response; expect(player).toBeDefined(); @@ -9,7 +9,7 @@ test("ensureGetPlayerLookupSuccess", async () => { }); test("ensureGetPlayerUuidSuccess", async () => { - const player = await mcUtils.player.getPlayerUuid("Notch"); + const player = await getPlayerUuid("Notch"); expect(player).toBeDefined(); expect(player).toHaveProperty("username"); @@ -17,7 +17,7 @@ test("ensureGetPlayerUuidSuccess", async () => { }); test("ensureGetPlayerSkinPartSuccess", async () => { - const response = await mcUtils.player.getPlayer("Notch"); + const response = await getPlayer("Notch"); const { player } = response; const skin = player.skin; @@ -25,7 +25,7 @@ test("ensureGetPlayerSkinPartSuccess", async () => { // Test each skin part for (const part in skinParts) { - const partBuffer = await mcUtils.player.getPlayerSkinPart(part, player.uniqueId); + const partBuffer = await getPlayerSkinPart(part, player.uniqueId); expect(partBuffer).toBeDefined(); expect(partBuffer.byteLength).toBeGreaterThan(0); } diff --git a/test/server.ts b/test/server.ts index b7ffdb5..8ec7da7 100644 --- a/test/server.ts +++ b/test/server.ts @@ -1,8 +1,8 @@ -import mcUtils from "../src/index"; +import { getBlockedStatus, getServer, getServerIcon } from "../dist"; import { ServerPlatform } from "../src/types/server/platform"; test("ensureGetServerLookupSuccess", async () => { - const cachedServer = await mcUtils.server.getServer(ServerPlatform.Java, "mc.hypixel.net"); + const cachedServer = await getServer(ServerPlatform.Java, "mc.hypixel.net"); const { server } = cachedServer; expect(server).toBeDefined(); @@ -10,12 +10,14 @@ test("ensureGetServerLookupSuccess", async () => { }); test("ensureGetServerIconSuccess", async () => { - const icon = await mcUtils.server.getServerIcon("mc.hypixel.net"); + const icon = await getServerIcon("mc.hypixel.net"); expect(icon).toBeDefined(); expect(icon.byteLength).toBeGreaterThan(0); // The server has an icon }); test("ensureGetServerBlockedStatusSuccess", async () => { - const blockedStatus = await mcUtils.server.getBlockedStatus("mc.hypixel.net"); - expect(blockedStatus).toBe(false); // The server is not blocked + const blockedStatus = await getBlockedStatus("mc.hypixel.net"); + const { blocked } = blockedStatus; + + expect(blocked).toBe(false); // The server is not blocked });