This commit is contained in:
parent
3342f696af
commit
b9d79659c8
@ -1,4 +1,5 @@
|
|||||||
import axios, { AxiosResponse } from "axios";
|
import axios from "axios";
|
||||||
|
import { Error } from "../types/error";
|
||||||
|
|
||||||
export default class WebRequest {
|
export default class WebRequest {
|
||||||
/**
|
/**
|
||||||
@ -7,12 +8,31 @@ export default class WebRequest {
|
|||||||
* @param url the url
|
* @param url the url
|
||||||
* @returns the response
|
* @returns the response
|
||||||
*/
|
*/
|
||||||
public static get(url: string): Promise<AxiosResponse<any, any>> {
|
public static get<T>(url: string): Promise<T> {
|
||||||
return axios.get(url, {
|
return new Promise(async (resolve, reject) => {
|
||||||
validateStatus: () => true, // Don't throw errors
|
const response = await axios.get(url, {
|
||||||
headers: {
|
validateStatus: () => true, // Don't throw errors
|
||||||
"User-Agent": "McUtils-JS-Library/1.0",
|
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);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
27
src/index.ts
27
src/index.ts
@ -1,24 +1,5 @@
|
|||||||
import MojangTools from "./server/mojang";
|
export const API_ENDPOINT = "https://api.mcutils.xyz";
|
||||||
import PlayerTools from "./server/player";
|
|
||||||
import ServerTools from "./server/server";
|
|
||||||
|
|
||||||
export class MinecraftUtils {
|
export * from "./lib/mojang";
|
||||||
public static API_ENDPOINT = "https://api.mcutils.xyz";
|
export * from "./lib/player";
|
||||||
|
export * from "./lib/server";
|
||||||
/**
|
|
||||||
* 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();
|
|
||||||
|
14
src/lib/mojang.ts
Normal file
14
src/lib/mojang.ts
Normal file
@ -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<CachedEndpointStatus> {
|
||||||
|
return WebRequest.get(endpointStatusEndpoint);
|
||||||
|
}
|
39
src/lib/player.ts
Normal file
39
src/lib/player.ts
Normal file
@ -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<CachedPlayer> {
|
||||||
|
return WebRequest.get<CachedPlayer>(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<CachedUsernameToUuid> {
|
||||||
|
return WebRequest.get<CachedUsernameToUuid>(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<Buffer> {
|
||||||
|
return WebRequest.get<Buffer>(playerSkinPartEndpoint.replace("{part}", part).replace("{id}", id));
|
||||||
|
}
|
46
src/lib/server.ts
Normal file
46
src/lib/server.ts
Normal file
@ -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<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));
|
||||||
|
}
|
@ -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<CachedEndpointStatus> {
|
|
||||||
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);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
@ -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<CachedPlayer> {
|
|
||||||
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<CachedUsernameToUuid> {
|
|
||||||
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<Buffer> {
|
|
||||||
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"));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
@ -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<CachedMinecraftServer> {
|
|
||||||
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<Buffer> {
|
|
||||||
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<boolean> {
|
|
||||||
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);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
6
src/types/server/blockedStatus.ts
Normal file
6
src/types/server/blockedStatus.ts
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
export type BlockedStatus = {
|
||||||
|
/**
|
||||||
|
* The mojang blocked status for the server.
|
||||||
|
*/
|
||||||
|
blocked: boolean;
|
||||||
|
};
|
@ -1,7 +1,7 @@
|
|||||||
import mcUtils from "../src/index";
|
import { getMojangEndpointStatus } from "../dist";
|
||||||
|
|
||||||
test("ensureMojangEndpointStatusLookupSuccess", async () => {
|
test("ensureMojangEndpointStatusLookupSuccess", async () => {
|
||||||
const response = await mcUtils.mojang.getMojangEndpointStatus();
|
const response = await getMojangEndpointStatus();
|
||||||
|
|
||||||
expect(response).toHaveProperty("endpoints");
|
expect(response).toHaveProperty("endpoints");
|
||||||
});
|
});
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import mcUtils from "../src/index";
|
import { getPlayer, getPlayerSkinPart, getPlayerUuid } from "../dist/index";
|
||||||
|
|
||||||
test("ensureGetPlayerLookupSuccess", async () => {
|
test("ensureGetPlayerLookupSuccess", async () => {
|
||||||
const response = await mcUtils.player.getPlayer("Notch");
|
const response = await getPlayer("Notch");
|
||||||
const { player } = response;
|
const { player } = response;
|
||||||
|
|
||||||
expect(player).toBeDefined();
|
expect(player).toBeDefined();
|
||||||
@ -9,7 +9,7 @@ test("ensureGetPlayerLookupSuccess", async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("ensureGetPlayerUuidSuccess", async () => {
|
test("ensureGetPlayerUuidSuccess", async () => {
|
||||||
const player = await mcUtils.player.getPlayerUuid("Notch");
|
const player = await getPlayerUuid("Notch");
|
||||||
|
|
||||||
expect(player).toBeDefined();
|
expect(player).toBeDefined();
|
||||||
expect(player).toHaveProperty("username");
|
expect(player).toHaveProperty("username");
|
||||||
@ -17,7 +17,7 @@ test("ensureGetPlayerUuidSuccess", async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("ensureGetPlayerSkinPartSuccess", async () => {
|
test("ensureGetPlayerSkinPartSuccess", async () => {
|
||||||
const response = await mcUtils.player.getPlayer("Notch");
|
const response = await getPlayer("Notch");
|
||||||
const { player } = response;
|
const { player } = response;
|
||||||
|
|
||||||
const skin = player.skin;
|
const skin = player.skin;
|
||||||
@ -25,7 +25,7 @@ test("ensureGetPlayerSkinPartSuccess", async () => {
|
|||||||
|
|
||||||
// Test each skin part
|
// Test each skin part
|
||||||
for (const part in skinParts) {
|
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).toBeDefined();
|
||||||
expect(partBuffer.byteLength).toBeGreaterThan(0);
|
expect(partBuffer.byteLength).toBeGreaterThan(0);
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import mcUtils from "../src/index";
|
import { getBlockedStatus, getServer, getServerIcon } from "../dist";
|
||||||
import { ServerPlatform } from "../src/types/server/platform";
|
import { ServerPlatform } from "../src/types/server/platform";
|
||||||
|
|
||||||
test("ensureGetServerLookupSuccess", async () => {
|
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;
|
const { server } = cachedServer;
|
||||||
|
|
||||||
expect(server).toBeDefined();
|
expect(server).toBeDefined();
|
||||||
@ -10,12 +10,14 @@ test("ensureGetServerLookupSuccess", async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("ensureGetServerIconSuccess", 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).toBeDefined();
|
||||||
expect(icon.byteLength).toBeGreaterThan(0); // The server has an icon
|
expect(icon.byteLength).toBeGreaterThan(0); // The server has an icon
|
||||||
});
|
});
|
||||||
|
|
||||||
test("ensureGetServerBlockedStatusSuccess", async () => {
|
test("ensureGetServerBlockedStatusSuccess", async () => {
|
||||||
const blockedStatus = await mcUtils.server.getBlockedStatus("mc.hypixel.net");
|
const blockedStatus = await getBlockedStatus("mc.hypixel.net");
|
||||||
expect(blockedStatus).toBe(false); // The server is not blocked
|
const { blocked } = blockedStatus;
|
||||||
|
|
||||||
|
expect(blocked).toBe(false); // The server is not blocked
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user