Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to Set header in Response #89

Closed
willSonic opened this issue Mar 31, 2017 · 2 comments
Closed

How to Set header in Response #89

willSonic opened this issue Mar 31, 2017 · 2 comments

Comments

@willSonic
Copy link

willSonic commented Mar 31, 2017

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

@nenadalm
Copy link
Collaborator

nenadalm commented Jul 8, 2017

Hi. This could be solved as part of #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 #94).

Feel free to contribute in discussion here: #130 for improving controller to support your use cases in simple way.

isman-usoh added a commit to isman-usoh/tsoa that referenced this issue Jul 20, 2017
- update route schema and update type
- change swagger enum inline type to enum definitions
- add new rule for tslint
- fix set status not work lukeautry#94
- support custom response header lukeautry#89
isman-usoh added a commit to isman-usoh/tsoa that referenced this issue Jul 21, 2017
- update route schema and update type
- change swagger enum inline type to enum definitions
- add new rule for tslint
- fix set status not work lukeautry#94
- support custom response header lukeautry#89
isman-usoh added a commit that referenced this issue Jul 27, 2017
- Support Query array type 
- Support custom response header (#89)
- Support tsoa config yaml format (#133)
- Support generate swagger yaml format
- Support string, number and boolan UnionType (#5)
- Support AnyKeyword (#114)
- update route schema and update type
- Change swagger enum inline type to enum definitions
- Add new rule for tslint
- Fix set status not work #94
- Fix validate 
- Set query array collectionFormat, default to multi
- Create file for Circle CI
isman-usoh added a commit that referenced this issue Jul 27, 2017
- Support Query array type
- Support custom response header (#89)
- Support tsoa config yaml format (#133)
- Support generate swagger yaml format
- Support string, number and boolan UnionType (#5)
- Support AnyKeyword (#114)
- update route schema and update type
- Change swagger enum inline type to enum definitions
- Add new rule for tslint
- Fix set status not work #94
- Fix validate
- Set query array collectionFormat, default to multi
- Create file for Circle CI
@isman-usoh
Copy link
Collaborator

Fix: 0534cbd

example:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants