diff --git a/packages/http-json-body-parser/index.d.ts b/packages/http-json-body-parser/index.d.ts index 8d7a53de5..a7c1a3fe4 100644 --- a/packages/http-json-body-parser/index.d.ts +++ b/packages/http-json-body-parser/index.d.ts @@ -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 = Omit & { - /** - * The body of the HTTP request. - */ - body: JsonValue -} - -declare function jsonBodyParser (options?: Options): middy.MiddlewareObj> +declare function jsonBodyParser (options?: Options): middy.MiddlewareObj export default jsonBodyParser diff --git a/packages/http-json-body-parser/index.test-d.ts b/packages/http-json-body-parser/index.test-d.ts index 121d7a772..da2a9b420 100644 --- a/packages/http-json-body-parser/index.test-d.ts +++ b/packages/http-json-body-parser/index.test-d.ts @@ -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>(middleware) +expectType>(middleware) // use with all options middleware = jsonBodyParser({ reviver: (key: string, value: any) => Boolean(value) }) -expectType>(middleware) +expectType>(middleware) + +const baseEvent: Omit = { + 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>(middifiedHandler({ + ...baseEvent, + body: 'string' +}, {} as any)) +expectType>(middifiedHandler({ + ...baseEvent, + body: null +}, {} as any)) + +middifiedHandler({ + ...baseEvent, + // @ts-expect-error + body: {} +}, {} as any).then(() => {}).catch(() => {}) // allow specifying the event type const apiGatewayV1Middleware = jsonBodyParser() -expectType>>(apiGatewayV1Middleware) +expectType>(apiGatewayV1Middleware) const apiGatewayV2Middleware = jsonBodyParser() -expectType>>(apiGatewayV2Middleware) +expectType>(apiGatewayV2Middleware)