Skip to content

Commit

Permalink
Merge pull request #1131 from naorpeled/fix/http-json-body-parser/res…
Browse files Browse the repository at this point in the history
…olve-type-issues

fix(middlewares/http-json-body-parser): narrow body type to string
  • Loading branch information
willfarrell authored Nov 11, 2023
2 parents 7effb08 + 1c4f5a2 commit 2136d53
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 15 deletions.
12 changes: 2 additions & 10 deletions packages/http-json-body-parser/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
import middy from '@middy/core'
import { APIGatewayEvent, APIGatewayProxyEventV2 } from 'aws-lambda'
import { JsonValue } from 'type-fest'

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

type VersionedApiGatewayEvent = APIGatewayEvent | APIGatewayProxyEventV2
export 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<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
72 changes: 67 additions & 5 deletions packages/http-json-body-parser/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,82 @@
import { APIGatewayEvent, APIGatewayProxyEventV2 } from 'aws-lambda'
import middy from '@middy/core'
import { expectType } from 'tsd'
import jsonBodyParser, { Event } from '.'
import jsonBodyParser from '.'

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

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

const baseEvent: Omit<APIGatewayEvent, 'body'> = {
headers: {},
isBase64Encoded: false,
httpMethod: 'GET',
path: '/',
multiValueHeaders: {},
pathParameters: null,
queryStringParameters: null,
multiValueQueryStringParameters: null,
stageVariables: null,
requestContext: {
accountId: '',
apiId: '',
authorizer: null,
protocol: '',
httpMethod: '',
path: '',
stage: '',
requestId: '',
requestTimeEpoch: 0,
resourceId: '',
resourcePath: '',
identity: {
accessKey: null,
accountId: null,
apiKey: null,
apiKeyId: null,
caller: null,
clientCert: null,
cognitoAuthenticationProvider: null,
cognitoAuthenticationType: null,
cognitoIdentityId: null,
cognitoIdentityPoolId: null,
principalOrgId: null,
sourceIp: '',
user: null,
userAgent: null,
userArn: null
}
},
resource: ''
}

// allow body to only be string or null
middleware = jsonBodyParser()
const middifiedHandler = middy(() => {}).use(middleware)

expectType<Promise<void>>(middifiedHandler({
...baseEvent,
body: 'string'
}, {} as any))
expectType<Promise<void>>(middifiedHandler({
...baseEvent,
body: null
}, {} as any))

middifiedHandler({
...baseEvent,
// @ts-expect-error
body: {}
}, {} 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 2136d53

Please sign in to comment.