mcutils-js-api 1.0.5 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,15 +1,20 @@
1
- # mcutils-js-api
1
+ # Minecraft Utilities - JavaScript API
2
2
 
3
- To install dependencies:
3
+ TypeScript client for the [McUtils API](https://mc.fascinated.cc/api) - server status, player lookups, skins, that sort of thing.
4
4
 
5
5
  ```bash
6
- bun install
6
+ bun add mcutils-js-api
7
7
  ```
8
8
 
9
- To run:
9
+ ```typescript
10
+ import McUtilsAPI from "mcutils-js-api";
10
11
 
11
- ```bash
12
- bun run src/index.ts
13
- ```
12
+ const api = new McUtilsAPI();
13
+ const { player, error } = await api.fetchPlayer("ImFascinated");
14
14
 
15
- This project was created using `bun init` in bun v1.3.1. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime.
15
+ if (error) {
16
+ console.error(error.message);
17
+ } else {
18
+ console.log(player?.username);
19
+ }
20
+ ```
package/dist/index.d.ts CHANGED
@@ -1,23 +1,114 @@
1
1
  import type { ErrorResponse } from "./types/response/error-response";
2
- import { BedrockServer } from "./types/server/bedrock-server";
3
- import type { JavaServer } from "./types/server/java-server";
4
- /**
5
- * Fetch a Java Minecraft server.
6
- *
7
- * @param host the host to fetch the server using (eg: aetheria.cc)
8
- * @returns the server or the error (if one occured)
9
- */
10
- export declare function fetchJavaServer(host: string): Promise<{
11
- server?: JavaServer;
12
- error?: ErrorResponse;
13
- }>;
14
- /**
15
- * Fetch a Bedrock Minecraft server.
16
- *
17
- * @param host the host to fetch the server using (eg: geo.hivebedrock.network)
18
- * @returns the server or the error (if one occured)
19
- */
20
- export declare function fetchBedrockServer(host: string): Promise<{
21
- server?: BedrockServer;
22
- error?: ErrorResponse;
23
- }>;
2
+ import type { BedrockServer } from "./types/server/impl/bedrock-server";
3
+ import type { JavaServer } from "./types/server/impl/java-server";
4
+ import type { CachedPlayer } from "./types/cache/cached-player";
5
+ import type { CachedPlayerName } from "./types/cache/cached-player-name";
6
+ export declare class McUtilsAPI {
7
+ private readonly endpoint;
8
+ constructor(endpoint?: string);
9
+ /**
10
+ * Build URL search params string from a record of key-value pairs.
11
+ *
12
+ * @param params record of param names to string values
13
+ * @returns query string including leading `?`, or empty string if no params
14
+ */
15
+ buildParams(params: Record<string, string>): string;
16
+ /**
17
+ * Fetch a Java Minecraft server.
18
+ *
19
+ * @param host the host to fetch the server using (eg: aetheria.cc)
20
+ * @returns the server or the error (if one occurred)
21
+ */
22
+ fetchJavaServer(host: string): Promise<{
23
+ server?: JavaServer;
24
+ error?: ErrorResponse;
25
+ }>;
26
+ /**
27
+ * Fetch a Bedrock Minecraft server.
28
+ *
29
+ * @param host the host to fetch the server using (eg: geo.hivebedrock.network)
30
+ * @returns the server or the error (if one occurred)
31
+ */
32
+ fetchBedrockServer(host: string): Promise<{
33
+ server?: BedrockServer;
34
+ error?: ErrorResponse;
35
+ }>;
36
+ /**
37
+ * Fetch whether a server is blocked by Mojang.
38
+ *
39
+ * @param host the hostname to check (eg: aetheria.cc)
40
+ * @returns the blocked status or the error (if one occurred)
41
+ */
42
+ fetchServerBlocked(host: string): Promise<{
43
+ blocked?: boolean;
44
+ error?: ErrorResponse;
45
+ }>;
46
+ /**
47
+ * Fetch a player by UUID or username.
48
+ *
49
+ * @param id the UUID or username of the player (eg: ImFascinated)
50
+ * @returns the player or the error (if one occurred)
51
+ */
52
+ fetchPlayer(id: string): Promise<{
53
+ player?: CachedPlayer;
54
+ error?: ErrorResponse;
55
+ }>;
56
+ /**
57
+ * Resolve a username to UUID (or UUID to username).
58
+ *
59
+ * @param id the UUID or username to resolve (eg: ImFascinated)
60
+ * @returns the player name data or the error (if one occurred)
61
+ */
62
+ fetchPlayerUuid(id: string): Promise<{
63
+ playerName?: CachedPlayerName;
64
+ error?: ErrorResponse;
65
+ }>;
66
+ /**
67
+ * Fetch a server favicon/icon image.
68
+ *
69
+ * @param host the hostname of the server (eg: aetheria.cc)
70
+ * @returns the PNG image or the error (if one occurred)
71
+ */
72
+ fetchServerIcon(host: string): Promise<{
73
+ image?: ArrayBuffer;
74
+ error?: ErrorResponse;
75
+ }>;
76
+ /**
77
+ * Fetch a server preview image.
78
+ *
79
+ * @param platform the platform (java or bedrock)
80
+ * @param host the hostname of the server (eg: aetheria.cc)
81
+ * @param size the image size (default: 768)
82
+ * @returns the PNG image or the error (if one occurred)
83
+ */
84
+ fetchServerPreview(platform: string, host: string, size?: number): Promise<{
85
+ image?: ArrayBuffer;
86
+ error?: ErrorResponse;
87
+ }>;
88
+ /**
89
+ * Fetch a player's skin image.
90
+ *
91
+ * @param id the UUID or username of the player (eg: ImFascinated)
92
+ * @param extension the image format - png or jpg (default: png)
93
+ * @returns the skin image or the error (if one occurred)
94
+ */
95
+ fetchPlayerSkin(id: string, extension?: string): Promise<{
96
+ image?: ArrayBuffer;
97
+ error?: ErrorResponse;
98
+ }>;
99
+ /**
100
+ * Fetch a specific part of a player's skin (eg: head, body).
101
+ *
102
+ * @param id the UUID or username of the player (eg: ImFascinated)
103
+ * @param part the skin part to fetch (eg: head)
104
+ * @param extension the image format - png or jpg (default: png)
105
+ * @param size the image size (default: 256)
106
+ * @param overlays whether to render skin overlay layers (default: false)
107
+ * @returns the skin part image or the error (if one occurred)
108
+ */
109
+ fetchPlayerSkinPart(id: string, part: string, extension?: string, size?: number, overlays?: boolean): Promise<{
110
+ image?: ArrayBuffer;
111
+ error?: ErrorResponse;
112
+ }>;
113
+ }
114
+ export default McUtilsAPI;
package/dist/index.js CHANGED
@@ -1,35 +1,167 @@
1
- const API_BASE = process.env.MCUTILS_API_BASE || "https://mc.fascinated.cc/api";
2
- /**
3
- * Fetch a Java Minecraft server.
4
- *
5
- * @param host the host to fetch the server using (eg: aetheria.cc)
6
- * @returns the server or the error (if one occured)
7
- */
8
- export async function fetchJavaServer(host) {
9
- const response = await fetch(`${API_BASE}/server/java/${host}`);
10
- if (response.ok) {
11
- return {
12
- server: (await response.json()),
13
- };
14
- }
15
- return {
16
- error: (await response.json()),
17
- };
18
- }
19
- /**
20
- * Fetch a Bedrock Minecraft server.
21
- *
22
- * @param host the host to fetch the server using (eg: geo.hivebedrock.network)
23
- * @returns the server or the error (if one occured)
24
- */
25
- export async function fetchBedrockServer(host) {
26
- const response = await fetch(`${API_BASE}/server/bedrock/${host}`);
27
- if (response.ok) {
28
- return {
29
- server: (await response.json()),
30
- };
31
- }
32
- return {
33
- error: (await response.json()),
34
- };
1
+ export class McUtilsAPI {
2
+ constructor(endpoint = "https://mc.fascinated.cc/api") {
3
+ this.endpoint = endpoint;
4
+ }
5
+ /**
6
+ * Build URL search params string from a record of key-value pairs.
7
+ *
8
+ * @param params record of param names to string values
9
+ * @returns query string including leading `?`, or empty string if no params
10
+ */
11
+ buildParams(params) {
12
+ const str = new URLSearchParams(params).toString();
13
+ return str ? `?${str}` : "";
14
+ }
15
+ /**
16
+ * Fetch a Java Minecraft server.
17
+ *
18
+ * @param host the host to fetch the server using (eg: aetheria.cc)
19
+ * @returns the server or the error (if one occurred)
20
+ */
21
+ async fetchJavaServer(host) {
22
+ const response = await fetch(`${this.endpoint}/server/java/${host}`);
23
+ if (response.ok) {
24
+ return {
25
+ server: (await response.json()),
26
+ };
27
+ }
28
+ return {
29
+ error: (await response.json()),
30
+ };
31
+ }
32
+ /**
33
+ * Fetch a Bedrock Minecraft server.
34
+ *
35
+ * @param host the host to fetch the server using (eg: geo.hivebedrock.network)
36
+ * @returns the server or the error (if one occurred)
37
+ */
38
+ async fetchBedrockServer(host) {
39
+ const response = await fetch(`${this.endpoint}/server/bedrock/${host}`);
40
+ if (response.ok) {
41
+ return {
42
+ server: (await response.json()),
43
+ };
44
+ }
45
+ return {
46
+ error: (await response.json()),
47
+ };
48
+ }
49
+ /**
50
+ * Fetch whether a server is blocked by Mojang.
51
+ *
52
+ * @param host the hostname to check (eg: aetheria.cc)
53
+ * @returns the blocked status or the error (if one occurred)
54
+ */
55
+ async fetchServerBlocked(host) {
56
+ const response = await fetch(`${this.endpoint}/server/blocked/${host}`);
57
+ if (response.ok) {
58
+ const json = (await response.json());
59
+ return { blocked: json.blocked };
60
+ }
61
+ return {
62
+ error: (await response.json()),
63
+ };
64
+ }
65
+ /**
66
+ * Fetch a player by UUID or username.
67
+ *
68
+ * @param id the UUID or username of the player (eg: ImFascinated)
69
+ * @returns the player or the error (if one occurred)
70
+ */
71
+ async fetchPlayer(id) {
72
+ const response = await fetch(`${this.endpoint}/player/${id}`);
73
+ if (response.ok) {
74
+ return {
75
+ player: (await response.json()),
76
+ };
77
+ }
78
+ return {
79
+ error: (await response.json()),
80
+ };
81
+ }
82
+ /**
83
+ * Resolve a username to UUID (or UUID to username).
84
+ *
85
+ * @param id the UUID or username to resolve (eg: ImFascinated)
86
+ * @returns the player name data or the error (if one occurred)
87
+ */
88
+ async fetchPlayerUuid(id) {
89
+ const response = await fetch(`${this.endpoint}/player/uuid/${id}`);
90
+ if (response.ok) {
91
+ return {
92
+ playerName: (await response.json()),
93
+ };
94
+ }
95
+ return {
96
+ error: (await response.json()),
97
+ };
98
+ }
99
+ /**
100
+ * Fetch a server favicon/icon image.
101
+ *
102
+ * @param host the hostname of the server (eg: aetheria.cc)
103
+ * @returns the PNG image or the error (if one occurred)
104
+ */
105
+ async fetchServerIcon(host) {
106
+ const response = await fetch(`${this.endpoint}/server/icon/${host}`);
107
+ if (response.ok) {
108
+ return { image: await response.arrayBuffer() };
109
+ }
110
+ return {
111
+ error: (await response.json()),
112
+ };
113
+ }
114
+ /**
115
+ * Fetch a server preview image.
116
+ *
117
+ * @param platform the platform (java or bedrock)
118
+ * @param host the hostname of the server (eg: aetheria.cc)
119
+ * @param size the image size (default: 768)
120
+ * @returns the PNG image or the error (if one occurred)
121
+ */
122
+ async fetchServerPreview(platform, host, size = 768) {
123
+ const response = await fetch(`${this.endpoint}/server/${platform}/preview/${host}${this.buildParams({ size: String(size) })}`);
124
+ if (response.ok) {
125
+ return { image: await response.arrayBuffer() };
126
+ }
127
+ return {
128
+ error: (await response.json()),
129
+ };
130
+ }
131
+ /**
132
+ * Fetch a player's skin image.
133
+ *
134
+ * @param id the UUID or username of the player (eg: ImFascinated)
135
+ * @param extension the image format - png or jpg (default: png)
136
+ * @returns the skin image or the error (if one occurred)
137
+ */
138
+ async fetchPlayerSkin(id, extension = "png") {
139
+ const response = await fetch(`${this.endpoint}/player/${id}/skin.${extension}`);
140
+ if (response.ok) {
141
+ return { image: await response.arrayBuffer() };
142
+ }
143
+ return {
144
+ error: (await response.json()),
145
+ };
146
+ }
147
+ /**
148
+ * Fetch a specific part of a player's skin (eg: head, body).
149
+ *
150
+ * @param id the UUID or username of the player (eg: ImFascinated)
151
+ * @param part the skin part to fetch (eg: head)
152
+ * @param extension the image format - png or jpg (default: png)
153
+ * @param size the image size (default: 256)
154
+ * @param overlays whether to render skin overlay layers (default: false)
155
+ * @returns the skin part image or the error (if one occurred)
156
+ */
157
+ async fetchPlayerSkinPart(id, part, extension = "png", size = 256, overlays = false) {
158
+ const response = await fetch(`${this.endpoint}/player/${id}/skin/${part}.${extension}${this.buildParams({ size: String(size), overlays: String(overlays) })}`);
159
+ if (response.ok) {
160
+ return { image: await response.arrayBuffer() };
161
+ }
162
+ return {
163
+ error: (await response.json()),
164
+ };
165
+ }
35
166
  }
167
+ export default McUtilsAPI;
package/dist/test.js ADDED
@@ -0,0 +1,8 @@
1
+ import McUtilsAPI from ".";
2
+ const api = new McUtilsAPI();
3
+ api.fetchJavaServer("aetheria.cc").then((result) => {
4
+ console.log(result);
5
+ });
6
+ api.fetchPlayer("ImFascinated").then((result) => {
7
+ console.log(result);
8
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ import type { Cache } from "./cache";
2
+ export type CachedPlayerNameData = {
3
+ username: string;
4
+ uniqueId: string;
5
+ };
6
+ export type CachedPlayerName = Cache & CachedPlayerNameData;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ import type { Cache } from "./cache";
2
+ import type { Player } from "../player/player";
3
+ export type CachedPlayer = Cache & Player;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,5 @@
1
+ import type { ARecord } from "./impl/a-record";
2
+ import type { SRVRecord } from "./impl/srv-record";
3
+ export type { ARecord } from "./impl/a-record";
4
+ export type { SRVRecord } from "./impl/srv-record";
5
+ export type DnsRecord = ARecord | SRVRecord;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ export interface ARecord {
2
+ type: "A";
3
+ ttl: number;
4
+ name?: string;
5
+ address?: string;
6
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,9 @@
1
+ export interface SRVRecord {
2
+ type: "SRV";
3
+ ttl: number;
4
+ name: string;
5
+ target: string;
6
+ priority: number;
7
+ weight: number;
8
+ port: number;
9
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,24 @@
1
+ export type SkinModel = "DEFAULT" | "SLIM";
2
+ export type SkinParts = Record<string, string>;
3
+ export type Skin = {
4
+ model: SkinModel;
5
+ legacy: boolean;
6
+ url: string;
7
+ parts: SkinParts;
8
+ };
9
+ export type Cape = {
10
+ id?: string;
11
+ };
12
+ export type ProfileProperty = {
13
+ name: string;
14
+ value: string;
15
+ signature?: string;
16
+ };
17
+ export type Player = {
18
+ uniqueId: string;
19
+ username: string;
20
+ legacyAccount: boolean;
21
+ skin?: Skin | null;
22
+ cape?: Cape | null;
23
+ rawProperties?: ProfileProperty[];
24
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -1,5 +1,6 @@
1
- import type { Server } from "./server";
1
+ import type { Server } from "../server";
2
2
  export interface BedrockServer extends Server {
3
+ id: string;
3
4
  edition: ServerEdition;
4
5
  version: ServerVersion;
5
6
  gamemode: ServerGamemode;
@@ -0,0 +1,40 @@
1
+ import type { Server } from "../server";
2
+ export interface JavaServer extends Server {
3
+ version: ServerVersion;
4
+ favicon: ServerFavicon | null;
5
+ modInfo?: ForgeModInfo;
6
+ forgeData?: ForgeData;
7
+ preventsChatReports: boolean;
8
+ enforcesSecureChat: boolean;
9
+ previewsChat: boolean;
10
+ mojangBlocked: boolean;
11
+ }
12
+ export type ServerVersion = {
13
+ name: string;
14
+ platform: string;
15
+ protocol: number;
16
+ protocolName: string;
17
+ };
18
+ export type ServerFavicon = {
19
+ base64?: string;
20
+ url: string;
21
+ };
22
+ export type ForgeModInfo = {
23
+ type: string;
24
+ modList?: ForgeMod[];
25
+ };
26
+ export type ForgeData = {
27
+ channels?: ForgeChannel[];
28
+ mods?: ForgeMod[];
29
+ truncated: boolean;
30
+ fmlNetworkVersion: number;
31
+ };
32
+ export type ForgeChannel = {
33
+ name: string;
34
+ version: string;
35
+ required?: boolean;
36
+ };
37
+ export type ForgeMod = {
38
+ name: string;
39
+ version: string;
40
+ };
@@ -1,13 +1,14 @@
1
- import type { Cache } from "../cache";
1
+ import type { Cache } from "../cache/cache";
2
+ import type { DnsRecord } from "../dns/dns-record";
2
3
  export interface Server extends Cache {
3
4
  hostname: string;
4
5
  ip: string;
5
6
  port: number;
6
7
  motd: ServerMotd;
7
8
  players: ServerPlayers;
8
- records: Array<DnsRecord>;
9
- location: ServerLocation;
10
- asn: AsnData;
9
+ records: DnsRecord[];
10
+ location?: ServerLocation | null;
11
+ asn?: AsnData | null;
11
12
  }
12
13
  export type ServerMotd = {
13
14
  raw: string[];
@@ -15,14 +16,20 @@ export type ServerMotd = {
15
16
  html: string[];
16
17
  preview: string;
17
18
  };
19
+ export type ServerPlayerSampleName = {
20
+ raw: string;
21
+ clean: string;
22
+ html: string;
23
+ };
24
+ export type ServerPlayerSample = {
25
+ id: string;
26
+ name: ServerPlayerSampleName;
27
+ url: string;
28
+ };
18
29
  export type ServerPlayers = {
19
30
  online: number;
20
31
  max: number;
21
- };
22
- export type DnsRecord = {
23
- type: string;
24
- ttl: number;
25
- address: string;
32
+ sample?: ServerPlayerSample[];
26
33
  };
27
34
  export type ServerLocation = {
28
35
  country: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mcutils-js-api",
3
- "version": "1.0.5",
3
+ "version": "2.0.1",
4
4
  "module": "dist/index.js",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -14,5 +14,9 @@
14
14
  "devDependencies": {
15
15
  "@types/bun": "latest",
16
16
  "typescript": "^5"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "https://github.com/Minecraft-Utilities/JS-API"
17
21
  }
18
22
  }
@@ -1,18 +0,0 @@
1
- import type { Server } from "./server";
2
- export interface JavaServer extends Server {
3
- version: ServerVersion;
4
- favicon: ServerFavicon;
5
- preventsChatReports: boolean;
6
- enforcesSecureChat: boolean;
7
- previewsChat: boolean;
8
- mojangBlocked: boolean;
9
- }
10
- export type ServerVersion = {
11
- name: string;
12
- platform: string;
13
- protocol: number;
14
- protocolName: string;
15
- };
16
- export type ServerFavicon = {
17
- url: string;
18
- };
File without changes
File without changes