augustusnaz / nestjs-request-context

Helper library for accesing request from singleton services

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

NestJS request-context

Workaround for getting Request information from a non request-scoped service in NestJs. Initially taken from this gist, and then changed to use AsyncLocalStorage

Usage

Full example in test folder

@Module({
  imports: [RequestContextModule], // automatically binds the proper middleware 
  providers: [...],
  controllers: [..],
})
export class AppModule {}

Accessing request from service

@Injectable()
export class SingletonService {

  getRequestId() {
    const req: Request = RequestContext.currentContext.req;
    return req.requestId;
  }

}

Release Notes

0.1.0

Updated peer dependencies to 8.x

1.0.0

Use async_hooks instead asyncctx as it's deprecated

2.0.1

Updated NestJs peer dependencies to 9.x

2.1.0

  • Removed express from peer dependencies
  • Added minimal generic support

About

Helper library for accesing request from singleton services

License:MIT License


Languages

Language:TypeScript 93.7%Language:JavaScript 6.3%