mcutils-js-api 2.0.35 → 2.0.37

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
@@ -12,7 +12,16 @@ import { Skin } from "./types/player/skin/skin";
12
12
  import { Page } from "./types/pagination/pagination";
13
13
  export declare class McUtilsAPI {
14
14
  private readonly endpoint;
15
- constructor(endpoint?: string);
15
+ private readonly fetchOptions?;
16
+ constructor(endpoint?: string, fetchOptions?: RequestInit);
17
+ /**
18
+ * Requests data from the API.
19
+ *
20
+ * @param path the path to the API endpoint
21
+ * @param options the options for the request
22
+ * @returns the data or the error (if one occurred)
23
+ */
24
+ private request;
16
25
  /**
17
26
  * Build URL search params string from a record of key-value pairs.
18
27
  *
@@ -75,10 +84,9 @@ export declare class McUtilsAPI {
75
84
  * Fetch a player by UUID or username.
76
85
  *
77
86
  * @param id the UUID or username of the player (eg: ImFascinated)
78
- * @param fetchOptifineCape whether to fetch the optifine cape (default: true)
79
87
  * @returns the player or the error (if one occurred)
80
88
  */
81
- fetchPlayer(id: string, fetchOptifineCape?: boolean): Promise<{
89
+ fetchPlayer(id: string): Promise<{
82
90
  player?: Player;
83
91
  error?: ErrorResponse;
84
92
  }>;
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,31 +80,17 @@ 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.
100
87
  *
101
88
  * @param id the UUID or username of the player (eg: ImFascinated)
102
- * @param fetchOptifineCape whether to fetch the optifine cape (default: true)
103
89
  * @returns the player or the error (if one occurred)
104
90
  */
105
- async fetchPlayer(id, fetchOptifineCape = true) {
106
- const response = await fetch(`${this.endpoint}/players/${id}${this.buildParams({ fetchOptifineCape: String(fetchOptifineCape) })}`);
107
- if (response.ok) {
108
- return {
109
- player: (await response.json()),
110
- };
111
- }
112
- return {
113
- error: (await response.json()),
114
- };
91
+ async fetchPlayer(id) {
92
+ const { data, error } = await this.request(`/players/${id}`);
93
+ return error ? { error } : { player: data };
115
94
  }
116
95
  /**
117
96
  * Resolve a username to UUID (or UUID to username).
@@ -120,15 +99,8 @@ export class McUtilsAPI {
120
99
  * @returns the player name data or the error (if one occurred)
121
100
  */
122
101
  async fetchPlayerUuid(id) {
123
- const response = await fetch(`${this.endpoint}/players/uuid/${id}`);
124
- if (response.ok) {
125
- return {
126
- playerName: (await response.json()),
127
- };
128
- }
129
- return {
130
- error: (await response.json()),
131
- };
102
+ const { data, error } = await this.request(`/players/uuid/${id}`);
103
+ return error ? { error } : { playerName: data };
132
104
  }
133
105
  /**
134
106
  * Fetch a server favicon/icon image.
@@ -137,13 +109,8 @@ export class McUtilsAPI {
137
109
  * @returns the PNG image or the error (if one occurred)
138
110
  */
139
111
  async fetchServerIcon(host) {
140
- const response = await fetch(`${this.endpoint}/servers/icon/${host}`);
141
- if (response.ok) {
142
- return { image: await response.arrayBuffer() };
143
- }
144
- return {
145
- error: (await response.json()),
146
- };
112
+ const { data, error } = await this.request(`/servers/icon/${host}`, { responseType: "arrayBuffer" });
113
+ return error ? { error } : { image: data };
147
114
  }
148
115
  /**
149
116
  * Fetch a server preview image.
@@ -154,13 +121,8 @@ export class McUtilsAPI {
154
121
  * @returns the PNG image or the error (if one occurred)
155
122
  */
156
123
  async fetchServerPreview(platform, host, size = 768) {
157
- const response = await fetch(`${this.endpoint}/servers/${platform}/preview/${host}${this.buildParams({ size: String(size) })}`);
158
- if (response.ok) {
159
- return { image: await response.arrayBuffer() };
160
- }
161
- return {
162
- error: (await response.json()),
163
- };
124
+ const { data, error } = await this.request(`/servers/${platform}/preview/${host}${this.buildParams({ size: String(size) })}`, { responseType: "arrayBuffer" });
125
+ return error ? { error } : { image: data };
164
126
  }
165
127
  /**
166
128
  * Fetch a player's skin image.
@@ -169,13 +131,8 @@ export class McUtilsAPI {
169
131
  * @returns the skin PNG image or the error (if one occurred)
170
132
  */
171
133
  async fetchPlayerSkinTexture(id) {
172
- const response = await fetch(`${this.endpoint}/skins/${id}/texture.png`);
173
- if (response.ok) {
174
- return { image: await response.arrayBuffer() };
175
- }
176
- return {
177
- error: (await response.json()),
178
- };
134
+ const { data, error } = await this.request(`/skins/${id}/texture.png`, { responseType: "arrayBuffer" });
135
+ return error ? { error } : { image: data };
179
136
  }
180
137
  /**
181
138
  * Fetch a specific part of a player's skin (eg: head, body).
@@ -187,13 +144,8 @@ export class McUtilsAPI {
187
144
  * @returns the skin part PNG image or the error (if one occurred)
188
145
  */
189
146
  async fetchPlayerSkin(id, part, size = 768, overlays = true) {
190
- const response = await fetch(`${this.endpoint}/skins/${id}/${part}.png${this.buildParams({ size: String(size), overlays: String(overlays) })}`);
191
- if (response.ok) {
192
- return { image: await response.arrayBuffer() };
193
- }
194
- return {
195
- error: (await response.json()),
196
- };
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 };
197
149
  }
198
150
  /**
199
151
  * Fetch the list of available capes (e.g. Migrator).
@@ -201,13 +153,8 @@ export class McUtilsAPI {
201
153
  * @returns the list of cape data or the error (if one occurred)
202
154
  */
203
155
  async fetchCapes() {
204
- const response = await fetch(`${this.endpoint}/capes`);
205
- if (response.ok) {
206
- return { capes: (await response.json()) };
207
- }
208
- return {
209
- error: (await response.json()),
210
- };
156
+ const { data, error } = await this.request(`/capes`);
157
+ return error ? { error } : { capes: data };
211
158
  }
212
159
  /**
213
160
  * Fetch a cape texture image.
@@ -216,13 +163,8 @@ export class McUtilsAPI {
216
163
  * @returns the cape PNG image or the error (if one occurred)
217
164
  */
218
165
  async fetchCapeTexture(query) {
219
- const response = await fetch(`${this.endpoint}/capes/${query}/texture.png`);
220
- if (response.ok) {
221
- return { image: await response.arrayBuffer() };
222
- }
223
- return {
224
- error: (await response.json()),
225
- };
166
+ const { data, error } = await this.request(`/capes/${query}/texture.png`, { responseType: "arrayBuffer" });
167
+ return error ? { error } : { image: data };
226
168
  }
227
169
  /**
228
170
  * Fetch a rendered cape part (e.g. front).
@@ -233,13 +175,8 @@ export class McUtilsAPI {
233
175
  * @returns the cape part PNG image or the error (if one occurred)
234
176
  */
235
177
  async fetchCapePart(query, type, size = 768) {
236
- const response = await fetch(`${this.endpoint}/capes/${query}/${type}.png${this.buildParams({ size: String(size) })}`);
237
- if (response.ok) {
238
- return { image: await response.arrayBuffer() };
239
- }
240
- return {
241
- error: (await response.json()),
242
- };
178
+ const { data, error } = await this.request(`/capes/${query}/${type}.png${this.buildParams({ size: String(size) })}`, { responseType: "arrayBuffer" });
179
+ return error ? { error } : { image: data };
243
180
  }
244
181
  /**
245
182
  * Fetch the list of available server registry entries.
@@ -248,13 +185,8 @@ export class McUtilsAPI {
248
185
  * @returns the list of server registry entries or the error (if one occurred)
249
186
  */
250
187
  async fetchServerRegistryEntries(query) {
251
- const response = await fetch(`${this.endpoint}/servers${this.buildParams({ query: query })}`);
252
- if (response.ok) {
253
- return { entries: (await response.json()) };
254
- }
255
- return {
256
- error: (await response.json()),
257
- };
188
+ const { data, error } = await this.request(`/servers${this.buildParams({ query: query })}`);
189
+ return error ? { error } : { entries: data };
258
190
  }
259
191
  /**
260
192
  * Fetch the statistics of the API.
@@ -262,13 +194,8 @@ export class McUtilsAPI {
262
194
  * @returns the statistics or the error (if one occurred)
263
195
  */
264
196
  async fetchStatistics() {
265
- const response = await fetch(`${this.endpoint}/statistics`);
266
- if (response.ok) {
267
- return { statistics: (await response.json()) };
268
- }
269
- return {
270
- error: (await response.json()),
271
- };
197
+ const { data, error } = await this.request(`/statistics`);
198
+ return error ? { error } : { statistics: data };
272
199
  }
273
200
  /**
274
201
  * Fetch the list of available skins.
@@ -277,13 +204,8 @@ export class McUtilsAPI {
277
204
  * @returns the list of skins or the error (if one occurred)
278
205
  */
279
206
  async fetchSkins(page = 1) {
280
- const response = await fetch(`${this.endpoint}/skins${this.buildParams({ page: String(page) })}`);
281
- if (response.ok) {
282
- return { skins: (await response.json()) };
283
- }
284
- return {
285
- error: (await response.json()),
286
- };
207
+ const { data, error } = await this.request(`/skins${this.buildParams({ page: String(page) })}`);
208
+ return error ? { error } : { skins: data };
287
209
  }
288
210
  }
289
211
  export default McUtilsAPI;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mcutils-js-api",
3
- "version": "2.0.35",
3
+ "version": "2.0.37",
4
4
  "module": "dist/index.js",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",