Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(middlewares/http-json-body-parser): narrow body type to string #1131

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading
Loading