@catbee/utils 2.0.3 → 2.0.4

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.
@@ -0,0 +1,58 @@
1
+ /*
2
+ * The MIT License
3
+ *
4
+ * Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ 'use strict';
26
+
27
+ var __defProp = Object.defineProperty;
28
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
29
+
30
+ // src/error/error.utils.ts
31
+ function ensureError(err) {
32
+ if (err instanceof Error) {
33
+ return err;
34
+ }
35
+ const message = typeof err === "string" ? err : hasErrorMessage(err) ? err.message : String(err);
36
+ return new Error(message, {
37
+ cause: err
38
+ });
39
+ }
40
+ __name(ensureError, "ensureError");
41
+ function hasErrorMessage(value) {
42
+ return typeof value === "object" && value !== null && "message" in value && typeof value.message === "string";
43
+ }
44
+ __name(hasErrorMessage, "hasErrorMessage");
45
+ function serializeError(err) {
46
+ const error = ensureError(err);
47
+ return {
48
+ name: error.name,
49
+ message: error.message,
50
+ stack: error.stack,
51
+ cause: error.cause
52
+ };
53
+ }
54
+ __name(serializeError, "serializeError");
55
+
56
+ exports.ensureError = ensureError;
57
+ exports.hasErrorMessage = hasErrorMessage;
58
+ exports.serializeError = serializeError;
@@ -0,0 +1,62 @@
1
+ /*
2
+ * The MIT License
3
+ *
4
+ * Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ /**
26
+ * Ensures the provided value is returned as an {@link Error}.
27
+ *
28
+ * If the value is not already an Error, a new Error is created using the
29
+ * value's message (if available) or its string representation. The original
30
+ * value is attached as the error's `cause`.
31
+ *
32
+ * @param err - The value to convert to an Error.
33
+ * @returns An Error instance.
34
+ */
35
+ declare function ensureError(err: unknown): Error;
36
+ /**
37
+ * Returns the error message for the provided value.
38
+ *
39
+ * If the value is not already an Error, it is first converted using
40
+ * {@link ensureError}.
41
+ *
42
+ * @param err - The value to extract the error message from.
43
+ * @returns The error message.
44
+ */
45
+ declare function hasErrorMessage(value: unknown): value is {
46
+ message: string;
47
+ };
48
+ interface SerializedError {
49
+ name: string;
50
+ message: string;
51
+ stack?: string;
52
+ cause?: unknown;
53
+ }
54
+ /**
55
+ * Serializes an error into a plain object containing its name, message, stack trace, and cause.
56
+ * @param err - The error to serialize.
57
+ * @returns An object representing the serialized error.
58
+ */
59
+ declare function serializeError(err: unknown): SerializedError;
60
+
61
+ export { ensureError, hasErrorMessage, serializeError };
62
+ export type { SerializedError };
@@ -0,0 +1,54 @@
1
+ /*
2
+ * The MIT License
3
+ *
4
+ * Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ var __defProp = Object.defineProperty;
26
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
27
+
28
+ // src/error/error.utils.ts
29
+ function ensureError(err) {
30
+ if (err instanceof Error) {
31
+ return err;
32
+ }
33
+ const message = typeof err === "string" ? err : hasErrorMessage(err) ? err.message : String(err);
34
+ return new Error(message, {
35
+ cause: err
36
+ });
37
+ }
38
+ __name(ensureError, "ensureError");
39
+ function hasErrorMessage(value) {
40
+ return typeof value === "object" && value !== null && "message" in value && typeof value.message === "string";
41
+ }
42
+ __name(hasErrorMessage, "hasErrorMessage");
43
+ function serializeError(err) {
44
+ const error = ensureError(err);
45
+ return {
46
+ name: error.name,
47
+ message: error.message,
48
+ stack: error.stack,
49
+ cause: error.cause
50
+ };
51
+ }
52
+ __name(serializeError, "serializeError");
53
+
54
+ export { ensureError, hasErrorMessage, serializeError };
package/index.cjs CHANGED
@@ -36,6 +36,7 @@ var date = require("./date/index.cjs");
36
36
  var decorator = require("./decorator/index.cjs");
37
37
  var directory = require("./directory/index.cjs");
38
38
  var env = require("./env/index.cjs");
39
+ var error = require("./error/index.cjs");
39
40
  var exception = require("./exception/index.cjs");
40
41
  var fs = require("./fs/index.cjs");
41
42
  var httpStatusCodes = require("./http-status-codes/index.cjs");
@@ -126,6 +127,12 @@ Object.keys(env).forEach(function (k) {
126
127
  get: function () { return env[k]; }
127
128
  });
128
129
  });
130
+ Object.keys(error).forEach(function (k) {
131
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
132
+ enumerable: true,
133
+ get: function () { return error[k]; }
134
+ });
135
+ });
129
136
  Object.keys(exception).forEach(function (k) {
130
137
  if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
131
138
  enumerable: true,
package/index.d.ts CHANGED
@@ -34,6 +34,7 @@ export * from './date';
34
34
  export * from './decorator';
35
35
  export * from './directory';
36
36
  export * from './env';
37
+ export * from './error';
37
38
  export * from './exception';
38
39
  export * from './fs';
39
40
  export * from './http-status-codes';
package/index.mjs CHANGED
@@ -34,6 +34,7 @@ export * from "./date/index.mjs";
34
34
  export * from "./decorator/index.mjs";
35
35
  export * from "./directory/index.mjs";
36
36
  export * from "./env/index.mjs";
37
+ export * from "./error/index.mjs";
37
38
  export * from "./exception/index.mjs";
38
39
  export * from "./fs/index.mjs";
39
40
  export * from "./http-status-codes/index.mjs";
package/logger/index.cjs CHANGED
@@ -151,13 +151,14 @@ var _globalThis = typeof globalThis === "object" ? globalThis : global;
151
151
  var _global = _globalThis;
152
152
  function setupLogger(isGlobal = true) {
153
153
  const sensitiveFields = getExpandedSensitiveFields();
154
+ const { logger: loggerConfig } = config.getCatbeeGlobalConfig();
154
155
  const paths = /* @__PURE__ */ new Set([
155
156
  ...defaultRedactPaths,
156
157
  ...sensitiveFields.flatMap((field) => generateDeepPaths(field, 2))
157
158
  ]);
158
159
  const logParams = {
159
- name: config.getCatbeeGlobalConfig().logger?.name || "@catbee/utils",
160
- level: config.getCatbeeGlobalConfig().logger?.level || "info",
160
+ name: loggerConfig?.name ?? "@catbee/utils",
161
+ level: loggerConfig?.level ?? "info",
161
162
  redact: {
162
163
  paths: Array.from(paths),
163
164
  censor: /* @__PURE__ */ __name((value, path) => redact(value, path), "censor")
@@ -173,25 +174,25 @@ function setupLogger(isGlobal = true) {
173
174
  timestamp: pino.stdTimeFunctions.isoTime
174
175
  };
175
176
  let logger;
176
- const logDir = config.getCatbeeGlobalConfig().logger?.dir?.trim();
177
+ const logDir = loggerConfig?.dir?.trim();
177
178
  const hasFileLogging = Boolean(logDir);
178
- if (hasFileLogging && config.getCatbeeGlobalConfig().logger?.pretty) {
179
+ if (hasFileLogging && loggerConfig?.pretty) {
179
180
  logger = pino__default.default(logParams, pino__default.default.transport({
180
181
  targets: [
181
182
  {
182
183
  target: "pino-pretty",
183
- level: config.getCatbeeGlobalConfig().logger?.level ?? "info",
184
+ level: loggerConfig?.level ?? "info",
184
185
  options: {
185
- colorize: config.getCatbeeGlobalConfig().logger?.colorize,
186
+ colorize: loggerConfig?.colorize,
186
187
  translateTime: "SYS:standard",
187
188
  ignore: "pid,hostname",
188
- singleLine: config.getCatbeeGlobalConfig().logger?.singleLine,
189
+ singleLine: loggerConfig?.singleLine,
189
190
  levelFirst: true
190
191
  }
191
192
  },
192
193
  {
193
194
  target: "pino/file",
194
- level: config.getCatbeeGlobalConfig().logger?.level ?? "info",
195
+ level: loggerConfig?.level ?? "info",
195
196
  options: {
196
197
  destination: `${logDir}/app.log`,
197
198
  mkdir: true
@@ -207,14 +208,14 @@ function setupLogger(isGlobal = true) {
207
208
  mkdir: true
208
209
  }
209
210
  }));
210
- } else if (config.getCatbeeGlobalConfig().logger?.pretty) {
211
+ } else if (loggerConfig?.pretty) {
211
212
  logger = pino__default.default(logParams, pino__default.default.transport({
212
213
  target: "pino-pretty",
213
214
  options: {
214
- colorize: config.getCatbeeGlobalConfig().logger?.colorize,
215
+ colorize: loggerConfig?.colorize,
215
216
  translateTime: "SYS:standard",
216
217
  ignore: "pid,hostname",
217
- singleLine: config.getCatbeeGlobalConfig().logger?.singleLine,
218
+ singleLine: loggerConfig?.singleLine,
218
219
  levelFirst: true
219
220
  }
220
221
  }));
@@ -243,7 +244,7 @@ function getLogger(newInstance = false) {
243
244
  }
244
245
  __name(getLogger, "getLogger");
245
246
  function createChildLogger(bindings, parentLogger) {
246
- const logger = parentLogger || getLogger();
247
+ const logger = parentLogger ?? getLogger();
247
248
  return logger.child(bindings);
248
249
  }
249
250
  __name(createChildLogger, "createChildLogger");
package/logger/index.d.ts CHANGED
@@ -172,23 +172,23 @@ declare function getLogger(newInstance?: boolean): Logger$1;
172
172
  * @param {Logger} [parentLogger] - Parent logger (defaults to current context logger or global)
173
173
  * @returns {Logger} Child logger with merged context
174
174
  */
175
- declare function createChildLogger(bindings: Record<string, any>, parentLogger?: Logger$1): Logger$1;
175
+ declare function createChildLogger(bindings: Record<string, unknown>, parentLogger?: Logger$1): Logger$1;
176
176
  /**
177
177
  * Creates a request-scoped logger with request ID and stores it in context
178
178
  *
179
179
  * @param {string} requestId - Unique request identifier
180
- * @param {object} [additionalContext] - Additional context to include in logs
180
+ * @param {Record<string, unknown>} [additionalContext] - Additional context to include in logs
181
181
  * @returns {Logger} Request-scoped logger instance
182
182
  */
183
- declare function createRequestLogger(requestId: string, additionalContext?: Record<string, any>): Logger$1;
183
+ declare function createRequestLogger(requestId: string, additionalContext?: Record<string, unknown>): Logger$1;
184
184
  /**
185
185
  * Utility to safely log errors with proper stack trace extraction
186
186
  *
187
187
  * @param {Error|unknown} error - Error object to log
188
188
  * @param {string} [message] - Optional message to include
189
- * @param {Record<string, any>} [context] - Additional context properties
189
+ * @param {Record<string, unknown>} [context] - Additional context properties
190
190
  */
191
- declare function logError(error: Error | string, message?: string, context?: Record<string, any>): void;
191
+ declare function logError(error: Error | string, message?: string, context?: Record<string, unknown>): void;
192
192
  /**
193
193
  * Expands multiple sensitive field names into their variants.
194
194
  * Useful to match fields like `api_key`, `apiKey`, `apikey`, `APIKEY`, etc.
package/logger/index.mjs CHANGED
@@ -145,13 +145,14 @@ var _globalThis = typeof globalThis === "object" ? globalThis : global;
145
145
  var _global = _globalThis;
146
146
  function setupLogger(isGlobal = true) {
147
147
  const sensitiveFields = getExpandedSensitiveFields();
148
+ const { logger: loggerConfig } = getCatbeeGlobalConfig();
148
149
  const paths = /* @__PURE__ */ new Set([
149
150
  ...defaultRedactPaths,
150
151
  ...sensitiveFields.flatMap((field) => generateDeepPaths(field, 2))
151
152
  ]);
152
153
  const logParams = {
153
- name: getCatbeeGlobalConfig().logger?.name || "@catbee/utils",
154
- level: getCatbeeGlobalConfig().logger?.level || "info",
154
+ name: loggerConfig?.name ?? "@catbee/utils",
155
+ level: loggerConfig?.level ?? "info",
155
156
  redact: {
156
157
  paths: Array.from(paths),
157
158
  censor: /* @__PURE__ */ __name((value, path) => redact(value, path), "censor")
@@ -167,25 +168,25 @@ function setupLogger(isGlobal = true) {
167
168
  timestamp: stdTimeFunctions.isoTime
168
169
  };
169
170
  let logger;
170
- const logDir = getCatbeeGlobalConfig().logger?.dir?.trim();
171
+ const logDir = loggerConfig?.dir?.trim();
171
172
  const hasFileLogging = Boolean(logDir);
172
- if (hasFileLogging && getCatbeeGlobalConfig().logger?.pretty) {
173
+ if (hasFileLogging && loggerConfig?.pretty) {
173
174
  logger = pino(logParams, pino.transport({
174
175
  targets: [
175
176
  {
176
177
  target: "pino-pretty",
177
- level: getCatbeeGlobalConfig().logger?.level ?? "info",
178
+ level: loggerConfig?.level ?? "info",
178
179
  options: {
179
- colorize: getCatbeeGlobalConfig().logger?.colorize,
180
+ colorize: loggerConfig?.colorize,
180
181
  translateTime: "SYS:standard",
181
182
  ignore: "pid,hostname",
182
- singleLine: getCatbeeGlobalConfig().logger?.singleLine,
183
+ singleLine: loggerConfig?.singleLine,
183
184
  levelFirst: true
184
185
  }
185
186
  },
186
187
  {
187
188
  target: "pino/file",
188
- level: getCatbeeGlobalConfig().logger?.level ?? "info",
189
+ level: loggerConfig?.level ?? "info",
189
190
  options: {
190
191
  destination: `${logDir}/app.log`,
191
192
  mkdir: true
@@ -201,14 +202,14 @@ function setupLogger(isGlobal = true) {
201
202
  mkdir: true
202
203
  }
203
204
  }));
204
- } else if (getCatbeeGlobalConfig().logger?.pretty) {
205
+ } else if (loggerConfig?.pretty) {
205
206
  logger = pino(logParams, pino.transport({
206
207
  target: "pino-pretty",
207
208
  options: {
208
- colorize: getCatbeeGlobalConfig().logger?.colorize,
209
+ colorize: loggerConfig?.colorize,
209
210
  translateTime: "SYS:standard",
210
211
  ignore: "pid,hostname",
211
- singleLine: getCatbeeGlobalConfig().logger?.singleLine,
212
+ singleLine: loggerConfig?.singleLine,
212
213
  levelFirst: true
213
214
  }
214
215
  }));
@@ -237,7 +238,7 @@ function getLogger(newInstance = false) {
237
238
  }
238
239
  __name(getLogger, "getLogger");
239
240
  function createChildLogger(bindings, parentLogger) {
240
- const logger = parentLogger || getLogger();
241
+ const logger = parentLogger ?? getLogger();
241
242
  return logger.child(bindings);
242
243
  }
243
244
  __name(createChildLogger, "createChildLogger");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@catbee/utils",
3
- "version": "2.0.3",
3
+ "version": "2.0.4",
4
4
  "description": "A modular, production-grade utility toolkit for Node.js and TypeScript, designed for robust, scalable applications (including Express-based services). All utilities are tree-shakable and can be imported independently.",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -32,7 +32,6 @@
32
32
  "express-rate-limit": "^8.3.2",
33
33
  "helmet": "^8.1.0",
34
34
  "pino-pretty": "^13.1.3",
35
- "prom-client": "^15.1.3",
36
35
  "reflect-metadata": "^0.2.2"
37
36
  },
38
37
  "peerDependencies": {
@@ -131,6 +130,11 @@
131
130
  "require": "./env/index.cjs",
132
131
  "types": "./env/index.d.ts"
133
132
  },
133
+ "./error": {
134
+ "import": "./error/index.mjs",
135
+ "require": "./error/index.cjs",
136
+ "types": "./error/index.d.ts"
137
+ },
134
138
  "./exception": {
135
139
  "import": "./exception/index.mjs",
136
140
  "require": "./exception/index.cjs",