title: Nest JS: Using Node AsyncLocalStorage inside Nest Pipes
body: <p>I am planning to use the NestJS Pipe for storing some app specific information through AsyncLocalStorage of Node JS. (Node version is 18.x).</p>
  <p>I am planning to mimic the <a href="https://github.com/abonifacio/nestjs-request-context" rel="nofollow noreferrer">Nest JS Request Context</a> to store app-specific request context.</p>
  <p>For that I need to adhere to the <a href="https://nodejs.org/api/async_context.html#asynclocalstoragerunstore-callback-args" rel="nofollow noreferrer">AsyncStorage Run method</a>.</p>
  <p>Something like this:</p>
  <pre class="lang-js prettyprint-override"><code>
  import {ArgumentMetadata, Injectable, PipeTransform} from '@nestjs/common';
  import { Request, Response } from 'express';
  import { MyRequestContext } from './my-request-context.model';
  
  @Injectable()
  export class RequestContextPipe implements PipeTransform {
      constructor() {}  // any services can be injected here
  
      async transform(publicId: string, metadata: ArgumentMetadata) {
          // Here we will have logic which will get the various values we want to set in the RequestContext. Lets call that as appContext.
          // MyRequestContext.cls.run(new MyRequestContext(req, res, appContext), next);
  
      }
  }
  </code></pre>
  <pre class="lang-js prettyprint-override"><code>import { AsyncLocalStorage } from 'async_hooks';
  import { Request, Response } from 'express';
  
  export class MyRequestContext {
    static cls = new AsyncLocalStorage&lt;RequestContext&gt;();
  
    static get currentContext() {
      return this.cls.getStore();
    }
  
    constructor(public readonly req: Request, public readonly res: Response, public readonly appContext: AppData) {}
  }
  </code></pre>
  <p>But the problem I am facing:</p>
  <p>How to get the request, response and next inside a Nest JS pipe?</p>
  <p>I want to execute this line of code inside NestJS Pipe's <code>transform</code> method:</p>
  <pre class="lang-js prettyprint-override"><code>     MyRequestContext.cls.run(new MyRequestContext(req, res, appContext), next);
  </code></pre>
  <p>So I need the <code>request</code>, <code>response</code> and <code>next</code> inside the Pipe.</p>
  <p>How may I get it?</p>