tsoa
tsoa copied to clipboard
Allow injection of the response parameter into controllers
Sorting
-
I'm submitting a ...
- [ ] bug report
- [x] feature request
- [ ] support request
-
I confirm that I
- [x] 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.
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.
As a workaround, you can access the Response object from the request object via req.res
.
Example:
import { Route, Get, Tags, Controller, Request } from 'tsoa'
@Route('/')
export class IndexController extends Controller {
@Get('/')
@Tags('Index')
public index(@Request() req: Express.Request): any {
const response = req.res;
}
}
Facing the same issue here - any further insight on this, please? @echentw this didn't work for me :(
any news on this ?
Hi Team, any update on this?
I have similar question. Usually request handler can access request
and response
objects. Is there a reason why tsoa provides only @Request()
decorator, but no @Response
? Or something like context
in koa.
Yes, i also need @Response
. Using req.res
or req.req.res
, it work for me but it seem not clear.