From 62e97b98d14dcf271130f6e3f11d1e61308c1863 Mon Sep 17 00:00:00 2001 From: Alexander Schueren Date: Mon, 8 Jan 2024 14:08:43 +0100 Subject: [PATCH 1/3] add types for built-in schemas --- packages/parser/src/types/schema.ts | 146 +++++++++++++++++++++++++++- 1 file changed, 141 insertions(+), 5 deletions(-) diff --git a/packages/parser/src/types/schema.ts b/packages/parser/src/types/schema.ts index cc5869a1e2..eb76b55774 100644 --- a/packages/parser/src/types/schema.ts +++ b/packages/parser/src/types/schema.ts @@ -1,17 +1,153 @@ -import { KafkaRecordSchema } from '../schemas/kafka.js'; +import { + KafkaRecordSchema, + 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, + CloudWatchLogEventSchema, +} 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, SesRecordSchema } from '../schemas/ses.js'; +import { + SnsSchema, + SnsRecordSchema, + SnsNotificationSchema, + SnsSqsNotificationSchema, +} from '../schemas/sns.js'; +import { VpcLatticeSchema } from '../schemas/vpc-lattice.js'; +import { VpcLatticeV2Schema } from '../schemas/vpc-latticev2.js'; + +type Alb = z.infer; + +type AlbMultiValueHeaders = z.infer; + +type APIGatewayProxyEvent = z.infer; +type APIGatewayProxyEventV2 = z.infer; + +type CloudFormationCustomResourceCreate = z.infer< + typeof CloudFormationCustomResourceCreateSchema +>; + +type CloudFormationCustomResourceDelete = z.infer< + typeof CloudFormationCustomResourceDeleteSchema +>; + +type CloudFormationCustomResourceUpdate = z.infer< + typeof CloudFormationCustomResourceUpdateSchema +>; + +type CloudWatchLogs = z.infer; + +type CloudWatchLogsEvent = z.infer; + +type DynamoDBStream = z.infer; + +type EventBridge = z.infer; -export type KafkaRecord = z.infer; +type KafkaRecord = z.infer; -export type KinesisDataStreamRecord = z.infer; +type KafkaSelfManagedEvent = z.infer; -export type KinesisDataStreamRecordPayload = z.infer< +type KafkaMskEvent = z.infer; + +type KinesisDataStream = z.infer; + +type KinesisDataStreamRecord = z.infer; + +type KinesisDataStreamRecordPayload = z.infer< typeof KinesisDataStreamRecordPayload >; -export type ApiGatewayProxyEvent = z.infer; +type KinesisFireHose = z.infer; + +type KinesisFireHoseSqs = z.infer; + +type LambdaFunctionUrl = z.infer; + +type S3 = z.infer; + +type S3EventNotificationEventBridge = z.infer< + typeof S3EventNotificationEventBridgeSchema +>; + +type S3SqsEventNotification = z.infer; + +type Ses = z.infer; + +type SesRecord = z.infer; + +type Sns = z.infer; + +type SnsRecord = z.infer; + +type SnsNotification = z.infer; + +type SnsSqsNotification = z.infer; + +type Sqs = z.infer; + +type VpcLattice = z.infer; + +type VpcLatticeV2 = z.infer; + +export { + type Alb, + type AlbMultiValueHeaders, + type APIGatewayProxyEvent, + type APIGatewayProxyEventV2, + type CloudFormationCustomResourceCreate, + type CloudFormationCustomResourceDelete, + type CloudFormationCustomResourceUpdate, + type CloudWatchLogs, + type CloudWatchLogsEvent, + type DynamoDBStream, + type EventBridge, + type KafkaSelfManagedEvent, + type KafkaMskEvent, + type KinesisDataStream, + type KinesisDataStreamRecord, + type KinesisDataStreamRecordPayload, + type KinesisFireHose, + type KinesisFireHoseSqs, + type LambdaFunctionUrl, + type S3, + type S3EventNotificationEventBridge, + type S3SqsEventNotification, + type Ses, + type SesRecord, + type Sns, + type SnsRecord, + type SnsNotification, + type SnsSqsNotification, + type Sqs, + type VpcLattice, + type VpcLatticeV2, + type KafkaRecord, +}; From dd9081eeb303c80ec91e4d837a8a8a493e6f56e1 Mon Sep 17 00:00:00 2001 From: Alexander Schueren Date: Mon, 8 Jan 2024 19:06:36 +0100 Subject: [PATCH 2/3] fixed imports --- packages/parser/tests/unit/envelopes/apigwt.test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/parser/tests/unit/envelopes/apigwt.test.ts b/packages/parser/tests/unit/envelopes/apigwt.test.ts index 6c51736b16..b50e18b439 100644 --- a/packages/parser/tests/unit/envelopes/apigwt.test.ts +++ b/packages/parser/tests/unit/envelopes/apigwt.test.ts @@ -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); @@ -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(); From 1f76eb09b1e35d3da8538ada94a3cf19706c50ff Mon Sep 17 00:00:00 2001 From: Alexander Schueren Date: Mon, 8 Jan 2024 19:23:16 +0100 Subject: [PATCH 3/3] only use top level schema --- packages/parser/src/envelopes/kafka.ts | 3 +- packages/parser/src/types/schema.ts | 109 +++++++++---------------- 2 files changed, 39 insertions(+), 73 deletions(-) diff --git a/packages/parser/src/envelopes/kafka.ts b/packages/parser/src/envelopes/kafka.ts index 32529d4255..86eb44063f 100644 --- a/packages/parser/src/envelopes/kafka.ts +++ b/packages/parser/src/envelopes/kafka.ts @@ -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 @@ -32,7 +31,7 @@ export const kafkaEnvelope = ( : KafkaSelfManagedEventSchema.parse(data); return Object.values(parsedEnvelope.records).map((topicRecord) => { - return topicRecord.map((record: KafkaRecord) => { + return topicRecord.map((record) => { return parse(record.value, schema); }); }); diff --git a/packages/parser/src/types/schema.ts b/packages/parser/src/types/schema.ts index eb76b55774..6e15f17bd0 100644 --- a/packages/parser/src/types/schema.ts +++ b/packages/parser/src/types/schema.ts @@ -1,5 +1,4 @@ import { - KafkaRecordSchema, KafkaSelfManagedEventSchema, KafkaMskEventSchema, } from '../schemas/kafka.js'; @@ -19,10 +18,7 @@ import { CloudFormationCustomResourceDeleteSchema, CloudFormationCustomResourceUpdateSchema, } from '../schemas/cloudformation-custom-resource.js'; -import { - CloudWatchLogsSchema, - CloudWatchLogEventSchema, -} from '../schemas/cloudwatch.js'; +import { CloudWatchLogsSchema } from '../schemas/cloudwatch.js'; import { EventBridgeSchema } from '../schemas/eventbridge.js'; import { KinesisFirehoseSchema, @@ -34,64 +30,49 @@ import { S3Schema, S3SqsEventNotificationSchema, } from '../schemas/s3.js'; -import { SesSchema, SesRecordSchema } from '../schemas/ses.js'; -import { - SnsSchema, - SnsRecordSchema, - SnsNotificationSchema, - SnsSqsNotificationSchema, -} from '../schemas/sns.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 Alb = z.infer; +type ALBEvent = z.infer; -type AlbMultiValueHeaders = z.infer; +type ALBMultiValueHeadersEvent = z.infer; type APIGatewayProxyEvent = z.infer; type APIGatewayProxyEventV2 = z.infer; -type CloudFormationCustomResourceCreate = z.infer< +type CloudFormationCustomResourceCreateEvent = z.infer< typeof CloudFormationCustomResourceCreateSchema >; -type CloudFormationCustomResourceDelete = z.infer< +type CloudFormationCustomResourceDeleteEvent = z.infer< typeof CloudFormationCustomResourceDeleteSchema >; -type CloudFormationCustomResourceUpdate = z.infer< +type CloudFormationCustomResourceUpdateEvent = z.infer< typeof CloudFormationCustomResourceUpdateSchema >; -type CloudWatchLogs = z.infer; - -type CloudWatchLogsEvent = z.infer; +type CloudWatchLogsEvent = z.infer; -type DynamoDBStream = z.infer; +type DynamoDBStreamEvent = z.infer; -type EventBridge = z.infer; - -type KafkaRecord = z.infer; +type EventBridgeEvent = z.infer; type KafkaSelfManagedEvent = z.infer; type KafkaMskEvent = z.infer; -type KinesisDataStream = z.infer; - -type KinesisDataStreamRecord = z.infer; +type KinesisDataStreamEvent = z.infer; -type KinesisDataStreamRecordPayload = z.infer< - typeof KinesisDataStreamRecordPayload ->; +type KinesisFireHoseEvent = z.infer; -type KinesisFireHose = z.infer; +type KinesisFireHoseSqsEvent = z.infer; -type KinesisFireHoseSqs = z.infer; +type LambdaFunctionUrlEvent = z.infer; -type LambdaFunctionUrl = z.infer; - -type S3 = z.infer; +type S3Event = z.infer; type S3EventNotificationEventBridge = z.infer< typeof S3EventNotificationEventBridgeSchema @@ -99,55 +80,41 @@ type S3EventNotificationEventBridge = z.infer< type S3SqsEventNotification = z.infer; -type Ses = z.infer; - -type SesRecord = z.infer; - -type Sns = z.infer; - -type SnsRecord = z.infer; - -type SnsNotification = z.infer; +type SesEvent = z.infer; -type SnsSqsNotification = z.infer; +type SnsEvent = z.infer; -type Sqs = z.infer; +type SqsEvent = z.infer; -type VpcLattice = z.infer; +type VpcLatticeEvent = z.infer; -type VpcLatticeV2 = z.infer; +type VpcLatticeEventV2 = z.infer; export { - type Alb, - type AlbMultiValueHeaders, + type ALBEvent, + type ALBMultiValueHeadersEvent, type APIGatewayProxyEvent, type APIGatewayProxyEventV2, - type CloudFormationCustomResourceCreate, - type CloudFormationCustomResourceDelete, - type CloudFormationCustomResourceUpdate, - type CloudWatchLogs, + type CloudFormationCustomResourceCreateEvent, + type CloudFormationCustomResourceDeleteEvent, + type CloudFormationCustomResourceUpdateEvent, type CloudWatchLogsEvent, - type DynamoDBStream, - type EventBridge, + type DynamoDBStreamEvent, + type EventBridgeEvent, type KafkaSelfManagedEvent, type KafkaMskEvent, - type KinesisDataStream, + type KinesisDataStreamEvent, type KinesisDataStreamRecord, type KinesisDataStreamRecordPayload, - type KinesisFireHose, - type KinesisFireHoseSqs, - type LambdaFunctionUrl, - type S3, + type KinesisFireHoseEvent, + type KinesisFireHoseSqsEvent, + type LambdaFunctionUrlEvent, + type S3Event, type S3EventNotificationEventBridge, type S3SqsEventNotification, - type Ses, - type SesRecord, - type Sns, - type SnsRecord, - type SnsNotification, - type SnsSqsNotification, - type Sqs, - type VpcLattice, - type VpcLatticeV2, - type KafkaRecord, + type SesEvent, + type SnsEvent, + type SqsEvent, + type VpcLatticeEvent, + type VpcLatticeEventV2, };