Skip to content

Commit

Permalink
build: fix build Papooch#4, update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Papooch committed Oct 13, 2021
1 parent 13190ec commit 5554ecd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ To make CLS work, it is required to set up the CLS context first. This is done b

## HTTP

Since in NestJS, HTTP **middleware** is the first thing to run when a request arrives, it is an ideal place to initialise the cls context. This package provides `ClsMidmidleware` that can be mounted to all (or selected) routes inside which the context is set up before the `next()`
Since in NestJS, HTTP **middleware** is the first thing to run when a request arrives, it is an ideal place to initialise the cls context. This package provides `ClsMidmidleware` that can be mounted to all (or selected) routes inside which the context is set up before the `next()` call.

All you have to do is mount it to routes in which you want to use CLS, or pass `middleware: { mount: true }` to the `ClsModule.register` options which automatically mounts it to all routes.

Expand Down Expand Up @@ -211,11 +211,11 @@ The `ClsModule.register()` method takes the following options:
- **_`namespaceName`_: `string`**
The name of the cls namespace. This is the namespace that will be used by the ClsService and ClsMiddleware (most of the time you will not need to touch this setting)
- **_`global:`_ `boolean`** (default _`false`_)
Whether to make the module global, so you do to import `ClsModule` in other modules.
Whether to make the module global, so you do not have to import `ClsModule.forFeature()` in other modules.
- **_`middleware:`_ `ClsMiddlewareOptions`**
An object with additional middleware options, see below
An object with additional ClsMiddleware options, see below
- **_`guard:`_ `ClsGuardOptions`**
An object with additional guard options, see below
An object with additional ClsGuard options, see below (do not use together with ClsMiddleware)

The `ClsMiddleware` takes the following options (either set up in `ClsModuleOptions` or directly when instantiating it manually):

Expand All @@ -236,6 +236,8 @@ The `ClsMiddleware` takes the following options (either set up in `ClsModuleOpti
- **_`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`.

The `ClsGuard` takes the following options:

- **`ClsGuardOptions`**

- **_`mount`_: `boolean`** (default _`false`_)
Expand All @@ -251,7 +253,7 @@ The `ClsMiddleware` takes the following options (either set up in `ClsModuleOpti

Because of a shared storage, CLS is an ideal tool for tracking request (correlation) IDs for the purpose of logging. This package provides an option to automatically generate request IDs in the middleware/guard, if you pass `{ generateId: true }` to the middleware/guard options. By default, the generated ID is a string based on `Math.random()`, but you can provide a custom function in the `idGenerator` option.

This function receives the `Request` (or `ExecutionContext` in case a `ClsGuard` is used) as the first parameter, which can be used in the generation process and should return a string id that will be stored in the CLS for later use.
This function receives the `Request` (or `ExecutionContext` in case a `ClsGuard` is used) as the first parameter, which can be used in the generation process and should return a string ID that will be stored in the CLS for later use.

Below is an example of retrieving the request ID from the request header with a fallback to an autogenerated one.

Expand Down Expand Up @@ -295,7 +297,7 @@ class MyService {

# Additional CLS Setup

The CLS middleware/guard provide some default functionality, but sometimes you might want to store more thing in the context by default. This can be of course done in a custom enhancer bound after, but for this scenario the `ClsMiddleware/ClsGuard` options expose the `setup` function, which will be executed in the middleware/guard after the CLS context is set up.
The CLS middleware/guard provide some default functionality, but sometimes you might want to store more things in the context by default. This can be of course done in a custom enhancer bound after, but for this scenario the `ClsMiddleware/ClsGuard` options expose the `setup` function, which will be executed in the middleware/guard after the CLS context is set up.

The function receives the `ClsService` and the `Request` (or `ExecutionContext`) object, and can be asynchronous.

Expand Down
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false,
"tsBuildInfoFile": "tmp/.tsbuildinfo"
},
"include": ["src/**/*.ts", "test/**/*.ts"]
}

0 comments on commit 5554ecd

Please sign in to comment.