From eeda959130dbb8febe33a6060f34a213759675db Mon Sep 17 00:00:00 2001 From: Naor Peled Date: Thu, 16 May 2024 01:28:54 +0300 Subject: [PATCH] fix(packages/http-router): refine return type to include event and response --- packages/http-router/index.d.ts | 12 +++++++----- packages/http-router/index.test-d.ts | 22 ++++++++++++++++++++-- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/packages/http-router/index.d.ts b/packages/http-router/index.d.ts index bfe685ab7..6ff043fad 100644 --- a/packages/http-router/index.d.ts +++ b/packages/http-router/index.d.ts @@ -11,9 +11,7 @@ import { export type Method = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'ANY' -type TResult = ALBResult | APIGatewayProxyResult | APIGatewayProxyResultV2 - -export interface Route { +export interface Route { method: Method path: string handler: LambdaHandler | MiddyfiedHandler @@ -23,7 +21,11 @@ declare function httpRouterHandler< TEvent extends | ALBEvent | APIGatewayProxyEvent - | APIGatewayProxyEventV2 = APIGatewayProxyEvent -> (routes: Array>): middy.MiddyfiedHandler + | APIGatewayProxyEventV2 = APIGatewayProxyEvent, + TResult extends + | ALBResult + | APIGatewayProxyResult + | APIGatewayProxyResultV2 = APIGatewayProxyResult +> (routes: Array>): middy.MiddyfiedHandler export default httpRouterHandler diff --git a/packages/http-router/index.test-d.ts b/packages/http-router/index.test-d.ts index eea386b3e..c26d12a11 100644 --- a/packages/http-router/index.test-d.ts +++ b/packages/http-router/index.test-d.ts @@ -2,6 +2,7 @@ import { expectType } from 'tsd' import middy from '@middy/core' import httpRouterHandler from '.' import { + ALBEvent, ALBResult, APIGatewayProxyEvent, APIGatewayProxyEventV2, APIGatewayProxyResult, @@ -23,7 +24,7 @@ const middleware = httpRouterHandler([ handler: lambdaHandler } ]) -expectType(middleware) +expectType>(middleware) const lambdaHandlerV2: LambdaHandler = async (event) => { return { @@ -39,4 +40,21 @@ const middlewareV2 = httpRouterHandler([ handler: lambdaHandlerV2 } ]) -expectType(middlewareV2) +expectType>(middlewareV2) + +const lambdaHandlerALB: LambdaHandler = async (event) => { + return { + statusCode: 200, + body: 'Hello world' + } +} + +const middlewareALB = httpRouterHandler([ + { + method: 'GET', + path: '/', + handler: lambdaHandlerALB + } +]) + +expectType>(middlewareALB)