mowojang 0.4.0 → 0.4.2

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
@@ -18,6 +18,22 @@ const UUID = await Mowojang.getUUID("Pixelic")
18
18
  const username = await Mowojang.getUsername("14727fae-fbdc-4aff-848c-d2713eb9939e")
19
19
  ```
20
20
 
21
+ ## ⚙️ Configuration
22
+
23
+ The `Client` constructor accepts an **optional** configuration object.
24
+
25
+ ```TS
26
+ const Mowojang = new MowojangClient({
27
+ baseURL: "https://mowojang.matdoes.dev", // The base URL for the Mowojang API
28
+ fallback: true, // Whether to fallback to mowojang.seraph.si if the default baseURL is down
29
+ timeout: 10000, // Request timeout in milliseconds
30
+ validation: {
31
+ enabled: true, // Enable automatic validation for inputs (UUIDs and Usernames)
32
+ minimumUsernameLength: 1, // Minimum username length required to pass validation (1 or 2), default is 3.
33
+ },
34
+ })
35
+ ```
36
+
21
37
  > [!TIP]
22
38
  > If you plan on using this in a long-running process with heavy usage check out this guide on how to setup custom caching to prevent memory leaks.
23
39
  > Guide: [Custom-Caching](docs/custom-caching.md)
@@ -73,11 +89,11 @@ All Validator Functions are exported combined as `validate.x`.
73
89
  <details>
74
90
  <summary>Show status badges</summary>
75
91
 
76
- - Mowojang API - GET /:username -> ![30d uptime](https://mowojang-status.pixelic.dev/api/v1/endpoints/mowojang_mowojang-api---get--:username/uptimes/30d/badge.svg) ![latency](https://img.shields.io/badge/latency-116ms-blue)
77
- - Mowojang API - GET /:UUID -> ![30d uptime](https://mowojang-status.pixelic.dev/api/v1/endpoints/mowojang_mowojang-api---get--:uuid/uptimes/30d/badge.svg) ![latency](https://img.shields.io/badge/latency-116ms-blue)
78
- - Mowojang API - GET /session/minecraft/profile/:UUID -> ![30d uptime](https://mowojang-status.pixelic.dev/api/v1/endpoints/mowojang_mowojang-api---get--session-minecraft-profile-:uuid/uptimes/30d/badge.svg) ![latency](https://img.shields.io/badge/latency-135ms-blue)
79
- - Mowojang API - GET /users/profiles/minecraft/:username -> ![30d uptime](https://mowojang-status.pixelic.dev/api/v1/endpoints/mowojang_mowojang-api---get--users-profiles-minecraft-:username/uptimes/30d/badge.svg) ![latency](https://img.shields.io/badge/latency-116ms-blue)
80
- - Mowojang API - POST / - [username,UUID] -> ![30d uptime](https://mowojang-status.pixelic.dev/api/v1/endpoints/mowojang_mowojang-api---post-----[username-uuid]/uptimes/30d/badge.svg) ![latency](https://img.shields.io/badge/latency-115ms-blue)
81
- - Minecraft - GET /texture/:hash -> ![30d uptime](https://mowojang-status.pixelic.dev/api/v1/endpoints/minecraft_minecraft---get--texture-:hash/uptimes/30d/badge.svg) ![latency](https://img.shields.io/badge/latency-26ms-blue)
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)
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)
94
+ - Mowojang API - GET /session/minecraft/profile/:UUID -> ![30d uptime](https://mowojang-status.pixelic.dev/api/v1/endpoints/mowojang-api_mowojang-api---get--session-minecraft-profile-:uuid/uptimes/30d/badge.svg) ![latency](https://mowojang-status.pixelic.dev/api/v1/endpoints/mowojang-api_mowojang-api---get--session-minecraft-profile-:uuid/response-times/30d/badge.svg)
95
+ - Mowojang API - GET /users/profiles/minecraft/:username -> ![30d uptime](https://mowojang-status.pixelic.dev/api/v1/endpoints/mowojang-api_mowojang-api---get--users-profiles-minecraft-:username/uptimes/30d/badge.svg) ![latency](https://mowojang-status.pixelic.dev/api/v1/endpoints/mowojang-api_mowojang-api---get--users-profiles-minecraft-:username/response-times/30d/badge.svg)
96
+ - Mowojang API - POST / - [username,UUID] -> ![30d uptime](https://mowojang-status.pixelic.dev/api/v1/endpoints/mowojang-api_mowojang-api---post-----[username-uuid]/uptimes/30d/badge.svg) ![latency](https://mowojang-status.pixelic.dev/api/v1/endpoints/mowojang-api_mowojang-api---post-----[username-uuid]/response-times/30d/badge.svg)
97
+ - Minecraft API - GET /texture/:hash -> ![30d uptime](https://mowojang-status.pixelic.dev/api/v1/endpoints/minecraft-api_minecraft-api---get--texture-:hash/uptimes/30d/badge.svg) ![latency](https://mowojang-status.pixelic.dev/api/v1/endpoints/minecraft-api_minecraft-api---get--texture-:hash/response-times/30d/badge.svg)
82
98
 
83
99
  </details>
package/dist/package.json CHANGED
@@ -1,16 +1,19 @@
1
1
  {
2
2
  "name": "mowojang",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "description": "A simple JavaScript and TypeScript compatible wrapper for the Mowojang-API.",
5
5
  "main": "dist/src/index.js",
6
6
  "types": "dist/src/index.d.ts",
7
- "files": ["dist"],
7
+ "files": [
8
+ "dist"
9
+ ],
8
10
  "type": "module",
9
11
  "scripts": {
10
12
  "build": "pnpm tsc",
11
13
  "test": "node test test/index.js",
12
14
  "prettier:check": "pnpm dlx prettier . --check",
13
- "prettier:write": "pnpm dlx prettier . --write"
15
+ "prettier:write": "pnpm dlx prettier . --write",
16
+ "prepare": "pnpm build"
14
17
  },
15
18
  "repository": {
16
19
  "type": "git",
@@ -28,11 +31,12 @@
28
31
  "node": ">=22.0.0"
29
32
  },
30
33
  "dependencies": {
31
- "axios": "^1.7.9",
32
- "axios-cache-interceptor": "^1.6.2"
34
+ "@pixelic/logger": "npm:@jsr/pixelic__logger@^0.0.6",
35
+ "axios": "^1.16.1",
36
+ "axios-cache-interceptor": "^1.12.0"
33
37
  },
34
38
  "devDependencies": {
35
39
  "@types/node": "^25.9.1",
36
- "typescript": "^5.7.3"
40
+ "typescript": "^6.0.3"
37
41
  }
38
42
  }
@@ -1,4 +1,5 @@
1
1
  import { type AxiosCacheInstance } from "axios-cache-interceptor";
2
2
  import type { AxiosOptions } from "../types/index.d.ts";
3
- declare const _default: (options?: AxiosOptions) => AxiosCacheInstance;
3
+ import type { Logger } from "@pixelic/logger";
4
+ declare const _default: (options: AxiosOptions, logger: Logger) => AxiosCacheInstance;
4
5
  export default _default;
@@ -1,8 +1,8 @@
1
1
  import axios from "axios";
2
2
  import { setupCache } from "axios-cache-interceptor";
3
3
  import PackageJSON from "../package.json" with { type: "json" };
4
- export default (options) => {
5
- return setupCache(axios.create({
4
+ export default (options, logger) => {
5
+ const instance = setupCache(axios.create({
6
6
  timeout: options?.timeout ?? 10000,
7
7
  maxRedirects: 0,
8
8
  headers: {
@@ -14,4 +14,54 @@ export default (options) => {
14
14
  cacheTakeover: false,
15
15
  ...options?.cache,
16
16
  });
17
+ instance.interceptors.request.use((req) => {
18
+ // @ts-expect-error
19
+ req.internalId = Math.random().toString(16).slice(2, 10).padEnd(8, "0");
20
+ // @ts-expect-error
21
+ logger.debug("Mowojang", `(${req.internalId}) ${req.method?.toUpperCase()} ${axios.getUri(req)}`);
22
+ return req;
23
+ });
24
+ instance.interceptors.response.use((res) => {
25
+ if (res.cached) {
26
+ logger.debug("Mowojang",
27
+ // @ts-expect-error
28
+ `(${res.config.internalId}) ${res.stale ? "GET-CACHE-STALE" : "GET-CACHE"} ${axios.getUri(res.config)}`);
29
+ }
30
+ return res;
31
+ }, (err) => {
32
+ const isConnectionError = err.code === "ECONNRESET" || err.code === "ETIMEDOUT" || (err?.response?.status ?? 0) >= 500;
33
+ if (isConnectionError) {
34
+ const fullURL = axios.getUri(err.config);
35
+ const host = new URL(fullURL).host;
36
+ /**
37
+ * If Mowojang (mowojang.matdoes.dev) is down, this falls back to Seraph (mowojang.seraph.si).
38
+ * This gets ignored if a different `baseURL` is passed in the initial config.
39
+ */
40
+ if (host === "mowojang.matdoes.dev" && !err.config._retry) {
41
+ err.config._retry = true;
42
+ err.config.url = fullURL.replace("mowojang.matdoes.dev", "mowojang.seraph.si");
43
+ if (err.config.baseURL)
44
+ delete err.config.baseURL;
45
+ logger.critical("Mowojang", `(${err.config.internalId}) Failed to establish connection to mowojang.matdoes.dev`);
46
+ if (options.fallback) {
47
+ logger.info("Mowojang", `(${err.config.internalId}) Retrying request with fallback of mowojang.seraph.si`);
48
+ }
49
+ logger.info("Mowojang", `Check Status of Mowojang on: https://mowojang-status.pixelic.dev`);
50
+ if (options.fallback)
51
+ return instance(err.config);
52
+ }
53
+ else if (host === "mowojang.seraph.si") {
54
+ logger.critical("Mowojang", `(${err.config.internalId}) Failed to establish connection to mowojang.seraph.si`);
55
+ logger.info("Mowojang", `Check Status of Seraph on: https://mowojang-status.pixelic.dev`);
56
+ }
57
+ else {
58
+ /**
59
+ * Logging fallback for a different `baseURL` passed in the initial config.
60
+ */
61
+ logger.critical("Mowojang", `(${err.config.internalId}) Failed to establish connection to ${host}`);
62
+ }
63
+ }
64
+ return Promise.reject(err);
65
+ });
66
+ return instance;
17
67
  };
@@ -1,7 +1,9 @@
1
1
  import type { Player, Username, UUID, ClientOptions, MowojangCape, MowojangProfile, MowojangRequestConfig, MowojangResponse, MowojangSession, MowojangSkin } from "../types/index.d.ts";
2
2
  export default class Client {
3
+ private logger;
3
4
  private axios;
4
5
  private validation;
6
+ private baseURL;
5
7
  constructor(clientOptions?: ClientOptions);
6
8
  private shouldValidate;
7
9
  private getValidationMinLength;
@@ -2,12 +2,17 @@ import { isAxiosError } from "axios";
2
2
  import axiosInstance from "./axiosInstance.js";
3
3
  import { validateArray, validatePlayer, validateUUID } from "./validators.js";
4
4
  import { undashUUID } from "./utils.js";
5
+ import { Logger } from "@pixelic/logger";
5
6
  export default class Client {
7
+ logger;
6
8
  axios;
7
9
  validation;
10
+ baseURL;
8
11
  constructor(clientOptions) {
9
- this.axios = axiosInstance(clientOptions);
12
+ this.logger = new Logger(clientOptions?.logger);
10
13
  this.validation = clientOptions?.validation ?? {};
14
+ this.baseURL = (clientOptions?.baseURL ?? "https://mowojang.matdoes.dev").toLowerCase();
15
+ this.axios = axiosInstance({ fallback: true, ...clientOptions }, this.logger);
11
16
  }
12
17
  shouldValidate(config) {
13
18
  return this.validation?.enabled || config?.validation?.enabled || false;
@@ -41,7 +46,7 @@ export default class Client {
41
46
  .sort();
42
47
  const usePost = config?.usePost ?? false;
43
48
  if (usePost) {
44
- const fetchResponse = await this.axios.post("https://mowojang.matdoes.dev/", players, {
49
+ const fetchResponse = await this.axios.post(this.baseURL, players, {
45
50
  cache: config?.cache ?? { ttl: 15 * 60 * 1000 },
46
51
  });
47
52
  if (!Array.isArray(fetchResponse?.data))
@@ -81,7 +86,7 @@ export default class Client {
81
86
  try {
82
87
  if (this.shouldValidate(config) && !validatePlayer(player, this.getValidationMinLength(config)))
83
88
  return { data: null, error: "INVALID_INPUT" };
84
- const fetchResponse = await this.axios.get(`https://mowojang.matdoes.dev/${player}`, {
89
+ const fetchResponse = await this.axios.get(`${this.baseURL}/${player}`, {
85
90
  cache: config?.cache ?? { ttl: 15 * 60 * 1000 },
86
91
  });
87
92
  return {
@@ -159,7 +164,7 @@ export default class Client {
159
164
  if (!UUID)
160
165
  return { data: null, error: "INVALID_PLAYER" };
161
166
  return await this.axios
162
- .get(`https://mowojang.matdoes.dev/session/minecraft/profile/${UUID}`, {
167
+ .get(`${this.baseURL}/session/minecraft/profile/${UUID}`, {
163
168
  cache: config?.cache ?? { ttl: 15 * 60 * 1000 },
164
169
  })
165
170
  .then((fetchResponse) => {
@@ -1,12 +1,17 @@
1
+ import { LoggerConfig } from "@pixelic/logger";
1
2
  import type { CacheOptions, CacheRequestConfig } from "axios-cache-interceptor";
2
3
  export type ClientOptions = {
3
- timeout?: number;
4
+ logger?: LoggerConfig;
4
5
  validation?: ValidationOptions;
6
+ timeout?: number;
5
7
  cache?: CacheOptions;
8
+ baseURL?: string;
9
+ fallback?: boolean;
6
10
  };
7
11
  export type AxiosOptions = {
8
12
  timeout?: number;
9
13
  cache?: CacheOptions;
14
+ fallback: boolean;
10
15
  };
11
16
  export type ValidationOptions = {
12
17
  enabled?: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mowojang",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "description": "A simple JavaScript and TypeScript compatible wrapper for the Mowojang-API.",
5
5
  "main": "dist/src/index.js",
6
6
  "types": "dist/src/index.d.ts",
@@ -24,12 +24,13 @@
24
24
  "node": ">=22.0.0"
25
25
  },
26
26
  "dependencies": {
27
- "axios": "^1.7.9",
28
- "axios-cache-interceptor": "^1.6.2"
27
+ "@pixelic/logger": "npm:@jsr/pixelic__logger@^0.0.6",
28
+ "axios": "^1.16.1",
29
+ "axios-cache-interceptor": "^1.12.0"
29
30
  },
30
31
  "devDependencies": {
31
32
  "@types/node": "^25.9.1",
32
- "typescript": "^5.7.3"
33
+ "typescript": "^6.0.3"
33
34
  },
34
35
  "scripts": {
35
36
  "build": "pnpm tsc",