diff --git a/packages/parser/src/schemas/apigwv2.ts b/packages/parser/src/schemas/apigwv2.ts index 7d7031fc24..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 APIGatewayV2RequestAuthorizer = z.object({ +const APIGatewayRequestAuthorizerV2Schema = 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 APIGatewayRequestContextV2Schema = z.object({ accountId: z.string(), apiId: z.string(), - authorizer: APIGatewayV2RequestAuthorizer.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: APIGatewayV2RequestContext, + 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: APIGatewayV2RequestContext, + requestContext: APIGatewayRequestContextV2Schema, pathParameters: APIGatewayRecord.nullish(), stageVariables: APIGatewayRecord.nullish(), }); @@ -245,4 +245,6 @@ const APIGatewayRequestAuthorizerEventV2Schema = z.object({ export { APIGatewayProxyEventV2Schema, APIGatewayRequestAuthorizerEventV2Schema, + APIGatewayRequestAuthorizerV2Schema, + APIGatewayRequestContextV2Schema, }; diff --git a/packages/parser/src/schemas/index.ts b/packages/parser/src/schemas/index.ts index 6a087bf9e6..b50de2aac9 100644 --- a/packages/parser/src/schemas/index.ts +++ b/packages/parser/src/schemas/index.ts @@ -7,6 +7,8 @@ export { export { APIGatewayProxyEventV2Schema, APIGatewayRequestAuthorizerEventV2Schema, + 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 fc3b8c2614..7c1cf3032b 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, + APIGatewayRequestAuthorizerV2Schema, + APIGatewayRequestContextV2Schema, AlbMultiValueHeadersSchema, AlbSchema, CloudFormationCustomResourceCreateSchema, @@ -43,6 +45,14 @@ type APIGatewayProxyEvent = z.infer; type APIGatewayProxyEventV2 = z.infer; +type APIGatewayRequestAuthorizerV2 = z.infer< + typeof APIGatewayRequestAuthorizerV2Schema +>; + +type APIGatewayRequestContextV2 = z.infer< + typeof APIGatewayRequestContextV2Schema +>; + type CloudFormationCustomResourceCreateEvent = z.infer< typeof CloudFormationCustomResourceCreateSchema >; @@ -114,6 +124,8 @@ export type { ALBMultiValueHeadersEvent, APIGatewayProxyEvent, APIGatewayProxyEventV2, + 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); + }); + }); });