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
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.
Fix: 0534cbdbe2931efb58b0ee1d1780632bb3869b76
Most helpful comment
Fix: 0534cbdbe2931efb58b0ee1d1780632bb3869b76
example: https://github.com/lukeautry/tsoa/blob/524db88d7da8beadb8b717425531bd659d6a0110/tests/fixtures/controllers/testController.ts#L32