From 9b9f9d110f8ee39611a038b9616924046d833478 Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Fri, 11 Oct 2024 11:40:53 +0000 Subject: [PATCH 1/3] improv(parser): export API Gateway v2 request schemas --- packages/parser/src/schemas/apigwv2.ts | 2 ++ packages/parser/src/schemas/index.ts | 2 ++ 2 files changed, 4 insertions(+) diff --git a/packages/parser/src/schemas/apigwv2.ts b/packages/parser/src/schemas/apigwv2.ts index 7d7031fc24..50319517cb 100644 --- a/packages/parser/src/schemas/apigwv2.ts +++ b/packages/parser/src/schemas/apigwv2.ts @@ -245,4 +245,6 @@ const APIGatewayRequestAuthorizerEventV2Schema = z.object({ export { APIGatewayProxyEventV2Schema, APIGatewayRequestAuthorizerEventV2Schema, + APIGatewayV2RequestAuthorizer, + APIGatewayV2RequestContext, }; diff --git a/packages/parser/src/schemas/index.ts b/packages/parser/src/schemas/index.ts index 6a087bf9e6..2d36eb9938 100644 --- a/packages/parser/src/schemas/index.ts +++ b/packages/parser/src/schemas/index.ts @@ -7,6 +7,8 @@ export { export { APIGatewayProxyEventV2Schema, APIGatewayRequestAuthorizerEventV2Schema, + APIGatewayV2RequestAuthorizer, + APIGatewayV2RequestContext, } from './apigwv2.js'; export { CloudFormationCustomResourceCreateSchema, From 319aae4d7921cf547b41b41aaa063b12c24c4e48 Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Wed, 30 Oct 2024 10:24:42 +0100 Subject: [PATCH 2/3] improv(parser): export API Gateway v2 request schemas --- packages/parser/src/schemas/apigwv2.ts | 14 +++++++------- packages/parser/src/schemas/index.ts | 4 ++-- packages/parser/src/types/schema.ts | 12 ++++++++++++ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/packages/parser/src/schemas/apigwv2.ts b/packages/parser/src/schemas/apigwv2.ts index 50319517cb..610c486ccd 100644 --- a/packages/parser/src/schemas/apigwv2.ts +++ b/packages/parser/src/schemas/apigwv2.ts @@ -38,7 +38,7 @@ import { * * @see {@link https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-access-control.html} */ -const APIGatewayV2RequestAuthorizer = z.object({ +const APIGatewayV2RequestAuthorizerSchema = z.object({ jwt: z .object({ claims: z.record(z.string(), z.any()), @@ -91,10 +91,10 @@ const APIGatewayV2RequestAuthorizer = z.object({ * } * ``` */ -const APIGatewayV2RequestContext = z.object({ +const APIGatewayV2RequestContextSchema = z.object({ accountId: z.string(), apiId: z.string(), - authorizer: APIGatewayV2RequestAuthorizer.optional(), + authorizer: APIGatewayV2RequestAuthorizerSchema.optional(), authentication: z .object({ clientCert: APIGatewayCert.optional(), @@ -171,7 +171,7 @@ const APIGatewayProxyEventV2Schema = z.object({ cookies: APIGatewayStringArray.optional(), headers: APIGatewayRecord, queryStringParameters: APIGatewayRecord.optional(), - requestContext: APIGatewayV2RequestContext, + requestContext: APIGatewayV2RequestContextSchema, body: z.string().optional(), pathParameters: APIGatewayRecord.nullish(), isBase64Encoded: z.boolean(), @@ -237,7 +237,7 @@ const APIGatewayRequestAuthorizerEventV2Schema = z.object({ cookies: APIGatewayStringArray.optional(), headers: APIGatewayRecord.optional(), queryStringParameters: APIGatewayRecord.optional(), - requestContext: APIGatewayV2RequestContext, + requestContext: APIGatewayV2RequestContextSchema, pathParameters: APIGatewayRecord.nullish(), stageVariables: APIGatewayRecord.nullish(), }); @@ -245,6 +245,6 @@ const APIGatewayRequestAuthorizerEventV2Schema = z.object({ export { APIGatewayProxyEventV2Schema, APIGatewayRequestAuthorizerEventV2Schema, - APIGatewayV2RequestAuthorizer, - APIGatewayV2RequestContext, + APIGatewayV2RequestAuthorizerSchema, + APIGatewayV2RequestContextSchema, }; diff --git a/packages/parser/src/schemas/index.ts b/packages/parser/src/schemas/index.ts index 2d36eb9938..8f052e6852 100644 --- a/packages/parser/src/schemas/index.ts +++ b/packages/parser/src/schemas/index.ts @@ -7,8 +7,8 @@ export { export { APIGatewayProxyEventV2Schema, APIGatewayRequestAuthorizerEventV2Schema, - APIGatewayV2RequestAuthorizer, - APIGatewayV2RequestContext, + APIGatewayV2RequestAuthorizerSchema, + APIGatewayV2RequestContextSchema, } from './apigwv2.js'; export { CloudFormationCustomResourceCreateSchema, diff --git a/packages/parser/src/types/schema.ts b/packages/parser/src/types/schema.ts index fc3b8c2614..3ec790514b 100644 --- a/packages/parser/src/types/schema.ts +++ b/packages/parser/src/types/schema.ts @@ -2,6 +2,8 @@ import type { z } from 'zod'; import type { APIGatewayProxyEventSchema, APIGatewayProxyEventV2Schema, + APIGatewayV2RequestAuthorizerSchema, + APIGatewayV2RequestContextSchema, AlbMultiValueHeadersSchema, AlbSchema, CloudFormationCustomResourceCreateSchema, @@ -43,6 +45,14 @@ type APIGatewayProxyEvent = z.infer; type APIGatewayProxyEventV2 = z.infer; +type APIGatewayV2RequestAuthorizer = z.infer< + typeof APIGatewayV2RequestAuthorizerSchema +>; + +type APIGatewayV2RequestContext = z.infer< + typeof APIGatewayV2RequestContextSchema +>; + type CloudFormationCustomResourceCreateEvent = z.infer< typeof CloudFormationCustomResourceCreateSchema >; @@ -114,6 +124,8 @@ export type { ALBMultiValueHeadersEvent, APIGatewayProxyEvent, APIGatewayProxyEventV2, + APIGatewayV2RequestAuthorizer, + APIGatewayV2RequestContext, CloudFormationCustomResourceCreateEvent, CloudFormationCustomResourceDeleteEvent, CloudFormationCustomResourceUpdateEvent, From 33afaecf416a25899cdd1bec482474319461960e Mon Sep 17 00:00:00 2001 From: Andrea Amorosi Date: Wed, 30 Oct 2024 10:45:31 +0100 Subject: [PATCH 3/3] improv(parser): export API Gateway v2 request schemas --- packages/parser/src/schemas/apigwv2.ts | 14 ++++---- packages/parser/src/schemas/index.ts | 4 +-- packages/parser/src/types/index.ts | 2 ++ packages/parser/src/types/schema.ts | 16 ++++----- .../parser/tests/unit/schema/apigwv2.test.ts | 35 +++++++++++++++++++ 5 files changed, 54 insertions(+), 17 deletions(-) diff --git a/packages/parser/src/schemas/apigwv2.ts b/packages/parser/src/schemas/apigwv2.ts index 610c486ccd..94193e2f80 100644 --- a/packages/parser/src/schemas/apigwv2.ts +++ b/packages/parser/src/schemas/apigwv2.ts @@ -38,7 +38,7 @@ import { * * @see {@link https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-access-control.html} */ -const APIGatewayV2RequestAuthorizerSchema = z.object({ +const APIGatewayRequestAuthorizerV2Schema = z.object({ jwt: z .object({ claims: z.record(z.string(), z.any()), @@ -91,10 +91,10 @@ const APIGatewayV2RequestAuthorizerSchema = z.object({ * } * ``` */ -const APIGatewayV2RequestContextSchema = z.object({ +const APIGatewayRequestContextV2Schema = z.object({ accountId: z.string(), apiId: z.string(), - authorizer: APIGatewayV2RequestAuthorizerSchema.optional(), + authorizer: APIGatewayRequestAuthorizerV2Schema.optional(), authentication: z .object({ clientCert: APIGatewayCert.optional(), @@ -171,7 +171,7 @@ const APIGatewayProxyEventV2Schema = z.object({ cookies: APIGatewayStringArray.optional(), headers: APIGatewayRecord, queryStringParameters: APIGatewayRecord.optional(), - requestContext: APIGatewayV2RequestContextSchema, + requestContext: APIGatewayRequestContextV2Schema, body: z.string().optional(), pathParameters: APIGatewayRecord.nullish(), isBase64Encoded: z.boolean(), @@ -237,7 +237,7 @@ const APIGatewayRequestAuthorizerEventV2Schema = z.object({ cookies: APIGatewayStringArray.optional(), headers: APIGatewayRecord.optional(), queryStringParameters: APIGatewayRecord.optional(), - requestContext: APIGatewayV2RequestContextSchema, + requestContext: APIGatewayRequestContextV2Schema, pathParameters: APIGatewayRecord.nullish(), stageVariables: APIGatewayRecord.nullish(), }); @@ -245,6 +245,6 @@ const APIGatewayRequestAuthorizerEventV2Schema = z.object({ export { APIGatewayProxyEventV2Schema, APIGatewayRequestAuthorizerEventV2Schema, - APIGatewayV2RequestAuthorizerSchema, - APIGatewayV2RequestContextSchema, + APIGatewayRequestAuthorizerV2Schema, + APIGatewayRequestContextV2Schema, }; diff --git a/packages/parser/src/schemas/index.ts b/packages/parser/src/schemas/index.ts index 8f052e6852..b50de2aac9 100644 --- a/packages/parser/src/schemas/index.ts +++ b/packages/parser/src/schemas/index.ts @@ -7,8 +7,8 @@ export { export { APIGatewayProxyEventV2Schema, APIGatewayRequestAuthorizerEventV2Schema, - APIGatewayV2RequestAuthorizerSchema, - APIGatewayV2RequestContextSchema, + APIGatewayRequestAuthorizerV2Schema, + APIGatewayRequestContextV2Schema, } from './apigwv2.js'; export { CloudFormationCustomResourceCreateSchema, diff --git a/packages/parser/src/types/index.ts b/packages/parser/src/types/index.ts index 93864c43ee..89dc0b8029 100644 --- a/packages/parser/src/types/index.ts +++ b/packages/parser/src/types/index.ts @@ -11,6 +11,8 @@ export type { APIGatewayProxyEvent, ALBMultiValueHeadersEvent, APIGatewayProxyEventV2, + APIGatewayRequestContextV2, + APIGatewayRequestAuthorizerV2, S3Event, S3EventNotificationEventBridge, S3SqsEventNotification, diff --git a/packages/parser/src/types/schema.ts b/packages/parser/src/types/schema.ts index 3ec790514b..7c1cf3032b 100644 --- a/packages/parser/src/types/schema.ts +++ b/packages/parser/src/types/schema.ts @@ -2,8 +2,8 @@ import type { z } from 'zod'; import type { APIGatewayProxyEventSchema, APIGatewayProxyEventV2Schema, - APIGatewayV2RequestAuthorizerSchema, - APIGatewayV2RequestContextSchema, + APIGatewayRequestAuthorizerV2Schema, + APIGatewayRequestContextV2Schema, AlbMultiValueHeadersSchema, AlbSchema, CloudFormationCustomResourceCreateSchema, @@ -45,12 +45,12 @@ type APIGatewayProxyEvent = z.infer; type APIGatewayProxyEventV2 = z.infer; -type APIGatewayV2RequestAuthorizer = z.infer< - typeof APIGatewayV2RequestAuthorizerSchema +type APIGatewayRequestAuthorizerV2 = z.infer< + typeof APIGatewayRequestAuthorizerV2Schema >; -type APIGatewayV2RequestContext = z.infer< - typeof APIGatewayV2RequestContextSchema +type APIGatewayRequestContextV2 = z.infer< + typeof APIGatewayRequestContextV2Schema >; type CloudFormationCustomResourceCreateEvent = z.infer< @@ -124,8 +124,8 @@ export type { ALBMultiValueHeadersEvent, APIGatewayProxyEvent, APIGatewayProxyEventV2, - APIGatewayV2RequestAuthorizer, - APIGatewayV2RequestContext, + APIGatewayRequestAuthorizerV2, + APIGatewayRequestContextV2, CloudFormationCustomResourceCreateEvent, CloudFormationCustomResourceDeleteEvent, CloudFormationCustomResourceUpdateEvent, diff --git a/packages/parser/tests/unit/schema/apigwv2.test.ts b/packages/parser/tests/unit/schema/apigwv2.test.ts index 15716ab30f..73a03a3431 100644 --- a/packages/parser/tests/unit/schema/apigwv2.test.ts +++ b/packages/parser/tests/unit/schema/apigwv2.test.ts @@ -6,7 +6,10 @@ import { APIGatewayProxyEventV2Schema, APIGatewayRequestAuthorizerEventV2Schema, + APIGatewayRequestAuthorizerV2Schema, + APIGatewayRequestContextV2Schema, } from '../../../src/schemas/index.js'; +import type { APIGatewayProxyEventV2 } from '../../../src/types/schema.js'; import { getTestEvent } from './utils.js'; describe('API Gateway HTTP (v2) Schemas', () => { @@ -100,4 +103,36 @@ describe('API Gateway HTTP (v2) Schemas', () => { expect(parsedEvent).toEqual(event); }); }); + + describe('APIGatewayRequestContextV2Schema', () => { + it('parses the request context', () => { + // Prepare + const payload = getTestEvent({ + eventsPath, + filename: 'iam-auth', + }).requestContext; + + // Act + const parsedPayload = APIGatewayRequestContextV2Schema.parse(payload); + + // Assess + expect(parsedPayload).toEqual(payload); + }); + }); + + describe('APIGatewayRequestAuthorizerV2Schema', () => { + it('parses the authorizer', () => { + // Prepare + const payload = getTestEvent({ + eventsPath, + filename: 'iam-auth', + }).requestContext.authorizer; + + // Act + const parsedPayload = APIGatewayRequestAuthorizerV2Schema.parse(payload); + + // Assess + expect(parsedPayload).toEqual(payload); + }); + }); });