-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathcls.options.ts
258 lines (226 loc) · 7.16 KB
/
cls.options.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { ExecutionContext, ModuleMetadata, Type } from '@nestjs/common';
import type { ClsService } from './cls.service';
import { ClsPlugin } from './plugin/cls-plugin.interface';
const getRandomString = () => Math.random().toString(36).slice(-8);
export class ClsModuleOptions {
/**
* whether to make the module global, so you don't need
* to import ClsModule.forFeature()` in other modules
*/
global? = false;
/**
* An object with additional options for the `ClsMiddleware`
*/
middleware?: ClsMiddlewareOptions;
/**
* An object with additional options for the `ClsGuard`
*/
guard?: ClsGuardOptions;
/**
* An object with additional options for the `ClsInterceptor`
*/
interceptor?: ClsInterceptorOptions;
/**
* Array of Proxy Provider classes to register
*/
proxyProviders?: Type[];
/**
* Array of ClsPlugin instances to register
*/
plugins?: ClsPlugin[];
}
export type ClsModuleFactoryOptions = Omit<
ClsModuleOptions,
'global' | 'proxyProviders' | 'plugins'
>;
export interface ClsModuleAsyncOptions extends Pick<ModuleMetadata, 'imports'> {
inject?: any[];
useFactory: (
...args: any[]
) => Promise<ClsModuleFactoryOptions> | ClsModuleFactoryOptions;
/**
* whether to make the module global, so you don't need
* to import `ClsModule.forFeature()` in other modules
*
* Default: `false`
*/
global?: boolean;
/**
* Array of Proxy Provider classes to register
*/
proxyProviders?: Type[];
/**
* Array of ClsPlugin instances to register
*/
plugins?: ClsPlugin[];
}
export class ClsContextOptions {
/**
* Sets the behavior of nested CLS context creation. Has no effect if no parent context exists.
*
* `inherit` (default) - Run the callback with a shallow copy of the parent context.
* Assignments to top-level properties will not be reflected in the parent context.
*
* `reuse` - Reuse existing context without creating a new one.
*
* `override` - Run the callback with an new empty context.
* Warning: No values from the parent context will be accessible.
*/
ifNested?: 'inherit' | 'reuse' | 'override' = 'inherit';
}
export class ClsInitializerCommonOptions {
/**
* whether to automatically generate request ids
*
* Default: `false`
*/
generateId? = false;
/**
* Whether to resolve proxy providers as a part
* of the CLS context registration
*
* Default: `true`
*/
resolveProxyProviders? = true;
/**
* Whether to run the initialization hooks for plugins as a part
* of the CLS context registration in this initializer
*
* Default: `true`
*/
initializePlugins? = true;
}
export class ClsMiddlewareOptions extends ClsInitializerCommonOptions {
/**
* whether to mount the middleware to every route
*
* Default: `false`
*/
mount? = false;
/**
* the function to generate request ids for the CLS context
*/
idGenerator?: (req: any) => string | Promise<string> = getRandomString;
/**
* Function that executes after the CLS context has been initialized.
* It can be used to put additional variables in the CLS context.
*/
setup?: (cls: ClsService, req: any, res: any) => void | Promise<void>;
/**
* Whether to store the Request object to the CLS
* It will be available under the CLS_REQ key
*/
saveReq? = true;
/**
* Whether to store the Response object to the CLS
* It will be available under the CLS_RES key
*/
saveRes? = false;
/**
* Set to true to set up the context using a call to
* `AsyncLocalStorage#enterWith` instead of wrapping the
* `next()` call with the safer `AsyncLocalStorage#run`
*
* Most of the time this should not be necessary, but
* some frameworks are known to lose the context wih `run`.
*/
useEnterWith? = false;
}
export class ClsGuardOptions extends ClsInitializerCommonOptions {
/**
* whether to mount the guard globally
*
* Default: `false`
*/
mount? = false;
/**
* the function to generate request ids inside the guard
*/
idGenerator?: (context: ExecutionContext) => string | Promise<string> =
getRandomString;
/**
* Function that executes after the CLS context has been initialised.
* It can be used to put additional variables in the CLS context.
*/
setup?: (
cls: ClsService,
context: ExecutionContext,
) => void | Promise<void>;
/**
* Whether to store the ExecutionContext object to the CLS
* It will be available under the CLS_CTX key
*
* Default: `true`
*/
saveCtx? = true;
}
export class ClsInterceptorOptions extends ClsInitializerCommonOptions {
/**
* whether to mount the interceptor globally
*
* Default: `false`
*/
mount? = false;
/**
* the function to generate request ids inside the interceptor
*/
idGenerator?: (context: ExecutionContext) => string | Promise<string> =
getRandomString;
/**
* Function that executes after the CLS context has been initialised.
* It can be used to put additional variables in the CLS context.
*/
setup?: (
cls: ClsService,
context: ExecutionContext,
) => void | Promise<void>;
/**
* Whether to store the ExecutionContext object to the CLS
* It will be available under the CLS_CTX key
*
* Default: `true`
*/
saveCtx? = true;
}
export class ClsDecoratorOptions<
T extends any[],
> extends ClsInitializerCommonOptions {
/**
* Additional options for the `ClsService#run` method.
*/
runOptions?: ClsContextOptions;
/**
* The function to generate request ids inside the interceptor.
*
* Takes the same parameters in the same order as the decorated function.
*
* If you use a `function` expression, it will executed with the `this` context of the decorated class instance.
* to get type safety, use:
*
* `idGenerator: function (this: MyClass, ...args) { ... }`
*
* Note: To avoid type errors, you must list all parameters, even if they're not used,
* or type the decorator as:
*
* `@UseCls<[arg1: Type1, arg2: Type2]>()`
*/
idGenerator?: (...args: T) => string | Promise<string> = getRandomString;
/**
* Function that executes after the CLS context has been initialized.
* Takes ClsService as the first parameter and then the same parameters in the same order as the decorated function.
*
* If you use a `function` expression, it will executed with the `this` context of the decorated class instance.
* to get type safety, use:
*
* `setup: function (this: MyClass, cls: ClsService, ...args) { ... }`
*
* Note: To avoid type errors, you must list all parameters, even if they're not used,
* or type the decorator as:
*
* `@UseCls<[arg1: Type1, arg2: Type2]>()`
*/
setup?: (cls: ClsService, ...args: T) => void | Promise<void>;
}
export interface ClsStore {
[key: symbol]: any;
}