Skip to content

Commit

Permalink
Merge pull request #13027 from dammy001/chore/allow-passing-function-…
Browse files Browse the repository at this point in the history
…in-header

chore(core): allow dynamic value for header value
  • Loading branch information
kamilmysliwiec committed Jan 23, 2024
2 parents 7d06225 + 6adfccc commit 783cf5c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/common/decorators/http/header.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { extendArrayMetadata } from '../../utils/extend-metadata.util';
*
* For example:
* `@Header('Cache-Control', 'none')`
* `@Header('Cache-Control', () => 'none')`
*
* @param name string to be used for header name
* @param value string to be used for header value
Expand All @@ -14,7 +15,7 @@ import { extendArrayMetadata } from '../../utils/extend-metadata.util';
*
* @publicApi
*/
export function Header(name: string, value: string): MethodDecorator {
export function Header(name: string, value: string | (() => string)): MethodDecorator {
return (
target: object,
key: string | symbol,
Expand Down
8 changes: 6 additions & 2 deletions packages/core/router/router-response-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {

export interface CustomHeader {
name: string;
value: string;
value: string | (() => string);
}

export interface RedirectResponse {
Expand Down Expand Up @@ -84,7 +84,11 @@ export class RouterResponseController {
headers: CustomHeader[],
) {
headers.forEach(({ name, value }) =>
this.applicationRef.setHeader(response, name, value),
this.applicationRef.setHeader(
response,
name,
typeof value === 'function' ? value() : value
),
);
}

Expand Down

0 comments on commit 783cf5c

Please sign in to comment.