tsoa icon indicating copy to clipboard operation
tsoa copied to clipboard

Allow injection of the response parameter into controllers

Open Cu3PO42 opened this issue 3 years ago • 7 comments

Sorting

  • I'm submitting a ...

    • [ ] bug report
    • [x] feature request
    • [ ] support request
  • I confirm that I

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.

Cu3PO42 avatar Jun 16 '21 07:06 Cu3PO42

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.

geoffoliver avatar Jul 10 '21 20:07 geoffoliver

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;
    }
}

echentw avatar Oct 08 '21 08:10 echentw

Facing the same issue here - any further insight on this, please? @echentw this didn't work for me :(

irstavr avatar Nov 23 '21 19:11 irstavr

any news on this ?

SalhiYassine avatar Aug 20 '22 11:08 SalhiYassine

Hi Team, any update on this?

Mayank-jain34 avatar Oct 27 '22 16:10 Mayank-jain34

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.

fomin-alexander avatar Feb 11 '23 12:02 fomin-alexander

Yes, i also need @Response. Using req.res or req.req.res, it work for me but it seem not clear.

lutuantai95 avatar Mar 18 '23 04:03 lutuantai95