I would like to inject the executionContext into my custom validator so that I can access the request headers. Apparently it's possible to inject request or context when using scope.REQUEST, but I want to stick to singleton (no request): https://docs.nestjs.com/fundamentals/injection-scopes#request-provider. What is a possible solution to keep singleton and access the ExecutionContext?
#Access ExecutionContext from a custom validator (class-validator)
5 messages · Page 1 of 1 (latest)
In this post: https://github.com/nestjs/nest/issues/173 there was an interesting discussion. Also an interesting question: @kamilmysliwiec what do you think about introducing RequestHolder service, which will return actual Request object? In this case we can use Request in SINGLETON-scoped services.
Symfony framework works in such way: https://symfony.com/blog/new-in-symfony-2-4-the-request-stack
I would like to inject the executionContext
Not possible. There's no injection token for it. And pipes don't have access to theExecutionContextanyways.
As for the RequestHolder, I don't know if I talked about that with Kamil here on Discord or over in GitHub, but basically because Node isn't multi-threaded we can't use a singleton to hold the request as it could be overwritten during a subsequent request and then point to the wrong request object.
You could try using nestjs-cls to use async_hooks and have a way to get the request that way, so it's no longer REQUEST scoped