Skip to content

Commit

Permalink
Merge pull request #1212 from naorpeled/fix/http-router/resolve-too-w…
Browse files Browse the repository at this point in the history
…ide-return-type

fix(packages/http-router): refine return type to include event and response
  • Loading branch information
willfarrell committed May 16, 2024
2 parents 37632cf + eeda959 commit 2a9abae
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
12 changes: 7 additions & 5 deletions packages/http-router/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import {

export type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'ANY'

type TResult = ALBResult | APIGatewayProxyResult | APIGatewayProxyResultV2

export interface Route<TEvent> {
export interface Route<TEvent, TResult> {
method: Method
path: string
handler: LambdaHandler<TEvent, TResult> | MiddyfiedHandler<TEvent, TResult, any, any>
Expand All @@ -23,7 +21,11 @@ declare function httpRouterHandler<
TEvent extends
| ALBEvent
| APIGatewayProxyEvent
| APIGatewayProxyEventV2 = APIGatewayProxyEvent
> (routes: Array<Route<TEvent>>): middy.MiddyfiedHandler
| APIGatewayProxyEventV2 = APIGatewayProxyEvent,
TResult extends
| ALBResult
| APIGatewayProxyResult
| APIGatewayProxyResultV2 = APIGatewayProxyResult
> (routes: Array<Route<TEvent, TResult>>): middy.MiddyfiedHandler<TEvent, TResult>

export default httpRouterHandler
22 changes: 20 additions & 2 deletions packages/http-router/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expectType } from 'tsd'
import middy from '@middy/core'
import httpRouterHandler from '.'
import {
ALBEvent, ALBResult,
APIGatewayProxyEvent,
APIGatewayProxyEventV2,
APIGatewayProxyResult,
Expand All @@ -23,7 +24,7 @@ const middleware = httpRouterHandler([
handler: lambdaHandler
}
])
expectType<middy.MiddyfiedHandler>(middleware)
expectType<middy.MiddyfiedHandler<APIGatewayProxyEvent, APIGatewayProxyResult>>(middleware)

const lambdaHandlerV2: LambdaHandler<APIGatewayProxyEventV2, APIGatewayProxyResultV2> = async (event) => {
return {
Expand All @@ -39,4 +40,21 @@ const middlewareV2 = httpRouterHandler([
handler: lambdaHandlerV2
}
])
expectType<middy.MiddyfiedHandler>(middlewareV2)
expectType<middy.MiddyfiedHandler<APIGatewayProxyEventV2, APIGatewayProxyResultV2>>(middlewareV2)

const lambdaHandlerALB: LambdaHandler<ALBEvent, ALBResult> = async (event) => {
return {
statusCode: 200,
body: 'Hello world'
}
}

const middlewareALB = httpRouterHandler([
{
method: 'GET',
path: '/',
handler: lambdaHandlerALB
}
])

expectType<middy.MiddyfiedHandler<ALBEvent, ALBResult>>(middlewareALB)

0 comments on commit 2a9abae

Please sign in to comment.