New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
A Guard with dependencies can't be injected into another module. #3856
Comments
Guards are not providers and you shouldn't register them as Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements. |
Ok, then this is very misleading eh? I could have missed a note or something but nowhere do I see any indication that a guard would not be treated like any other provider and be part of the DI pattern. Why wouldn't I want to take advantage of DI and modularization with guards that will likely be dependent on reusable providers?
Sorry, I really like nestjs and the type of architecture it enables for typescript projects. I have been using it for 3 years and I had no idea there was a discord channel. I have never opened an issue before and I was certain that this was a bug. If this isn't a bug then it's at least a request for better documentation. Also, as a feature request I think it makes perfect sense. |
In case anyone comes across this. Here is some info from the discord channel and how I decided to handle it:
|
@kamilmysliwiec Are you able to please explain why? A Guard, Interceptor, Filter, etc are beans right? They're instantiated and managed by the DI container. Why are these beans special? Why can't I pass them between modules? (I had this issue with an Interceptor) I think there's really good use cases for making these beans regular providers, not pseudo providers (to borrow from @jmcdo29)
@greenreign I had exactly the same thoughts/problems. |
Please, don't use Java Spring nomenclature. We don't have "beans".
Yes and no. Enhancers (interceptors/guards/pipes/filters) can be either resolved by the scope and managed by the DI container or instantiated manually (with
Enhancers can't be injected to other providers. Hence, they are not providers. Likewise, enhancers can be tied & injected to the method evaluation context, but you can't bind providers this way. There's no reason to change this, especially if you start thinking about enhancers scoped per host (module in which they exist). Adding them to the Please, use our Discord channel (support) for such questions. We are using GitHub to track bugs, feature requests, and potential improvements. |
Bug Report
A Guard with dependencies can't be injected into another module.
Current behavior
I have 3 modules UserModule, AuthModule and ProtectedModule
ProtectedModule
importsAuthModule
.AuthModule
importsUserModule
UserModule
exportsUserService
.AuthModule
exportsAuthGuard
ProtectedService
usesAuthGuard
Nest is unable to create
AuthGuard
even thoughUserModule
is imported intoAuthModule
andUserService
is exported fromUserModule
.This means I'd have to inject dependencies of dependencies wherever used. This negates the entire point of Modules and DI.
I've probably just missed something?
Input Code
https://github.com/greenreign/nest-dep-injection-issue
Expected behavior
Modules with imported providers should not have to also import the modules that those providers depend on when this is already defined in the provider's module.
Environment
The text was updated successfully, but these errors were encountered: