Skip to content

Commit 783cf5c

Browse files
authoredJan 23, 2024
Merge pull request #13027 from dammy001/chore/allow-passing-function-in-header
chore(core): allow dynamic value for header value
2 parents 7d06225 + 6adfccc commit 783cf5c

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed
 

‎packages/common/decorators/http/header.decorator.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { extendArrayMetadata } from '../../utils/extend-metadata.util';
66
*
77
* For example:
88
* `@Header('Cache-Control', 'none')`
9+
* `@Header('Cache-Control', () => 'none')`
910
*
1011
* @param name string to be used for header name
1112
* @param value string to be used for header value
@@ -14,7 +15,7 @@ import { extendArrayMetadata } from '../../utils/extend-metadata.util';
1415
*
1516
* @publicApi
1617
*/
17-
export function Header(name: string, value: string): MethodDecorator {
18+
export function Header(name: string, value: string | (() => string)): MethodDecorator {
1819
return (
1920
target: object,
2021
key: string | symbol,

‎packages/core/router/router-response-controller.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717

1818
export interface CustomHeader {
1919
name: string;
20-
value: string;
20+
value: string | (() => string);
2121
}
2222

2323
export interface RedirectResponse {
@@ -84,7 +84,11 @@ export class RouterResponseController {
8484
headers: CustomHeader[],
8585
) {
8686
headers.forEach(({ name, value }) =>
87-
this.applicationRef.setHeader(response, name, value),
87+
this.applicationRef.setHeader(
88+
response,
89+
name,
90+
typeof value === 'function' ? value() : value
91+
),
8892
);
8993
}
9094

0 commit comments

Comments
 (0)
Please sign in to comment.