Skip to content

Commit

Permalink
chore: rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
naorpeled committed Nov 11, 2023
1 parent a996c81 commit d1dd479
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
4 changes: 2 additions & 2 deletions packages/http-json-body-parser/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ interface Options {
disableContentTypeError?: boolean
}

type VersionedApiGatewayEvent = APIGatewayEvent | APIGatewayProxyEventV2
export type VersionedApiGatewayEvent = APIGatewayEvent | APIGatewayProxyEventV2

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

export default jsonBodyParser
20 changes: 9 additions & 11 deletions packages/http-json-body-parser/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
import { APIGatewayEvent, APIGatewayProxyEventV2 } from 'aws-lambda'
import middy from '@middy/core'
import { expectType, expectError } from 'tsd'
import { expectType } from 'tsd'
import jsonBodyParser from '.'
import { APIGatewayProxyEvent, APIGatewayProxyEventV2 } from 'aws-lambda'

// use with default options
let middleware = jsonBodyParser()
expectType<middy.MiddlewareObj<APIGatewayProxyEvent | APIGatewayProxyEventV2>>(middleware)
expectType<middy.MiddlewareObj<APIGatewayEvent | APIGatewayProxyEventV2>>(middleware)

// use with all options
middleware = jsonBodyParser({
reviver: (key: string, value: any) => Boolean(value)
})
expectType<middy.MiddlewareObj<APIGatewayProxyEvent | APIGatewayProxyEventV2>>(middleware)
expectType<middy.MiddlewareObj<APIGatewayEvent | APIGatewayProxyEventV2>>(middleware)

const baseEvent: Omit<APIGatewayProxyEvent, 'body'> = {
const baseEvent: Omit<APIGatewayEvent, 'body'> = {
headers: {},
multiValueHeaders: {},
httpMethod: 'GET',
isBase64Encoded: false,
httpMethod: 'GET',
path: '/',
multiValueHeaders: {},
pathParameters: null,
queryStringParameters: null,
multiValueQueryStringParameters: null,
Expand Down Expand Up @@ -74,11 +73,10 @@ middifiedHandler({
...baseEvent,
// @ts-expect-error
body: {}
}, {} as any)
expectType<middy.MiddlewareObj<Event>>(middleware)
}, {} as any).then(() => {}).catch(() => {})

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

0 comments on commit d1dd479

Please sign in to comment.