Tsoa: How to Set header in Response

Created on 31 Mar 2017  路  2Comments  路  Source: lukeautry/tsoa

Hi Everyone,

First I would thank Luke Autry and everyone who is contributing to this wonderful framework. It is exactly what I was looking for after trying so many different projects. Can anyone tell me the best way to go about accessing the Response Header in the Controller. I have an Authorization Controller, and I would like to place a token in the header on a successful response.

typically I would have done res.set(field, [value]) or res.header(field, [value])

So in side of controller method login, I am not sure where to even begin...

  @Post('Login')
  public async login(@Body() request: IUserLoginRequest): Promise<IUserResponse> {
      let result = await this.userService.getAuthorizedUser(request);
      ...
     //     var  authedUser = new UserModel(result);

    /*  the following returns access token in the body
         however I would like to return it as part of the header like
        response.set(  'access_token', 'value') */


    //    let loginResult = Object.assign({}, {
    //                                    user:authedUser.getClientUserModel(), 
    //                                          access_token:createAuthToken( authedUser.id) } );
    // return <IUserResponse>(loginResult);

      return  <IUserResponse>(result);

  }

Thanks In Advance for any help

help wanted

Most helpful comment

All 2 comments

Hi. This could be solved as part of https://github.com/lukeautry/tsoa/issues/130 by adding headers into response (or creating custom response and overwriting template generating handler code in case headers wouldn't be as part of the response by default).

As for current situation, your can make it work similarly to custom status by a few changes.
See setStatus https://github.com/lukeautry/tsoa#create-controllers which was added by extending tsoa Controller. So I suggest you to create your controller which will extend tsoa controller and add setHeaders method (I consider to be ugly, but that's the way it currently works...).

Then you have to update template for your library, so in case of express this is the place to set headers: https://github.com/lukeautry/tsoa/blob/master/src/routeGeneration/templates/express.ts#L91 (see https://github.com/lukeautry/tsoa#override-route-template on how to override template).

Also while you're at it, there is a bug which causes status to be read before promise resolves https://github.com/lukeautry/tsoa/blob/master/src/routeGeneration/templates/express.ts#L69 (see https://github.com/lukeautry/tsoa/issues/94).

Feel free to contribute in discussion here: https://github.com/lukeautry/tsoa/issues/130 for improving controller to support your use cases in simple way.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JimmyBjorklund picture JimmyBjorklund  路  5Comments

williamdes picture williamdes  路  5Comments

jakubzloczewski picture jakubzloczewski  路  6Comments

picturelink picture picturelink  路  5Comments

sundowndev picture sundowndev  路  4Comments