Pass action (request and response) when resolving middleware from container#1034
Pass action (request and response) when resolving middleware from container#1034alenkralj wants to merge 2 commits intotypestack:developfrom
Conversation
tomislavherman
left a comment
There was a problem hiding this comment.
Seems very simple fix with a big gain. Hope it will be merged soon since I'm using ugly hack in my project to overcome this.
|
|
||
| // if its an error handler then register it with proper signature in express | ||
| if ((middleware.instance as ExpressErrorMiddlewareInterface).error) { | ||
| if (middleware.target.prototype && middleware.target.prototype.error) { |
There was a problem hiding this comment.
You can simplify expression as if (middleware.target?.prototype?.error). Same for others here.
|
@alenkralj @tomislavherman I haven't had a use case personally so far to use a request-scoped container. What is a good example use case 🤔 |
|
I'll be honest, this looks like a memory leak :). I believe that passing the request and response when resolving is probably a mistake that was not removed previously but If we find a use case then we can implement this. |
Description
With in mind that resolving instances is done via:
When resolving controllers and interceptors
actionparameter is passed, while for middlewares it is not. Perhaps it should be consistent.My goal is to have a HTTP request scoped containers. Meaning, if I want to have an injectable class e.g.
RequestContextwhich contains information about the request that other services can consume I must have only one instance per HTTP request for such class. I could achieve this by opening child container for each HTTP request and bind it as a constant. The problem is where to keep child container. My current thinking is it should be part of therequest, like:Which works fine for e.g. controllers, but not for middlewares which have injected dependencies through a constructor. As a workaround I can in middleware
usemethod resolve dependencies viarequest.iocContainer.Checklist
Update index.md)develop)npm run prettier:checkpassesnpm run lint:checkpasses