Skip to content

Commit

Permalink
feat: Add res to middleware setup (#61)
Browse files Browse the repository at this point in the history
* added res to middleware setup

* updated README
  • Loading branch information
moofoo authored Mar 23, 2023
1 parent 8e39ff7 commit ac6b72a
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,13 +307,13 @@ class MyService {

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

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

```ts
ClsModule.forRoot({
middleware: {
mount: true,
setup: (cls, req: Request) => {
setup: (cls, req: Request, res: Response) => {
// put some additional default info in the CLS
cls.set('TENANT_ID', req.params('tenant_id'));
cls.set('AUTH', { authenticated: false });
Expand Down
2 changes: 1 addition & 1 deletion src/lib/cls-initializers/cls.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class ClsMiddleware implements NestMiddleware {
if (this.options.saveReq) cls.set<any>(CLS_REQ, req);
if (this.options.saveRes) cls.set<any>(CLS_RES, res);
if (this.options.setup) {
await this.options.setup(cls, req);
await this.options.setup(cls, req, res);
}
if (this.options.resolveProxyProviders)
await cls.resolveProxyProviders();
Expand Down
2 changes: 1 addition & 1 deletion src/lib/cls.options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class ClsMiddlewareOptions {
* 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: any) => void | Promise<void>;
setup?: (cls: ClsService, req: any, res: any) => void | Promise<void>;

/**
* Whether to resolve proxy providers as a part
Expand Down

0 comments on commit ac6b72a

Please sign in to comment.