Papooch / nestjs-cls

A continuation-local storage (async context) module compatible with NestJS's dependency injection.
https://papooch.github.io/nestjs-cls/
MIT License
453 stars 29 forks source link

Add proper support for injecting proxy providers into other proxy providers #169

Open Papooch opened 3 months ago

Papooch commented 3 months ago

Currently, only the built-in CLS_REQ and CLS_RES proxy providers are able to be injected in other proxy providers.

There is only a partial support for injecting custom proxy providers with some manual work by calling await cls.resolveProxyProviders(TheOtherProvider) in proxy provider factories when they depend on other proxy providers and their value is needed at the time of construction.

There is no support for this workflow in class proxy providers, because the constructor must be synchronous, but resolving proxy providers is an asynchronous operation.


Solution: when resolving proxy providers, scan their dependency array for other proxy providers and resolve those first. That should ensure that dependent proxy providers are always resolved when needed in other proxy providers.