Nest cannot resolve a middleware's dependencies when targeting es2017
When applying a middleware to a module class (module A) which implements NestModule, where the middleware depends on a service within its own module (module B), nest cannot resolve the dependencies of the middleware, even if module B exports the dependency, and module A imports module B
targeting es6 resolved the issue
-- Module A
Module({
imports: [
EnvironmentModule,
// api modules
AuthModule,
StoreModule,
UserModule,
OrderModule,
// core api module
CoreApiModule,
// storage
StorageModule,
],
controllers: [
AppController,
],
providers: [
AppService,
],
})
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer
.apply(ClientAccessMiddleware)
.forRoutes('auth/*');
}
}
-- Middleware
@Injectable()
export class ClientAccessMiddleware implements NestMiddleware {
constructor(private readonly authService: AuthService) { }
async use(req: Request, res: Response, next: Function) {
const id = req.headers.get('client-id');
const secret = req.headers.get('client-secret');
const clientWithScopes = await this.authService.validateClient(id, secret);
let clientAuth = {};
if (clientWithScopes) {
clientAuth = {
apiClientScopes: clientWithScopes.scopes,
clientId: clientWithScopes.clientId,
};
}
(req as RequestWithClientAuth).clientAuth = clientAuth as any;
next();
}
}
-- Module B
@Module({
imports: [
EnvironmentModule,
CoreApiModule,
StorageModule,
],
controllers: [
AuthController,
],
providers: [
AuthService,
],
exports: [
AuthService,
],
})
export class AuthModule {
}
-- File structure
src/
app/
app.module/
app.module.ts <- middleware applied here, auth.module imported here
auth.module/
auth.module.ts <- middleware dependency exported here
middleware/
api-client-middleware.ts
-- tsconfig
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es6",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"paths": {
"src/": [
"src/"
]
},
"incremental": true
},
"exclude": [
"node_modules",
"dist",
".scaffs"
]
}
Expect that nest DI is able to resolve middleware's dependencies
Nest version:
"@nestjs/common": "^6.10.14",
"@nestjs/core": "^6.10.14",
"@nestjs/platform-express": "^6.10.14",
"@nestjs/platform-fastify": "^6.11.8",
For Tooling issues:
- Node version: 12.14.1
- Platform: macOS catalina
Others:
Where is ClientAccessMiddleware
added to the providers
array? Do you have a minimum reproduction of this?
Where is ClientAccessMiddleware added to the providers array?
Middleware doesn't have to be added to the providers
array :)
Please, provide a minimal repository which reproduces your issue.
@kamilmysliwiec
In your reproduction, you don't have AuthModule
, which exports AuthService
, imported into the AppModule
, so the dependencies of the middleware class could not be resolved.
-Oops i did not commit that, will fix. But I am confident issue will persist. -
Welllll shoot. I played myself. Whats scary is that in my actual code...it is imported/exported :(
...must be another dragon. Thanks for taking a look. @jmcdo29
Going to revise my repro to bring it closer to my actual module structure
nest is gr8
@jmcdo29 Is there a cli command to output a nest dependency graph ?
Sadly, no. I'm working on a custom module that does it, but the application needs to compile before hand for it to work 馃檨. You can take a look if you want
@jmcdo29 just took a look, thats pretty helpful, despite the caveat. Tried to find you on discord but your tag tweren't there. Know this isn't the appropriate channel (or repo), but i love what you're doing. I've been running nest in production for about 2 years, and have been looking for exactly what your tool does. Some day...when you stabilize it would be interesting to output an svg representation of those dependency graphs. Although, i suppose...i could do that myself, given that I could use the module tokens as graph node identifiers.
@sloan-dog interesting. Well there is the Nest Discord where I'm incredibly active. You could always find me from there. Just put a message in the #introductions channel, that way we can take the discussion away from here.
Great module @jmcdo29, can't wait to see the final version!
@sloan-dog Let's move this discussion to our Discord instead. We are using GitHub to track bugs, feature requests, and potential improvements.
Ran into the same issue, how did you make injections working in your middleware?