Skip to content

Commit

Permalink
feat(parser): add types for built-in schemas (#1838)
Browse files Browse the repository at this point in the history
* add types for built-in schemas

* fixed imports

* only use top level schema
  • Loading branch information
am29d authored Mar 6, 2024
1 parent 5343894 commit d81445d
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 11 deletions.
3 changes: 1 addition & 2 deletions packages/parser/src/envelopes/kafka.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
KafkaMskEventSchema,
KafkaSelfManagedEventSchema,
} from '../schemas/kafka.js';
import { type KafkaRecord } from '../types/schema.js';

/**
* Kafka event envelope to extract data within body key
Expand Down Expand Up @@ -32,7 +31,7 @@ export const kafkaEnvelope = <T extends ZodSchema>(
: KafkaSelfManagedEventSchema.parse(data);

return Object.values(parsedEnvelope.records).map((topicRecord) => {
return topicRecord.map((record: KafkaRecord) => {
return topicRecord.map((record) => {
return parse(record.value, schema);
});
});
Expand Down
115 changes: 109 additions & 6 deletions packages/parser/src/types/schema.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,120 @@
import { KafkaRecordSchema } from '../schemas/kafka.js';
import {
KafkaSelfManagedEventSchema,
KafkaMskEventSchema,
} from '../schemas/kafka.js';
import { z } from 'zod';
import {
KinesisDataStreamRecord,
KinesisDataStreamRecordPayload,
KinesisDataStreamSchema,
} from '../schemas/kinesis.js';
import { APIGatewayProxyEventSchema } from '../schemas/apigw.js';
import { AlbSchema, AlbMultiValueHeadersSchema } from '../schemas/alb.js';
import { APIGatewayProxyEventV2Schema } from '../schemas/apigwv2.js';
import { DynamoDBStreamSchema } from '../schemas/dynamodb.js';
import { SqsSchema } from '../schemas/sqs.js';
import {
CloudFormationCustomResourceCreateSchema,
CloudFormationCustomResourceDeleteSchema,
CloudFormationCustomResourceUpdateSchema,
} from '../schemas/cloudformation-custom-resource.js';
import { CloudWatchLogsSchema } from '../schemas/cloudwatch.js';
import { EventBridgeSchema } from '../schemas/eventbridge.js';
import {
KinesisFirehoseSchema,
KinesisFirehoseSqsSchema,
} from '../schemas/kinesis-firehose.js';
import { LambdaFunctionUrlSchema } from '../schemas/lambda.js';
import {
S3EventNotificationEventBridgeSchema,
S3Schema,
S3SqsEventNotificationSchema,
} from '../schemas/s3.js';
import { SesSchema } from '../schemas/ses.js';
import { SnsSchema } from '../schemas/sns.js';
import { VpcLatticeSchema } from '../schemas/vpc-lattice.js';
import { VpcLatticeV2Schema } from '../schemas/vpc-latticev2.js';

type ALBEvent = z.infer<typeof AlbSchema>;

type ALBMultiValueHeadersEvent = z.infer<typeof AlbMultiValueHeadersSchema>;

type APIGatewayProxyEvent = z.infer<typeof APIGatewayProxyEventSchema>;
type APIGatewayProxyEventV2 = z.infer<typeof APIGatewayProxyEventV2Schema>;

export type KafkaRecord = z.infer<typeof KafkaRecordSchema>;
type CloudFormationCustomResourceCreateEvent = z.infer<
typeof CloudFormationCustomResourceCreateSchema
>;

export type KinesisDataStreamRecord = z.infer<typeof KinesisDataStreamRecord>;
type CloudFormationCustomResourceDeleteEvent = z.infer<
typeof CloudFormationCustomResourceDeleteSchema
>;

export type KinesisDataStreamRecordPayload = z.infer<
typeof KinesisDataStreamRecordPayload
type CloudFormationCustomResourceUpdateEvent = z.infer<
typeof CloudFormationCustomResourceUpdateSchema
>;

export type ApiGatewayProxyEvent = z.infer<typeof APIGatewayProxyEventSchema>;
type CloudWatchLogsEvent = z.infer<typeof CloudWatchLogsSchema>;

type DynamoDBStreamEvent = z.infer<typeof DynamoDBStreamSchema>;

type EventBridgeEvent = z.infer<typeof EventBridgeSchema>;

type KafkaSelfManagedEvent = z.infer<typeof KafkaSelfManagedEventSchema>;

type KafkaMskEvent = z.infer<typeof KafkaMskEventSchema>;

type KinesisDataStreamEvent = z.infer<typeof KinesisDataStreamSchema>;

type KinesisFireHoseEvent = z.infer<typeof KinesisFirehoseSchema>;

type KinesisFireHoseSqsEvent = z.infer<typeof KinesisFirehoseSqsSchema>;

type LambdaFunctionUrlEvent = z.infer<typeof LambdaFunctionUrlSchema>;

type S3Event = z.infer<typeof S3Schema>;

type S3EventNotificationEventBridge = z.infer<
typeof S3EventNotificationEventBridgeSchema
>;

type S3SqsEventNotification = z.infer<typeof S3SqsEventNotificationSchema>;

type SesEvent = z.infer<typeof SesSchema>;

type SnsEvent = z.infer<typeof SnsSchema>;

type SqsEvent = z.infer<typeof SqsSchema>;

type VpcLatticeEvent = z.infer<typeof VpcLatticeSchema>;

type VpcLatticeEventV2 = z.infer<typeof VpcLatticeV2Schema>;

export {
type ALBEvent,
type ALBMultiValueHeadersEvent,
type APIGatewayProxyEvent,
type APIGatewayProxyEventV2,
type CloudFormationCustomResourceCreateEvent,
type CloudFormationCustomResourceDeleteEvent,
type CloudFormationCustomResourceUpdateEvent,
type CloudWatchLogsEvent,
type DynamoDBStreamEvent,
type EventBridgeEvent,
type KafkaSelfManagedEvent,
type KafkaMskEvent,
type KinesisDataStreamEvent,
type KinesisDataStreamRecord,
type KinesisDataStreamRecordPayload,
type KinesisFireHoseEvent,
type KinesisFireHoseSqsEvent,
type LambdaFunctionUrlEvent,
type S3Event,
type S3EventNotificationEventBridge,
type S3SqsEventNotification,
type SesEvent,
type SnsEvent,
type SqsEvent,
type VpcLatticeEvent,
type VpcLatticeEventV2,
};
6 changes: 3 additions & 3 deletions packages/parser/tests/unit/envelopes/apigwt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

import { generateMock } from '@anatine/zod-mock';
import { TestEvents, TestSchema } from '../schema/utils.js';
import { ApiGatewayProxyEvent } from '../../../src/types/schema.js';
import { APIGatewayProxyEvent } from '../../../src/types/schema.js';
import { apiGatewayEnvelope } from '../../../src/envelopes/apigw';

describe('ApigwEnvelope ', () => {
it('should parse custom schema in envelope', () => {
const testCustomSchemaObject = generateMock(TestSchema);
const testEvent = TestEvents.apiGatewayProxyEvent as ApiGatewayProxyEvent;
const testEvent = TestEvents.apiGatewayProxyEvent as APIGatewayProxyEvent;

testEvent.body = JSON.stringify(testCustomSchemaObject);

Expand All @@ -21,7 +21,7 @@ describe('ApigwEnvelope ', () => {
});

it('should throw no body provided', () => {
const testEvent = TestEvents.apiGatewayProxyEvent as ApiGatewayProxyEvent;
const testEvent = TestEvents.apiGatewayProxyEvent as APIGatewayProxyEvent;
testEvent.body = undefined;

expect(() => apiGatewayEnvelope(testEvent, TestSchema)).toThrow();
Expand Down

0 comments on commit d81445d

Please sign in to comment.