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 1 commit
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,19 +1,11 @@
import middy from '@middy/core'
import { APIGatewayEvent } from 'aws-lambda'
import { JsonValue } from 'type-fest'
import { APIGatewayEvent, APIGatewayProxyEventV2 } from 'aws-lambda'

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

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

declare function jsonBodyParser (options?: Options): middy.MiddlewareObj<Event>
declare function jsonBodyParser (options?: Options): middy.MiddlewareObj<APIGatewayEvent | APIGatewayProxyEventV2>

export default jsonBodyParser
71 changes: 67 additions & 4 deletions packages/http-json-body-parser/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,76 @@
import middy from '@middy/core'
import { expectType } from 'tsd'
import jsonBodyParser, { Event } from '.'
import { expectType, expectError } from 'tsd'
Fixed Show fixed Hide fixed
import jsonBodyParser from '.'
import { APIGatewayProxyEvent, APIGatewayProxyEventV2 } from 'aws-lambda'

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

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

const baseEvent: Omit<APIGatewayProxyEvent, 'body'> = {
headers: {},
multiValueHeaders: {},
httpMethod: 'GET',
isBase64Encoded: false,
path: '/',
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)
Loading