nestjs-cls
Advanced tools
Comparing version 3.0.2 to 3.0.3
{ | ||
"name": "nestjs-cls", | ||
"version": "3.0.2", | ||
"version": "3.0.3", | ||
"description": "A continuation-local storage module compatible with NestJS's dependency injection.", | ||
@@ -5,0 +5,0 @@ "author": "papooch", |
@@ -625,14 +625,21 @@ # NestJS CLS (Async Context) | ||
Set a value on the CLS context. | ||
- **_`get`_**`(key?: string): any` | ||
Retrieve a value from the CLS context by key. Get the whole store if key is omitted. | ||
- **_`has`_**`(key: string): boolean` | ||
Check if a key is in the CLS context. | ||
- **_`getId`_**`(): string;` | ||
Retrieve the request ID (a shorthand for `cls.get(CLS_ID)`) | ||
- **_`enter`_**`(): void;` | ||
Run any following code in a shared CLS context. | ||
- **_`enterWith`_**`(store: any): void;` | ||
Run any following code in a shared CLS context (while supplying the default contents). | ||
- **_`run`_**`(callback: () => T): T;` | ||
Run the callback in a shared CLS context. | ||
- **_`runWith`_**`(store: any, callback: () => T): T;` | ||
@@ -647,15 +654,21 @@ Run the callback in a shared CLS context (while supplying the default contents). | ||
The `ClsModule.forRoot()` method takes the following `ClsModuleOptions`: | ||
### Root | ||
- **_`middleware:`_ `ClsMiddlewareOptions`** | ||
An object with additional options for the `ClsMiddleware`, see below | ||
- **_`guard:`_ `ClsGuardOptions`** | ||
An object with additional options for the `ClsGuard`, see below | ||
- **_`interceptor:`_ `ClsInterceptorOptions`** | ||
An object with additional options for the `ClsInterceptor`, see below | ||
- **_`global:`_ `boolean`** (default _`false`_) | ||
The `ClsModule.forRoot()` method takes the following **`ClsModuleOptions`**: | ||
- **_`middleware?:`_ `ClsMiddlewareOptions`** | ||
An object with additional options for the `ClsMiddleware`, see [below](#enhancer-options). | ||
- **_`guard?:`_ `ClsGuardOptions`** | ||
An object with additional options for the `ClsGuard`, see [below](#enhancer-options). | ||
- **_`interceptor?:`_ `ClsInterceptorOptions`** | ||
An object with additional options for the `ClsInterceptor`, see [below](#enhancer-options). | ||
- **_`global?:`_ `boolean`** (default _`false`_) | ||
Whether to make the module global, so you do not have to import `ClsModule.forFeature()` in other modules. | ||
- **_`namespaceName`_: `string`** (default _unset_) | ||
The namespace that will be set up. When used, `ClsService` must be injected using the `@InjectCls('name')` decorator. (most of the time you will not need to touch this setting) | ||
- **_`proxyProviders?:`_ `Type[]`** | ||
Array of [Proxy Providers](#proxy-providers) that should be registered in the root module. Currently only accepts sync class Proxy providers, use `ClsModule.forFeatureAsync()` for more complex use-cases. | ||
> **Please note**: the `middleware`, `guard` and `interceptor` options should be _mutually exclusive_ - do not use more than one of them, otherwise the context will be overwritten by the one that runs after. | ||
@@ -665,20 +678,57 @@ | ||
All of the `Cls{Middleware,Guard,Interceptor}Options` take the following parameters (either in `ClsModuleOptions` or directly when instantiating them manually): | ||
### Feature | ||
- **_`mount`_: `boolean`** (default _`false`_) | ||
Whether to automatically mount the middleware/guard/interceptor to every route (not applicable when instantiating manually) | ||
- **_`generateId`_: `boolean`** (default _`false`_) | ||
Whether to automatically generate request IDs. | ||
- **_`idGenerator`_: `(req: Request | ExecutionContext) => string | Promise<string>`** | ||
The `ClsModule.forFeature()` method can be used to register a [Proxy Providers](#proxy-providers). The Sync method only accepts Class Proxy providers. | ||
The `ClsModule.forFeatureAsync()` method accepts either `ClsModuleProxyClassProviderOptions` or `ClsModuleProxyFactoryProviderOptions` that both accept these options: | ||
- **_`provide?:`_ `any`** | ||
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. | ||
- **_`imports?`_ `any[]`** | ||
Optional list of imported modules that export the providers which are required for the provider. | ||
- **_`extraProviders?:`_ `Provider[]`** | ||
Optional list of additional providers that should be available to the Proxy. Useful for passing configuration from a parent dynamic module. | ||
The `ClsModuleProxyClassProviderOptions` interface further accepts: | ||
- **_`useClass:`_ `Type`** | ||
The target class that will be used by this Proxy Provider. Make sure it is decorated with `@InjectableProxy`. | ||
The `ClsModuleProxyFactoryProviderOptions` interface further accepts: | ||
- **_`inject:`_ `any[]`** | ||
An array of injection tokens for providers used in the `useFactory`. | ||
- **_`useFactory:`_ `(...args: any[]) => any`** | ||
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. | ||
### Enhancer options | ||
All of the **`Cls{Middleware,Guard,Interceptor}Options`** take the following parameters (either in `ClsModuleOptions` or directly when instantiating them manually): | ||
- **_`mount?:`_ `boolean`** (default _`false`_) | ||
Whether to automatically mount the middleware/guard/interceptor to every route (not applicable when instantiating them manually) | ||
- **_`generateId?:`_ `boolean`** (default _`false`_) | ||
Whether to automatically generate a request ID. It will be available under the `CLS_ID` key. | ||
- **_`idGenerator?:`_ `(req: Request) => string | Promise<string>`** | ||
**_`idGenerator?:`_ `(ctx: ExecutionContext) => string | Promise<string>`** | ||
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. | ||
- **_`setup`_: `(cls: ClsService, req: Request) => void | Promise<void>;`** | ||
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, req: Request) => void | Promise<void>;`** | ||
**_`setup?:`_ `(cls: ClsService, ctx: ExecutionContext) => void | Promise<void>;`** | ||
Function that executes after the CLS context had been initialised. It can be used to put additional variables in the CLS context. | ||
The `ClsMiddlewareOptions` additionally takes the following parameters: | ||
- **_`saveReq`_: `boolean`** (default _`true`_) | ||
- **_`saveReq?:`_ `boolean`** (default _`true`_) | ||
Whether to store the _Request_ object to the context. It will be available under the `CLS_REQ` key. | ||
- **_`saveRes`_: `boolean`** (default _`false`_) | ||
- **_`saveRes?:`_ `boolean`** (default _`false`_) | ||
Whether to store the _Response_ object to the context. It will be available under the `CLS_RES` key | ||
- **_`useEnterWith`_: `boolean`** (default _`false`_) | ||
- **_`useEnterWith?:`_ `boolean`** (default _`false`_) | ||
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](#graphql) are known to lose the context with `run`. | ||
@@ -685,0 +735,0 @@ |
824
95769
53
1235