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

fix: allow returning Response objects from function Handlers #459

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ndhoule
Copy link

@ndhoule ndhoule commented Jan 18, 2024

Which problem is this pull request solving?

Per the functions documentation, it seems like you should be able to return a standard Response object from function handlers. However, the existing types only allows a user to return a HandlerResponse object.

List other issues or pull requests related to this problem

It looks like #351 also addresses this issue, but that PR is a little stale at this point.

Describe the solution you've chosen

This updates the Handler type to accept both HandlerResponse and Response as a return value.

Checklist

Please add a x inside each checkbox:

  • I have read the contribution guidelines.
  • The status checks are successful (continuous integration). Those can be seen below.

Per the [functions documentation][1], it seems like you should be able
to return a standard `Response` object from function handlers. However,
the existing types only allows a user to return a `HandlerResponse`
object.

This updates the Handler type to accept both types as a return value.

(It looks like netlify#351 also
addresses this issue, but that PR is a little stale at this point.)

[1]: https://docs.netlify.com/functions/get-started/?fn-language=ts#synchronous-function
@ndhoule ndhoule marked this pull request as ready for review January 19, 2024 00:59
@ndhoule ndhoule requested a review from a team as a code owner January 19, 2024 00:59
@Skn0tt
Copy link
Contributor

Skn0tt commented Jan 19, 2024

Hey Nathan, appreciate the PR! The Handler type is for Lambda Compatibility Mode, which doesn't support Response. So the change you're proposing won't work.

@@ -18,7 +18,7 @@ export interface BackgroundHandler<C extends HandlerContext = HandlerContext> {
(event: HandlerEvent, context: C): void | Promise<void>
}

export type Handler = BaseHandler<HandlerResponse, HandlerContext>
export type Handler = BaseHandler<HandlerResponse | Response, HandlerContext>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could add a docs comment like this one instead?

Suggested change
export type Handler = BaseHandler<HandlerResponse | Response, HandlerContext>
/* For Lambda Compatiblity Mode */
export type Handler = BaseHandler<HandlerResponse | Response, HandlerContext>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants