mowojang 0.4.8 → 0.5.0

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
@@ -3,9 +3,8 @@
3
3
  ![NPM Version](https://img.shields.io/npm/v/mowojang?label=NPM)
4
4
  ![NPM Downloads](https://img.shields.io/npm/dm/mowojang?label=Downloads)
5
5
  ![NPM License](https://img.shields.io/npm/l/mowojang?label=License)
6
-
7
- > [!IMPORTANT]
8
- > This is a third party wrapper for the [Mowojang-API](https://mowojang.matdoes.dev/)!
6
+ ![Publish Size](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fpackagephobia.com%2Fv2%2Fapi.json%3Fp%3Dmowojang&query=%24.publish.pretty&label=Publish%20Size)
7
+ ![Install Size](https://img.shields.io/badge/dynamic/json?url=https%3A%2F%2Fpackagephobia.com%2Fv2%2Fapi.json%3Fp%3Dmowojang&query=%24.install.pretty&label=Install%20Size)
9
8
 
10
9
  ## ✨ Quick Start
11
10
 
@@ -69,22 +68,26 @@ All Validator Functions are exported combined as `validate.x`.
69
68
 
70
69
  - `UUID()`: Returns whether the provided String is an valid UUID (Accepts both undashed and dashed UUIDv4 Strings)
71
70
  - `username()`: Returns whether the provided String is an valid Username (Accepts a second argument stating the minimum length required to pass the validation)
72
- - `player()` Combines the above
71
+ - `player()`: Combines the above
72
+
73
+ ## 📦 CLI
74
+
75
+ You can also use this package from the CLI using [mowojang-cli](https://www.npmjs.com/package/mowojang-cli). ([Repository](https://github.com/Pixelicc/mowojang-cli))
73
76
 
74
77
  ## ⚙️ Development
75
78
 
76
79
  - `pnpm install`: Installs all required dependencies
77
- - `pnpm build`: Runs the TypeScript compiler
78
- - `pnpm test`: Runs tests for all Functions
80
+ - `pnpm build`: Runs the compiler
81
+ - `pnpm test`: Runs tests for all functions
79
82
 
80
83
  ## 📎 Credits
81
84
 
82
85
  - Mowojang Creator (Mat): [Website](https://matdoes.dev), [GitHub](https://github.com/mat-1) or [Ko-Fi](https://ko-fi.com/matdoesdev)
83
86
 
84
- ## 📡 [Mowojang Status](https://mowojang-status.pixelic.dev/)
87
+ ## 📡 [Status](https://mowojang-status.pixelic.dev/)
85
88
 
86
89
  <details>
87
- <summary>Show status badges</summary>
90
+ <summary>Show badges</summary>
88
91
 
89
92
  - Mowojang API - GET /:username -> ![30d uptime](https://mowojang-status.pixelic.dev/api/v1/endpoints/mowojang-api_mowojang-api---get--:username/uptimes/30d/badge.svg) ![latency](https://mowojang-status.pixelic.dev/api/v1/endpoints/mowojang-api_mowojang-api---get--:username/response-times/30d/badge.svg)
90
93
  - Mowojang API - GET /:UUID -> ![30d uptime](https://mowojang-status.pixelic.dev/api/v1/endpoints/mowojang-api_mowojang-api---get--:uuid/uptimes/30d/badge.svg) ![latency](https://mowojang-status.pixelic.dev/api/v1/endpoints/mowojang-api_mowojang-api---get--:uuid/response-times/30d/badge.svg)
package/dist/index.d.ts CHANGED
@@ -20,6 +20,19 @@ type Username = string;
20
20
  */
21
21
  type UUID = string;
22
22
 
23
+ /**
24
+ * Options for the Mowojang Client Instance
25
+ *
26
+ * @example
27
+ * ```TS
28
+ * const options: ClientOptions = {
29
+ * timeout: 5000,
30
+ * validation: {
31
+ * enabled: true
32
+ * }
33
+ * };
34
+ * ```
35
+ */
23
36
  type ClientOptions = {
24
37
  logger?: LoggerConfig;
25
38
  validation?: ValidationOptions;
@@ -78,6 +91,24 @@ declare class Client {
78
91
  private axios;
79
92
  private validation;
80
93
  private baseURL;
94
+ /**
95
+ * Creates a new Mowojang Client Instance
96
+ *
97
+ * @example
98
+ * ```TS
99
+ * const client = new Client();
100
+ * ```
101
+ *
102
+ * @example
103
+ * ```TS
104
+ * const client = new Client({
105
+ * timeout: 5000,
106
+ * validation: {
107
+ * enabled: true
108
+ * }
109
+ * });
110
+ * ```
111
+ */
81
112
  constructor(clientOptions?: ClientOptions);
82
113
  private shouldValidate;
83
114
  private getValidationMinLength;
@@ -85,6 +116,11 @@ declare class Client {
85
116
  * Returns an Array of Player Profiles consisting of their Usernames and UUIDs
86
117
  *
87
118
  * Players considered "INVALID" are excluded from the results
119
+ *
120
+ * @example
121
+ * ```TS
122
+ * const profiles = await client.getProfiles(["Pixelic", "14727faefbdc4aff848cd2713eb9939e"]);
123
+ * ```
88
124
  */
89
125
  getProfiles(players: Player[], config?: MowojangRequestConfig & {
90
126
  usePost?: boolean;
@@ -92,47 +128,84 @@ declare class Client {
92
128
  /**
93
129
  * Returns a Player's Profile consisting of their Username and UUID
94
130
  *
131
+ * @example
132
+ * ```TS
133
+ * const profile = await client.getProfile("Pixelic");
134
+ * ```
95
135
  */
96
136
  getProfile(player: Player, config?: MowojangRequestConfig): MowojangResponse<MowojangProfile, "INVALID_PLAYER">;
97
137
  /**
98
138
  * A simple Wrapper to retrieve only the UUID of a Player's Profile
99
139
  *
140
+ * @example
141
+ * ```TS
142
+ * const uuid = await client.getUUID("Pixelic");
143
+ * ```
100
144
  */
101
145
  getUUID(username: Username, config?: MowojangRequestConfig): Promise<null | UUID>;
102
146
  /**
103
147
  * A simple Wrapper to retrieve only the Username of a Player's Profile
104
148
  *
149
+ * @example
150
+ * ```TS
151
+ * const username = await client.getUsername("14727faefbdc4aff848cd2713eb9939e");
152
+ * ```
105
153
  */
106
154
  getUsername(UUID: UUID, config?: MowojangRequestConfig): Promise<null | Username>;
107
155
  /**
108
156
  * Returns an Array of Player Sessions consisting of their Usernames, UUIDs, Skins and Capes
109
157
  *
110
158
  * Players considered "INVALID" are excluded from the results
159
+ *
160
+ * @example
161
+ * ```TS
162
+ * const sessions = await client.getSessions(["Pixelic", "14727faefbdc4aff848cd2713eb9939e"]);
163
+ * ```
111
164
  */
112
165
  getSessions(players: Player[], config?: MowojangRequestConfig): MowojangResponse<MowojangSession[], undefined>;
113
166
  /**
114
167
  * Returns a Player's Sessions consisting of their Username, UUID, Skin and Cape
115
168
  *
169
+ * @example
170
+ * ```TS
171
+ * const session = await client.getSession("Pixelic");
172
+ * ```
116
173
  */
117
174
  getSession(player: Player, config?: MowojangRequestConfig): MowojangResponse<MowojangSession, "INVALID_PLAYER">;
118
175
  /**
119
176
  * A simple Wrapper to retrieve only the Skin from a Player's Session
120
177
  *
178
+ * @example
179
+ * ```TS
180
+ * const skin = await client.getSkin("Pixelic");
181
+ * ```
121
182
  */
122
183
  getSkin(player: Player, config?: MowojangRequestConfig): Promise<null | MowojangSkin>;
123
184
  /**
124
185
  * A simple Wrapper to retrieve only the Player's Skin from their Session Data loaded into a Buffer
125
186
  *
187
+ * @example
188
+ * ```TS
189
+ * const skinBuffer = await client.getSkinBuffer("Pixelic");
190
+ * ```
126
191
  */
127
192
  getSkinBuffer(player: Player, config?: MowojangRequestConfig): Promise<null | Buffer>;
128
193
  /**
129
194
  * A simple Wrapper to retrieve only the Cape Data from a Player's Session
130
195
  *
196
+ * @example
197
+ * ```TS
198
+ * const cape = await client.getCape("Pixelic");
199
+ * ```
131
200
  */
132
201
  getCape(player: Player, config?: MowojangRequestConfig): Promise<null | MowojangCape>;
133
202
  /**
134
203
  * A simple Wrapper to retrieve only the Player's Cape from their Session Data loaded into a Buffer
135
204
  *
205
+ * @example
206
+ * ```TS
207
+ * const capeBuffer = await client.getCapeBuffer("Pixelic");
208
+ * ```
136
209
  */
137
210
  getCapeBuffer(player: Player, config?: MowojangRequestConfig): Promise<null | Buffer>;
138
211
  }
package/dist/index.js CHANGED
@@ -37,8 +37,8 @@ import { setupCache } from "axios-cache-interceptor";
37
37
  // package.json
38
38
  var package_default = {
39
39
  name: "mowojang",
40
- version: "0.4.8",
41
- description: "A simple JavaScript and TypeScript compatible wrapper for the Mowojang-API.",
40
+ version: "0.5.0",
41
+ description: "A simple TypeScript compatible wrapper for the Mowojang-API.",
42
42
  exports: {
43
43
  ".": {
44
44
  types: "./dist/index.d.ts",
@@ -102,7 +102,7 @@ var axiosInstance_default = /* @__PURE__ */ __name((options, logger) => {
102
102
  );
103
103
  instance.interceptors.request.use((req) => {
104
104
  req.internalId = Math.random().toString(16).slice(2, 10).padEnd(8, "0");
105
- logger.debug("Mowojang", `(${req.internalId}) ${req.method?.toUpperCase()} ${axios.getUri(req)}`);
105
+ logger.debug("Mowojang", `${req.method?.toUpperCase()} ${axios.getUri(req)}`, req.internalId);
106
106
  return req;
107
107
  });
108
108
  instance.interceptors.response.use(
@@ -110,8 +110,9 @@ var axiosInstance_default = /* @__PURE__ */ __name((options, logger) => {
110
110
  if (res.cached) {
111
111
  logger.debug(
112
112
  "Mowojang",
113
+ `${res.stale ? "GET-CACHE-STALE" : "GET-CACHE"} ${axios.getUri(res.config)}`,
113
114
  // @ts-expect-error
114
- `(${res.config.internalId}) ${res.stale ? "GET-CACHE-STALE" : "GET-CACHE"} ${axios.getUri(res.config)}`
115
+ res.config.internalId
115
116
  );
116
117
  }
117
118
  return res;
@@ -125,23 +126,17 @@ var axiosInstance_default = /* @__PURE__ */ __name((options, logger) => {
125
126
  err.config._retry = true;
126
127
  err.config.url = fullURL.replace("mowojang.matdoes.dev", "mowojang.seraph.si");
127
128
  if (err.config.baseURL) delete err.config.baseURL;
128
- logger.critical(
129
- "Mowojang",
130
- `(${err.config.internalId}) Failed to establish connection to mowojang.matdoes.dev`
131
- );
129
+ logger.critical("Mowojang", `Failed to establish connection to mowojang.matdoes.dev`, err.config.internalId);
132
130
  if (options.fallback) {
133
- logger.info("Mowojang", `(${err.config.internalId}) Retrying request with fallback of mowojang.seraph.si`);
131
+ logger.info("Mowojang", `Retrying request with fallback of mowojang.seraph.si`, err.config.internalId);
134
132
  }
135
133
  logger.info("Mowojang", `Check Status of Mowojang on: https://mowojang-status.pixelic.dev`);
136
134
  if (options.fallback) return instance(err.config);
137
135
  } else if (host === "mowojang.seraph.si") {
138
- logger.critical(
139
- "Mowojang",
140
- `(${err.config.internalId}) Failed to establish connection to mowojang.seraph.si`
141
- );
136
+ logger.critical("Mowojang", `Failed to establish connection to mowojang.seraph.si`, err.config.internalId);
142
137
  logger.info("Mowojang", `Check Status of Seraph on: https://mowojang-status.pixelic.dev`);
143
138
  } else {
144
- logger.critical("Mowojang", `(${err.config.internalId}) Failed to establish connection to ${host}`);
139
+ logger.critical("Mowojang", `Failed to establish connection to ${host}`, err.config.internalId);
145
140
  }
146
141
  }
147
142
  return Promise.reject(err);
@@ -288,6 +283,24 @@ var Client = class {
288
283
  axios;
289
284
  validation;
290
285
  baseURL;
286
+ /**
287
+ * Creates a new Mowojang Client Instance
288
+ *
289
+ * @example
290
+ * ```TS
291
+ * const client = new Client();
292
+ * ```
293
+ *
294
+ * @example
295
+ * ```TS
296
+ * const client = new Client({
297
+ * timeout: 5000,
298
+ * validation: {
299
+ * enabled: true
300
+ * }
301
+ * });
302
+ * ```
303
+ */
291
304
  constructor(clientOptions) {
292
305
  this.logger = new Logger(clientOptions?.logger);
293
306
  this.validation = clientOptions?.validation ?? {};
@@ -295,19 +308,20 @@ var Client = class {
295
308
  this.axios = axiosInstance_default({ fallback: true, ...clientOptions }, this.logger);
296
309
  }
297
310
  shouldValidate(config) {
298
- return this.validation?.enabled || config?.validation?.enabled || false;
311
+ return Boolean(this.validation?.enabled || config?.validation?.enabled);
299
312
  }
300
313
  getValidationMinLength(config) {
301
- if (this.validation?.minimumUsernameLength === 1) return 1;
302
- if (this.validation?.minimumUsernameLength === 2) return 2;
303
- if (config?.validation?.minimumUsernameLength === 1) return 1;
304
- if (config?.validation?.minimumUsernameLength === 2) return 2;
305
- return void 0;
314
+ return this.validation?.minimumUsernameLength ?? config?.validation?.minimumUsernameLength;
306
315
  }
307
316
  /**
308
317
  * Returns an Array of Player Profiles consisting of their Usernames and UUIDs
309
318
  *
310
319
  * Players considered "INVALID" are excluded from the results
320
+ *
321
+ * @example
322
+ * ```TS
323
+ * const profiles = await client.getProfiles(["Pixelic", "14727faefbdc4aff848cd2713eb9939e"]);
324
+ * ```
311
325
  */
312
326
  async getProfiles(players, config) {
313
327
  try {
@@ -333,10 +347,7 @@ var Client = class {
333
347
  error: null
334
348
  };
335
349
  }
336
- const profilePromises = [];
337
- players.forEach((player) => {
338
- profilePromises.push(this.getProfile(player, config));
339
- });
350
+ const profilePromises = players.map((player) => this.getProfile(player, config));
340
351
  const profiles = (await Promise.all(profilePromises)).filter((p) => p.data !== null).map((p) => p.data);
341
352
  return {
342
353
  data: profiles,
@@ -349,6 +360,10 @@ var Client = class {
349
360
  /**
350
361
  * Returns a Player's Profile consisting of their Username and UUID
351
362
  *
363
+ * @example
364
+ * ```TS
365
+ * const profile = await client.getProfile("Pixelic");
366
+ * ```
352
367
  */
353
368
  async getProfile(player, config) {
354
369
  try {
@@ -371,6 +386,10 @@ var Client = class {
371
386
  /**
372
387
  * A simple Wrapper to retrieve only the UUID of a Player's Profile
373
388
  *
389
+ * @example
390
+ * ```TS
391
+ * const uuid = await client.getUUID("Pixelic");
392
+ * ```
374
393
  */
375
394
  async getUUID(username, config) {
376
395
  const profile = await this.getProfile(username, config);
@@ -380,6 +399,10 @@ var Client = class {
380
399
  /**
381
400
  * A simple Wrapper to retrieve only the Username of a Player's Profile
382
401
  *
402
+ * @example
403
+ * ```TS
404
+ * const username = await client.getUsername("14727faefbdc4aff848cd2713eb9939e");
405
+ * ```
383
406
  */
384
407
  async getUsername(UUID, config) {
385
408
  const profile = await this.getProfile(UUID, config);
@@ -390,6 +413,11 @@ var Client = class {
390
413
  * Returns an Array of Player Sessions consisting of their Usernames, UUIDs, Skins and Capes
391
414
  *
392
415
  * Players considered "INVALID" are excluded from the results
416
+ *
417
+ * @example
418
+ * ```TS
419
+ * const sessions = await client.getSessions(["Pixelic", "14727faefbdc4aff848cd2713eb9939e"]);
420
+ * ```
393
421
  */
394
422
  async getSessions(players, config) {
395
423
  try {
@@ -399,10 +427,7 @@ var Client = class {
399
427
  if (validateUUID(player)) return undashUUID(player);
400
428
  return player.toLowerCase();
401
429
  });
402
- const sessionsPromises = [];
403
- players.forEach((player) => {
404
- sessionsPromises.push(this.getSession(player, config));
405
- });
430
+ const sessionsPromises = players.map((player) => this.getSession(player, config));
406
431
  const sessions = (await Promise.all(sessionsPromises)).filter((session) => session.data !== null).map((session) => session.data);
407
432
  return {
408
433
  data: sessions,
@@ -415,11 +440,15 @@ var Client = class {
415
440
  /**
416
441
  * Returns a Player's Sessions consisting of their Username, UUID, Skin and Cape
417
442
  *
443
+ * @example
444
+ * ```TS
445
+ * const session = await client.getSession("Pixelic");
446
+ * ```
418
447
  */
419
448
  async getSession(player, config) {
420
449
  if (this.shouldValidate(config) && !validatePlayer(player, this.getValidationMinLength(config)))
421
450
  return { data: null, error: "INVALID_INPUT" };
422
- const UUID = await this.getUUID(player);
451
+ const UUID = validateUUID(player) ? undashUUID(player) : await this.getUUID(player);
423
452
  if (!UUID) return { data: null, error: "INVALID_PLAYER" };
424
453
  return await this.axios.get(`${this.baseURL}/session/minecraft/profile/${UUID}`, {
425
454
  cache: config?.cache ?? { ttl: 15 * 60 * 1e3 }
@@ -465,6 +494,10 @@ var Client = class {
465
494
  /**
466
495
  * A simple Wrapper to retrieve only the Skin from a Player's Session
467
496
  *
497
+ * @example
498
+ * ```TS
499
+ * const skin = await client.getSkin("Pixelic");
500
+ * ```
468
501
  */
469
502
  async getSkin(player, config) {
470
503
  const session = await this.getSession(player, config);
@@ -474,6 +507,10 @@ var Client = class {
474
507
  /**
475
508
  * A simple Wrapper to retrieve only the Player's Skin from their Session Data loaded into a Buffer
476
509
  *
510
+ * @example
511
+ * ```TS
512
+ * const skinBuffer = await client.getSkinBuffer("Pixelic");
513
+ * ```
477
514
  */
478
515
  async getSkinBuffer(player, config) {
479
516
  const session = await this.getSession(player, config);
@@ -482,11 +519,15 @@ var Client = class {
482
519
  responseType: "arraybuffer",
483
520
  cache: config?.cache ?? { ttl: 24 * 60 * 60 * 1e3 }
484
521
  });
485
- return Buffer.from(fetchResponse.data, "base64");
522
+ return Buffer.from(fetchResponse.data);
486
523
  }
487
524
  /**
488
525
  * A simple Wrapper to retrieve only the Cape Data from a Player's Session
489
526
  *
527
+ * @example
528
+ * ```TS
529
+ * const cape = await client.getCape("Pixelic");
530
+ * ```
490
531
  */
491
532
  async getCape(player, config) {
492
533
  const session = await this.getSession(player, config);
@@ -496,6 +537,10 @@ var Client = class {
496
537
  /**
497
538
  * A simple Wrapper to retrieve only the Player's Cape from their Session Data loaded into a Buffer
498
539
  *
540
+ * @example
541
+ * ```TS
542
+ * const capeBuffer = await client.getCapeBuffer("Pixelic");
543
+ * ```
499
544
  */
500
545
  async getCapeBuffer(player, config) {
501
546
  const session = await this.getSession(player, config);
@@ -504,7 +549,7 @@ var Client = class {
504
549
  responseType: "arraybuffer",
505
550
  cache: config?.cache ?? { ttl: 30 * 24 * 60 * 60 * 1e3 }
506
551
  });
507
- return Buffer.from(fetchResponse.data, "base64");
552
+ return Buffer.from(fetchResponse.data);
508
553
  }
509
554
  };
510
555
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mowojang",
3
- "version": "0.4.8",
4
- "description": "A simple JavaScript and TypeScript compatible wrapper for the Mowojang-API.",
3
+ "version": "0.5.0",
4
+ "description": "A simple TypeScript compatible wrapper for the Mowojang-API.",
5
5
  "exports": {
6
6
  ".": {
7
7
  "types": "./dist/index.d.ts",