Skip to content

Commit 654f2c9

Browse files
update types
1 parent b0981a9 commit 654f2c9

5 files changed

Lines changed: 76 additions & 94 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mcutils-js-api",
3-
"version": "2.0.48",
3+
"version": "2.0.49",
44
"module": "dist/index.js",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/index.ts

Lines changed: 58 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
1+
import { CachedPlayerName } from "./types/cache/cached-player-name";
2+
import { Player } from "./types/player/player";
3+
import { PlayerSearchEntry } from "./types/player/player-search-entry";
4+
import { CapeDTO } from "./types/response/cape/cape-dto";
5+
import { CapesResponsePage } from "./types/response/cape/capes-response";
16
import type { ErrorResponse } from "./types/response/error-response";
2-
import type { ServerBlockedResponse } from "./types/response/server-blocked-response";
37
import type { IpLookup } from "./types/response/ip-lookup-response";
8+
import type { ServerBlockedResponse } from "./types/response/server-blocked-response";
9+
import { SkinDTO } from "./types/response/skin/skin-dto";
10+
import { SkinsResponsePage } from "./types/response/skin/skins-response";
11+
import { StatisticsResponse } from "./types/response/statistics-response";
12+
import { ServerRegistryEntry } from "./types/server-registry/server-registry-entry";
413
import type { BedrockServer } from "./types/server/impl/bedrock-server";
514
import type { JavaServer } from "./types/server/impl/java-server";
615
import type { ServerPlatform } from "./types/server/server";
7-
import type { Cape } from "./types/player/cape/cape";
8-
import { ServerRegistryEntry } from "./types/server-registry/server-registry-entry";
9-
import { Player } from "./types/player/player";
10-
import { CachedPlayerName } from "./types/cache/cached-player-name";
11-
import { StatisticsResponse } from "./types/response/statistics-response";
12-
import { Skin } from "./types/player/skin/skin";
13-
import { Page } from "./types/pagination/pagination";
14-
import { PlayerSearchEntry } from "./types/player/player-search-entry";
15-
import { SkinsResponsePage } from "./types/response/skin/skins-response";
16-
import { SkinDTO } from "./types/response/skin/skin-dto";
1716

1817
type RequestOptions = RequestInit & { responseType?: "json" | "arrayBuffer" };
1918

2019
export class McUtilsAPI {
2120
private readonly endpoint: string;
2221
private readonly fetchOptions?: RequestInit;
2322

24-
constructor(
25-
endpoint: string = "https://mc.fascinated.cc/api",
26-
fetchOptions?: RequestInit
27-
) {
23+
constructor(endpoint: string = "https://mc.fascinated.cc/api", fetchOptions?: RequestInit) {
2824
this.endpoint = endpoint;
2925
this.fetchOptions = fetchOptions;
3026
}
@@ -36,10 +32,7 @@ export class McUtilsAPI {
3632
* @param options the options for the request
3733
* @returns the data or the error (if one occurred)
3834
*/
39-
private async request<T>(
40-
path: string,
41-
options?: RequestOptions
42-
): Promise<{ data?: T; error?: ErrorResponse }> {
35+
private async request<T>(path: string, options?: RequestOptions): Promise<{ data?: T; error?: ErrorResponse }> {
4336
const { responseType = "json", ...init } = options ?? {};
4437
const url = path.startsWith("http") ? path : `${this.endpoint}${path}`;
4538
const response = await fetch(url, { ...this.fetchOptions, ...init });
@@ -48,8 +41,7 @@ export class McUtilsAPI {
4841
return { error: (await response.json()) as ErrorResponse };
4942
}
5043

51-
const data =
52-
responseType === "arrayBuffer" ? await response.arrayBuffer() : await response.json();
44+
const data = responseType === "arrayBuffer" ? await response.arrayBuffer() : await response.json();
5345
return { data: data as T };
5446
}
5547

@@ -73,11 +65,9 @@ export class McUtilsAPI {
7365
*/
7466
async fetchServer(
7567
host: string,
76-
type: ServerPlatform
68+
type: ServerPlatform,
7769
): Promise<{ server?: JavaServer | BedrockServer; error?: ErrorResponse }> {
78-
const { data, error } = await this.request<JavaServer | BedrockServer>(
79-
`/servers/${type}/${host}`
80-
);
70+
const { data, error } = await this.request<JavaServer | BedrockServer>(`/servers/${type}/${host}`);
8171
return error ? { error } : { server: data };
8272
}
8373

@@ -87,9 +77,7 @@ export class McUtilsAPI {
8777
* @param host the host to fetch the server using (eg: aetheria.cc)
8878
* @returns the server or the error (if one occurred)
8979
*/
90-
async fetchJavaServer(
91-
host: string
92-
): Promise<{ server?: JavaServer; error?: ErrorResponse }> {
80+
async fetchJavaServer(host: string): Promise<{ server?: JavaServer; error?: ErrorResponse }> {
9381
const { data, error } = await this.request<JavaServer>(`/servers/java/${host}`);
9482
return error ? { error } : { server: data };
9583
}
@@ -100,12 +88,8 @@ export class McUtilsAPI {
10088
* @param host the host to fetch the server using (eg: geo.hivebedrock.network)
10189
* @returns the server or the error (if one occurred)
10290
*/
103-
async fetchBedrockServer(
104-
host: string
105-
): Promise<{ server?: BedrockServer; error?: ErrorResponse }> {
106-
const { data, error } = await this.request<BedrockServer>(
107-
`/servers/bedrock/${host}`
108-
);
91+
async fetchBedrockServer(host: string): Promise<{ server?: BedrockServer; error?: ErrorResponse }> {
92+
const { data, error } = await this.request<BedrockServer>(`/servers/bedrock/${host}`);
10993
return error ? { error } : { server: data };
11094
}
11195

@@ -115,12 +99,8 @@ export class McUtilsAPI {
11599
* @param host the hostname to check (eg: aetheria.cc)
116100
* @returns the blocked status or the error (if one occurred)
117101
*/
118-
async fetchServerBlocked(
119-
host: string
120-
): Promise<{ blocked?: boolean; error?: ErrorResponse }> {
121-
const { data, error } = await this.request<ServerBlockedResponse>(
122-
`/servers/blocked/${host}`
123-
);
102+
async fetchServerBlocked(host: string): Promise<{ blocked?: boolean; error?: ErrorResponse }> {
103+
const { data, error } = await this.request<ServerBlockedResponse>(`/servers/blocked/${host}`);
124104
if (error) return { error };
125105
return { blocked: data!.blocked };
126106
}
@@ -131,9 +111,7 @@ export class McUtilsAPI {
131111
* @param query the IP address to lookup (eg: 127.0.0.1)
132112
* @returns the IP lookup response or the error (if one occurred)
133113
*/
134-
async fetchIpLookup(
135-
query: string
136-
): Promise<{ data?: IpLookup; error?: ErrorResponse }> {
114+
async fetchIpLookup(query: string): Promise<{ data?: IpLookup; error?: ErrorResponse }> {
137115
return this.request<IpLookup>(`/ips/${query}`);
138116
}
139117

@@ -143,9 +121,7 @@ export class McUtilsAPI {
143121
* @param id the UUID or username of the player (eg: ImFascinated)
144122
* @returns the player or the error (if one occurred)
145123
*/
146-
async fetchPlayer(
147-
id: string
148-
): Promise<{ player?: Player; error?: ErrorResponse }> {
124+
async fetchPlayer(id: string): Promise<{ player?: Player; error?: ErrorResponse }> {
149125
const { data, error } = await this.request<Player>(`/players/${id}`);
150126
return error ? { error } : { player: data };
151127
}
@@ -156,12 +132,8 @@ export class McUtilsAPI {
156132
* @param id the UUID or username to resolve (eg: ImFascinated)
157133
* @returns the player name data or the error (if one occurred)
158134
*/
159-
async fetchPlayerUuid(
160-
id: string
161-
): Promise<{ playerName?: CachedPlayerName; error?: ErrorResponse }> {
162-
const { data, error } = await this.request<CachedPlayerName>(
163-
`/players/uuid/${id}`
164-
);
135+
async fetchPlayerUuid(id: string): Promise<{ playerName?: CachedPlayerName; error?: ErrorResponse }> {
136+
const { data, error } = await this.request<CachedPlayerName>(`/players/uuid/${id}`);
165137
return error ? { error } : { playerName: data };
166138
}
167139

@@ -171,13 +143,8 @@ export class McUtilsAPI {
171143
* @param host the hostname of the server (eg: aetheria.cc)
172144
* @returns the PNG image or the error (if one occurred)
173145
*/
174-
async fetchServerIcon(
175-
host: string
176-
): Promise<{ image?: ArrayBuffer; error?: ErrorResponse }> {
177-
const { data, error } = await this.request<ArrayBuffer>(
178-
`/servers/icon/${host}`,
179-
{ responseType: "arrayBuffer" }
180-
);
146+
async fetchServerIcon(host: string): Promise<{ image?: ArrayBuffer; error?: ErrorResponse }> {
147+
const { data, error } = await this.request<ArrayBuffer>(`/servers/icon/${host}`, { responseType: "arrayBuffer" });
181148
return error ? { error } : { image: data };
182149
}
183150

@@ -192,11 +159,11 @@ export class McUtilsAPI {
192159
async fetchServerPreview(
193160
platform: string,
194161
host: string,
195-
size = 768
162+
size = 768,
196163
): Promise<{ image?: ArrayBuffer; error?: ErrorResponse }> {
197164
const { data, error } = await this.request<ArrayBuffer>(
198165
`/servers/${platform}/preview/${host}${this.buildParams({ size: String(size) })}`,
199-
{ responseType: "arrayBuffer" }
166+
{ responseType: "arrayBuffer" },
200167
);
201168
return error ? { error } : { image: data };
202169
}
@@ -207,13 +174,10 @@ export class McUtilsAPI {
207174
* @param id the UUID or username of the player (eg: ImFascinated)
208175
* @returns the skin PNG image or the error (if one occurred)
209176
*/
210-
async fetchPlayerSkinTexture(
211-
id: string
212-
): Promise<{ image?: ArrayBuffer; error?: ErrorResponse }> {
213-
const { data, error } = await this.request<ArrayBuffer>(
214-
`/skins/${id}/texture.png`,
215-
{ responseType: "arrayBuffer" }
216-
);
177+
async fetchPlayerSkinTexture(id: string): Promise<{ image?: ArrayBuffer; error?: ErrorResponse }> {
178+
const { data, error } = await this.request<ArrayBuffer>(`/skins/${id}/texture.png`, {
179+
responseType: "arrayBuffer",
180+
});
217181
return error ? { error } : { image: data };
218182
}
219183

@@ -230,41 +194,47 @@ export class McUtilsAPI {
230194
id: string,
231195
part: string,
232196
size = 768,
233-
overlays = true
197+
overlays = true,
234198
): Promise<{ image?: ArrayBuffer; error?: ErrorResponse }> {
235199
const { data, error } = await this.request<ArrayBuffer>(
236200
`/skins/${id}/${part}.png${this.buildParams({ size: String(size), overlays: String(overlays) })}`,
237-
{ responseType: "arrayBuffer" }
201+
{ responseType: "arrayBuffer" },
238202
);
239203
return error ? { error } : { image: data };
240204
}
241205

242206
/**
243-
* Fetch the list of available capes (e.g. Migrator).
207+
* Fetch a paginated list of capes.
244208
*
245-
* @returns the list of cape data or the error (if one occurred)
209+
* @param page the page to fetch (default: 1)
210+
* @returns the page of capes or the error (if one occurred)
246211
*/
247-
async fetchCapes(): Promise<{
248-
capes?: Cape[];
249-
error?: ErrorResponse;
250-
}> {
251-
const { data, error } = await this.request<Cape[]>(`/capes`);
212+
async fetchCapes(page: number = 1): Promise<{ capes?: CapesResponsePage; error?: ErrorResponse }> {
213+
const { data, error } = await this.request<CapesResponsePage>(`/capes${this.buildParams({ page: String(page) })}`);
252214
return error ? { error } : { capes: data };
253215
}
254216

217+
/**
218+
* Fetch the details of a specific cape.
219+
*
220+
* @param id the ID of the cape (UUID)
221+
* @returns the cape details or the error (if one occurred)
222+
*/
223+
async fetchCape(id: string): Promise<{ cape?: CapeDTO; error?: ErrorResponse }> {
224+
const { data, error } = await this.request<CapeDTO>(`/capes/${id}`);
225+
return error ? { error } : { cape: data };
226+
}
227+
255228
/**
256229
* Fetch a cape texture image.
257230
*
258231
* @param query player UUID/username or 64-char cape texture id
259232
* @returns the cape PNG image or the error (if one occurred)
260233
*/
261-
async fetchCapeTexture(
262-
query: string
263-
): Promise<{ image?: ArrayBuffer; error?: ErrorResponse }> {
264-
const { data, error } = await this.request<ArrayBuffer>(
265-
`/capes/${query}/texture.png`,
266-
{ responseType: "arrayBuffer" }
267-
);
234+
async fetchCapeTexture(query: string): Promise<{ image?: ArrayBuffer; error?: ErrorResponse }> {
235+
const { data, error } = await this.request<ArrayBuffer>(`/capes/${query}/texture.png`, {
236+
responseType: "arrayBuffer",
237+
});
268238
return error ? { error } : { image: data };
269239
}
270240

@@ -279,11 +249,11 @@ export class McUtilsAPI {
279249
async fetchCapePart(
280250
query: string,
281251
type: string,
282-
size = 768
252+
size = 768,
283253
): Promise<{ image?: ArrayBuffer; error?: ErrorResponse }> {
284254
const { data, error } = await this.request<ArrayBuffer>(
285255
`/capes/${query}/${type}.png${this.buildParams({ size: String(size) })}`,
286-
{ responseType: "arrayBuffer" }
256+
{ responseType: "arrayBuffer" },
287257
);
288258
return error ? { error } : { image: data };
289259
}
@@ -295,9 +265,7 @@ export class McUtilsAPI {
295265
* @returns the list of server registry entries or the error (if one occurred)
296266
*/
297267
async fetchServerRegistryEntries(query: string): Promise<{ entries?: ServerRegistryEntry[]; error?: ErrorResponse }> {
298-
const { data, error } = await this.request<ServerRegistryEntry[]>(
299-
`/servers${this.buildParams({ query: query })}`
300-
);
268+
const { data, error } = await this.request<ServerRegistryEntry[]>(`/servers${this.buildParams({ query: query })}`);
301269
return error ? { error } : { entries: data };
302270
}
303271

@@ -318,9 +286,7 @@ export class McUtilsAPI {
318286
* @returns the list of skins or the error (if one occurred)
319287
*/
320288
async fetchSkins(page: number = 1): Promise<{ skins?: SkinsResponsePage; error?: ErrorResponse }> {
321-
const { data, error } = await this.request<SkinsResponsePage>(
322-
`/skins${this.buildParams({ page: String(page) })}`
323-
);
289+
const { data, error } = await this.request<SkinsResponsePage>(`/skins${this.buildParams({ page: String(page) })}`);
324290
return error ? { error } : { skins: data };
325291
}
326292

src/types/player/player.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export type Player = {
1111
skinHistory: SkinHistory[];
1212
cape?: Cape;
1313
capeHistory?: CapeHistory[];
14-
optifineCape?: Cape;
1514
submittedUuids: number;
1615
lastUpdated: Date;
1716
firstSeen: Date;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export type CapeDTO = {
2+
id: string;
3+
textureId: string;
4+
name: string;
5+
imageUrl: string;
6+
accountsOwned: number;
7+
accountsSeenOwning: string[];
8+
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Page } from "../../pagination/pagination";
2+
3+
export type CapesResponse = {
4+
id: string;
5+
imageUrl: string;
6+
accountsOwned: number;
7+
};
8+
9+
export type CapesResponsePage = Page<CapesResponse>;

0 commit comments

Comments
 (0)