mcutils-js-api 2.0.36 → 2.0.38

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/dist/index.d.ts CHANGED
@@ -10,9 +10,19 @@ import { CachedPlayerName } from "./types/cache/cached-player-name";
10
10
  import { StatisticsResponse } from "./types/response/statistics-response";
11
11
  import { Skin } from "./types/player/skin/skin";
12
12
  import { Page } from "./types/pagination/pagination";
13
+ import { PlayerSearchEntry } from "./types/player/player-search-entry";
13
14
  export declare class McUtilsAPI {
14
15
  private readonly endpoint;
15
- constructor(endpoint?: string);
16
+ private readonly fetchOptions?;
17
+ constructor(endpoint?: string, fetchOptions?: RequestInit);
18
+ /**
19
+ * Requests data from the API.
20
+ *
21
+ * @param path the path to the API endpoint
22
+ * @param options the options for the request
23
+ * @returns the data or the error (if one occurred)
24
+ */
25
+ private request;
16
26
  /**
17
27
  * Build URL search params string from a record of key-value pairs.
18
28
  *
@@ -196,5 +206,15 @@ export declare class McUtilsAPI {
196
206
  skins?: Page<Skin>;
197
207
  error?: ErrorResponse;
198
208
  }>;
209
+ /**
210
+ * Search for players by username.
211
+ *
212
+ * @param query the query to search for (eg: aetheria)
213
+ * @returns the player search entry or the error (if one occurred)
214
+ */
215
+ searchPlayers(query: string): Promise<{
216
+ entry?: PlayerSearchEntry;
217
+ error?: ErrorResponse;
218
+ }>;
199
219
  }
200
220
  export default McUtilsAPI;
package/dist/index.js CHANGED
@@ -1,6 +1,24 @@
1
1
  export class McUtilsAPI {
2
- constructor(endpoint = "https://mc.fascinated.cc/api") {
2
+ constructor(endpoint = "https://mc.fascinated.cc/api", fetchOptions) {
3
3
  this.endpoint = endpoint;
4
+ this.fetchOptions = fetchOptions;
5
+ }
6
+ /**
7
+ * Requests data from the API.
8
+ *
9
+ * @param path the path to the API endpoint
10
+ * @param options the options for the request
11
+ * @returns the data or the error (if one occurred)
12
+ */
13
+ async request(path, options) {
14
+ const { responseType = "json", ...init } = options ?? {};
15
+ const url = path.startsWith("http") ? path : `${this.endpoint}${path}`;
16
+ const response = await fetch(url, { ...this.fetchOptions, ...init });
17
+ if (!response.ok) {
18
+ return { error: (await response.json()) };
19
+ }
20
+ const data = responseType === "arrayBuffer" ? await response.arrayBuffer() : await response.json();
21
+ return { data: data };
4
22
  }
5
23
  /**
6
24
  * Build URL search params string from a record of key-value pairs.
@@ -20,15 +38,8 @@ export class McUtilsAPI {
20
38
  * @returns the server or the error (if one occurred)
21
39
  */
22
40
  async fetchServer(host, type) {
23
- const response = await fetch(`${this.endpoint}/servers/${type}/${host}`);
24
- if (response.ok) {
25
- return {
26
- server: (await response.json()),
27
- };
28
- }
29
- return {
30
- error: (await response.json()),
31
- };
41
+ const { data, error } = await this.request(`/servers/${type}/${host}`);
42
+ return error ? { error } : { server: data };
32
43
  }
33
44
  /**
34
45
  * Fetch a Java Minecraft server.
@@ -37,15 +48,8 @@ export class McUtilsAPI {
37
48
  * @returns the server or the error (if one occurred)
38
49
  */
39
50
  async fetchJavaServer(host) {
40
- const response = await fetch(`${this.endpoint}/servers/java/${host}`);
41
- if (response.ok) {
42
- return {
43
- server: (await response.json()),
44
- };
45
- }
46
- return {
47
- error: (await response.json()),
48
- };
51
+ const { data, error } = await this.request(`/servers/java/${host}`);
52
+ return error ? { error } : { server: data };
49
53
  }
50
54
  /**
51
55
  * Fetch a Bedrock Minecraft server.
@@ -54,15 +58,8 @@ export class McUtilsAPI {
54
58
  * @returns the server or the error (if one occurred)
55
59
  */
56
60
  async fetchBedrockServer(host) {
57
- const response = await fetch(`${this.endpoint}/servers/bedrock/${host}`);
58
- if (response.ok) {
59
- return {
60
- server: (await response.json()),
61
- };
62
- }
63
- return {
64
- error: (await response.json()),
65
- };
61
+ const { data, error } = await this.request(`/servers/bedrock/${host}`);
62
+ return error ? { error } : { server: data };
66
63
  }
67
64
  /**
68
65
  * Fetch whether a server is blocked by Mojang.
@@ -71,14 +68,10 @@ export class McUtilsAPI {
71
68
  * @returns the blocked status or the error (if one occurred)
72
69
  */
73
70
  async fetchServerBlocked(host) {
74
- const response = await fetch(`${this.endpoint}/servers/blocked/${host}`);
75
- if (response.ok) {
76
- const json = (await response.json());
77
- return { blocked: json.blocked };
78
- }
79
- return {
80
- error: (await response.json()),
81
- };
71
+ const { data, error } = await this.request(`/servers/blocked/${host}`);
72
+ if (error)
73
+ return { error };
74
+ return { blocked: data.blocked };
82
75
  }
83
76
  /**
84
77
  * Look up IP address (geo + ASN).
@@ -87,13 +80,7 @@ export class McUtilsAPI {
87
80
  * @returns the IP lookup response or the error (if one occurred)
88
81
  */
89
82
  async fetchIpLookup(query) {
90
- const response = await fetch(`${this.endpoint}/ips/${query}`);
91
- if (response.ok) {
92
- return { data: (await response.json()) };
93
- }
94
- return {
95
- error: (await response.json()),
96
- };
83
+ return this.request(`/ips/${query}`);
97
84
  }
98
85
  /**
99
86
  * Fetch a player by UUID or username.
@@ -102,15 +89,8 @@ export class McUtilsAPI {
102
89
  * @returns the player or the error (if one occurred)
103
90
  */
104
91
  async fetchPlayer(id) {
105
- const response = await fetch(`${this.endpoint}/players/${id}`);
106
- if (response.ok) {
107
- return {
108
- player: (await response.json()),
109
- };
110
- }
111
- return {
112
- error: (await response.json()),
113
- };
92
+ const { data, error } = await this.request(`/players/${id}`);
93
+ return error ? { error } : { player: data };
114
94
  }
115
95
  /**
116
96
  * Resolve a username to UUID (or UUID to username).
@@ -119,15 +99,8 @@ export class McUtilsAPI {
119
99
  * @returns the player name data or the error (if one occurred)
120
100
  */
121
101
  async fetchPlayerUuid(id) {
122
- const response = await fetch(`${this.endpoint}/players/uuid/${id}`);
123
- if (response.ok) {
124
- return {
125
- playerName: (await response.json()),
126
- };
127
- }
128
- return {
129
- error: (await response.json()),
130
- };
102
+ const { data, error } = await this.request(`/players/uuid/${id}`);
103
+ return error ? { error } : { playerName: data };
131
104
  }
132
105
  /**
133
106
  * Fetch a server favicon/icon image.
@@ -136,13 +109,8 @@ export class McUtilsAPI {
136
109
  * @returns the PNG image or the error (if one occurred)
137
110
  */
138
111
  async fetchServerIcon(host) {
139
- const response = await fetch(`${this.endpoint}/servers/icon/${host}`);
140
- if (response.ok) {
141
- return { image: await response.arrayBuffer() };
142
- }
143
- return {
144
- error: (await response.json()),
145
- };
112
+ const { data, error } = await this.request(`/servers/icon/${host}`, { responseType: "arrayBuffer" });
113
+ return error ? { error } : { image: data };
146
114
  }
147
115
  /**
148
116
  * Fetch a server preview image.
@@ -153,13 +121,8 @@ export class McUtilsAPI {
153
121
  * @returns the PNG image or the error (if one occurred)
154
122
  */
155
123
  async fetchServerPreview(platform, host, size = 768) {
156
- const response = await fetch(`${this.endpoint}/servers/${platform}/preview/${host}${this.buildParams({ size: String(size) })}`);
157
- if (response.ok) {
158
- return { image: await response.arrayBuffer() };
159
- }
160
- return {
161
- error: (await response.json()),
162
- };
124
+ const { data, error } = await this.request(`/servers/${platform}/preview/${host}${this.buildParams({ size: String(size) })}`, { responseType: "arrayBuffer" });
125
+ return error ? { error } : { image: data };
163
126
  }
164
127
  /**
165
128
  * Fetch a player's skin image.
@@ -168,13 +131,8 @@ export class McUtilsAPI {
168
131
  * @returns the skin PNG image or the error (if one occurred)
169
132
  */
170
133
  async fetchPlayerSkinTexture(id) {
171
- const response = await fetch(`${this.endpoint}/skins/${id}/texture.png`);
172
- if (response.ok) {
173
- return { image: await response.arrayBuffer() };
174
- }
175
- return {
176
- error: (await response.json()),
177
- };
134
+ const { data, error } = await this.request(`/skins/${id}/texture.png`, { responseType: "arrayBuffer" });
135
+ return error ? { error } : { image: data };
178
136
  }
179
137
  /**
180
138
  * Fetch a specific part of a player's skin (eg: head, body).
@@ -186,13 +144,8 @@ export class McUtilsAPI {
186
144
  * @returns the skin part PNG image or the error (if one occurred)
187
145
  */
188
146
  async fetchPlayerSkin(id, part, size = 768, overlays = true) {
189
- const response = await fetch(`${this.endpoint}/skins/${id}/${part}.png${this.buildParams({ size: String(size), overlays: String(overlays) })}`);
190
- if (response.ok) {
191
- return { image: await response.arrayBuffer() };
192
- }
193
- return {
194
- error: (await response.json()),
195
- };
147
+ const { data, error } = await this.request(`/skins/${id}/${part}.png${this.buildParams({ size: String(size), overlays: String(overlays) })}`, { responseType: "arrayBuffer" });
148
+ return error ? { error } : { image: data };
196
149
  }
197
150
  /**
198
151
  * Fetch the list of available capes (e.g. Migrator).
@@ -200,13 +153,8 @@ export class McUtilsAPI {
200
153
  * @returns the list of cape data or the error (if one occurred)
201
154
  */
202
155
  async fetchCapes() {
203
- const response = await fetch(`${this.endpoint}/capes`);
204
- if (response.ok) {
205
- return { capes: (await response.json()) };
206
- }
207
- return {
208
- error: (await response.json()),
209
- };
156
+ const { data, error } = await this.request(`/capes`);
157
+ return error ? { error } : { capes: data };
210
158
  }
211
159
  /**
212
160
  * Fetch a cape texture image.
@@ -215,13 +163,8 @@ export class McUtilsAPI {
215
163
  * @returns the cape PNG image or the error (if one occurred)
216
164
  */
217
165
  async fetchCapeTexture(query) {
218
- const response = await fetch(`${this.endpoint}/capes/${query}/texture.png`);
219
- if (response.ok) {
220
- return { image: await response.arrayBuffer() };
221
- }
222
- return {
223
- error: (await response.json()),
224
- };
166
+ const { data, error } = await this.request(`/capes/${query}/texture.png`, { responseType: "arrayBuffer" });
167
+ return error ? { error } : { image: data };
225
168
  }
226
169
  /**
227
170
  * Fetch a rendered cape part (e.g. front).
@@ -232,13 +175,8 @@ export class McUtilsAPI {
232
175
  * @returns the cape part PNG image or the error (if one occurred)
233
176
  */
234
177
  async fetchCapePart(query, type, size = 768) {
235
- const response = await fetch(`${this.endpoint}/capes/${query}/${type}.png${this.buildParams({ size: String(size) })}`);
236
- if (response.ok) {
237
- return { image: await response.arrayBuffer() };
238
- }
239
- return {
240
- error: (await response.json()),
241
- };
178
+ const { data, error } = await this.request(`/capes/${query}/${type}.png${this.buildParams({ size: String(size) })}`, { responseType: "arrayBuffer" });
179
+ return error ? { error } : { image: data };
242
180
  }
243
181
  /**
244
182
  * Fetch the list of available server registry entries.
@@ -247,13 +185,8 @@ export class McUtilsAPI {
247
185
  * @returns the list of server registry entries or the error (if one occurred)
248
186
  */
249
187
  async fetchServerRegistryEntries(query) {
250
- const response = await fetch(`${this.endpoint}/servers${this.buildParams({ query: query })}`);
251
- if (response.ok) {
252
- return { entries: (await response.json()) };
253
- }
254
- return {
255
- error: (await response.json()),
256
- };
188
+ const { data, error } = await this.request(`/servers${this.buildParams({ query: query })}`);
189
+ return error ? { error } : { entries: data };
257
190
  }
258
191
  /**
259
192
  * Fetch the statistics of the API.
@@ -261,13 +194,8 @@ export class McUtilsAPI {
261
194
  * @returns the statistics or the error (if one occurred)
262
195
  */
263
196
  async fetchStatistics() {
264
- const response = await fetch(`${this.endpoint}/statistics`);
265
- if (response.ok) {
266
- return { statistics: (await response.json()) };
267
- }
268
- return {
269
- error: (await response.json()),
270
- };
197
+ const { data, error } = await this.request(`/statistics`);
198
+ return error ? { error } : { statistics: data };
271
199
  }
272
200
  /**
273
201
  * Fetch the list of available skins.
@@ -276,13 +204,18 @@ export class McUtilsAPI {
276
204
  * @returns the list of skins or the error (if one occurred)
277
205
  */
278
206
  async fetchSkins(page = 1) {
279
- const response = await fetch(`${this.endpoint}/skins${this.buildParams({ page: String(page) })}`);
280
- if (response.ok) {
281
- return { skins: (await response.json()) };
282
- }
283
- return {
284
- error: (await response.json()),
285
- };
207
+ const { data, error } = await this.request(`/skins${this.buildParams({ page: String(page) })}`);
208
+ return error ? { error } : { skins: data };
209
+ }
210
+ /**
211
+ * Search for players by username.
212
+ *
213
+ * @param query the query to search for (eg: aetheria)
214
+ * @returns the player search entry or the error (if one occurred)
215
+ */
216
+ async searchPlayers(query) {
217
+ const { data, error } = await this.request(`/players/search/${query}`);
218
+ return error ? { error } : { entry: data };
286
219
  }
287
220
  }
288
221
  export default McUtilsAPI;
@@ -0,0 +1,6 @@
1
+ import { Skin } from "./skin/skin";
2
+ export type PlayerSearchEntry = {
3
+ uuid: string;
4
+ username: string;
5
+ skin: Skin;
6
+ };
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mcutils-js-api",
3
- "version": "2.0.36",
3
+ "version": "2.0.38",
4
4
  "module": "dist/index.js",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",