Skip to content

Commit

Permalink
Merge pull request #1100 from naorpeled/feat/json-body-parser/support…
Browse files Browse the repository at this point in the history
…-specifying-apigw-versioned-events

feat(json-body-parser): allow specifying versioned APIGW events
  • Loading branch information
willfarrell authored Sep 17, 2023
2 parents 7364d6a + 653c035 commit 2342b52
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/http-json-body-parser/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import middy from '@middy/core'
import { APIGatewayEvent } from 'aws-lambda'
import { APIGatewayEvent, APIGatewayProxyEventV2 } from 'aws-lambda'
import { JsonValue } from 'type-fest'

interface Options {
reviver?: (key: string, value: any) => any
disableContentTypeError?: boolean
}

export type Event = Omit<APIGatewayEvent, 'body'> & {
type VersionedApiGatewayEvent = APIGatewayEvent | APIGatewayProxyEventV2

export type Event<APIGatewayEventType extends VersionedApiGatewayEvent = VersionedApiGatewayEvent> = Omit<APIGatewayEventType, 'body'> & {
/**
* The body of the HTTP request.
*/
body: JsonValue
}

declare function jsonBodyParser (options?: Options): middy.MiddlewareObj<Event>
declare function jsonBodyParser<APIGatewayEventType extends VersionedApiGatewayEvent = VersionedApiGatewayEvent> (options?: Options): middy.MiddlewareObj<Event<APIGatewayEventType>>

export default jsonBodyParser
7 changes: 7 additions & 0 deletions packages/http-json-body-parser/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { APIGatewayEvent, APIGatewayProxyEventV2 } from 'aws-lambda'
import middy from '@middy/core'
import { expectType } from 'tsd'
import jsonBodyParser, { Event } from '.'
Expand All @@ -11,3 +12,9 @@ middleware = jsonBodyParser({
reviver: (key: string, value: any) => Boolean(value)
})
expectType<middy.MiddlewareObj<Event>>(middleware)

// allow specifying the event type
const apiGatewayV1Middleware = jsonBodyParser<APIGatewayEvent>()
expectType<middy.MiddlewareObj<Event<APIGatewayEvent>>>(apiGatewayV1Middleware)
const apiGatewayV2Middleware = jsonBodyParser<APIGatewayProxyEventV2>()
expectType<middy.MiddlewareObj<Event<APIGatewayProxyEventV2>>>(apiGatewayV2Middleware)

0 comments on commit 2342b52

Please sign in to comment.