@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/cache/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,158 @@
|
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
/**
|
|
26
|
+
* Options for configuring a TTLCache instance.
|
|
27
|
+
*/
|
|
28
|
+
interface TTLCacheOptions {
|
|
29
|
+
/** Default time-to-live in milliseconds for cache entries */
|
|
30
|
+
ttlMs?: number;
|
|
31
|
+
/** Maximum number of entries to keep in cache (uses LRU eviction policy) */
|
|
32
|
+
maxSize?: number;
|
|
33
|
+
/** Auto-cleanup interval in milliseconds (disabled if 0 or negative) */
|
|
34
|
+
autoCleanupMs?: number;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* An in-memory cache with time-to-live (TTL) support for each entry.
|
|
38
|
+
*
|
|
39
|
+
* @typeParam K - Type of the key.
|
|
40
|
+
* @typeParam V - Type of the value.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* const cache = new TTLCache<string, number>({ ttlMs: 1000 });
|
|
44
|
+
* cache.set("x", 123);
|
|
45
|
+
* const value = cache.get("x"); // 123
|
|
46
|
+
*/
|
|
47
|
+
declare class TTLCache<K, V> {
|
|
48
|
+
private readonly cache;
|
|
49
|
+
private readonly ttlMs;
|
|
50
|
+
private readonly maxSize?;
|
|
51
|
+
private cleanupInterval?;
|
|
52
|
+
/**
|
|
53
|
+
* @param options - Configuration options for the cache
|
|
54
|
+
*/
|
|
55
|
+
constructor(options?: TTLCacheOptions);
|
|
56
|
+
/**
|
|
57
|
+
* Sets a key-value pair in the cache with the default TTL.
|
|
58
|
+
*
|
|
59
|
+
* @param key - The key to set.
|
|
60
|
+
* @param value - The value to associate with the key.
|
|
61
|
+
*/
|
|
62
|
+
set(key: K, value: V): void;
|
|
63
|
+
/**
|
|
64
|
+
* Sets a key-value pair in the cache with a custom TTL.
|
|
65
|
+
*
|
|
66
|
+
* @param key - The key to set.
|
|
67
|
+
* @param value - The value to associate with the key.
|
|
68
|
+
* @param ttlMs - Time-to-live in milliseconds.
|
|
69
|
+
*/
|
|
70
|
+
setWithTTL(key: K, value: V, ttlMs: number): void;
|
|
71
|
+
/**
|
|
72
|
+
* Retrieves the value for a given key if it hasn't expired.
|
|
73
|
+
*
|
|
74
|
+
* @param key - The key to retrieve.
|
|
75
|
+
* @returns The cached value, or undefined if not found or expired.
|
|
76
|
+
*/
|
|
77
|
+
get(key: K): V | undefined;
|
|
78
|
+
/**
|
|
79
|
+
* Retrieves or computes a value if it's not in the cache or has expired.
|
|
80
|
+
*
|
|
81
|
+
* @param key - The key to retrieve
|
|
82
|
+
* @param producer - Function to generate the value if not cached
|
|
83
|
+
* @param ttlMs - Optional custom TTL for the computed value
|
|
84
|
+
* @returns The cached or computed value
|
|
85
|
+
*/
|
|
86
|
+
getOrCompute(key: K, producer: () => Promise<V>, ttlMs?: number): Promise<V>;
|
|
87
|
+
/**
|
|
88
|
+
* Checks if the key exists and hasn't expired.
|
|
89
|
+
*
|
|
90
|
+
* @param key - The key to check.
|
|
91
|
+
* @returns `true` if key exists and is valid, otherwise `false`.
|
|
92
|
+
*/
|
|
93
|
+
has(key: K): boolean;
|
|
94
|
+
/**
|
|
95
|
+
* Deletes a key from the cache.
|
|
96
|
+
*
|
|
97
|
+
* @param key - The key to delete.
|
|
98
|
+
* @returns `true` if the key existed and was removed, `false` otherwise.
|
|
99
|
+
*/
|
|
100
|
+
delete(key: K): boolean;
|
|
101
|
+
/**
|
|
102
|
+
* Clears all entries from the cache.
|
|
103
|
+
*/
|
|
104
|
+
clear(): void;
|
|
105
|
+
/**
|
|
106
|
+
* Returns the number of entries currently in the cache (includes expired entries until next access/cleanup).
|
|
107
|
+
*
|
|
108
|
+
* @returns Number of items in the cache (may include expired keys).
|
|
109
|
+
*/
|
|
110
|
+
size(): number;
|
|
111
|
+
/**
|
|
112
|
+
* Set multiple key-value pairs at once with the default TTL.
|
|
113
|
+
*
|
|
114
|
+
* @param entries - Array of [key, value] tuples to set
|
|
115
|
+
*/
|
|
116
|
+
setMany(entries: [K, V][]): void;
|
|
117
|
+
/**
|
|
118
|
+
* Get multiple values at once.
|
|
119
|
+
*
|
|
120
|
+
* @param keys - Array of keys to retrieve
|
|
121
|
+
* @returns Array of values (undefined for keys that don't exist or expired)
|
|
122
|
+
*/
|
|
123
|
+
getMany(keys: K[]): (V | undefined)[];
|
|
124
|
+
/**
|
|
125
|
+
* Removes all expired entries from the cache.
|
|
126
|
+
*
|
|
127
|
+
* @returns Number of entries removed.
|
|
128
|
+
*/
|
|
129
|
+
cleanup(): number;
|
|
130
|
+
/**
|
|
131
|
+
* Returns an iterator of all current valid [key, value] pairs.
|
|
132
|
+
*
|
|
133
|
+
* @returns IterableIterator<[K, V]>
|
|
134
|
+
*/
|
|
135
|
+
entries(): IterableIterator<[K, V]>;
|
|
136
|
+
/**
|
|
137
|
+
* Returns an iterator of all current valid keys.
|
|
138
|
+
*
|
|
139
|
+
* @returns IterableIterator<K>
|
|
140
|
+
*/
|
|
141
|
+
keys(): IterableIterator<K>;
|
|
142
|
+
/**
|
|
143
|
+
* Returns an iterator of all current valid values.
|
|
144
|
+
*
|
|
145
|
+
* @returns IterableIterator<V>
|
|
146
|
+
*/
|
|
147
|
+
values(): IterableIterator<V>;
|
|
148
|
+
/**
|
|
149
|
+
* Extends the expiration of a key by the specified time or default TTL.
|
|
150
|
+
*
|
|
151
|
+
* @param key - The key to refresh
|
|
152
|
+
* @param ttlMs - Optional new TTL in milliseconds (uses default if not specified)
|
|
153
|
+
* @returns true if the key was found and refreshed, false otherwise
|
|
154
|
+
*/
|
|
155
|
+
refresh(key: K, ttlMs?: number): boolean;
|
|
156
|
+
/**
|
|
157
|
+
* Returns a snapshot of cache stats.
|
|
158
|
+
*
|
|
159
|
+
* @returns Object containing cache statistics
|
|
160
|
+
*/
|
|
161
|
+
stats(): {
|
|
162
|
+
size: number;
|
|
163
|
+
validEntries: number;
|
|
164
|
+
expiredEntries: number;
|
|
165
|
+
maxSize: number | undefined;
|
|
166
|
+
};
|
|
167
|
+
/**
|
|
168
|
+
* Stop the auto-cleanup interval if it's running.
|
|
169
|
+
*/
|
|
170
|
+
destroy(): void;
|
|
171
|
+
/**
|
|
172
|
+
* Evict the least recently used entry from the cache.
|
|
173
|
+
* @private
|
|
174
|
+
*/
|
|
175
|
+
private evictLRU;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export { TTLCache };
|
|
179
|
+
export type { TTLCacheOptions };
|
package/cache/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
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
import {
|
|
25
|
+
import { getCatbeeGlobalConfig } from '@catbee/utils/config';
|
|
26
26
|
|
|
27
27
|
var __defProp = Object.defineProperty;
|
|
28
28
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
@@ -38,7 +38,7 @@ var TTLCache = class {
|
|
|
38
38
|
* @param options - Configuration options for the cache
|
|
39
39
|
*/
|
|
40
40
|
constructor(options = {}) {
|
|
41
|
-
this.ttlMs = options.ttlMs ??
|
|
41
|
+
this.ttlMs = options.ttlMs ?? getCatbeeGlobalConfig().cache.defaultTtl;
|
|
42
42
|
this.maxSize = options.maxSize;
|
|
43
43
|
const autoCleanupMs = options.autoCleanupMs;
|
|
44
44
|
if (autoCleanupMs && autoCleanupMs > 0) {
|
package/config/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
|
|
@@ -26,57 +26,39 @@
|
|
|
26
26
|
|
|
27
27
|
var env = require('@catbee/utils/env');
|
|
28
28
|
var id = require('@catbee/utils/id');
|
|
29
|
-
var
|
|
29
|
+
var object = require('@catbee/utils/object');
|
|
30
30
|
|
|
31
31
|
var __defProp = Object.defineProperty;
|
|
32
32
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
33
|
-
var defaultCatbeeConfig = {
|
|
34
|
-
logger: {
|
|
35
|
-
level: env.Env.get("LOGGER_LEVEL", env.Env.isDev() ? "debug" : "info"),
|
|
36
|
-
name: env.Env.get("LOGGER_NAME", env.Env.get("npm_package_name", "@catbee/utils")),
|
|
37
|
-
pretty: env.Env.getBoolean("LOGGER_PRETTY", false),
|
|
38
|
-
colorize: env.Env.getBoolean("LOGGER_PRETTY_COLORIZE", true),
|
|
39
|
-
singleLine: env.Env.getBoolean("LOGGER_PRETTY_SINGLE_LINE", false),
|
|
40
|
-
dir: env.Env.getPath("LOGGER_DIR", "", {
|
|
41
|
-
mustExist: false
|
|
42
|
-
})
|
|
43
|
-
},
|
|
44
|
-
cache: {
|
|
45
|
-
defaultTtl: env.Env.getNumber("CACHE_DEFAULT_TTL_SECONDS", 3600) * 1e3
|
|
46
|
-
},
|
|
47
|
-
server: {
|
|
48
|
-
skipHealthz: env.Env.getBoolean("SERVER_SKIP_HEALTHZ", false)
|
|
49
|
-
}
|
|
50
|
-
};
|
|
51
33
|
var defaultServerConfig = {
|
|
52
|
-
port: 3e3,
|
|
53
|
-
host: "0.0.0.0",
|
|
54
|
-
cors: false,
|
|
55
|
-
helmet: false,
|
|
56
|
-
compression: false,
|
|
34
|
+
port: env.Env.getNumber("SERVER_PORT", 0) || env.Env.getNumber("PORT", 3e3),
|
|
35
|
+
host: env.Env.get("SERVER_HOST", "") || env.Env.get("HOST", "0.0.0.0"),
|
|
36
|
+
cors: env.Env.getBoolean("SERVER_CORS_ENABLE", false) ? {} : false,
|
|
37
|
+
helmet: env.Env.getBoolean("SERVER_HELMET_ENABLE", false) ? {} : false,
|
|
38
|
+
compression: env.Env.getBoolean("SERVER_COMPRESSION_ENABLE", false) ? {} : false,
|
|
57
39
|
bodyParser: {
|
|
58
40
|
json: {
|
|
59
|
-
limit: "1mb"
|
|
41
|
+
limit: env.Env.get("SERVER_BODY_PARSER_JSON_LIMIT", "1mb")
|
|
60
42
|
},
|
|
61
43
|
urlencoded: {
|
|
62
44
|
extended: true,
|
|
63
|
-
limit: "1mb"
|
|
45
|
+
limit: env.Env.get("SERVER_BODY_PARSER_URLENCODED_LIMIT", "1mb")
|
|
64
46
|
}
|
|
65
47
|
},
|
|
66
|
-
cookieParser: false,
|
|
67
|
-
isMicroservice: false,
|
|
68
|
-
appName: "
|
|
69
|
-
globalHeaders: {},
|
|
48
|
+
cookieParser: env.Env.getBoolean("SERVER_COOKIE_PARSER_ENABLE", false) ? {} : false,
|
|
49
|
+
isMicroservice: env.Env.getBoolean("SERVER_IS_MICROSERVICE", false),
|
|
50
|
+
appName: env.Env.get("SERVER_APP_NAME", env.Env.get("npm_package_name", "catbee_server")),
|
|
51
|
+
globalHeaders: env.Env.getJSON("SERVER_GLOBAL_HEADERS", {}),
|
|
70
52
|
rateLimit: {
|
|
71
|
-
enable: false,
|
|
72
|
-
windowMs: 15 * 60 * 1e3,
|
|
73
|
-
max: 100,
|
|
74
|
-
message: "Too many requests",
|
|
75
|
-
standardHeaders: true,
|
|
76
|
-
legacyHeaders: false
|
|
53
|
+
enable: env.Env.getBoolean("SERVER_RATE_LIMIT_ENABLE", false),
|
|
54
|
+
windowMs: env.Env.getDuration("SERVER_RATE_LIMIT_WINDOW_MS", 15 * 60 * 1e3),
|
|
55
|
+
max: env.Env.getNumber("SERVER_RATE_LIMIT_MAX", 100),
|
|
56
|
+
message: env.Env.get("SERVER_RATE_LIMIT_MESSAGE", "Too many requests, please try again later."),
|
|
57
|
+
standardHeaders: env.Env.getBoolean("SERVER_RATE_LIMIT_STANDARD_HEADERS", true),
|
|
58
|
+
legacyHeaders: env.Env.getBoolean("SERVER_RATE_LIMIT_LEGACY_HEADERS", false)
|
|
77
59
|
},
|
|
78
60
|
requestLogging: {
|
|
79
|
-
enable: env.Env.isDev(),
|
|
61
|
+
enable: env.Env.getBoolean("SERVER_REQUEST_LOGGING_ENABLE", env.Env.isDev()),
|
|
80
62
|
ignorePaths: /* @__PURE__ */ __name((req, _res) => {
|
|
81
63
|
const skipPaths = [
|
|
82
64
|
"/healthz",
|
|
@@ -87,50 +69,82 @@ var defaultServerConfig = {
|
|
|
87
69
|
];
|
|
88
70
|
return skipPaths.some((path) => req.path.startsWith(path));
|
|
89
71
|
}, "ignorePaths"),
|
|
90
|
-
skipNotFoundRoutes:
|
|
72
|
+
skipNotFoundRoutes: env.Env.getBoolean("SERVER_REQUEST_LOGGING_SKIP_NOT_FOUND_ROUTES", true)
|
|
91
73
|
},
|
|
74
|
+
trustProxy: env.Env.getBoolean("SERVER_TRUST_PROXY_ENABLE", false),
|
|
92
75
|
openApi: {
|
|
93
|
-
enable: false,
|
|
94
|
-
mountPath: "/docs",
|
|
95
|
-
verbose: false,
|
|
96
|
-
withGlobalPrefix: false
|
|
76
|
+
enable: env.Env.getBoolean("SERVER_OPENAPI_ENABLE", false),
|
|
77
|
+
mountPath: env.Env.get("SERVER_OPENAPI_MOUNT_PATH", "/docs"),
|
|
78
|
+
verbose: env.Env.getBoolean("SERVER_OPENAPI_VERBOSE", false),
|
|
79
|
+
withGlobalPrefix: env.Env.getBoolean("SERVER_OPENAPI_WITH_GLOBAL_PREFIX", false)
|
|
97
80
|
},
|
|
98
81
|
healthCheck: {
|
|
99
|
-
path: "/healthz",
|
|
100
|
-
detailed: true
|
|
82
|
+
path: env.Env.get("SERVER_HEALTH_CHECK_PATH", "/healthz"),
|
|
83
|
+
detailed: env.Env.getBoolean("SERVER_HEALTH_CHECK_DETAILED_OUTPUT", true),
|
|
84
|
+
withGlobalPrefix: env.Env.getBoolean("SERVER_HEALTH_CHECK_WITH_GLOBAL_PREFIX", false)
|
|
101
85
|
},
|
|
102
|
-
requestTimeout:
|
|
86
|
+
requestTimeout: env.Env.getDuration("SERVER_REQUEST_TIMEOUT_MS", 0),
|
|
103
87
|
responseTime: {
|
|
104
|
-
enable: false,
|
|
105
|
-
addHeader: true,
|
|
106
|
-
logOnComplete: false
|
|
88
|
+
enable: env.Env.getBoolean("SERVER_RESPONSE_TIME_ENABLE", false),
|
|
89
|
+
addHeader: env.Env.getBoolean("SERVER_RESPONSE_TIME_ADD_HEADER", true),
|
|
90
|
+
logOnComplete: env.Env.getBoolean("SERVER_RESPONSE_TIME_LOG_ON_COMPLETE", false)
|
|
107
91
|
},
|
|
108
92
|
requestId: {
|
|
109
|
-
headerName: "x-request-id",
|
|
110
|
-
exposeHeader: true,
|
|
93
|
+
headerName: env.Env.get("SERVER_REQUEST_ID_HEADER_NAME", "x-request-id"),
|
|
94
|
+
exposeHeader: env.Env.getBoolean("SERVER_REQUEST_ID_EXPOSE_HEADER", true),
|
|
111
95
|
generator: /* @__PURE__ */ __name(() => id.uuid(), "generator")
|
|
112
96
|
},
|
|
113
97
|
metrics: {
|
|
114
|
-
enable: false,
|
|
115
|
-
path: "/metrics",
|
|
116
|
-
withGlobalPrefix: false
|
|
98
|
+
enable: env.Env.getBoolean("SERVER_METRICS_ENABLE", false),
|
|
99
|
+
path: env.Env.get("SERVER_METRICS_PATH", "/metrics"),
|
|
100
|
+
withGlobalPrefix: env.Env.getBoolean("SERVER_METRICS_WITH_GLOBAL_PREFIX", false)
|
|
117
101
|
},
|
|
118
102
|
serviceVersion: {
|
|
119
|
-
enable: false,
|
|
120
|
-
headerName: "x-service-version",
|
|
121
|
-
version: "0.0.0"
|
|
122
|
-
}
|
|
103
|
+
enable: env.Env.getBoolean("SERVER_SERVICE_VERSION_ENABLE", false),
|
|
104
|
+
headerName: env.Env.get("SERVER_SERVICE_VERSION_HEADER_NAME", "x-service-version"),
|
|
105
|
+
version: env.Env.get("${npm_package_version}", "0.0.0")
|
|
106
|
+
},
|
|
107
|
+
skipHealthzChecksValidation: env.Env.getBoolean("SERVER_SKIP_HEALTHZ_CHECKS_VALIDATION", false)
|
|
123
108
|
};
|
|
124
|
-
|
|
125
|
-
|
|
109
|
+
var defaultCatbeeConfig = {
|
|
110
|
+
logger: {
|
|
111
|
+
level: env.Env.get("LOGGER_LEVEL", env.Env.isDev() || env.Env.isTest() ? "debug" : "info"),
|
|
112
|
+
name: env.Env.get("LOGGER_NAME", env.Env.get("npm_package_name", "@catbee/utils")),
|
|
113
|
+
pretty: env.Env.getBoolean("LOGGER_PRETTY", false),
|
|
114
|
+
colorize: env.Env.getBoolean("LOGGER_PRETTY_COLORIZE", true),
|
|
115
|
+
singleLine: env.Env.getBoolean("LOGGER_PRETTY_SINGLE_LINE", false),
|
|
116
|
+
dir: env.Env.getPath("LOGGER_DIR", "", {
|
|
117
|
+
mustExist: false
|
|
118
|
+
})
|
|
119
|
+
},
|
|
120
|
+
cache: {
|
|
121
|
+
defaultTtl: env.Env.getNumber("CACHE_DEFAULT_TTL_SECONDS", 3600) * 1e3
|
|
122
|
+
},
|
|
123
|
+
server: defaultServerConfig
|
|
124
|
+
};
|
|
125
|
+
var _config = object.deepClone(defaultCatbeeConfig);
|
|
126
|
+
function getCatbeeGlobalConfig() {
|
|
127
|
+
return object.deepClone(_config);
|
|
128
|
+
}
|
|
129
|
+
__name(getCatbeeGlobalConfig, "getCatbeeGlobalConfig");
|
|
130
|
+
var getConfig = getCatbeeGlobalConfig;
|
|
131
|
+
function setCatbeeGlobalConfig(value) {
|
|
132
|
+
_config = object.deepObjMerge(_config, value);
|
|
133
|
+
}
|
|
134
|
+
__name(setCatbeeGlobalConfig, "setCatbeeGlobalConfig");
|
|
135
|
+
var setConfig = setCatbeeGlobalConfig;
|
|
136
|
+
function setCatbeeServerGlobalConfig(value) {
|
|
137
|
+
_config.server = object.deepObjMerge(_config.server, value);
|
|
126
138
|
}
|
|
127
|
-
__name(
|
|
128
|
-
function
|
|
129
|
-
return
|
|
139
|
+
__name(setCatbeeServerGlobalConfig, "setCatbeeServerGlobalConfig");
|
|
140
|
+
function getCatbeeServerGlobalConfig() {
|
|
141
|
+
return getCatbeeGlobalConfig().server;
|
|
130
142
|
}
|
|
131
|
-
__name(
|
|
143
|
+
__name(getCatbeeServerGlobalConfig, "getCatbeeServerGlobalConfig");
|
|
132
144
|
|
|
133
|
-
exports.
|
|
134
|
-
exports.
|
|
145
|
+
exports.getCatbeeGlobalConfig = getCatbeeGlobalConfig;
|
|
146
|
+
exports.getCatbeeServerGlobalConfig = getCatbeeServerGlobalConfig;
|
|
135
147
|
exports.getConfig = getConfig;
|
|
148
|
+
exports.setCatbeeGlobalConfig = setCatbeeGlobalConfig;
|
|
149
|
+
exports.setCatbeeServerGlobalConfig = setCatbeeServerGlobalConfig;
|
|
136
150
|
exports.setConfig = setConfig;
|
package/config/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,5 +22,67 @@
|
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
|
-
|
|
26
|
-
|
|
25
|
+
import { CatbeeConfig, CatbeeGlobalServerConfig } from '@catbee/utils/types';
|
|
26
|
+
import { Logger } from '@catbee/utils/logger';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Extends Express Request interface to add request ID tracking.
|
|
30
|
+
* Added by requestId middleware during request processing.
|
|
31
|
+
*/
|
|
32
|
+
declare global {
|
|
33
|
+
namespace Express {
|
|
34
|
+
interface Request {
|
|
35
|
+
/** Unique request identifier (set by middleware) */
|
|
36
|
+
id?: string;
|
|
37
|
+
/** Logger instance for the request (set by middleware) */
|
|
38
|
+
logger?: Logger;
|
|
39
|
+
/** User information (set by authentication middleware) */
|
|
40
|
+
user?: any;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Gets the global Catbee configuration.
|
|
46
|
+
*
|
|
47
|
+
* @returns The current Catbee configuration.
|
|
48
|
+
*/
|
|
49
|
+
declare function getCatbeeGlobalConfig(): CatbeeConfig;
|
|
50
|
+
/**
|
|
51
|
+
* Gets the global Catbee configuration.
|
|
52
|
+
*
|
|
53
|
+
* @deprecated Use `getCatbeeGlobalConfig` from `@catbee/utils/config` instead.
|
|
54
|
+
* @alias getCatbeeGlobalConfig
|
|
55
|
+
* @returns The current Catbee configuration.
|
|
56
|
+
*/
|
|
57
|
+
declare const getConfig: typeof getCatbeeGlobalConfig;
|
|
58
|
+
/**
|
|
59
|
+
* Sets the global Catbee configuration.
|
|
60
|
+
* Merges the provided partial configuration with the existing one.
|
|
61
|
+
*
|
|
62
|
+
* @param value - Partial Catbee configuration to set
|
|
63
|
+
*/
|
|
64
|
+
declare function setCatbeeGlobalConfig(value: Partial<CatbeeConfig>): void;
|
|
65
|
+
/**
|
|
66
|
+
* Sets the global Catbee configuration.
|
|
67
|
+
* Merges the provided partial configuration with the existing one.
|
|
68
|
+
*
|
|
69
|
+
* @deprecated Use `setCatbeeGlobalConfig` from `@catbee/utils/config` instead.
|
|
70
|
+
* @alias setCatbeeGlobalConfig
|
|
71
|
+
* @param value - Partial Catbee configuration to set
|
|
72
|
+
*/
|
|
73
|
+
declare const setConfig: typeof setCatbeeGlobalConfig;
|
|
74
|
+
/**
|
|
75
|
+
* Sets the global Catbee server configuration.
|
|
76
|
+
* Merges the provided partial server configuration with the existing one.
|
|
77
|
+
*
|
|
78
|
+
* @param value - Partial Catbee server configuration to set
|
|
79
|
+
*/
|
|
80
|
+
declare function setCatbeeServerGlobalConfig(value: Partial<CatbeeGlobalServerConfig>): void;
|
|
81
|
+
/**
|
|
82
|
+
* Gets the global Catbee server configuration.
|
|
83
|
+
*
|
|
84
|
+
* @returns The current Catbee server configuration.
|
|
85
|
+
*/
|
|
86
|
+
declare function getCatbeeServerGlobalConfig(): CatbeeGlobalServerConfig;
|
|
87
|
+
|
|
88
|
+
export { getCatbeeGlobalConfig, getCatbeeServerGlobalConfig, getConfig, setCatbeeGlobalConfig, setCatbeeServerGlobalConfig, setConfig };
|
package/config/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
|
|
@@ -24,57 +24,39 @@
|
|
|
24
24
|
|
|
25
25
|
import { Env } from '@catbee/utils/env';
|
|
26
26
|
import { uuid } from '@catbee/utils/id';
|
|
27
|
-
import { deepObjMerge } from '@catbee/utils/
|
|
27
|
+
import { deepClone, deepObjMerge } from '@catbee/utils/object';
|
|
28
28
|
|
|
29
29
|
var __defProp = Object.defineProperty;
|
|
30
30
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
31
|
-
var defaultCatbeeConfig = {
|
|
32
|
-
logger: {
|
|
33
|
-
level: Env.get("LOGGER_LEVEL", Env.isDev() ? "debug" : "info"),
|
|
34
|
-
name: Env.get("LOGGER_NAME", Env.get("npm_package_name", "@catbee/utils")),
|
|
35
|
-
pretty: Env.getBoolean("LOGGER_PRETTY", false),
|
|
36
|
-
colorize: Env.getBoolean("LOGGER_PRETTY_COLORIZE", true),
|
|
37
|
-
singleLine: Env.getBoolean("LOGGER_PRETTY_SINGLE_LINE", false),
|
|
38
|
-
dir: Env.getPath("LOGGER_DIR", "", {
|
|
39
|
-
mustExist: false
|
|
40
|
-
})
|
|
41
|
-
},
|
|
42
|
-
cache: {
|
|
43
|
-
defaultTtl: Env.getNumber("CACHE_DEFAULT_TTL_SECONDS", 3600) * 1e3
|
|
44
|
-
},
|
|
45
|
-
server: {
|
|
46
|
-
skipHealthz: Env.getBoolean("SERVER_SKIP_HEALTHZ", false)
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
31
|
var defaultServerConfig = {
|
|
50
|
-
port: 3e3,
|
|
51
|
-
host: "0.0.0.0",
|
|
52
|
-
cors: false,
|
|
53
|
-
helmet: false,
|
|
54
|
-
compression: false,
|
|
32
|
+
port: Env.getNumber("SERVER_PORT", 0) || Env.getNumber("PORT", 3e3),
|
|
33
|
+
host: Env.get("SERVER_HOST", "") || Env.get("HOST", "0.0.0.0"),
|
|
34
|
+
cors: Env.getBoolean("SERVER_CORS_ENABLE", false) ? {} : false,
|
|
35
|
+
helmet: Env.getBoolean("SERVER_HELMET_ENABLE", false) ? {} : false,
|
|
36
|
+
compression: Env.getBoolean("SERVER_COMPRESSION_ENABLE", false) ? {} : false,
|
|
55
37
|
bodyParser: {
|
|
56
38
|
json: {
|
|
57
|
-
limit: "1mb"
|
|
39
|
+
limit: Env.get("SERVER_BODY_PARSER_JSON_LIMIT", "1mb")
|
|
58
40
|
},
|
|
59
41
|
urlencoded: {
|
|
60
42
|
extended: true,
|
|
61
|
-
limit: "1mb"
|
|
43
|
+
limit: Env.get("SERVER_BODY_PARSER_URLENCODED_LIMIT", "1mb")
|
|
62
44
|
}
|
|
63
45
|
},
|
|
64
|
-
cookieParser: false,
|
|
65
|
-
isMicroservice: false,
|
|
66
|
-
appName: "
|
|
67
|
-
globalHeaders: {},
|
|
46
|
+
cookieParser: Env.getBoolean("SERVER_COOKIE_PARSER_ENABLE", false) ? {} : false,
|
|
47
|
+
isMicroservice: Env.getBoolean("SERVER_IS_MICROSERVICE", false),
|
|
48
|
+
appName: Env.get("SERVER_APP_NAME", Env.get("npm_package_name", "catbee_server")),
|
|
49
|
+
globalHeaders: Env.getJSON("SERVER_GLOBAL_HEADERS", {}),
|
|
68
50
|
rateLimit: {
|
|
69
|
-
enable: false,
|
|
70
|
-
windowMs: 15 * 60 * 1e3,
|
|
71
|
-
max: 100,
|
|
72
|
-
message: "Too many requests",
|
|
73
|
-
standardHeaders: true,
|
|
74
|
-
legacyHeaders: false
|
|
51
|
+
enable: Env.getBoolean("SERVER_RATE_LIMIT_ENABLE", false),
|
|
52
|
+
windowMs: Env.getDuration("SERVER_RATE_LIMIT_WINDOW_MS", 15 * 60 * 1e3),
|
|
53
|
+
max: Env.getNumber("SERVER_RATE_LIMIT_MAX", 100),
|
|
54
|
+
message: Env.get("SERVER_RATE_LIMIT_MESSAGE", "Too many requests, please try again later."),
|
|
55
|
+
standardHeaders: Env.getBoolean("SERVER_RATE_LIMIT_STANDARD_HEADERS", true),
|
|
56
|
+
legacyHeaders: Env.getBoolean("SERVER_RATE_LIMIT_LEGACY_HEADERS", false)
|
|
75
57
|
},
|
|
76
58
|
requestLogging: {
|
|
77
|
-
enable: Env.isDev(),
|
|
59
|
+
enable: Env.getBoolean("SERVER_REQUEST_LOGGING_ENABLE", Env.isDev()),
|
|
78
60
|
ignorePaths: /* @__PURE__ */ __name((req, _res) => {
|
|
79
61
|
const skipPaths = [
|
|
80
62
|
"/healthz",
|
|
@@ -85,47 +67,77 @@ var defaultServerConfig = {
|
|
|
85
67
|
];
|
|
86
68
|
return skipPaths.some((path) => req.path.startsWith(path));
|
|
87
69
|
}, "ignorePaths"),
|
|
88
|
-
skipNotFoundRoutes:
|
|
70
|
+
skipNotFoundRoutes: Env.getBoolean("SERVER_REQUEST_LOGGING_SKIP_NOT_FOUND_ROUTES", true)
|
|
89
71
|
},
|
|
72
|
+
trustProxy: Env.getBoolean("SERVER_TRUST_PROXY_ENABLE", false),
|
|
90
73
|
openApi: {
|
|
91
|
-
enable: false,
|
|
92
|
-
mountPath: "/docs",
|
|
93
|
-
verbose: false,
|
|
94
|
-
withGlobalPrefix: false
|
|
74
|
+
enable: Env.getBoolean("SERVER_OPENAPI_ENABLE", false),
|
|
75
|
+
mountPath: Env.get("SERVER_OPENAPI_MOUNT_PATH", "/docs"),
|
|
76
|
+
verbose: Env.getBoolean("SERVER_OPENAPI_VERBOSE", false),
|
|
77
|
+
withGlobalPrefix: Env.getBoolean("SERVER_OPENAPI_WITH_GLOBAL_PREFIX", false)
|
|
95
78
|
},
|
|
96
79
|
healthCheck: {
|
|
97
|
-
path: "/healthz",
|
|
98
|
-
detailed: true
|
|
80
|
+
path: Env.get("SERVER_HEALTH_CHECK_PATH", "/healthz"),
|
|
81
|
+
detailed: Env.getBoolean("SERVER_HEALTH_CHECK_DETAILED_OUTPUT", true),
|
|
82
|
+
withGlobalPrefix: Env.getBoolean("SERVER_HEALTH_CHECK_WITH_GLOBAL_PREFIX", false)
|
|
99
83
|
},
|
|
100
|
-
requestTimeout:
|
|
84
|
+
requestTimeout: Env.getDuration("SERVER_REQUEST_TIMEOUT_MS", 0),
|
|
101
85
|
responseTime: {
|
|
102
|
-
enable: false,
|
|
103
|
-
addHeader: true,
|
|
104
|
-
logOnComplete: false
|
|
86
|
+
enable: Env.getBoolean("SERVER_RESPONSE_TIME_ENABLE", false),
|
|
87
|
+
addHeader: Env.getBoolean("SERVER_RESPONSE_TIME_ADD_HEADER", true),
|
|
88
|
+
logOnComplete: Env.getBoolean("SERVER_RESPONSE_TIME_LOG_ON_COMPLETE", false)
|
|
105
89
|
},
|
|
106
90
|
requestId: {
|
|
107
|
-
headerName: "x-request-id",
|
|
108
|
-
exposeHeader: true,
|
|
91
|
+
headerName: Env.get("SERVER_REQUEST_ID_HEADER_NAME", "x-request-id"),
|
|
92
|
+
exposeHeader: Env.getBoolean("SERVER_REQUEST_ID_EXPOSE_HEADER", true),
|
|
109
93
|
generator: /* @__PURE__ */ __name(() => uuid(), "generator")
|
|
110
94
|
},
|
|
111
95
|
metrics: {
|
|
112
|
-
enable: false,
|
|
113
|
-
path: "/metrics",
|
|
114
|
-
withGlobalPrefix: false
|
|
96
|
+
enable: Env.getBoolean("SERVER_METRICS_ENABLE", false),
|
|
97
|
+
path: Env.get("SERVER_METRICS_PATH", "/metrics"),
|
|
98
|
+
withGlobalPrefix: Env.getBoolean("SERVER_METRICS_WITH_GLOBAL_PREFIX", false)
|
|
115
99
|
},
|
|
116
100
|
serviceVersion: {
|
|
117
|
-
enable: false,
|
|
118
|
-
headerName: "x-service-version",
|
|
119
|
-
version: "0.0.0"
|
|
120
|
-
}
|
|
101
|
+
enable: Env.getBoolean("SERVER_SERVICE_VERSION_ENABLE", false),
|
|
102
|
+
headerName: Env.get("SERVER_SERVICE_VERSION_HEADER_NAME", "x-service-version"),
|
|
103
|
+
version: Env.get("${npm_package_version}", "0.0.0")
|
|
104
|
+
},
|
|
105
|
+
skipHealthzChecksValidation: Env.getBoolean("SERVER_SKIP_HEALTHZ_CHECKS_VALIDATION", false)
|
|
121
106
|
};
|
|
122
|
-
|
|
123
|
-
|
|
107
|
+
var defaultCatbeeConfig = {
|
|
108
|
+
logger: {
|
|
109
|
+
level: Env.get("LOGGER_LEVEL", Env.isDev() || Env.isTest() ? "debug" : "info"),
|
|
110
|
+
name: Env.get("LOGGER_NAME", Env.get("npm_package_name", "@catbee/utils")),
|
|
111
|
+
pretty: Env.getBoolean("LOGGER_PRETTY", false),
|
|
112
|
+
colorize: Env.getBoolean("LOGGER_PRETTY_COLORIZE", true),
|
|
113
|
+
singleLine: Env.getBoolean("LOGGER_PRETTY_SINGLE_LINE", false),
|
|
114
|
+
dir: Env.getPath("LOGGER_DIR", "", {
|
|
115
|
+
mustExist: false
|
|
116
|
+
})
|
|
117
|
+
},
|
|
118
|
+
cache: {
|
|
119
|
+
defaultTtl: Env.getNumber("CACHE_DEFAULT_TTL_SECONDS", 3600) * 1e3
|
|
120
|
+
},
|
|
121
|
+
server: defaultServerConfig
|
|
122
|
+
};
|
|
123
|
+
var _config = deepClone(defaultCatbeeConfig);
|
|
124
|
+
function getCatbeeGlobalConfig() {
|
|
125
|
+
return deepClone(_config);
|
|
126
|
+
}
|
|
127
|
+
__name(getCatbeeGlobalConfig, "getCatbeeGlobalConfig");
|
|
128
|
+
var getConfig = getCatbeeGlobalConfig;
|
|
129
|
+
function setCatbeeGlobalConfig(value) {
|
|
130
|
+
_config = deepObjMerge(_config, value);
|
|
131
|
+
}
|
|
132
|
+
__name(setCatbeeGlobalConfig, "setCatbeeGlobalConfig");
|
|
133
|
+
var setConfig = setCatbeeGlobalConfig;
|
|
134
|
+
function setCatbeeServerGlobalConfig(value) {
|
|
135
|
+
_config.server = deepObjMerge(_config.server, value);
|
|
124
136
|
}
|
|
125
|
-
__name(
|
|
126
|
-
function
|
|
127
|
-
return
|
|
137
|
+
__name(setCatbeeServerGlobalConfig, "setCatbeeServerGlobalConfig");
|
|
138
|
+
function getCatbeeServerGlobalConfig() {
|
|
139
|
+
return getCatbeeGlobalConfig().server;
|
|
128
140
|
}
|
|
129
|
-
__name(
|
|
141
|
+
__name(getCatbeeServerGlobalConfig, "getCatbeeServerGlobalConfig");
|
|
130
142
|
|
|
131
|
-
export {
|
|
143
|
+
export { getCatbeeGlobalConfig, getCatbeeServerGlobalConfig, getConfig, setCatbeeGlobalConfig, setCatbeeServerGlobalConfig, setConfig };
|