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
Open
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
6 changes: 3 additions & 3 deletions src/function/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import type { HandlerContext } from './handler_context.js'
import type { HandlerEvent } from './handler_event.js'
import type { HandlerResponse, BuilderResponse, StreamingResponse } from './handler_response.js'

export interface HandlerCallback<ResponseType extends HandlerResponse = HandlerResponse> {
export interface HandlerCallback<ResponseType extends HandlerResponse | Response = HandlerResponse> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(error: any, response: ResponseType): void
}

export interface BaseHandler<
ResponseType extends HandlerResponse = HandlerResponse,
ResponseType extends HandlerResponse | Response = HandlerResponse,
C extends HandlerContext = HandlerContext,
> {
(event: HandlerEvent, context: C, callback?: HandlerCallback<ResponseType>): void | Promise<ResponseType>
Expand All @@ -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>

export type BuilderHandler = BaseHandler<BuilderResponse, HandlerContext>

export interface StreamingHandler {
Expand Down
8 changes: 7 additions & 1 deletion test/types/Handler.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expectError } from 'tsd'
import { expectAssignable, expectError } from 'tsd'

import { Handler } from '../../src/main.js'

Expand All @@ -9,3 +9,9 @@ expectError(() => {
// void
}
})

expectAssignable<Handler>(() => Promise.resolve({ statusCode: 200 }));

expectAssignable<Handler>(() => Promise.resolve(new Response("hello")));

expectError<Handler>(() => Promise.resolve({ missing: "status code" }));