medibloc / nestjs-request-context

NestJS Request Context using AsyncLocalStorage
MIT License
45 stars 4 forks source link

Context is undefined in singleton services #4

Open unlight opened 3 years ago

unlight commented 3 years ago

I'm doing how it is described in readme. However, it does not work for services which created on application start. Only for scoped services, like controllers, graphql resolvers.

Is it possible to get it worked for singleton services?

pylebecq commented 2 years ago

At least the typing needs to be fixed. RequestContext::get() method return type should be T | undefined as the docs says that getStore() can return undefined.

If called outside of an asynchronous context initialized by calling asyncLocalStorage.run() or asyncLocalStorage.enterWith(), it returns undefined.