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

chore(core): allow dynamic value for header value #13027

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading