#Access ExecutionContext from a custom validator (class-validator)

5 messages · Page 1 of 1 (latest)

gaunt sorrel
#

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?

#

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

GitHub

Hi, How to access current http context inside component method? As an example i would like to access current session user inside "typeorm" EventSubscriber class to automatically s...

Symfony 2.4 adds a new request stack service that is going to replace the request service

cobalt cargo
#

I would like to inject the executionContext
Not possible. There's no injection token for it. And pipes don't have access to the ExecutionContext anyways.

#

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