You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(core): add saveCtx option in enhancer setup (#212)
* refactor(core): split constants file into internal and public
* feat(core): add saveCtx option in enhancer setup
This allows storing the ExecutionContext in the CLS (enabled by default)
* docs: add docs on CLS_CTX
Copy file name to clipboardExpand all lines: docs/docs/03_features-and-use-cases/06_proxy-providers.md
+4-2
Original file line number
Diff line number
Diff line change
@@ -71,7 +71,7 @@ export class UserInterceptor implements NestInterceptor {
71
71
72
72
It is also possible to inject other providers into the Proxy Provider to avoid having to do this in a separate component.
73
73
74
-
For the convenience, the `CLS_REQ` and `CLS_RES` are also made into Proxy Providers and are exported from the `ClsModule`.
74
+
For the convenience, the `CLS_REQ` and `CLS_RES`(if enabled) and `CLS_CTX` (when an enhancer is used) are also made into Proxy Providers and are exported from the `ClsModule`.
75
75
76
76
```ts title=user-with-rile.proxy.ts
77
77
@InjectableProxy()
@@ -105,6 +105,8 @@ ClsModule.forFeatureAsync({
105
105
106
106
Using `@Inject(CLS_REQ)`, you can entirely replace `@Inject(REQUEST)` in REQUEST-SCOPED providers to turn them into CLS-enabled singletons without changing the implementation.
107
107
108
+
Also `@INJECT(CLS_CTX)` can be used to replace `@Inject(CONTEXT)`.
109
+
108
110
:::
109
111
110
112
## Factory Proxy Providers
@@ -305,6 +307,6 @@ In versions prior to `v4.0`, calling `typeof` on an instance of a Proxy provider
305
307
306
308
### Limited support for injecting Proxy Providers into each other
307
309
308
-
Apart from the built-in `CLS_REQ` and `CLS_RES` proxy providers, custom Proxy Providers cannot be _reliably_ injected into other Proxy Providers, because there is no system in place to resolve them in the correct order (as far as Nest is concerned, all of them have already been bootstrapped, so it can't help us here), so it may happen, that during the proxy provider resolution phase, a Proxy Provider that is injected into another Proxy Provider is not yet resolved and falls back to an empty object.
310
+
Apart from the built-in `CLS_REQ` and `CLS_RES`and `CLS_CTX`proxy providers, custom Proxy Providers cannot be _reliably_ injected into other Proxy Providers, because there is no system in place to resolve them in the correct order (as far as Nest is concerned, all of them have already been bootstrapped, so it can't help us here), so it may happen, that during the proxy provider resolution phase, a Proxy Provider that is injected into another Proxy Provider is not yet resolved and falls back to an empty object.
309
311
310
312
There is an open [feature request](https://github.com/Papooch/nestjs-cls/issues/169) to address this shortcoming, but until then, refer to the manual [Selective resolution of Proxy Providers](#selective-resolution-of-proxy-providers) technique. You can also leverage the [strict](#strict-proxy-providers) mode to find out which Proxy Providers are not yet resolved.
Whether to make the module global, so you do not have to import `ClsModule.forFeature()` in other modules.
18
18
19
-
-**_`proxyProviders?:`_**`Type[]`
20
-
Array of [Proxy Providers](../03_features-and-use-cases/06_proxy-providers.md) that should be registered in the root module. Currently only accepts sync class Proxy providers, use `ClsModule.forFeatureAsync()` for more complex use-cases.
19
+
-**_`proxyProviders?:`_**`Type[]`
20
+
Array of [Proxy Providers](../03_features-and-use-cases/06_proxy-providers.md) that should be registered in the root module. Currently only accepts sync class Proxy providers, use `ClsModule.forFeatureAsync()` for more complex use-cases.
21
21
22
22
`ClsModule.forRootAsync()` is also available. You can supply the usual `imports`, `inject` and `useFactory` parameters as usual.
23
23
@@ -33,65 +33,70 @@ The `ClsModule.forFeature()` method can be used to register a [Proxy Providers](
33
33
34
34
The `ClsModule.forFeatureAsync()` method accepts either `ClsModuleProxyClassProviderOptions` or `ClsModuleProxyFactoryProviderOptions` that both accept these options:
35
35
36
-
-**_`provide?:`_**`any`
37
-
Custom injection token to use for the provider. In case of a class provider, this parameter is optional, as the class reference passed to `useClass` will be used by default.
36
+
-**_`provide?:`_**`any`
37
+
Custom injection token to use for the provider. In case of a class provider, this parameter is optional, as the class reference passed to `useClass` will be used by default.
38
38
39
-
-**_`imports?`_**`any[]`
40
-
Optional list of imported modules that export the providers which are required for the provider.
39
+
-**_`imports?`_**`any[]`
40
+
Optional list of imported modules that export the providers which are required for the provider.
41
41
42
-
-**_`extraProviders?:`_**`Provider[]`
43
-
Optional list of additional providers that should be available to the Proxy. Useful for passing configuration from a parent dynamic module.
42
+
-**_`extraProviders?:`_**`Provider[]`
43
+
Optional list of additional providers that should be available to the Proxy. Useful for passing configuration from a parent dynamic module.
44
44
45
45
The `ClsModuleProxyClassProviderOptions` interface further accepts:
46
46
47
-
-**_`useClass:`_**`Type`
48
-
The target class that will be used by this Proxy Provider. Make sure it is decorated with `@InjectableProxy`.
47
+
-**_`useClass:`_**`Type`
48
+
The target class that will be used by this Proxy Provider. Make sure it is decorated with `@InjectableProxy`.
49
49
50
50
The `ClsModuleProxyFactoryProviderOptions` interface further accepts:
51
51
52
-
-**_`inject:`_**`any[]`
53
-
An array of injection tokens for providers used in the `useFactory`.
52
+
-**_`inject:`_**`any[]`
53
+
An array of injection tokens for providers used in the `useFactory`.
54
54
55
-
-**_`useFactory:`_**`(...args: any[]) => any`
56
-
Factory function that accepts an array of providers in the order of the according tokens in the `inject` array. Returns (or resolves with) an object (or a function) that will be used by this Proxy Provider.
55
+
-**_`useFactory:`_**`(...args: any[]) => any`
56
+
Factory function that accepts an array of providers in the order of the according tokens in the `inject` array. Returns (or resolves with) an object (or a function) that will be used by this Proxy Provider.
57
57
58
-
-**_`type?:`_**`'function' | 'object'`
59
-
Whether the Proxy Provider should be a function or an object. Defaults to `'object'`. See [Caveats](../03_features-and-use-cases/06_proxy-providers.md#caveats) for more information.
58
+
-**_`type?:`_**`'function' | 'object'`
59
+
Whether the Proxy Provider should be a function or an object. Defaults to `'object'`. See [Caveats](../03_features-and-use-cases/06_proxy-providers.md#caveats) for more information.
60
60
61
-
-**_`strict?:`_**`boolean`
62
-
Whether to register this Proxy Provider in [strict mode](../03_features-and-use-cases/06_proxy-providers.md#strict-proxy-providers). Defaults to `false`.
61
+
-**_`strict?:`_**`boolean`
62
+
Whether to register this Proxy Provider in [strict mode](../03_features-and-use-cases/06_proxy-providers.md#strict-proxy-providers). Defaults to `false`.
63
63
64
64
## Middleware & Enhancer options
65
65
66
66
All of the **`Cls{Middleware,Guard,Interceptor}Options`** take the following parameters (either in `ClsModuleOptions` or directly when instantiating them manually):
67
67
68
-
-**_`mount?:`_**`boolean` (default _`false`_)
69
-
Whether to automatically mount the middleware/guard/interceptor to every route (not applicable when instantiating them manually)
68
+
-**_`mount?:`_**`boolean` (default _`false`_)
69
+
Whether to automatically mount the middleware/guard/interceptor to every route (not applicable when instantiating them manually)
An optional function for generating the request ID. It takes the `Request` object (or the `ExecutionContext` in case of a Guard or Interceptor) as an argument and (synchronously or asynchronously) returns a string. The default implementation uses `Math.random()` to generate a string of 8 characters.
An optional function for generating the request ID. It takes the `Request` object (or the `ExecutionContext` in case of a Guard or Interceptor) as an argument and (synchronously or asynchronously) returns a string. The default implementation uses `Math.random()` to generate a string of 8 characters.
Set to `true` to set up the context using a call to [`AsyncLocalStorage#enterWith`](https://nodejs.org/api/async_context.html#async_context_asynclocalstorage_enterwith_store) instead of wrapping the `next()` call with the safer [`AsyncLocalStorage#run`](https://nodejs.org/api/async_context.html#async_context_asynclocalstorage_run_store_callback_args). Most of the time this should not be necessary, but [some frameworks](../05_considerations/02_compatibility.md#graphql) have been known to lose the context with `run`.
Set to `true` to set up the context using a call to [`AsyncLocalStorage#enterWith`](https://nodejs.org/api/async_context.html#async_context_asynclocalstorage_enterwith_store) instead of wrapping the `next()` call with the safer [`AsyncLocalStorage#run`](https://nodejs.org/api/async_context.html#async_context_asynclocalstorage_run_store_callback_args). Most of the time this should not be necessary, but [some frameworks](../05_considerations/02_compatibility.md#graphql) have been known to lose the context with `run`.
98
+
99
+
The `Cls{Guard,Interceptor}Options` additionally takes the following parameters:
0 commit comments