@catbee/utils 2.0.0-next.0 → 2.0.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/LICENSE +1 -1
- package/README.md +102 -52
- package/array/index.cjs +215 -74
- package/array/index.d.ts +345 -2
- package/array/index.mjs +201 -74
- package/async/index.cjs +116 -39
- package/async/index.d.ts +292 -2
- package/async/index.mjs +116 -40
- package/cache/index.cjs +2 -2
- package/cache/index.d.ts +156 -2
- package/cache/index.mjs +3 -3
- package/config/index.cjs +80 -66
- package/config/index.d.ts +65 -3
- package/config/index.mjs +77 -65
- package/context-store/index.cjs +2 -3
- package/context-store/index.d.ts +193 -2
- package/context-store/index.mjs +2 -3
- package/crypto/index.cjs +55 -5
- package/crypto/index.d.ts +225 -2
- package/crypto/index.mjs +52 -7
- package/date/index.cjs +676 -2
- package/date/index.d.ts +676 -2
- package/date/index.mjs +665 -3
- package/decorator/index.cjs +2172 -0
- package/{decorators/decorators.utils.d.ts → decorator/index.d.ts} +58 -54
- package/decorator/index.mjs +2131 -0
- package/{dir → directory}/index.cjs +5 -4
- package/{dir/dir.utils.d.ts → directory/index.d.ts} +24 -21
- package/{dir → directory}/index.mjs +5 -4
- package/env/index.cjs +100 -68
- package/env/index.d.ts +391 -2
- package/env/index.mjs +100 -68
- package/exception/index.cjs +1 -1
- package/exception/index.d.ts +233 -2
- package/exception/index.mjs +1 -1
- package/fs/index.cjs +71 -37
- package/fs/index.d.ts +206 -2
- package/fs/index.mjs +65 -35
- package/http-status-codes/index.cjs +1 -1
- package/http-status-codes/index.d.ts +268 -2
- package/http-status-codes/index.mjs +1 -1
- package/id/index.cjs +1 -1
- package/id/index.d.ts +38 -2
- package/id/index.mjs +1 -1
- package/index.cjs +13 -13
- package/index.d.ts +5 -5
- package/index.mjs +5 -5
- package/logger/index.cjs +13 -15
- package/logger/index.d.ts +190 -2
- package/logger/index.mjs +14 -16
- package/middleware/index.cjs +1 -1
- package/middleware/index.d.ts +104 -2
- package/middleware/index.mjs +1 -1
- package/object/index.cjs +379 -0
- package/{obj/obj.utils.d.ts → object/index.d.ts} +73 -33
- package/object/index.mjs +360 -0
- package/package.json +41 -23
- package/performance/index.cjs +4 -4
- package/performance/index.d.ts +139 -2
- package/performance/index.mjs +4 -4
- package/request/index.cjs +37 -25
- package/request/index.d.ts +242 -3
- package/request/index.mjs +37 -25
- package/response/index.cjs +1 -1
- package/response/index.d.ts +319 -3
- package/response/index.mjs +1 -1
- package/server/index.cjs +249 -146
- package/server/index.d.ts +866 -5
- package/server/index.mjs +248 -144
- package/stream/index.cjs +1 -1
- package/stream/index.d.ts +91 -2
- package/stream/index.mjs +1 -1
- package/string/index.cjs +34 -1
- package/string/index.d.ts +146 -2
- package/string/index.mjs +31 -2
- package/type/index.cjs +19 -2
- package/type/index.d.ts +144 -2
- package/type/index.mjs +17 -3
- package/types/index.cjs +1 -1
- package/types/index.d.ts +775 -5
- package/types/index.mjs +1 -1
- package/url/index.cjs +63 -5
- package/url/index.d.ts +200 -2
- package/url/index.mjs +59 -6
- package/{validate → validation}/index.cjs +91 -44
- package/{validate/validate.utils.d.ts → validation/index.d.ts} +33 -24
- package/{validate → validation}/index.mjs +87 -44
- package/array/array.utils.d.ts +0 -191
- package/async/async.utils.d.ts +0 -296
- package/cache/cache.utils.d.ts +0 -176
- package/config/config.d.ts +0 -57
- package/context-store/context-store.utils.d.ts +0 -212
- package/crypto/crypto.utils.d.ts +0 -183
- package/date/date.utils.d.ts +0 -190
- package/decorators/index.cjs +0 -913
- package/decorators/index.d.ts +0 -25
- package/decorators/index.mjs +0 -872
- package/dir/index.d.ts +0 -25
- package/env/env.utils.d.ts +0 -400
- package/exception/exception.utils.d.ts +0 -253
- package/fs/fs.utils.d.ts +0 -196
- package/http-status-codes/http-status-codes.d.ts +0 -289
- package/id/id.utils.d.ts +0 -59
- package/logger/logger.utils.d.ts +0 -210
- package/middleware/middleware.utils.d.ts +0 -123
- package/obj/index.cjs +0 -317
- package/obj/index.d.ts +0 -25
- package/obj/index.mjs +0 -301
- package/performance/performance.utils.d.ts +0 -159
- package/request/request.utils.d.ts +0 -109
- package/response/response.utils.d.ts +0 -186
- package/server/server.builder.d.ts +0 -531
- package/server/server.d.ts +0 -303
- package/stream/stream.utils.d.ts +0 -111
- package/string/string.utils.d.ts +0 -124
- package/type/type.utils.d.ts +0 -129
- package/types/api-response.d.ts +0 -175
- package/types/common.d.ts +0 -148
- package/types/config.d.ts +0 -88
- package/types/server.d.ts +0 -291
- package/url/url.utils.d.ts +0 -164
- package/validate/index.d.ts +0 -25
package/logger/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
|
@@ -22,4 +22,192 @@
|
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
import pino, { Logger as Logger$1 } from 'pino';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Logger type for application-wide logging.
|
|
29
|
+
*/
|
|
30
|
+
type Logger = Logger$1;
|
|
31
|
+
/**
|
|
32
|
+
* Logger levels for application-wide logging.
|
|
33
|
+
*/
|
|
34
|
+
type LoggerLevels = pino.Level;
|
|
35
|
+
declare const defaultSensitiveFields: string[];
|
|
36
|
+
/**
|
|
37
|
+
* Sets the global redaction censor function used throughout the application for log redaction.
|
|
38
|
+
*
|
|
39
|
+
* Use this function to customize how sensitive data is redacted in logs. The provided function
|
|
40
|
+
* will replace the default censor implementation.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* // Custom censor that redacts only specific values
|
|
45
|
+
* setRedactCensor((value, path, sensitiveFields) => {
|
|
46
|
+
* if (path.includes('password')) return '***';
|
|
47
|
+
* return value;
|
|
48
|
+
* });
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* @param fn - The redaction censor function to use globally. This function receives:
|
|
52
|
+
* - value: The data value to potentially redact
|
|
53
|
+
* - path: Array of strings representing the path to the value in the object
|
|
54
|
+
* - sensitiveFields: Optional array of field names to consider sensitive
|
|
55
|
+
* @returns void
|
|
56
|
+
*/
|
|
57
|
+
declare function setRedactCensor(fn: (value: unknown, path: string[], sensitiveFields?: string[]) => string): void;
|
|
58
|
+
/**
|
|
59
|
+
* Gets the current global redaction censor function used for log redaction.
|
|
60
|
+
*
|
|
61
|
+
* This function is called internally by the logger when determining how to redact
|
|
62
|
+
* sensitive information. It can also be used to access the current censor implementation
|
|
63
|
+
* for composition or extension.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* const currentCensor = getRedactCensor();
|
|
68
|
+
* // Create an enhanced censor that extends the current one
|
|
69
|
+
* setRedactCensor((value, path, fields) => {
|
|
70
|
+
* // Add custom logic before delegating to current censor
|
|
71
|
+
* if (someCondition) return customHandling();
|
|
72
|
+
* return currentCensor(value, path, fields);
|
|
73
|
+
* });
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* @returns The current redaction censor function
|
|
77
|
+
*/
|
|
78
|
+
declare function getRedactCensor(): (value: unknown, path: string[], sensitiveFields?: string[]) => string;
|
|
79
|
+
/**
|
|
80
|
+
* Convenience function to redact sensitive data using the current global redact censor.
|
|
81
|
+
*
|
|
82
|
+
* This is a direct wrapper around the global censor function that simplifies usage
|
|
83
|
+
* in application code without needing to access the censor function directly.
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```typescript
|
|
87
|
+
* // Redact a potential sensitive value
|
|
88
|
+
* const safeValue = redact(value, ['user', 'apiKey']);
|
|
89
|
+
* ```
|
|
90
|
+
*
|
|
91
|
+
* @param value - The value to potentially redact
|
|
92
|
+
* @param path - Array of strings representing the path to the value in the object
|
|
93
|
+
* @param sensitiveFields - Optional array of field names to consider sensitive
|
|
94
|
+
* @returns The redacted string value or "***" for redacted content
|
|
95
|
+
*/
|
|
96
|
+
declare function redact(value: unknown, path: string[], sensitiveFields?: string[]): string;
|
|
97
|
+
/**
|
|
98
|
+
* @deprecated Use addSensitiveFields instead.
|
|
99
|
+
*
|
|
100
|
+
* Extends the current redaction function with additional fields to redact.
|
|
101
|
+
*
|
|
102
|
+
* This function wraps the existing censor while adding more fields to be considered
|
|
103
|
+
* sensitive without replacing the entire redaction logic.
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* ```typescript
|
|
107
|
+
* // Add custom fields to be redacted in all future redaction operations
|
|
108
|
+
* addRedactFields(['customerId', 'accountNumber']);
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* @param fields - Array of additional field names to redact
|
|
112
|
+
*/
|
|
113
|
+
declare function addRedactFields(fields: string[]): void;
|
|
114
|
+
/**
|
|
115
|
+
* Retrieves the expanded list of sensitive fields.
|
|
116
|
+
*
|
|
117
|
+
* This function expands the default sensitive fields into their various naming
|
|
118
|
+
* conventions (e.g., camelCase, snake_case) and caches the result for efficiency.
|
|
119
|
+
*
|
|
120
|
+
* @returns Array of expanded sensitive field names
|
|
121
|
+
*/
|
|
122
|
+
declare function getExpandedSensitiveFields(): string[];
|
|
123
|
+
/**
|
|
124
|
+
* Replaces the default list of sensitive fields with a new list.
|
|
125
|
+
*
|
|
126
|
+
* This is useful when you want complete control over what fields are considered
|
|
127
|
+
* sensitive by default, rather than using the library's built-in list.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```typescript
|
|
131
|
+
* // Replace default sensitive fields with a custom list
|
|
132
|
+
* setSensitiveFields(['password', 'ssn', 'creditCard']);
|
|
133
|
+
* ```
|
|
134
|
+
*
|
|
135
|
+
* @param fields - Array of field names to set as the new default sensitive fields
|
|
136
|
+
*/
|
|
137
|
+
declare function setSensitiveFields(fields: string[]): void;
|
|
138
|
+
/**
|
|
139
|
+
* Adds additional field names to the default sensitive fields list.
|
|
140
|
+
*
|
|
141
|
+
* This preserves the existing sensitive fields while adding new ones for
|
|
142
|
+
* application-specific sensitive data.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```typescript
|
|
146
|
+
* // Add domain-specific sensitive fields to the default list
|
|
147
|
+
* addSensitiveFields(['socialSecurityNumber', 'medicalRecordNumber']);
|
|
148
|
+
* ```
|
|
149
|
+
*
|
|
150
|
+
* @param fields - Array of additional field names to add to the sensitive fields list
|
|
151
|
+
*/
|
|
152
|
+
declare function addSensitiveFields(fields: string[]): void;
|
|
153
|
+
/**
|
|
154
|
+
* Use an object compatible with either modern or legacy global scopes.
|
|
155
|
+
*/
|
|
156
|
+
declare const _globalThis: typeof globalThis;
|
|
157
|
+
/**
|
|
158
|
+
* Retrieves the current logger instance:
|
|
159
|
+
* - Returns a request-scoped logger from AsyncLocalStorage if available
|
|
160
|
+
* - Falls back to the global (singleton) logger
|
|
161
|
+
* - Initializes the global logger if not created yet
|
|
162
|
+
* - If newInstance is true, returns a fresh logger without any context
|
|
163
|
+
*
|
|
164
|
+
* @param {boolean} newInstance - If true, returns a fresh logger without any context
|
|
165
|
+
* @returns {Logger} The logger instance (request-bound or global root logger or fresh instance)
|
|
166
|
+
*/
|
|
167
|
+
declare function getLogger(newInstance?: boolean): Logger$1;
|
|
168
|
+
/**
|
|
169
|
+
* Creates a child logger with additional context.
|
|
170
|
+
*
|
|
171
|
+
* @param {Record<string, any>} bindings - Properties to attach to all log records
|
|
172
|
+
* @param {Logger} [parentLogger] - Parent logger (defaults to current context logger or global)
|
|
173
|
+
* @returns {Logger} Child logger with merged context
|
|
174
|
+
*/
|
|
175
|
+
declare function createChildLogger(bindings: Record<string, any>, parentLogger?: Logger$1): Logger$1;
|
|
176
|
+
/**
|
|
177
|
+
* Creates a request-scoped logger with request ID and stores it in context
|
|
178
|
+
*
|
|
179
|
+
* @param {string} requestId - Unique request identifier
|
|
180
|
+
* @param {object} [additionalContext] - Additional context to include in logs
|
|
181
|
+
* @returns {Logger} Request-scoped logger instance
|
|
182
|
+
*/
|
|
183
|
+
declare function createRequestLogger(requestId: string, additionalContext?: Record<string, any>): Logger$1;
|
|
184
|
+
/**
|
|
185
|
+
* Utility to safely log errors with proper stack trace extraction
|
|
186
|
+
*
|
|
187
|
+
* @param {Error|unknown} error - Error object to log
|
|
188
|
+
* @param {string} [message] - Optional message to include
|
|
189
|
+
* @param {Record<string, any>} [context] - Additional context properties
|
|
190
|
+
*/
|
|
191
|
+
declare function logError(error: Error | string, message?: string, context?: Record<string, any>): void;
|
|
192
|
+
/**
|
|
193
|
+
* Expands multiple sensitive field names into their variants.
|
|
194
|
+
* Useful to match fields like `api_key`, `apiKey`, `apikey`, `APIKEY`, etc.
|
|
195
|
+
*/
|
|
196
|
+
declare function expandSensitiveFields(fields: string[]): string[];
|
|
197
|
+
/**
|
|
198
|
+
* Generates multiple variants of a sensitive field name.
|
|
199
|
+
* Useful to match fields like `api_key`, `apiKey`, `apikey`, `APIKEY`, etc.
|
|
200
|
+
*/
|
|
201
|
+
declare function expandSensitiveField(field: string): string[];
|
|
202
|
+
/**
|
|
203
|
+
* Generates wildcard paths up to the given depth.
|
|
204
|
+
*
|
|
205
|
+
* depth = 2 ->
|
|
206
|
+
* password
|
|
207
|
+
* *.password
|
|
208
|
+
* *.*.password
|
|
209
|
+
*/
|
|
210
|
+
declare function generateDeepPaths(field: string, depth: number): string[];
|
|
211
|
+
|
|
212
|
+
export { _globalThis, addRedactFields, addSensitiveFields, createChildLogger, createRequestLogger, defaultSensitiveFields, expandSensitiveField, expandSensitiveFields, generateDeepPaths, getExpandedSensitiveFields, getLogger, getRedactCensor, logError, redact, setRedactCensor, setSensitiveFields };
|
|
213
|
+
export type { Logger, LoggerLevels };
|
package/logger/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
25
|
import pino, { stdTimeFunctions } from 'pino';
|
|
26
|
-
import {
|
|
26
|
+
import { getCatbeeGlobalConfig } from '@catbee/utils/config';
|
|
27
27
|
import { ContextStore, StoreKeys } from '@catbee/utils/context-store';
|
|
28
28
|
|
|
29
29
|
var __defProp = Object.defineProperty;
|
|
@@ -127,9 +127,7 @@ function addRedactFields(fields) {
|
|
|
127
127
|
__name(addRedactFields, "addRedactFields");
|
|
128
128
|
var cachedExpandedFields = null;
|
|
129
129
|
function getExpandedSensitiveFields() {
|
|
130
|
-
|
|
131
|
-
cachedExpandedFields = expandSensitiveFields(defaultSensitiveFields);
|
|
132
|
-
}
|
|
130
|
+
cachedExpandedFields ??= expandSensitiveFields(defaultSensitiveFields);
|
|
133
131
|
return cachedExpandedFields;
|
|
134
132
|
}
|
|
135
133
|
__name(getExpandedSensitiveFields, "getExpandedSensitiveFields");
|
|
@@ -152,8 +150,8 @@ function setupLogger(isGlobal = true) {
|
|
|
152
150
|
...sensitiveFields.flatMap((field) => generateDeepPaths(field, 2))
|
|
153
151
|
]);
|
|
154
152
|
const logParams = {
|
|
155
|
-
name:
|
|
156
|
-
level:
|
|
153
|
+
name: getCatbeeGlobalConfig().logger?.name || "@catbee/utils",
|
|
154
|
+
level: getCatbeeGlobalConfig().logger?.level || "info",
|
|
157
155
|
redact: {
|
|
158
156
|
paths: Array.from(paths),
|
|
159
157
|
censor: /* @__PURE__ */ __name((value, path) => redact(value, path), "censor")
|
|
@@ -169,25 +167,25 @@ function setupLogger(isGlobal = true) {
|
|
|
169
167
|
timestamp: stdTimeFunctions.isoTime
|
|
170
168
|
};
|
|
171
169
|
let logger;
|
|
172
|
-
const logDir =
|
|
170
|
+
const logDir = getCatbeeGlobalConfig().logger?.dir?.trim();
|
|
173
171
|
const hasFileLogging = Boolean(logDir);
|
|
174
|
-
if (hasFileLogging &&
|
|
172
|
+
if (hasFileLogging && getCatbeeGlobalConfig().logger?.pretty) {
|
|
175
173
|
logger = pino(logParams, pino.transport({
|
|
176
174
|
targets: [
|
|
177
175
|
{
|
|
178
176
|
target: "pino-pretty",
|
|
179
|
-
level:
|
|
177
|
+
level: getCatbeeGlobalConfig().logger?.level ?? "info",
|
|
180
178
|
options: {
|
|
181
|
-
colorize:
|
|
179
|
+
colorize: getCatbeeGlobalConfig().logger?.colorize,
|
|
182
180
|
translateTime: "SYS:standard",
|
|
183
181
|
ignore: "pid,hostname",
|
|
184
|
-
singleLine:
|
|
182
|
+
singleLine: getCatbeeGlobalConfig().logger?.singleLine,
|
|
185
183
|
levelFirst: true
|
|
186
184
|
}
|
|
187
185
|
},
|
|
188
186
|
{
|
|
189
187
|
target: "pino/file",
|
|
190
|
-
level:
|
|
188
|
+
level: getCatbeeGlobalConfig().logger?.level ?? "info",
|
|
191
189
|
options: {
|
|
192
190
|
destination: `${logDir}/app.log`,
|
|
193
191
|
mkdir: true
|
|
@@ -203,14 +201,14 @@ function setupLogger(isGlobal = true) {
|
|
|
203
201
|
mkdir: true
|
|
204
202
|
}
|
|
205
203
|
}));
|
|
206
|
-
} else if (
|
|
204
|
+
} else if (getCatbeeGlobalConfig().logger?.pretty) {
|
|
207
205
|
logger = pino(logParams, pino.transport({
|
|
208
206
|
target: "pino-pretty",
|
|
209
207
|
options: {
|
|
210
|
-
colorize:
|
|
208
|
+
colorize: getCatbeeGlobalConfig().logger?.colorize,
|
|
211
209
|
translateTime: "SYS:standard",
|
|
212
210
|
ignore: "pid,hostname",
|
|
213
|
-
singleLine:
|
|
211
|
+
singleLine: getCatbeeGlobalConfig().logger?.singleLine,
|
|
214
212
|
levelFirst: true
|
|
215
213
|
}
|
|
216
214
|
}));
|
package/middleware/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
package/middleware/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|
|
@@ -22,4 +22,106 @@
|
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
import { Request, Response, NextFunction } from 'express';
|
|
26
|
+
|
|
27
|
+
type Middleware = (req: Request, res: Response, next: NextFunction) => void | Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Attaches a unique request ID to each request.
|
|
30
|
+
* Useful for request tracing and correlation between logs.
|
|
31
|
+
*
|
|
32
|
+
* @param {object} [options] - Configuration options
|
|
33
|
+
* @param {string} [options.headerName='X-Request-ID'] - Header name for request ID
|
|
34
|
+
* @param {boolean} [options.exposeHeader=true] - Whether to expose the header in response
|
|
35
|
+
* @param {() => string} [options.generator] - Custom ID generator function
|
|
36
|
+
* @returns {Middleware} Express-compatible middleware
|
|
37
|
+
*/
|
|
38
|
+
declare function requestId(options?: {
|
|
39
|
+
headerName?: string;
|
|
40
|
+
exposeHeader?: boolean;
|
|
41
|
+
generator?: () => string;
|
|
42
|
+
}): Middleware;
|
|
43
|
+
/**
|
|
44
|
+
* Measures request processing time and logs or adds it to response headers.
|
|
45
|
+
*
|
|
46
|
+
* @param {object} [options] - Configuration options
|
|
47
|
+
* @param {boolean} [options.addHeader=true] - Whether to add X-Response-Time header
|
|
48
|
+
* @param {boolean} [options.logOnComplete=false] - Whether to log timing info
|
|
49
|
+
* @returns {Middleware} Express-compatible middleware
|
|
50
|
+
*/
|
|
51
|
+
declare function responseTime(options?: {
|
|
52
|
+
addHeader?: boolean;
|
|
53
|
+
logOnComplete?: boolean;
|
|
54
|
+
}): (req: Request, res: Response, next: NextFunction) => void;
|
|
55
|
+
/**
|
|
56
|
+
* Request timeout middleware.
|
|
57
|
+
* Aborts requests that take too long to process.
|
|
58
|
+
*
|
|
59
|
+
* @param {number} [timeoutMs=30000] - Timeout in milliseconds
|
|
60
|
+
* @returns {Middleware} Express-compatible middleware
|
|
61
|
+
*/
|
|
62
|
+
declare function timeout(timeoutMs?: number): Middleware;
|
|
63
|
+
/**
|
|
64
|
+
* Creates an Express middleware that initializes a per-request context.
|
|
65
|
+
*
|
|
66
|
+
* @param {object} [options] - Optional configuration
|
|
67
|
+
* @param {string} [options.headerName='x-request-id'] - Header to look for request ID
|
|
68
|
+
* @param {boolean} [options.autoLog=true] - Whether to log automatically when context is initialized
|
|
69
|
+
* @returns {(req: Request, res: Response, next: NextFunction) => void} Express middleware function
|
|
70
|
+
*/
|
|
71
|
+
declare function setupRequestContext(options?: {
|
|
72
|
+
headerName?: string;
|
|
73
|
+
autoLog?: boolean;
|
|
74
|
+
}): Middleware;
|
|
75
|
+
interface ErrorHandlerOptions {
|
|
76
|
+
/** Whether to log errors (default: true) */
|
|
77
|
+
logErrors?: boolean;
|
|
78
|
+
/** Whether to include error details in non-production (default: false) */
|
|
79
|
+
includeDetails?: boolean;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Global error handling middleware with enhanced features.
|
|
83
|
+
*
|
|
84
|
+
* @param {ErrorHandlerOptions} options - Error handler options
|
|
85
|
+
* @param {boolean} [options.logErrors=true] - Whether to log errors
|
|
86
|
+
* @param {boolean} [options.includeDetails=false] - Whether to include error details in non-production
|
|
87
|
+
* @returns Error middleware
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* import express from 'express';
|
|
91
|
+
* import { errorHandler } from '@catbee/utils';
|
|
92
|
+
*
|
|
93
|
+
* const app = express();
|
|
94
|
+
*
|
|
95
|
+
* // Your routes
|
|
96
|
+
* app.get('/ping', (req, res) => {
|
|
97
|
+
* throw new Error('Something went wrong');
|
|
98
|
+
* });
|
|
99
|
+
*
|
|
100
|
+
* // Error handler (must be last middleware)
|
|
101
|
+
* app.use(errorHandler({ includeDetails: true }));
|
|
102
|
+
*
|
|
103
|
+
* app.listen(3000, () => {
|
|
104
|
+
* console.log('Server running on port 3000');
|
|
105
|
+
* });
|
|
106
|
+
*/
|
|
107
|
+
declare function errorHandler(options?: ErrorHandlerOptions): (err: any, req: Request, res: Response, _next: NextFunction) => Response<any, Record<string, any>>;
|
|
108
|
+
/**
|
|
109
|
+
* Health check middleware for service status and custom checks.
|
|
110
|
+
*
|
|
111
|
+
* @param {object} [options] - Health check options
|
|
112
|
+
* @param {string} [options.path='/healthz'] - Health check endpoint path
|
|
113
|
+
* @param {Array<{name: string; check: () => Promise<boolean> | boolean}>} [options.checks] - Custom health checks
|
|
114
|
+
* @param {boolean} [options.detailed=true] - Show detailed check results
|
|
115
|
+
* @returns {Middleware} Express-compatible middleware
|
|
116
|
+
*/
|
|
117
|
+
declare function healthCheck(options?: {
|
|
118
|
+
path?: string;
|
|
119
|
+
checks?: Array<{
|
|
120
|
+
name: string;
|
|
121
|
+
check: () => Promise<boolean> | boolean;
|
|
122
|
+
}>;
|
|
123
|
+
detailed?: boolean;
|
|
124
|
+
}): Middleware;
|
|
125
|
+
|
|
126
|
+
export { errorHandler, healthCheck, requestId, responseTime, setupRequestContext, timeout };
|
|
127
|
+
export type { ErrorHandlerOptions, Middleware };
|
package/middleware/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/*
|
|
2
2
|
* The MIT License
|
|
3
3
|
*
|
|
4
|
-
* Copyright (c)
|
|
4
|
+
* Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
|
|
5
5
|
*
|
|
6
6
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
7
|
* of this software and associated documentation files (the "Software"), to deal
|