Skip to content

improv(parser): export API Gateway v2 request schemas #3271

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

Merged
Show file tree
Hide file tree
Changes from all commits
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: 7 additions & 5 deletions packages/parser/src/schemas/apigwv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -237,12 +237,14 @@ const APIGatewayRequestAuthorizerEventV2Schema = z.object({
cookies: APIGatewayStringArray.optional(),
headers: APIGatewayRecord.optional(),
queryStringParameters: APIGatewayRecord.optional(),
requestContext: APIGatewayV2RequestContext,
requestContext: APIGatewayRequestContextV2Schema,
pathParameters: APIGatewayRecord.nullish(),
stageVariables: APIGatewayRecord.nullish(),
});

export {
APIGatewayProxyEventV2Schema,
APIGatewayRequestAuthorizerEventV2Schema,
APIGatewayRequestAuthorizerV2Schema,
APIGatewayRequestContextV2Schema,
};
2 changes: 2 additions & 0 deletions packages/parser/src/schemas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export {
export {
APIGatewayProxyEventV2Schema,
APIGatewayRequestAuthorizerEventV2Schema,
APIGatewayRequestAuthorizerV2Schema,
APIGatewayRequestContextV2Schema,
} from './apigwv2.js';
export {
CloudFormationCustomResourceCreateSchema,
Expand Down
2 changes: 2 additions & 0 deletions packages/parser/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export type {
APIGatewayProxyEvent,
ALBMultiValueHeadersEvent,
APIGatewayProxyEventV2,
APIGatewayRequestContextV2,
APIGatewayRequestAuthorizerV2,
S3Event,
S3EventNotificationEventBridge,
S3SqsEventNotification,
Expand Down
12 changes: 12 additions & 0 deletions packages/parser/src/types/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { z } from 'zod';
import type {
APIGatewayProxyEventSchema,
APIGatewayProxyEventV2Schema,
APIGatewayRequestAuthorizerV2Schema,
APIGatewayRequestContextV2Schema,
AlbMultiValueHeadersSchema,
AlbSchema,
CloudFormationCustomResourceCreateSchema,
Expand Down Expand Up @@ -43,6 +45,14 @@ type APIGatewayProxyEvent = z.infer<typeof APIGatewayProxyEventSchema>;

type APIGatewayProxyEventV2 = z.infer<typeof APIGatewayProxyEventV2Schema>;

type APIGatewayRequestAuthorizerV2 = z.infer<
typeof APIGatewayRequestAuthorizerV2Schema
>;

type APIGatewayRequestContextV2 = z.infer<
typeof APIGatewayRequestContextV2Schema
>;

type CloudFormationCustomResourceCreateEvent = z.infer<
typeof CloudFormationCustomResourceCreateSchema
>;
Expand Down Expand Up @@ -114,6 +124,8 @@ export type {
ALBMultiValueHeadersEvent,
APIGatewayProxyEvent,
APIGatewayProxyEventV2,
APIGatewayRequestAuthorizerV2,
APIGatewayRequestContextV2,
CloudFormationCustomResourceCreateEvent,
CloudFormationCustomResourceDeleteEvent,
CloudFormationCustomResourceUpdateEvent,
Expand Down
35 changes: 35 additions & 0 deletions packages/parser/tests/unit/schema/apigwv2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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<APIGatewayProxyEventV2>({
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<APIGatewayProxyEventV2>({
eventsPath,
filename: 'iam-auth',
}).requestContext.authorizer;

// Act
const parsedPayload = APIGatewayRequestAuthorizerV2Schema.parse(payload);

// Assess
expect(parsedPayload).toEqual(payload);
});
});
});
Loading