8 Commits

Author SHA1 Message Date
d1365ae261 Cleanup
Some checks failed
Publish JS SDK / docker (push) Failing after 32s
2024-04-15 08:49:09 -04:00
3e42ed414a Add #getSkinPart and #getJavaServerFavicon 2024-04-15 08:48:57 -04:00
d834badb78 Use an enum for server platforms 2024-04-15 07:56:05 -04:00
baac1391ee Merge remote-tracking branch 'origin/master' 2024-04-15 07:52:47 -04:00
a218dc7ec3 oops, wasn't properly resolving the array buffer in web requests 2024-04-15 07:51:20 -04:00
1eb4a9ec0e Fix server types 2024-04-15 07:49:00 -04:00
bf9fa6582f JS minor version bump 2024-04-15 07:47:07 -04:00
b07d8c0888 Add better support to web requests 2024-04-15 07:46:45 -04:00
13 changed files with 175 additions and 74 deletions

Binary file not shown.

View File

@ -1,6 +1,6 @@
{ {
"name": "restfulmc-lib", "name": "restfulmc-lib",
"version": "1.0.0", "version": "1.0.1",
"author": "Braydon (Rainnny) <braydonrainnny@gmail.com>", "author": "Braydon (Rainnny) <braydonrainnny@gmail.com>",
"description": "A simple, yet useful RESTful API for Minecraft utilizing Springboot.", "description": "A simple, yet useful RESTful API for Minecraft utilizing Springboot.",
"keywords": [ "keywords": [

View File

@ -1,5 +1,6 @@
export * from "./types/dns";
export * from "./types/generic"; export * from "./types/generic";
export * from "./types/mojang"; export * from "./types/mojang";
export * from "./types/player"; export * from "./types/player";
export * from "./types/server"; export * from "./types/server/bedrock-server";
export * from "./types/server/java-server";
export * from "./types/server/server";

View File

@ -1 +1 @@
export * from "@/lib/restfulmc"; export * from "./lib/restfulmc";

View File

@ -1,9 +1,9 @@
import { makeWebRequest } from "@/lib/webRequest"; import { MojangServerStatus } from "../types/mojang";
import { MojangServerStatus } from "@/types/mojang"; import { CachedPlayer, SkinPart } from "../types/player";
import type { CachedPlayer } from "@/types/player"; import { CachedBedrockMinecraftServer } from "../types/server/bedrock-server";
import { Platform } from "@/types/server"; import { CachedJavaMinecraftServer } from "../types/server/java-server";
import { CachedBedrockMinecraftServer } from "@/types/server/bedrock-server"; import { Platform } from "../types/server/server";
import { CachedJavaMinecraftServer } from "@/types/server/java-server"; import { WebRequest } from "./webRequest";
/** /**
* Get a player by their username or UUID. * Get a player by their username or UUID.
@ -12,7 +12,27 @@ import { CachedJavaMinecraftServer } from "@/types/server/java-server";
* @returns the promised player * @returns the promised player
*/ */
export const getPlayer = (query: string): Promise<CachedPlayer> => export const getPlayer = (query: string): Promise<CachedPlayer> =>
makeWebRequest<CachedPlayer>(`/player/${query}`); 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. * Get a Minecraft server by its platform and hostname.
@ -25,13 +45,13 @@ export const getMinecraftServer = (
platform: Platform, platform: Platform,
hostname: string hostname: string
): Promise<CachedJavaMinecraftServer | CachedBedrockMinecraftServer> => ): Promise<CachedJavaMinecraftServer | CachedBedrockMinecraftServer> =>
platform === "java" platform === Platform.JAVA
? makeWebRequest<CachedJavaMinecraftServer>( ? new WebRequest(
`/server/${platform}/${hostname}` `/server/${platform}/${hostname}`
) ).execute<CachedJavaMinecraftServer>()
: makeWebRequest<CachedBedrockMinecraftServer>( : new WebRequest(
`/server/${platform}/${hostname}` `/server/${platform}/${hostname}`
); ).execute<CachedBedrockMinecraftServer>();
/** /**
* Check if the server with the * Check if the server with the
@ -41,7 +61,17 @@ export const getMinecraftServer = (
* @returns the promised blocked status * @returns the promised blocked status
*/ */
export const isMojangBlocked = (hostname: string): Promise<boolean> => export const isMojangBlocked = (hostname: string): Promise<boolean> =>
makeWebRequest<boolean>(`/server/blocked/${hostname}`); new WebRequest(`/server/blocked/${hostname}`).execute<boolean>();
/**
* 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>();
/** /**
* Get the status of Mojang servers. * Get the status of Mojang servers.
@ -49,4 +79,4 @@ export const isMojangBlocked = (hostname: string): Promise<boolean> =>
* @returns the promised status * @returns the promised status
*/ */
export const getMojangServerStatus = (): Promise<MojangServerStatus> => export const getMojangServerStatus = (): Promise<MojangServerStatus> =>
makeWebRequest<MojangServerStatus>("/mojang/status"); new WebRequest("/mojang/status").execute<MojangServerStatus>();

View File

@ -1,22 +1,44 @@
import { ErrorResponse } from "@/types/generic"; import { ErrorResponse } from "../types/generic";
const ENDPOINT = "https://mc.rainnny.club"; // The API endpoint to use const ENDPOINT = "https://mc.rainnny.club"; // The API endpoint to use
/** /**
* Make a web request to the API. * 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> => export class WebRequest {
new Promise(async (resolve, reject) => { /**
const response: Response = await fetch(`${ENDPOINT}/${endpoint}`); // Request the player * The endpoint to make the request to.
const json: any = await response.json(); */
endpoint: string;
// Resolve the response constructor(endpoint: string) {
if (response.ok) { this.endpoint = endpoint;
resolve(json as T); }
} else {
reject(json as ErrorResponse); // The request failed /**
} * Execute this web request.
}); *
* @returns the promised response
* @template T the type of the response
*/
execute = <T>(): Promise<T> =>
new Promise(async (resolve, reject) => {
const response: Response = await fetch(`${ENDPOINT}/${this.endpoint}`); // Request the player
const contentType: string | null = response.headers.get("Content-Type"); // Get the response content type
// Parse as Json
if (contentType === "application/json") {
const json: any = await response.json();
// Resolve the response
if (response.ok) {
resolve(json as T);
} else {
reject(json as ErrorResponse); // The request failed
}
} else {
// Fallback to an array buffer
resolve((await response.arrayBuffer()) as T);
}
});
}

View File

@ -1,7 +1,7 @@
/** /**
* An A record. * An A record.
*/ */
export interface ARecord extends DNSRecord { interface ARecord extends DNSRecord {
/** /**
* The address of this record, undefined if unresolved. * The address of this record, undefined if unresolved.
*/ */
@ -11,7 +11,7 @@ export interface ARecord extends DNSRecord {
/** /**
* An SRV record. * An SRV record.
*/ */
export interface SRVRecord extends DNSRecord { interface SRVRecord extends DNSRecord {
/** /**
* The priority of this record. * The priority of this record.
*/ */
@ -36,7 +36,7 @@ export interface SRVRecord extends DNSRecord {
/** /**
* A representation of a DNS record. * A representation of a DNS record.
*/ */
export type DNSRecord = { type DNSRecord = {
/** /**
* The type of this record. * The type of this record.
*/ */
@ -47,3 +47,11 @@ export type DNSRecord = {
*/ */
ttl: number; ttl: number;
}; };
/**
* Types of a DNS record.
*/
enum RecordType {
A,
SRV,
}

View File

@ -3,5 +3,14 @@
* a service provided by Mojang. * a service provided by Mojang.
*/ */
export type MojangServerStatus = { export type MojangServerStatus = {
[key: string]: string; [endpoint: string]: Status;
}; };
/**
* The status of a service.
*/
enum Status {
ONLINE,
DEGRADED,
OFFLINE,
}

View File

@ -57,7 +57,7 @@ export type Player = {
/** /**
* A skin for a {@link Player}. * A skin for a {@link Player}.
*/ */
export type Skin = { type Skin = {
/** /**
* The texture URL of this skin. * The texture URL of this skin.
*/ */
@ -76,24 +76,49 @@ export type Skin = {
/** /**
* URLs to the parts of this skin. * URLs to the parts of this skin.
* <p> * <p>
* The key is the part name, and * The key is the part, and the
* the value is the URL. * value is the URL to the part.
* </p> * </p>
*/ */
parts: { parts: {
[key: string]: string; [part in SkinPart]: string;
}; };
}; };
/** /**
* Possible models for a skin. * Possible models for a skin.
*/ */
export type SkinModel = "default" | "slim"; enum SkinModel {
DEFAULT,
SLIM,
}
/**
* A part of a skin texture.
*/
export enum SkinPart {
HEAD_OVERLAY_FACE,
HEAD_TOP,
HEAD,
FACE,
HEAD_LEFT,
HEAD_RIGHT,
HEAD_BOTTOM,
HEAD_BACK,
BODY_FRONT,
BODY,
LEFT_ARM_TOP,
RIGHT_ARM_TOP,
LEFT_ARM_FRONT,
RIGHT_ARM_FRONT,
LEFT_LEG_FRONT,
RIGHT_LEG_FRONT,
}
/** /**
* A cape for a {@link Player}. * A cape for a {@link Player}.
*/ */
export type Cape = { type Cape = {
/** /**
* The texture URL of this cape. * The texture URL of this cape.
*/ */
@ -103,7 +128,7 @@ export type Cape = {
/** /**
* A property of a Mojang profile. * A property of a Mojang profile.
*/ */
export type ProfileProperty = { type ProfileProperty = {
/** /**
* The name of this property. * The name of this property.
*/ */
@ -124,4 +149,7 @@ export type ProfileProperty = {
/** /**
* Profile actions that can * Profile actions that can
*/ */
export type ProfileAction = "FORCED_NAME_CHANGE" | "USING_BANNED_SKIN"; enum ProfileAction {
FORCED_NAME_CHANGE,
USING_BANNED_SKIN,
}

View File

@ -1,4 +1,4 @@
import { MinecraftServer } from "@/types/server"; import { MinecraftServer } from "./server";
/** /**
* A cacheable {@link BedrockMinecraftServer}. * A cacheable {@link BedrockMinecraftServer}.
@ -28,7 +28,7 @@ export interface BedrockMinecraftServer extends MinecraftServer {
/** /**
* The version information of this server. * The version information of this server.
*/ */
version: Version; version: BedrockVersion;
/** /**
* The gamemode of this server. * The gamemode of this server.
@ -39,12 +39,15 @@ export interface BedrockMinecraftServer extends MinecraftServer {
/** /**
* The edition of a Bedrock server. * The edition of a Bedrock server.
*/ */
export type Edition = "MCPE" | "MCEE"; enum Edition {
MCPE,
MCEE,
}
/** /**
* Version information for a server. * Version information for a server.
*/ */
export type Version = { type BedrockVersion = {
/** /**
* The protocol version of the server. * The protocol version of the server.
*/ */
@ -59,7 +62,7 @@ export type Version = {
/** /**
* The gamemode of a server. * The gamemode of a server.
*/ */
export type GameMode = { type GameMode = {
/** /**
* The name of this gamemode. * The name of this gamemode.
*/ */

View File

@ -1,4 +1,4 @@
import { MinecraftServer } from "@/types/server"; import { MinecraftServer } from "./server";
/** /**
* A cacheable {@link JavaMinecraftServer}. * A cacheable {@link JavaMinecraftServer}.
@ -18,7 +18,7 @@ export interface JavaMinecraftServer extends MinecraftServer {
/** /**
* The version information of this server. * The version information of this server.
*/ */
version: Version; version: JavaVersion;
/** /**
* The favicon of this server, undefined if none. * The favicon of this server, undefined if none.
@ -70,7 +70,7 @@ export interface JavaMinecraftServer extends MinecraftServer {
/** /**
* Version information for a server. * Version information for a server.
*/ */
export type Version = { type JavaVersion = {
/** /**
* The version name of the server. * The version name of the server.
*/ */
@ -100,7 +100,7 @@ export type Version = {
/** /**
* The favicon for a server. * The favicon for a server.
*/ */
export type Favicon = { type Favicon = {
/** /**
* The raw Base64 encoded favicon. * The raw Base64 encoded favicon.
*/ */
@ -118,7 +118,7 @@ export type Favicon = {
* This is for servers on 1.12 or below. * This is for servers on 1.12 or below.
* </p> * </p>
*/ */
export type ModInfo = { type ModInfo = {
/** /**
* The type of modded server this is. * The type of modded server this is.
*/ */
@ -133,7 +133,7 @@ export type ModInfo = {
/** /**
* A legacy Forge mod for a server. * A legacy Forge mod for a server.
*/ */
export type LegacyForgeMod = { type LegacyForgeMod = {
/** /**
* The name of this mod. * The name of this mod.
*/ */
@ -151,7 +151,7 @@ export type LegacyForgeMod = {
* This is for servers on 1.13 and above. * This is for servers on 1.13 and above.
* </p> * </p>
*/ */
export type ForgeData = { type ForgeData = {
/** /**
* The list of channels on this server, empty if none. * The list of channels on this server, empty if none.
*/ */
@ -180,7 +180,7 @@ export type ForgeData = {
/** /**
* A Forge channel for a server. * A Forge channel for a server.
*/ */
export type ForgeChannel = { type ForgeChannel = {
/** /**
* The name of this channel. * The name of this channel.
*/ */
@ -200,7 +200,7 @@ export type ForgeChannel = {
/** /**
* A modern Forge mod for a server. * A modern Forge mod for a server.
*/ */
export type ModernForgeMod = { type ModernForgeMod = {
/** /**
* The name of this mod. * The name of this mod.
*/ */

View File

@ -36,7 +36,7 @@ export type MinecraftServer = {
/** /**
* Player count data for a server. * Player count data for a server.
*/ */
export type Players = { type Players = {
/** /**
* The online players on this server. * The online players on this server.
*/ */
@ -56,7 +56,7 @@ export type Players = {
/** /**
* A sample player. * A sample player.
*/ */
export type PlayerSample = { type PlayerSample = {
/** /**
* The ID of this player. * The ID of this player.
*/ */
@ -71,7 +71,7 @@ export type PlayerSample = {
/** /**
* The name of a sample player. * The name of a sample player.
*/ */
export type PlayerSampleName = { type PlayerSampleName = {
/** /**
* The raw name. * The raw name.
*/ */
@ -91,7 +91,7 @@ export type PlayerSampleName = {
/** /**
* The MOTD for a server. * The MOTD for a server.
*/ */
export type MOTD = { type MOTD = {
/** /**
* The raw MOTD lines. * The raw MOTD lines.
*/ */
@ -112,9 +112,14 @@ export type MOTD = {
* A platform a Minecraft * A platform a Minecraft
* server can operate on. * server can operate on.
*/ */
export type Platform = "java" | "bedrock"; export enum Platform {
/**
* The Java edition of Minecraft.
*/
JAVA = "java",
/** /**
* Types of a DNS record. * The Bedrock edition of Minecraft.
*/ */
export type RecordType = "A" | "SRV"; BEDROCK = "bedrock",
}

View File

@ -11,11 +11,6 @@
"allowJs": true, "allowJs": true,
"noEmit": true, "noEmit": true,
"outDir": "dist", "outDir": "dist",
"resolveJsonModule": true, "resolveJsonModule": true
"paths": {
"@/*": ["./*"],
"@/lib/*": ["./src/lib/*"],
"@/types/*": ["./src/types/*"]
}
} }
} }