Comments (2)
Edit: Seems I was too eager. The ModuleRef has no access even to services from @global() Modules.
I was searching hours for a good solution on configurable+DI supported middlewares.
Finally when I found this lib I also found an idea on how to implement it.
It's a pretty individual solution and some may find it a bit dirty.
But maybe it will inspire someone to do a better setup (passing required DI into createModule for example).
The issue:
Functional middleware is easily configurable but lacks DI.
Class-based middleware can use DI but configuration is quite messy to do.
I did a little modification to copied source code:
constructor(
@Inject(optionsToken)
private readonly options: SyncOptions<T> | null,
private moduleRef:ModuleRef // inject moduleRef
) {}
configure(consumer: MiddlewareConsumer) {
this.options.moduleRef = this.moduleRef; // assign moduleRef to options
//...
this way, all middlewares wrapped with createModule will receive a moduleRef within their options object.
It's a dirty trick - but still the cleanest solution that I could find to reduce duplicate code and setup complexity.
Now I can set up the middleware in a clean way:
ApiRateLimitModule.forRootAsync({
useFactory: async () => {
return {
id: 'AccesscodesController',
forRoutes: [MaintenanceLoginAsUserController]
}
}
}),
and within the middleware, I have access to global DI services by using options.moduleRef.get(ThatDarnServiceINeed)
from create-nestjs-middleware-module.
At the moment it's not possible. Could you describe how you see this feature? What API should be for it?
from create-nestjs-middleware-module.
Related Issues (4)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from create-nestjs-middleware-module.