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

Allow passing a function to the @Header decorator for dynamic values #13012

Closed
1 task done
mrVito opened this issue Jan 6, 2024 · 2 comments · Fixed by #13027
Closed
1 task done

Allow passing a function to the @Header decorator for dynamic values #13012

mrVito opened this issue Jan 6, 2024 · 2 comments · Fixed by #13027
Labels
needs triage This issue has not been looked into type: enhancement 🐺

Comments

@mrVito
Copy link

mrVito commented Jan 6, 2024

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

I'm trying to attach a 'Last-Modified' header on my API response and was initially trying to use this code:

@Header('Last-Modified', new Date().toUTCString())

Unfortunatelly this doesn't work as expected, as the value for the header is only evaluated once at startup and thus it is the same value on every request.

Describe the solution you'd like

Would be great if we could pass a function to the value argument of the @Header decorator that is evaluated on every request to the given endpoint so we can have dynamic values for the header.

Teachability, documentation, adoption, migration strategy

Use a function for the value argument on the @Header decorator to evaluate an expression on every request:

@Header('Last-Modified', () => new Date().toUTCString())

What is the motivation / use case for changing the behavior?

I have currently moved on to using an injected response object (using @Res decorator) and setting the header directly on it which is all fine, but it is not really nice to have additional native response handling code when only a simple header is needed and thus the nest.js way would be much more elegant.

@mrVito mrVito added needs triage This issue has not been looked into type: enhancement 🐺 labels Jan 6, 2024
@kamilmysliwiec
Copy link
Member

Would you like to create a PR for this issue?

Simply about this method to check if value is function and if so, execute it:

public setHeaders<TResponse = unknown>(
response: TResponse,
headers: CustomHeader[],
) {
headers.forEach(({ name, value }) =>
this.applicationRef.setHeader(response, name, value),
);
}

@kamilmysliwiec
Copy link
Member

Let's track this here #13027

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage This issue has not been looked into type: enhancement 🐺
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants