mowojang 0.6.0 → 0.7.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.
Files changed (3) hide show
  1. package/dist/index.d.ts +100 -49
  2. package/dist/index.js +108 -13
  3. package/package.json +15 -7
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { CacheOptions, CacheRequestConfig, buildStorage, buildMemoryStorage } from 'axios-cache-interceptor';
1
+ import { CacheOptions, NotEmptyStorageValue, StorageValue, AxiosStorage, CacheRequestConfig, buildStorage, buildMemoryStorage } from 'axios-cache-interceptor';
2
2
  import { LoggerConfig } from '@pixelic/logger';
3
3
 
4
4
  /**
@@ -7,12 +7,14 @@ import { LoggerConfig } from '@pixelic/logger';
7
7
  * @example "Technoblade" or "b876ec32-e396-476b-a115-8438d83c67d4"
8
8
  */
9
9
  type Player = string | UUID;
10
+
10
11
  /**
11
12
  * Minecraft Username
12
13
  *
13
14
  * @example "Pixelic"
14
15
  */
15
16
  type Username = string;
17
+
16
18
  /**
17
19
  * Minecraft UUIDv4 (both undashed and dashed will be accepted)
18
20
  *
@@ -33,57 +35,95 @@ type UUID = string;
33
35
  * };
34
36
  * ```
35
37
  */
36
- type ClientOptions = {
37
- logger?: LoggerConfig;
38
- validation?: ValidationOptions;
39
- timeout?: number;
40
- cache?: CacheOptions;
41
- baseURL?: string;
42
- fallback?: boolean;
43
- };
44
- type AxiosOptions = {
45
- timeout?: number;
46
- cache?: CacheOptions;
47
- fallback: boolean;
48
- };
49
- type ValidationOptions = {
50
- enabled?: boolean;
51
- minimumUsernameLength?: 1 | 2;
52
- };
53
- type MowojangRequestConfig = {
54
- validation?: ValidationOptions;
55
- cache?: CacheRequestConfig<any, any>["cache"];
38
+ interface ClientOptions {
39
+ logger?: LoggerConfig;
40
+ validation?: ValidationOptions;
41
+ timeout?: number;
42
+ cache?: CacheOptions;
43
+ baseURL?: string;
44
+ fallback?: boolean;
45
+ }
46
+
47
+ interface AxiosOptions {
48
+ timeout?: number;
49
+ cache?: CacheOptions;
50
+ fallback: boolean;
51
+ }
52
+
53
+ interface ValidationOptions {
54
+ enabled?: boolean;
55
+ minimumUsernameLength?: 1 | 2;
56
+ }
57
+
58
+ /**
59
+ * Cache helpers that proxy the internal storage from `axios-cache-interceptor`.
60
+ *
61
+ * WARNING: This is an API intended for very specific usecases.
62
+ * Incorrect writes/deletes or clearing storage at the wrong time can break cache behavior or cause unexpected errors.
63
+ */
64
+ type MowojangCache = readonly {
65
+ /** Clears the entire internal cache storage. */
66
+ readonly clear: () => Promise<void>;
67
+ /** Sets a raw internal cache entry for a cache key. */
68
+ readonly set: (key: string, value: NotEmptyStorageValue) => Promise<void>;
69
+ /** Returns the raw internal cache entry for a cache key. */
70
+ readonly get: (key: string) => Promise<StorageValue>;
71
+ /** Deletes a raw internal cache entry by cache key. */
72
+ readonly del: (key: string) => Promise<void>;
73
+ /** Checks whether a cached value exists for a cache key. */
74
+ readonly has: (key: string) => Promise<boolean>;
75
+ /**
76
+ * Direct reference to the internal axios-cache-interceptor storage instance.
77
+ *
78
+ * **DO NOT USE THIS IF YOU ARE NOT 100% SURE WHAT YOU ARE DOING.**
79
+ *
80
+ * @link https://axios-cache-interceptor.js.org/guide/storages
81
+ */
82
+ readonly _storage: AxiosStorage;
56
83
  };
84
+
85
+ interface MowojangRequestConfig {
86
+ validation?: ValidationOptions;
87
+ cache?: CacheRequestConfig<any, any>["cache"];
88
+ }
89
+
57
90
  type MowojangError = "INVALID_PLAYER";
58
- type MowojangResponse<D, E extends MowojangError | undefined> = Promise<{
59
- data: D;
60
- error: null;
61
- } | {
62
- data: null;
63
- error: E extends undefined ? "UNKNOWN_ERROR" | "INVALID_INPUT" : E | "UNKNOWN_ERROR" | "INVALID_INPUT";
64
- }>;
65
-
66
- type MowojangProfile = {
67
- UUID: UUID;
68
- username: Username;
69
- };
70
- type MowojangSession = {
71
- UUID: UUID;
72
- username: Username;
73
- skin: MowojangSkin;
74
- cape: MowojangCape;
75
- actions: ("FORCED_NAME_CHANGE" | "USING_BANNED_SKIN")[];
76
- };
91
+
92
+ type MowojangResponse<D, E extends MowojangError | undefined> = Promise<
93
+ | {
94
+ data: D;
95
+ error: null;
96
+ }
97
+ | {
98
+ data: null;
99
+ error: E extends undefined ? "UNKNOWN_ERROR" | "INVALID_INPUT" : E | "UNKNOWN_ERROR" | "INVALID_INPUT";
100
+ }
101
+ >;
102
+
103
+ interface MowojangProfile {
104
+ UUID: UUID;
105
+ username: Username;
106
+ }
107
+
108
+ interface MowojangSession {
109
+ UUID: UUID;
110
+ username: Username;
111
+ skin: MowojangSkin;
112
+ cape: MowojangCape;
113
+ actions: ("FORCED_NAME_CHANGE" | "USING_BANNED_SKIN")[];
114
+ }
115
+
77
116
  type MowojangSkin = null | {
78
- url: string;
79
- hash: string;
80
- metadata: {
81
- slim: boolean;
82
- };
117
+ url: string;
118
+ hash: string;
119
+ metadata: {
120
+ slim: boolean;
121
+ };
83
122
  };
123
+
84
124
  type MowojangCape = null | {
85
- url: string;
86
- hash: string;
125
+ url: string;
126
+ hash: string;
87
127
  };
88
128
 
89
129
  declare class Client {
@@ -91,6 +131,9 @@ declare class Client {
91
131
  private axios;
92
132
  private validation;
93
133
  private baseURL;
134
+ readonly cache: MowojangCache;
135
+ readonly version: string;
136
+ static readonly version: string;
94
137
  /**
95
138
  * Creates a new Mowojang Client Instance
96
139
  *
@@ -112,8 +155,16 @@ declare class Client {
112
155
  constructor(clientOptions?: ClientOptions);
113
156
  private shouldValidate;
114
157
  private getValidationMinLength;
115
- private getProfileCacheID;
158
+ private getProfileCacheKey;
159
+ private getSessionCacheKey;
160
+ private getSkinBufferCacheKey;
161
+ private getCapeBufferCacheKey;
116
162
  private mirrorProfileCacheEntries;
163
+ private _clearCache;
164
+ private _setCacheKey;
165
+ private _getCacheKey;
166
+ private _delCacheKey;
167
+ private _hasCacheKey;
117
168
  /**
118
169
  * Returns an Array of Player Profiles consisting of their Usernames and UUIDs
119
170
  *
@@ -246,4 +297,4 @@ declare const cache: {
246
297
  buildMemoryStorage: typeof buildMemoryStorage;
247
298
  };
248
299
 
249
- export { type AxiosOptions, Client, type ClientOptions, type MowojangCape, type MowojangError, type MowojangProfile, type MowojangRequestConfig, type MowojangResponse, type MowojangSession, type MowojangSkin, type Player, type UUID, type Username, type ValidationOptions, cache, utils, validate };
300
+ export { type AxiosOptions, Client, type ClientOptions, type MowojangCache, type MowojangCape, type MowojangError, type MowojangProfile, type MowojangRequestConfig, type MowojangResponse, type MowojangSession, type MowojangSkin, type Player, type UUID, type Username, type ValidationOptions, cache, utils, validate };
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@ var __defProp = Object.defineProperty;
2
2
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
3
 
4
4
  // <define:VERSION>
5
- var define_VERSION_default = ["0.6.0"];
5
+ var define_VERSION_default = ["0.7.0"];
6
6
 
7
7
  // src/utils.ts
8
8
  var undashUUID = /* @__PURE__ */ __name((UUID) => UUID.replace(/-/g, "").toLowerCase(), "undashUUID");
@@ -97,7 +97,7 @@ var axiosInstance_default = /* @__PURE__ */ __name((options, logger) => {
97
97
  return instance;
98
98
  }, "default");
99
99
 
100
- // node_modules/.pnpm/@jsr+pixelic__logger@0.2.0/node_modules/@jsr/pixelic__logger/src/index.js
100
+ // node_modules/.pnpm/@jsr+pixelic__logger@0.3.0/node_modules/@jsr/pixelic__logger/src/index.js
101
101
  var ANSI = {
102
102
  reset: "\x1B[0m",
103
103
  gray: "\x1B[90m",
@@ -123,11 +123,13 @@ var Logger = class {
123
123
  __name(this, "Logger");
124
124
  }
125
125
  minLevel = "INFO";
126
+ services;
126
127
  /**
127
128
  * @constructor Creates a Logger Instance
128
129
  */
129
130
  constructor(config) {
130
131
  if (config?.minLevel) this.minLevel = config.minLevel;
132
+ this.services = config?.services ?? [];
131
133
  }
132
134
  /**
133
135
  * @private Checks whether the given Log Level satisfies the set `minLevel` of the constructor
@@ -235,6 +237,9 @@ var Client = class {
235
237
  axios;
236
238
  validation;
237
239
  baseURL;
240
+ cache;
241
+ version = define_VERSION_default[0];
242
+ static version = define_VERSION_default[0];
238
243
  /**
239
244
  * Creates a new Mowojang Client Instance
240
245
  *
@@ -258,29 +263,110 @@ var Client = class {
258
263
  this.validation = clientOptions?.validation ?? {};
259
264
  this.baseURL = (clientOptions?.baseURL ?? "https://mowojang.matdoes.dev").toLowerCase();
260
265
  this.axios = axiosInstance_default({ fallback: true, ...clientOptions }, this.logger);
266
+ this.cache = {
267
+ clear: /* @__PURE__ */ __name(async () => await this._clearCache(), "clear"),
268
+ set: /* @__PURE__ */ __name(async (key, value) => await this._setCacheKey(key, value), "set"),
269
+ get: /* @__PURE__ */ __name(async (key) => await this._getCacheKey(key), "get"),
270
+ del: /* @__PURE__ */ __name(async (key) => await this._delCacheKey(key), "del"),
271
+ has: /* @__PURE__ */ __name(async (key) => await this._hasCacheKey(key), "has"),
272
+ _storage: this.axios.storage
273
+ };
261
274
  }
262
275
  shouldValidate(config) {
263
- return Boolean(this.validation?.enabled || config?.validation?.enabled);
276
+ return Boolean(this.validation?.enabled ?? config?.validation?.enabled);
264
277
  }
265
278
  getValidationMinLength(config) {
266
279
  return this.validation?.minimumUsernameLength ?? config?.validation?.minimumUsernameLength;
267
280
  }
268
- getProfileCacheID(player) {
281
+ getProfileCacheKey(player) {
269
282
  const normalizedPlayer = validateUUID(player) ? `uuid:${undashUUID(player).toLowerCase()}` : `username:${player.toLowerCase()}`;
270
283
  return `profile:${normalizedPlayer}`;
271
284
  }
272
- async mirrorProfileCacheEntries(ID, username, UUID) {
285
+ getSessionCacheKey(player) {
286
+ const normalizedPlayer = validateUUID(player) ? `uuid:${undashUUID(player).toLowerCase()}` : `username:${player.toLowerCase()}`;
287
+ return `session:${normalizedPlayer}`;
288
+ }
289
+ getSkinBufferCacheKey(player) {
290
+ const normalizedPlayer = validateUUID(player) ? `uuid:${undashUUID(player).toLowerCase()}` : `username:${player.toLowerCase()}`;
291
+ return `skin:${normalizedPlayer}`;
292
+ }
293
+ getCapeBufferCacheKey(hash) {
294
+ return `cape:${hash}`;
295
+ }
296
+ async mirrorProfileCacheEntries(ID, username, UUID, internalId) {
273
297
  try {
274
298
  const storage = this.axios.storage;
275
- if (!storage.get || !storage.set) return;
299
+ if (!storage.get || !storage.set) {
300
+ this.logger.warning(
301
+ "Mowojang",
302
+ `No Storage Methods available or set up for Profile Mirror to work with`,
303
+ internalId
304
+ );
305
+ return;
306
+ }
276
307
  const value = await storage.get(ID);
277
308
  if (!value) return;
278
- const usernameID = this.getProfileCacheID(username);
279
- const uuidID = this.getProfileCacheID(UUID);
280
- if (ID !== usernameID) await storage.set(usernameID, value);
281
- if (ID !== uuidID) await storage.set(uuidID, value);
282
- } catch {
309
+ const usernameID = this.getProfileCacheKey(username);
310
+ const uuidID = this.getProfileCacheKey(UUID);
311
+ if (ID !== usernameID) {
312
+ await storage.set(usernameID, value);
313
+ this.logger.debug("Mowojang", `PROFILE-CACHE-MIRROR-SET ${ID} -> ${usernameID}`, internalId);
314
+ }
315
+ if (ID !== uuidID) {
316
+ await storage.set(uuidID, value);
317
+ this.logger.debug("Mowojang", `PROFILE-CACHE-MIRROR-SET ${ID} -> ${uuidID}`, internalId);
318
+ }
319
+ } catch (error) {
320
+ this.logger.error("Mowojang", error instanceof Error ? error.message : "Unknown Error", internalId);
321
+ }
322
+ }
323
+ async _clearCache() {
324
+ try {
325
+ if (this.axios.storage.clear) {
326
+ return await this.axios.storage.clear();
327
+ }
328
+ } catch (error) {
329
+ this.logger.error("Mowojang", error instanceof Error ? error.message : "Unknown Error");
330
+ }
331
+ }
332
+ async _setCacheKey(key, value) {
333
+ try {
334
+ if (this.axios.storage.set) {
335
+ return await this.axios.storage.set(key, value);
336
+ }
337
+ } catch (error) {
338
+ this.logger.error("Mowojang", error instanceof Error ? error.message : "Unknown Error");
339
+ }
340
+ }
341
+ async _getCacheKey(key) {
342
+ try {
343
+ if (this.axios.storage.get) {
344
+ return await this.axios.storage.get(key);
345
+ }
346
+ } catch (error) {
347
+ this.logger.error("Mowojang", error instanceof Error ? error.message : "Unknown Error");
348
+ }
349
+ return { state: "empty" };
350
+ }
351
+ async _delCacheKey(key) {
352
+ try {
353
+ if (this.axios.storage.remove) {
354
+ return await this.axios.storage.remove(key);
355
+ }
356
+ } catch (error) {
357
+ this.logger.error("Mowojang", error instanceof Error ? error.message : "Unknown Error");
358
+ }
359
+ }
360
+ async _hasCacheKey(key) {
361
+ try {
362
+ if (this.axios.storage.get) {
363
+ const value = await this.axios.storage.get(key);
364
+ return value.state === "cached" && "data" in value;
365
+ }
366
+ } catch (error) {
367
+ this.logger.error("Mowojang", error instanceof Error ? error.message : "Unknown Error");
283
368
  }
369
+ return false;
284
370
  }
285
371
  /**
286
372
  * Returns an Array of Player Profiles consisting of their Usernames and UUIDs
@@ -343,11 +429,17 @@ var Client = class {
343
429
  if (this.shouldValidate(config) && !validatePlayer(player, this.getValidationMinLength(config)))
344
430
  return { data: null, error: "INVALID_INPUT" };
345
431
  const fetchResponse = await this.axios.get(`${this.baseURL}/${player}`, {
346
- id: this.getProfileCacheID(player),
432
+ id: this.getProfileCacheKey(player),
347
433
  cache: config?.cache ?? { ttl: 60 * 60 * 1e3 }
348
434
  });
349
435
  if (config?.cache !== false && (typeof config?.cache !== "object" || config.cache.enabled !== false)) {
350
- await this.mirrorProfileCacheEntries(fetchResponse.id, fetchResponse.data.name, fetchResponse.data.id);
436
+ const internalId = fetchResponse.config?.internalId;
437
+ await this.mirrorProfileCacheEntries(
438
+ fetchResponse.id,
439
+ fetchResponse.data.name,
440
+ fetchResponse.data.id,
441
+ internalId
442
+ );
351
443
  }
352
444
  return {
353
445
  data: {
@@ -436,6 +528,7 @@ var Client = class {
436
528
  const UUID = validateUUID(player) ? undashUUID(player) : await this.getUUID(player);
437
529
  if (!UUID) return { data: null, error: "INVALID_PLAYER" };
438
530
  return await this.axios.get(`${this.baseURL}/session/minecraft/profile/${UUID}`, {
531
+ id: this.getSessionCacheKey(player),
439
532
  cache: config?.cache ?? { ttl: 15 * 60 * 1e3 }
440
533
  }).then((fetchResponse) => {
441
534
  let textures = fetchResponse.data.properties.find((property) => property.name === "textures").value;
@@ -505,6 +598,7 @@ var Client = class {
505
598
  const session = await this.getSession(player, config);
506
599
  if (session.error || !session.data.skin) return null;
507
600
  const fetchResponse = await this.axios.get(session.data.skin.url, {
601
+ id: this.getSkinBufferCacheKey(player),
508
602
  responseType: "arraybuffer",
509
603
  cache: config?.cache ?? { ttl: 24 * 60 * 60 * 1e3 }
510
604
  });
@@ -539,6 +633,7 @@ var Client = class {
539
633
  const session = await this.getSession(player, config);
540
634
  if (session.error || !session.data.cape) return null;
541
635
  const fetchResponse = await this.axios.get(session.data.cape.url, {
636
+ id: this.getCapeBufferCacheKey(session.data.cape.hash),
542
637
  responseType: "arraybuffer",
543
638
  cache: config?.cache ?? { ttl: 30 * 24 * 60 * 60 * 1e3 }
544
639
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mowojang",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "A simple TypeScript compatible wrapper for the Mowojang-API.",
5
5
  "exports": {
6
6
  ".": {
@@ -32,15 +32,23 @@
32
32
  "axios-cache-interceptor": "^1.12.0"
33
33
  },
34
34
  "devDependencies": {
35
- "@pixelic/logger": "npm:@jsr/pixelic__logger@~0.2.0",
36
- "@types/node": "^25.9.1",
35
+ "@eslint/js": "^10.0.1",
36
+ "@pixelic/logger": "npm:@jsr/pixelic__logger@^0.3.0",
37
+ "@types/node": "^26.1.2",
38
+ "@typescript-eslint/eslint-plugin": "^8.66.0",
39
+ "@typescript-eslint/parser": "^8.66.0",
40
+ "eslint": "^10.8.0",
41
+ "eslint-config-prettier": "^10.1.8",
42
+ "eslint-plugin-prettier": "^5.5.6",
43
+ "prettier": "^3.9.6",
37
44
  "tsup": "^8.5.1",
38
- "typescript": "^6.0.3"
45
+ "typescript": "^6.0.3",
46
+ "typescript-eslint": "^8.66.0"
39
47
  },
40
48
  "scripts": {
41
- "build": "tsup",
49
+ "build": "pnpm tsup",
42
50
  "test": "node test test/index.js",
43
- "prettier:check": "pnpm dlx prettier . --check",
44
- "prettier:write": "pnpm dlx prettier . --write"
51
+ "lint:check": "pnpm eslint .",
52
+ "lint:fix": "pnpm eslint . --fix"
45
53
  }
46
54
  }