When working with Nest, it has the capability to integrate with various HTTP frameworks (like Express by default). If you need to set a header value dynamically, based on run-time conditions, you'll have to utilize the API of your chosen HTTP framework. For instance, if utilizing Express, the code snippet would resemble this:
@Post('login')
async login(@Body() body: AuthDto, @Res() res: Response) {
const loginResponse = await this.authService.login(body);
res.header('x-access-token', loginResponse.access_token).json(loginResponse);
}
Nevertheless, in such scenarios, you might lose compatibility with some Nest features that rely on their standard response handling, like Interceptors and decorators such as @HttpCode() or @Header(). To overcome this limitation, you can enable the passthrough
option:
@Post('login')
async login(@Body() body: AuthDto, @Res({ passthrough: true }) res: Response) {
const loginResponse = await this.authService.login(body);
res.header('x-access-token', loginResponse.access_token);
return loginResponse;
}