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",
"version": "1.0.0",
"version": "1.0.1",
"author": "Braydon (Rainnny) <braydonrainnny@gmail.com>",
"description": "A simple, yet useful RESTful API for Minecraft utilizing Springboot.",
"keywords": [

View File

@ -1,5 +1,6 @@
export * from "./types/dns";
export * from "./types/generic";
export * from "./types/mojang";
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 type { CachedPlayer } from "@/types/player";
import { Platform } from "@/types/server";
import { CachedBedrockMinecraftServer } from "@/types/server/bedrock-server";
import { CachedJavaMinecraftServer } from "@/types/server/java-server";
import { MojangServerStatus } from "../types/mojang";
import { CachedPlayer, SkinPart } from "../types/player";
import { CachedBedrockMinecraftServer } from "../types/server/bedrock-server";
import { CachedJavaMinecraftServer } from "../types/server/java-server";
import { Platform } from "../types/server/server";
import { WebRequest } from "./webRequest";
/**
* Get a player by their username or UUID.
@ -12,7 +12,27 @@ import { CachedJavaMinecraftServer } from "@/types/server/java-server";
* @returns the promised player
*/
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.
@ -25,13 +45,13 @@ export const getMinecraftServer = (
platform: Platform,
hostname: string
): Promise<CachedJavaMinecraftServer | CachedBedrockMinecraftServer> =>
platform === "java"
? makeWebRequest<CachedJavaMinecraftServer>(
platform === Platform.JAVA
? new WebRequest(
`/server/${platform}/${hostname}`
)
: makeWebRequest<CachedBedrockMinecraftServer>(
).execute<CachedJavaMinecraftServer>()
: new WebRequest(
`/server/${platform}/${hostname}`
);
).execute<CachedBedrockMinecraftServer>();
/**
* Check if the server with the
@ -41,7 +61,17 @@ export const getMinecraftServer = (
* @returns the promised blocked status
*/
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.
@ -49,4 +79,4 @@ export const isMojangBlocked = (hostname: string): Promise<boolean> =>
* @returns the promised status
*/
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
/**
* 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> =>
new Promise(async (resolve, reject) => {
const response: Response = await fetch(`${ENDPOINT}/${endpoint}`); // Request the player
const json: any = await response.json();
export class WebRequest {
/**
* The endpoint to make the request to.
*/
endpoint: string;
// Resolve the response
if (response.ok) {
resolve(json as T);
} else {
reject(json as ErrorResponse); // The request failed
}
});
constructor(endpoint: string) {
this.endpoint = endpoint;
}
/**
* 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.
*/
export interface ARecord extends DNSRecord {
interface ARecord extends DNSRecord {
/**
* The address of this record, undefined if unresolved.
*/
@ -11,7 +11,7 @@ export interface ARecord extends DNSRecord {
/**
* An SRV record.
*/
export interface SRVRecord extends DNSRecord {
interface SRVRecord extends DNSRecord {
/**
* The priority of this record.
*/
@ -36,7 +36,7 @@ export interface SRVRecord extends DNSRecord {
/**
* A representation of a DNS record.
*/
export type DNSRecord = {
type DNSRecord = {
/**
* The type of this record.
*/
@ -47,3 +47,11 @@ export type DNSRecord = {
*/
ttl: number;
};
/**
* Types of a DNS record.
*/
enum RecordType {
A,
SRV,
}

View File

@ -3,5 +3,14 @@
* a service provided by Mojang.
*/
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}.
*/
export type Skin = {
type Skin = {
/**
* The texture URL of this skin.
*/
@ -76,24 +76,49 @@ export type Skin = {
/**
* URLs to the parts of this skin.
* <p>
* The key is the part name, and
* the value is the URL.
* The key is the part, and the
* value is the URL to the part.
* </p>
*/
parts: {
[key: string]: string;
[part in SkinPart]: string;
};
};
/**
* 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}.
*/
export type Cape = {
type Cape = {
/**
* The texture URL of this cape.
*/
@ -103,7 +128,7 @@ export type Cape = {
/**
* A property of a Mojang profile.
*/
export type ProfileProperty = {
type ProfileProperty = {
/**
* The name of this property.
*/
@ -124,4 +149,7 @@ export type ProfileProperty = {
/**
* 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}.
@ -28,7 +28,7 @@ export interface BedrockMinecraftServer extends MinecraftServer {
/**
* The version information of this server.
*/
version: Version;
version: BedrockVersion;
/**
* The gamemode of this server.
@ -39,12 +39,15 @@ export interface BedrockMinecraftServer extends MinecraftServer {
/**
* The edition of a Bedrock server.
*/
export type Edition = "MCPE" | "MCEE";
enum Edition {
MCPE,
MCEE,
}
/**
* Version information for a server.
*/
export type Version = {
type BedrockVersion = {
/**
* The protocol version of the server.
*/
@ -59,7 +62,7 @@ export type Version = {
/**
* The gamemode of a server.
*/
export type GameMode = {
type 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}.
@ -18,7 +18,7 @@ export interface JavaMinecraftServer extends MinecraftServer {
/**
* The version information of this server.
*/
version: Version;
version: JavaVersion;
/**
* The favicon of this server, undefined if none.
@ -70,7 +70,7 @@ export interface JavaMinecraftServer extends MinecraftServer {
/**
* Version information for a server.
*/
export type Version = {
type JavaVersion = {
/**
* The version name of the server.
*/
@ -100,7 +100,7 @@ export type Version = {
/**
* The favicon for a server.
*/
export type Favicon = {
type Favicon = {
/**
* The raw Base64 encoded favicon.
*/
@ -118,7 +118,7 @@ export type Favicon = {
* This is for servers on 1.12 or below.
* </p>
*/
export type ModInfo = {
type ModInfo = {
/**
* The type of modded server this is.
*/
@ -133,7 +133,7 @@ export type ModInfo = {
/**
* A legacy Forge mod for a server.
*/
export type LegacyForgeMod = {
type LegacyForgeMod = {
/**
* The name of this mod.
*/
@ -151,7 +151,7 @@ export type LegacyForgeMod = {
* This is for servers on 1.13 and above.
* </p>
*/
export type ForgeData = {
type ForgeData = {
/**
* The list of channels on this server, empty if none.
*/
@ -180,7 +180,7 @@ export type ForgeData = {
/**
* A Forge channel for a server.
*/
export type ForgeChannel = {
type ForgeChannel = {
/**
* The name of this channel.
*/
@ -200,7 +200,7 @@ export type ForgeChannel = {
/**
* A modern Forge mod for a server.
*/
export type ModernForgeMod = {
type ModernForgeMod = {
/**
* The name of this mod.
*/

View File

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

View File

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