@catbee/utils 1.1.0 → 2.0.0-next.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 +27 -27
- package/array/array.utils.d.ts +191 -0
- package/array/index.cjs +246 -0
- package/array/index.d.ts +25 -0
- package/array/index.mjs +228 -0
- package/async/async.utils.d.ts +296 -0
- package/async/index.cjs +428 -0
- package/async/index.d.ts +25 -0
- package/async/index.mjs +407 -0
- package/cache/cache.utils.d.ts +176 -0
- package/cache/index.cjs +292 -0
- package/cache/index.d.ts +25 -0
- package/cache/index.mjs +290 -0
- package/config/config.d.ts +57 -0
- package/config/index.cjs +136 -0
- package/config/index.d.ts +26 -0
- package/config/index.mjs +131 -0
- package/context-store/context-store.utils.d.ts +212 -0
- package/context-store/index.cjs +267 -0
- package/context-store/index.d.ts +25 -0
- package/context-store/index.mjs +261 -0
- package/crypto/crypto.utils.d.ts +183 -0
- package/crypto/index.cjs +182 -0
- package/crypto/index.d.ts +25 -0
- package/crypto/index.mjs +166 -0
- package/date/date.utils.d.ts +190 -0
- package/date/index.cjs +295 -0
- package/date/index.d.ts +25 -0
- package/date/index.mjs +283 -0
- package/decorators/decorators.utils.d.ts +705 -0
- package/decorators/index.cjs +913 -0
- package/decorators/index.d.ts +25 -0
- package/decorators/index.mjs +872 -0
- package/dir/dir.utils.d.ts +216 -0
- package/dir/index.cjs +416 -0
- package/dir/index.d.ts +25 -0
- package/dir/index.mjs +389 -0
- package/env/env.utils.d.ts +400 -0
- package/env/index.cjs +761 -0
- package/env/index.d.ts +25 -0
- package/env/index.mjs +758 -0
- package/exception/exception.utils.d.ts +253 -0
- package/exception/index.cjs +362 -0
- package/exception/index.d.ts +25 -0
- package/exception/index.mjs +338 -0
- package/fs/fs.utils.d.ts +196 -0
- package/fs/index.cjs +253 -0
- package/fs/index.d.ts +25 -0
- package/fs/index.mjs +228 -0
- package/http-status-codes/http-status-codes.d.ts +289 -0
- package/http-status-codes/index.cjs +96 -0
- package/http-status-codes/index.d.ts +25 -0
- package/http-status-codes/index.mjs +94 -0
- package/id/id.utils.d.ts +59 -0
- package/id/index.cjs +62 -0
- package/id/index.d.ts +25 -0
- package/id/index.mjs +56 -0
- package/index.cjs +218 -0
- package/index.d.ts +51 -0
- package/index.mjs +51 -0
- package/logger/index.cjs +334 -0
- package/logger/index.d.ts +25 -0
- package/logger/index.mjs +313 -0
- package/logger/logger.utils.d.ts +210 -0
- package/middleware/index.cjs +177 -0
- package/middleware/index.d.ts +25 -0
- package/middleware/index.mjs +170 -0
- package/middleware/middleware.utils.d.ts +123 -0
- package/obj/index.cjs +317 -0
- package/obj/index.d.ts +25 -0
- package/obj/index.mjs +301 -0
- package/obj/obj.utils.d.ts +156 -0
- package/package.json +171 -19
- package/performance/index.cjs +231 -0
- package/performance/index.d.ts +25 -0
- package/performance/index.mjs +225 -0
- package/performance/performance.utils.d.ts +159 -0
- package/request/index.cjs +202 -0
- package/request/index.d.ts +26 -0
- package/request/index.mjs +194 -0
- package/request/request.utils.d.ts +109 -0
- package/response/index.cjs +234 -0
- package/response/index.d.ts +26 -0
- package/response/index.mjs +222 -0
- package/response/response.utils.d.ts +186 -0
- package/server/index.cjs +1627 -0
- package/server/index.d.ts +28 -0
- package/server/index.mjs +1617 -0
- package/server/server.builder.d.ts +531 -0
- package/server/server.d.ts +303 -0
- package/stream/index.cjs +151 -0
- package/stream/index.d.ts +25 -0
- package/stream/index.mjs +144 -0
- package/stream/stream.utils.d.ts +111 -0
- package/string/index.cjs +109 -0
- package/string/index.d.ts +25 -0
- package/string/index.mjs +95 -0
- package/string/string.utils.d.ts +124 -0
- package/type/index.cjs +129 -0
- package/type/index.d.ts +25 -0
- package/type/index.mjs +119 -0
- package/type/type.utils.d.ts +129 -0
- package/types/api-response.d.ts +175 -0
- package/types/common.d.ts +148 -0
- package/types/config.d.ts +88 -0
- package/types/index.cjs +34 -0
- package/types/index.d.ts +28 -0
- package/types/index.mjs +32 -0
- package/types/server.d.ts +291 -0
- package/url/index.cjs +201 -0
- package/url/index.d.ts +25 -0
- package/url/index.mjs +189 -0
- package/url/url.utils.d.ts +164 -0
- package/validate/index.cjs +212 -0
- package/validate/index.d.ts +25 -0
- package/validate/index.mjs +188 -0
- package/validate/validate.utils.d.ts +200 -0
- package/build/index.cjs +0 -7694
- package/build/index.d.ts +0 -5792
- package/build/index.mjs +0 -7386
package/config/index.cjs
ADDED
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 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 env = require('@catbee/utils/env');
|
|
28
|
+
var id = require('@catbee/utils/id');
|
|
29
|
+
var obj = require('@catbee/utils/obj');
|
|
30
|
+
|
|
31
|
+
var __defProp = Object.defineProperty;
|
|
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
|
+
var defaultServerConfig = {
|
|
52
|
+
port: 3e3,
|
|
53
|
+
host: "0.0.0.0",
|
|
54
|
+
cors: false,
|
|
55
|
+
helmet: false,
|
|
56
|
+
compression: false,
|
|
57
|
+
bodyParser: {
|
|
58
|
+
json: {
|
|
59
|
+
limit: "1mb"
|
|
60
|
+
},
|
|
61
|
+
urlencoded: {
|
|
62
|
+
extended: true,
|
|
63
|
+
limit: "1mb"
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
cookieParser: false,
|
|
67
|
+
isMicroservice: false,
|
|
68
|
+
appName: "express_app",
|
|
69
|
+
globalHeaders: {},
|
|
70
|
+
rateLimit: {
|
|
71
|
+
enable: false,
|
|
72
|
+
windowMs: 15 * 60 * 1e3,
|
|
73
|
+
max: 100,
|
|
74
|
+
message: "Too many requests",
|
|
75
|
+
standardHeaders: true,
|
|
76
|
+
legacyHeaders: false
|
|
77
|
+
},
|
|
78
|
+
requestLogging: {
|
|
79
|
+
enable: env.Env.isDev(),
|
|
80
|
+
ignorePaths: /* @__PURE__ */ __name((req, _res) => {
|
|
81
|
+
const skipPaths = [
|
|
82
|
+
"/healthz",
|
|
83
|
+
"/favicon.ico",
|
|
84
|
+
"/metrics",
|
|
85
|
+
"/docs",
|
|
86
|
+
"/.well-known"
|
|
87
|
+
];
|
|
88
|
+
return skipPaths.some((path) => req.path.startsWith(path));
|
|
89
|
+
}, "ignorePaths"),
|
|
90
|
+
skipNotFoundRoutes: false
|
|
91
|
+
},
|
|
92
|
+
openApi: {
|
|
93
|
+
enable: false,
|
|
94
|
+
mountPath: "/docs",
|
|
95
|
+
verbose: false,
|
|
96
|
+
withGlobalPrefix: false
|
|
97
|
+
},
|
|
98
|
+
healthCheck: {
|
|
99
|
+
path: "/healthz",
|
|
100
|
+
detailed: true
|
|
101
|
+
},
|
|
102
|
+
requestTimeout: 3e4,
|
|
103
|
+
responseTime: {
|
|
104
|
+
enable: false,
|
|
105
|
+
addHeader: true,
|
|
106
|
+
logOnComplete: false
|
|
107
|
+
},
|
|
108
|
+
requestId: {
|
|
109
|
+
headerName: "x-request-id",
|
|
110
|
+
exposeHeader: true,
|
|
111
|
+
generator: /* @__PURE__ */ __name(() => id.uuid(), "generator")
|
|
112
|
+
},
|
|
113
|
+
metrics: {
|
|
114
|
+
enable: false,
|
|
115
|
+
path: "/metrics",
|
|
116
|
+
withGlobalPrefix: false
|
|
117
|
+
},
|
|
118
|
+
serviceVersion: {
|
|
119
|
+
enable: false,
|
|
120
|
+
headerName: "x-service-version",
|
|
121
|
+
version: "0.0.0"
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
function setConfig(value) {
|
|
125
|
+
obj.deepObjMerge(defaultCatbeeConfig, value);
|
|
126
|
+
}
|
|
127
|
+
__name(setConfig, "setConfig");
|
|
128
|
+
function getConfig() {
|
|
129
|
+
return defaultCatbeeConfig;
|
|
130
|
+
}
|
|
131
|
+
__name(getConfig, "getConfig");
|
|
132
|
+
|
|
133
|
+
exports.defaultCatbeeConfig = defaultCatbeeConfig;
|
|
134
|
+
exports.defaultServerConfig = defaultServerConfig;
|
|
135
|
+
exports.getConfig = getConfig;
|
|
136
|
+
exports.setConfig = setConfig;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 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
|
+
export { setConfig, getConfig } from './config';
|
|
26
|
+
export { defaultCatbeeConfig, defaultServerConfig } from './config';
|
package/config/index.mjs
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 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
|
+
import { Env } from '@catbee/utils/env';
|
|
26
|
+
import { uuid } from '@catbee/utils/id';
|
|
27
|
+
import { deepObjMerge } from '@catbee/utils/obj';
|
|
28
|
+
|
|
29
|
+
var __defProp = Object.defineProperty;
|
|
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
|
+
var defaultServerConfig = {
|
|
50
|
+
port: 3e3,
|
|
51
|
+
host: "0.0.0.0",
|
|
52
|
+
cors: false,
|
|
53
|
+
helmet: false,
|
|
54
|
+
compression: false,
|
|
55
|
+
bodyParser: {
|
|
56
|
+
json: {
|
|
57
|
+
limit: "1mb"
|
|
58
|
+
},
|
|
59
|
+
urlencoded: {
|
|
60
|
+
extended: true,
|
|
61
|
+
limit: "1mb"
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
cookieParser: false,
|
|
65
|
+
isMicroservice: false,
|
|
66
|
+
appName: "express_app",
|
|
67
|
+
globalHeaders: {},
|
|
68
|
+
rateLimit: {
|
|
69
|
+
enable: false,
|
|
70
|
+
windowMs: 15 * 60 * 1e3,
|
|
71
|
+
max: 100,
|
|
72
|
+
message: "Too many requests",
|
|
73
|
+
standardHeaders: true,
|
|
74
|
+
legacyHeaders: false
|
|
75
|
+
},
|
|
76
|
+
requestLogging: {
|
|
77
|
+
enable: Env.isDev(),
|
|
78
|
+
ignorePaths: /* @__PURE__ */ __name((req, _res) => {
|
|
79
|
+
const skipPaths = [
|
|
80
|
+
"/healthz",
|
|
81
|
+
"/favicon.ico",
|
|
82
|
+
"/metrics",
|
|
83
|
+
"/docs",
|
|
84
|
+
"/.well-known"
|
|
85
|
+
];
|
|
86
|
+
return skipPaths.some((path) => req.path.startsWith(path));
|
|
87
|
+
}, "ignorePaths"),
|
|
88
|
+
skipNotFoundRoutes: false
|
|
89
|
+
},
|
|
90
|
+
openApi: {
|
|
91
|
+
enable: false,
|
|
92
|
+
mountPath: "/docs",
|
|
93
|
+
verbose: false,
|
|
94
|
+
withGlobalPrefix: false
|
|
95
|
+
},
|
|
96
|
+
healthCheck: {
|
|
97
|
+
path: "/healthz",
|
|
98
|
+
detailed: true
|
|
99
|
+
},
|
|
100
|
+
requestTimeout: 3e4,
|
|
101
|
+
responseTime: {
|
|
102
|
+
enable: false,
|
|
103
|
+
addHeader: true,
|
|
104
|
+
logOnComplete: false
|
|
105
|
+
},
|
|
106
|
+
requestId: {
|
|
107
|
+
headerName: "x-request-id",
|
|
108
|
+
exposeHeader: true,
|
|
109
|
+
generator: /* @__PURE__ */ __name(() => uuid(), "generator")
|
|
110
|
+
},
|
|
111
|
+
metrics: {
|
|
112
|
+
enable: false,
|
|
113
|
+
path: "/metrics",
|
|
114
|
+
withGlobalPrefix: false
|
|
115
|
+
},
|
|
116
|
+
serviceVersion: {
|
|
117
|
+
enable: false,
|
|
118
|
+
headerName: "x-service-version",
|
|
119
|
+
version: "0.0.0"
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
function setConfig(value) {
|
|
123
|
+
deepObjMerge(defaultCatbeeConfig, value);
|
|
124
|
+
}
|
|
125
|
+
__name(setConfig, "setConfig");
|
|
126
|
+
function getConfig() {
|
|
127
|
+
return defaultCatbeeConfig;
|
|
128
|
+
}
|
|
129
|
+
__name(getConfig, "getConfig");
|
|
130
|
+
|
|
131
|
+
export { defaultCatbeeConfig, defaultServerConfig, getConfig, setConfig };
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 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
|
+
import { AsyncLocalStorage } from 'async_hooks';
|
|
26
|
+
/**
|
|
27
|
+
* Type representing the store object used in AsyncLocalStorage.
|
|
28
|
+
* Keys must be unique symbols to ensure isolation and prevent collisions.
|
|
29
|
+
*/
|
|
30
|
+
export interface Store {
|
|
31
|
+
[key: symbol]: unknown;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Predefined symbols used as keys in AsyncLocalStorage.
|
|
35
|
+
* Add new symbols here to avoid duplication.
|
|
36
|
+
*/
|
|
37
|
+
export declare const StoreKeys: {
|
|
38
|
+
LOGGER: symbol;
|
|
39
|
+
REQUEST_ID: symbol;
|
|
40
|
+
USER: symbol;
|
|
41
|
+
SESSION: symbol;
|
|
42
|
+
TRANSACTION_ID: symbol;
|
|
43
|
+
USER_ID: symbol;
|
|
44
|
+
TENANT_ID: symbol;
|
|
45
|
+
TRACE_ID: symbol;
|
|
46
|
+
CORRELATION_ID: symbol;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Retrieves the current request ID from the async context, if available.
|
|
50
|
+
*
|
|
51
|
+
* @returns {string | undefined} The request ID string or undefined if not present in the current context.
|
|
52
|
+
*/
|
|
53
|
+
export declare function getRequestId(): string | undefined;
|
|
54
|
+
/**
|
|
55
|
+
* Type-safe getter for common context values.
|
|
56
|
+
*
|
|
57
|
+
* @param key The store key symbol
|
|
58
|
+
* @returns The typed value from the store
|
|
59
|
+
*/
|
|
60
|
+
export declare function getFromContext<T>(key: symbol): T | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* ContextStore manages per-request scoped context using AsyncLocalStorage.
|
|
63
|
+
* It allows storing and retrieving data across async calls (e.g., request ID, logger).
|
|
64
|
+
*
|
|
65
|
+
* Example (Express middleware):
|
|
66
|
+
* ```
|
|
67
|
+
* // Middleware to initialize context and logger
|
|
68
|
+
* app.use((req, res, next) => {
|
|
69
|
+
* const requestId = req.headers["x-request-id"]?.toString() || crypto.randomUUID();
|
|
70
|
+
* ContextStore.run({ [StoreKeys.REQUEST_ID]: requestId }, () => {
|
|
71
|
+
* ContextStore.set(StoreKeys.LOGGER, getLogger().child({ requestId }));
|
|
72
|
+
* next();
|
|
73
|
+
* });
|
|
74
|
+
* });
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export declare class ContextStore {
|
|
78
|
+
/**
|
|
79
|
+
* The underlying AsyncLocalStorage instance for context.
|
|
80
|
+
* @private
|
|
81
|
+
*/
|
|
82
|
+
private static readonly storage;
|
|
83
|
+
/**
|
|
84
|
+
* Returns the raw AsyncLocalStorage instance for advanced access.
|
|
85
|
+
*
|
|
86
|
+
* @returns {AsyncLocalStorage<Store>} The AsyncLocalStorage instance.
|
|
87
|
+
*/
|
|
88
|
+
static getInstance(): AsyncLocalStorage<Store>;
|
|
89
|
+
/**
|
|
90
|
+
* Retrieves a value from the async context store by symbol key.
|
|
91
|
+
*
|
|
92
|
+
* @typeParam T - The expected return type of the value.
|
|
93
|
+
* @param {symbol} key - Unique symbol used as the store key.
|
|
94
|
+
* @returns {T | undefined} The value found (typed) or undefined if not present.
|
|
95
|
+
*/
|
|
96
|
+
static get<T>(key: symbol): T | undefined;
|
|
97
|
+
/**
|
|
98
|
+
* Sets a value in the current context store by symbol key.
|
|
99
|
+
*
|
|
100
|
+
* @typeParam T - The value type to set.
|
|
101
|
+
* @param {symbol} key - Unique symbol key.
|
|
102
|
+
* @param {T} value - Value to set in context.
|
|
103
|
+
* @throws {Error} If called outside an active context (not within a .run call or in the wrong async boundaries).
|
|
104
|
+
*/
|
|
105
|
+
static set<T>(key: symbol, value: T): void;
|
|
106
|
+
/**
|
|
107
|
+
* Retrieves the entire context store object for the current async context.
|
|
108
|
+
*
|
|
109
|
+
* @returns {Store | undefined} The current store object or undefined if called outside a context.
|
|
110
|
+
*/
|
|
111
|
+
static getAll(): Store | undefined;
|
|
112
|
+
/**
|
|
113
|
+
* Initializes a new async context and executes a callback within it.
|
|
114
|
+
* This must be called at the beginning of a request or logical async flow.
|
|
115
|
+
*
|
|
116
|
+
* @typeParam T - The callback's return type.
|
|
117
|
+
* @param {Store} store - The initial key-value store object.
|
|
118
|
+
* @param {() => T} callback - The function to run within the new context.
|
|
119
|
+
* @returns {T} The result of the callback function.
|
|
120
|
+
*/
|
|
121
|
+
static run<T>(store: Store, callback: () => T): T;
|
|
122
|
+
/**
|
|
123
|
+
* Checks if a key exists in the current context store.
|
|
124
|
+
*
|
|
125
|
+
* @param {symbol} key - The symbol key to check.
|
|
126
|
+
* @returns {boolean} True if the key exists, false otherwise.
|
|
127
|
+
*/
|
|
128
|
+
static has(key: symbol): boolean;
|
|
129
|
+
/**
|
|
130
|
+
* Removes a value from the current context store by symbol key.
|
|
131
|
+
*
|
|
132
|
+
* @param {symbol} key - The symbol key to delete.
|
|
133
|
+
* @returns {boolean} True if the key was deleted, false if the key wasn't found or no active context.
|
|
134
|
+
* @throws {Error} If called outside an active context.
|
|
135
|
+
*/
|
|
136
|
+
static delete(key: symbol): boolean;
|
|
137
|
+
/**
|
|
138
|
+
* Updates multiple values in the current context store at once.
|
|
139
|
+
*
|
|
140
|
+
* @param {Partial<Record<symbol, unknown>>} values - Object containing symbol keys and values to update.
|
|
141
|
+
* @throws {Error} If called outside an active context.
|
|
142
|
+
*/
|
|
143
|
+
static patch(values: Partial<Record<symbol, unknown>>): void;
|
|
144
|
+
/**
|
|
145
|
+
* Executes a callback with a temporary store value that only exists during execution.
|
|
146
|
+
* Original store is restored after callback completes.
|
|
147
|
+
*
|
|
148
|
+
* @typeParam T - The callback's return type.
|
|
149
|
+
* @param {symbol} key - The symbol key to temporarily set.
|
|
150
|
+
* @param {unknown} value - The temporary value.
|
|
151
|
+
* @param {() => T} callback - The function to execute with the temporary value.
|
|
152
|
+
* @returns {T} The result of the callback function.
|
|
153
|
+
* @throws {Error} If called outside an active context.
|
|
154
|
+
*/
|
|
155
|
+
static withValue<T>(key: symbol, value: unknown, callback: () => T): T;
|
|
156
|
+
/**
|
|
157
|
+
* Creates a new context that inherits values from the current context.
|
|
158
|
+
*
|
|
159
|
+
* @typeParam T - The callback's return type.
|
|
160
|
+
* @param {Partial<Record<symbol, unknown>>} newValues - New values to add to or override in the context.
|
|
161
|
+
* @param {() => T} callback - The function to execute in the new context.
|
|
162
|
+
* @returns {T} The result of the callback function.
|
|
163
|
+
*/
|
|
164
|
+
static extend<T>(newValues: Partial<Record<symbol, unknown>>, callback: () => T): T;
|
|
165
|
+
/**
|
|
166
|
+
* Creates Express middleware that initializes a context for each request.
|
|
167
|
+
*
|
|
168
|
+
* @param {(req: any) => Partial<Record<symbol, unknown>>} initialValuesFactory - Function that returns initial context values.
|
|
169
|
+
* @returns Express middleware function.
|
|
170
|
+
*/
|
|
171
|
+
static createExpressMiddleware(initialValuesFactory?: (req: any) => Partial<Record<symbol, unknown>>): (req: any, _res: any, next: any) => void;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Type-safe wrapper for accessing and modifying context values with specific types.
|
|
175
|
+
*
|
|
176
|
+
* @typeParam T - The value type.
|
|
177
|
+
*/
|
|
178
|
+
export declare class TypedContextKey<T> {
|
|
179
|
+
private symbol;
|
|
180
|
+
private defaultValue?;
|
|
181
|
+
/**
|
|
182
|
+
* Creates a new typed context key.
|
|
183
|
+
*
|
|
184
|
+
* @param symbol - The unique symbol for this key
|
|
185
|
+
* @param defaultValue - Optional default value if key is not found
|
|
186
|
+
*/
|
|
187
|
+
constructor(symbol: symbol, defaultValue?: T | undefined);
|
|
188
|
+
/**
|
|
189
|
+
* Gets the current value for this key.
|
|
190
|
+
*
|
|
191
|
+
* @returns The value or defaultValue if not found
|
|
192
|
+
*/
|
|
193
|
+
get(): T | undefined;
|
|
194
|
+
/**
|
|
195
|
+
* Sets the value for this key.
|
|
196
|
+
*
|
|
197
|
+
* @param value The value to set
|
|
198
|
+
*/
|
|
199
|
+
set(value: T): void;
|
|
200
|
+
/**
|
|
201
|
+
* Checks if this key exists in the context.
|
|
202
|
+
*
|
|
203
|
+
* @returns True if the key exists
|
|
204
|
+
*/
|
|
205
|
+
exists(): boolean;
|
|
206
|
+
/**
|
|
207
|
+
* Deletes this key from the context.
|
|
208
|
+
*
|
|
209
|
+
* @returns True if the key was deleted
|
|
210
|
+
*/
|
|
211
|
+
delete(): boolean;
|
|
212
|
+
}
|