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],
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