question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow injection of the response parameter into controllers

See original GitHub issue

Sorting

  • I’m submitting a …

    • bug report
    • 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.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:5

github_iconTop GitHub Comments

1reaction
echentwcommented, Oct 8, 2021

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;
    }
}
1reaction
geoffolivercommented, Jul 10, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Routing to controller actions in ASP.NET Core - Microsoft Learn
ASP.NET Core controllers use the Routing middleware to match the URLs of incoming requests and map them to actions. Route templates:.
Read more >
Dependency injection : Get Param in controller - Stack Overflow
For parameters, is $this->getParameter() is an anti pattern ? If yes, how can we get them in controller and of course we must...
Read more >
How to pass parameters to action methods in ASP.NET Core ...
Pass parameters via the URL in ASP.NET Core MVC​​ One of the simplest and easiest ways to pass parameters to an action method...
Read more >
Controllers | NestJS - A progressive Node.js framework
We can use the library-specific (e.g., Express) response object, which can be injected using the @Res() decorator in the method handler signature (e.g.,...
Read more >
Routing - Laravel - The PHP Framework For Web Artisans
Route parameters are injected into route callbacks / controllers based on their order - the names of the route callback / controller arguments...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found