From 21828af87296d67681b30f8f0fa359acc33fd985 Mon Sep 17 00:00:00 2001 From: Naor Peled <thehecticbyte@gmail.com> Date: Sun, 12 Nov 2023 00:01:14 +0200 Subject: [PATCH 1/6] fix(middlewares/http-json-body-parser): narrow body type to string --- packages/http-json-body-parser/index.d.ts | 12 +--- .../http-json-body-parser/index.test-d.ts | 71 +++++++++++++++++-- 2 files changed, 69 insertions(+), 14 deletions(-) diff --git a/packages/http-json-body-parser/index.d.ts b/packages/http-json-body-parser/index.d.ts index 1586a4f85..8165f5377 100644 --- a/packages/http-json-body-parser/index.d.ts +++ b/packages/http-json-body-parser/index.d.ts @@ -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 diff --git a/packages/http-json-body-parser/index.test-d.ts b/packages/http-json-body-parser/index.test-d.ts index c7bca7665..2f12bab8f 100644 --- a/packages/http-json-body-parser/index.test-d.ts +++ b/packages/http-json-body-parser/index.test-d.ts @@ -1,13 +1,76 @@ import middy from '@middy/core' -import { expectType } from 'tsd' -import jsonBodyParser, { Event } from '.' +import { expectType, expectError } from 'tsd' +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) From 53c0166d2c52062d4afdb77fde76253b6e92348b Mon Sep 17 00:00:00 2001 From: Naor Peled <thehecticbyte@gmail.com> Date: Sun, 12 Nov 2023 00:06:13 +0200 Subject: [PATCH 2/6] fix(style): lint --- packages/http-json-body-parser/index.test-d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/http-json-body-parser/index.test-d.ts b/packages/http-json-body-parser/index.test-d.ts index 2f12bab8f..00783495f 100644 --- a/packages/http-json-body-parser/index.test-d.ts +++ b/packages/http-json-body-parser/index.test-d.ts @@ -58,7 +58,7 @@ const baseEvent: Omit<APIGatewayProxyEvent, 'body'> = { // allow body to only be string or null middleware = jsonBodyParser() -const middifiedHandler = middy(()=>{}).use(middleware) +const middifiedHandler = middy(() => {}).use(middleware) expectType<Promise<void>>(middifiedHandler({ ...baseEvent, From d1dd479a37f37814d9f8692e107825364f2e3641 Mon Sep 17 00:00:00 2001 From: Naor Peled <thehecticbyte@gmail.com> Date: Sun, 12 Nov 2023 00:23:43 +0200 Subject: [PATCH 3/6] chore: rebase --- packages/http-json-body-parser/index.d.ts | 4 ++-- .../http-json-body-parser/index.test-d.ts | 20 +++++++++---------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/http-json-body-parser/index.d.ts b/packages/http-json-body-parser/index.d.ts index f954f4b4b..a7c1a3fe4 100644 --- a/packages/http-json-body-parser/index.d.ts +++ b/packages/http-json-body-parser/index.d.ts @@ -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 diff --git a/packages/http-json-body-parser/index.test-d.ts b/packages/http-json-body-parser/index.test-d.ts index b384c9dba..da2a9b420 100644 --- a/packages/http-json-body-parser/index.test-d.ts +++ b/packages/http-json-body-parser/index.test-d.ts @@ -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, @@ -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) From 612d8971bc71980a53dfd173e83913be43adc222 Mon Sep 17 00:00:00 2001 From: Naor Peled <thehecticbyte@gmail.com> Date: Sun, 12 Nov 2023 00:26:04 +0200 Subject: [PATCH 4/6] chore: rebase --- website/static/img/logo/amazon-web-services.svg | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/website/static/img/logo/amazon-web-services.svg b/website/static/img/logo/amazon-web-services.svg index f9058ed97..4715937ff 100644 --- a/website/static/img/logo/amazon-web-services.svg +++ b/website/static/img/logo/amazon-web-services.svg @@ -1,14 +1,10 @@ <?xml version="1.0" encoding="utf-8"?> <!-- Generator: Adobe Illustrator 19.0.1, SVG Export Plug-In . SVG Version: 6.00 Build 0) --> -<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" - xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" - viewBox="0 0 304 182" style="enable-background:new 0 0 304 182;" xml:space="preserve"> +<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" + viewBox="0 0 304 182" style="enable-background:new 0 0 304 182;" xml:space="preserve"> <style type="text/css"> .st0{fill:#252F3E;} .st1{fill-rule:evenodd;clip-rule:evenodd;fill:#FF9900;} - html[data-theme='dark'] .st0 { - fill: #fcfcfc; - } </style> <g> <path class="st0" d="M86.4,66.4c0,3.7,0.4,6.7,1.1,8.9c0.8,2.2,1.8,4.6,3.2,7.2c0.5,0.8,0.7,1.6,0.7,2.3c0,1-0.6,2-1.9,3l-6.3,4.2 From 721ef584c51397861186b3dd9f901d996a4d923c Mon Sep 17 00:00:00 2001 From: Naor Peled <thehecticbyte@gmail.com> Date: Sun, 12 Nov 2023 00:27:32 +0200 Subject: [PATCH 5/6] chore: rebase --- website/static/img/logo/amazon-web-services.svg | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/static/img/logo/amazon-web-services.svg b/website/static/img/logo/amazon-web-services.svg index 4715937ff..846a20e04 100644 --- a/website/static/img/logo/amazon-web-services.svg +++ b/website/static/img/logo/amazon-web-services.svg @@ -1,5 +1,5 @@ + <?xml version="1.0" encoding="utf-8"?> -<!-- Generator: Adobe Illustrator 19.0.1, SVG Export Plug-In . SVG Version: 6.00 Build 0) --> <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 304 182" style="enable-background:new 0 0 304 182;" xml:space="preserve"> <style type="text/css"> @@ -35,4 +35,4 @@ c3.6,4.5-1,35.4-18.6,50.2c-2.7,2.3-5.3,1.1-4.1-1.9C282.5,155.7,291.4,133.4,287.2,128.1z"/> </g> </g> -</svg> +</svg> From 1c4f5a2e6963115872cd07b146f05e922cdaedf1 Mon Sep 17 00:00:00 2001 From: Naor Peled <thehecticbyte@gmail.com> Date: Sun, 12 Nov 2023 00:30:57 +0200 Subject: [PATCH 6/6] chore: rebase --- website/docs/middlewares/third-party.md | 1 - 1 file changed, 1 deletion(-) diff --git a/website/docs/middlewares/third-party.md b/website/docs/middlewares/third-party.md index 95799f384..41df98a13 100644 --- a/website/docs/middlewares/third-party.md +++ b/website/docs/middlewares/third-party.md @@ -20,7 +20,6 @@ If your middleware is missing, feel free to [open a Pull Request](https://github - [middy-rds](https://www.npmjs.com/package/middy-rds): Creates RDS connection using `knex` or `pg` - [middy-recaptcha](https://www.npmjs.com/package/middy-recaptcha): reCAPTCHA validation middleware - [middy-sparks-joi](https://www.npmjs.com/package/middy-sparks-joi): Joi validator -- [@iress/middy-http-path-router](https://www.npmjs.com/package/@iress/middy-http-path-router): Routes AWS API Gateway events to handlers based on static and dynamic paths ## Version 2.x - 3.x