Allow injection of the response parameter into controllers
See original GitHub issueSorting
-
I’m submitting a …
- bug report
- feature request
- support request
-
I confirm that I
- used the search
A similar, possibly the same, request has been made (and closed) as #376. However, I don’t think it was actually resolved.
Expected Behavior
Hi there!
First off, thanks for your work on this project! I’m using tsoa with Express. I have custom middleware performing some validation and data lookup on some URLs. The results are written into res.locals
. Therefore I need to access the response object from my controllers in some way.
Current Behavior
No decorator allows injection of the response
parameter.
Possible Solution
Introduce a new decorator, possibly named NodeResponse
, that injects the response variable as a parameter.
Context (Environment)
Version of the library: 3.8.0 Version of NodeJS: v14.17.0
- Confirm you were using yarn not npm: [x]
Detailed Description
Since the request can already be injected, adding the response doesn’t seem too far-fetched.
I checked this part of the Handlebars template and believe no way to inject the response object itself currently exists. I propose adding a new case and the appropriate decorator in the runtime.
For Koa, I believe the value context.response
could be injected. I am not familiar enough with HAPI to judge if there’s a reasonable equivalent or maybe using this decorator with HAPI should constitute an error.
If you are open to such a change, I am happy to provide a pull request. Thank you for your consideration.
Breaking change?
This should not be a breaking change.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:4
- Comments:5
Top GitHub Comments
As a workaround, you can access the Response object from the request object via
req.res
.Example:
The ability to inject the
NextFunction
from express would also be useful.Most of the examples on the express site are functions that accept 3 parameters - a request object, a response object, and a
next
middleware function. Without being able to inject the response object and the next function, I don’t see any other way of using express and tsoa together without also writing additional middlewares to pull stuff out of the request and send stuff back to the client.