Skip to content

Commit d81445d

Browse files
authored
feat(parser): add types for built-in schemas (#1838)
* add types for built-in schemas * fixed imports * only use top level schema
1 parent 5343894 commit d81445d

File tree

3 files changed

+113
-11
lines changed

3 files changed

+113
-11
lines changed

packages/parser/src/envelopes/kafka.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
KafkaMskEventSchema,
55
KafkaSelfManagedEventSchema,
66
} from '../schemas/kafka.js';
7-
import { type KafkaRecord } from '../types/schema.js';
87

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

3433
return Object.values(parsedEnvelope.records).map((topicRecord) => {
35-
return topicRecord.map((record: KafkaRecord) => {
34+
return topicRecord.map((record) => {
3635
return parse(record.value, schema);
3736
});
3837
});

packages/parser/src/types/schema.ts

+109-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,120 @@
1-
import { KafkaRecordSchema } from '../schemas/kafka.js';
1+
import {
2+
KafkaSelfManagedEventSchema,
3+
KafkaMskEventSchema,
4+
} from '../schemas/kafka.js';
25
import { z } from 'zod';
36
import {
47
KinesisDataStreamRecord,
58
KinesisDataStreamRecordPayload,
9+
KinesisDataStreamSchema,
610
} from '../schemas/kinesis.js';
711
import { APIGatewayProxyEventSchema } from '../schemas/apigw.js';
12+
import { AlbSchema, AlbMultiValueHeadersSchema } from '../schemas/alb.js';
13+
import { APIGatewayProxyEventV2Schema } from '../schemas/apigwv2.js';
14+
import { DynamoDBStreamSchema } from '../schemas/dynamodb.js';
15+
import { SqsSchema } from '../schemas/sqs.js';
16+
import {
17+
CloudFormationCustomResourceCreateSchema,
18+
CloudFormationCustomResourceDeleteSchema,
19+
CloudFormationCustomResourceUpdateSchema,
20+
} from '../schemas/cloudformation-custom-resource.js';
21+
import { CloudWatchLogsSchema } from '../schemas/cloudwatch.js';
22+
import { EventBridgeSchema } from '../schemas/eventbridge.js';
23+
import {
24+
KinesisFirehoseSchema,
25+
KinesisFirehoseSqsSchema,
26+
} from '../schemas/kinesis-firehose.js';
27+
import { LambdaFunctionUrlSchema } from '../schemas/lambda.js';
28+
import {
29+
S3EventNotificationEventBridgeSchema,
30+
S3Schema,
31+
S3SqsEventNotificationSchema,
32+
} from '../schemas/s3.js';
33+
import { SesSchema } from '../schemas/ses.js';
34+
import { SnsSchema } from '../schemas/sns.js';
35+
import { VpcLatticeSchema } from '../schemas/vpc-lattice.js';
36+
import { VpcLatticeV2Schema } from '../schemas/vpc-latticev2.js';
37+
38+
type ALBEvent = z.infer<typeof AlbSchema>;
39+
40+
type ALBMultiValueHeadersEvent = z.infer<typeof AlbMultiValueHeadersSchema>;
41+
42+
type APIGatewayProxyEvent = z.infer<typeof APIGatewayProxyEventSchema>;
43+
type APIGatewayProxyEventV2 = z.infer<typeof APIGatewayProxyEventV2Schema>;
844

9-
export type KafkaRecord = z.infer<typeof KafkaRecordSchema>;
45+
type CloudFormationCustomResourceCreateEvent = z.infer<
46+
typeof CloudFormationCustomResourceCreateSchema
47+
>;
1048

11-
export type KinesisDataStreamRecord = z.infer<typeof KinesisDataStreamRecord>;
49+
type CloudFormationCustomResourceDeleteEvent = z.infer<
50+
typeof CloudFormationCustomResourceDeleteSchema
51+
>;
1252

13-
export type KinesisDataStreamRecordPayload = z.infer<
14-
typeof KinesisDataStreamRecordPayload
53+
type CloudFormationCustomResourceUpdateEvent = z.infer<
54+
typeof CloudFormationCustomResourceUpdateSchema
1555
>;
1656

17-
export type ApiGatewayProxyEvent = z.infer<typeof APIGatewayProxyEventSchema>;
57+
type CloudWatchLogsEvent = z.infer<typeof CloudWatchLogsSchema>;
58+
59+
type DynamoDBStreamEvent = z.infer<typeof DynamoDBStreamSchema>;
60+
61+
type EventBridgeEvent = z.infer<typeof EventBridgeSchema>;
62+
63+
type KafkaSelfManagedEvent = z.infer<typeof KafkaSelfManagedEventSchema>;
64+
65+
type KafkaMskEvent = z.infer<typeof KafkaMskEventSchema>;
66+
67+
type KinesisDataStreamEvent = z.infer<typeof KinesisDataStreamSchema>;
68+
69+
type KinesisFireHoseEvent = z.infer<typeof KinesisFirehoseSchema>;
70+
71+
type KinesisFireHoseSqsEvent = z.infer<typeof KinesisFirehoseSqsSchema>;
72+
73+
type LambdaFunctionUrlEvent = z.infer<typeof LambdaFunctionUrlSchema>;
74+
75+
type S3Event = z.infer<typeof S3Schema>;
76+
77+
type S3EventNotificationEventBridge = z.infer<
78+
typeof S3EventNotificationEventBridgeSchema
79+
>;
80+
81+
type S3SqsEventNotification = z.infer<typeof S3SqsEventNotificationSchema>;
82+
83+
type SesEvent = z.infer<typeof SesSchema>;
84+
85+
type SnsEvent = z.infer<typeof SnsSchema>;
86+
87+
type SqsEvent = z.infer<typeof SqsSchema>;
88+
89+
type VpcLatticeEvent = z.infer<typeof VpcLatticeSchema>;
90+
91+
type VpcLatticeEventV2 = z.infer<typeof VpcLatticeV2Schema>;
92+
93+
export {
94+
type ALBEvent,
95+
type ALBMultiValueHeadersEvent,
96+
type APIGatewayProxyEvent,
97+
type APIGatewayProxyEventV2,
98+
type CloudFormationCustomResourceCreateEvent,
99+
type CloudFormationCustomResourceDeleteEvent,
100+
type CloudFormationCustomResourceUpdateEvent,
101+
type CloudWatchLogsEvent,
102+
type DynamoDBStreamEvent,
103+
type EventBridgeEvent,
104+
type KafkaSelfManagedEvent,
105+
type KafkaMskEvent,
106+
type KinesisDataStreamEvent,
107+
type KinesisDataStreamRecord,
108+
type KinesisDataStreamRecordPayload,
109+
type KinesisFireHoseEvent,
110+
type KinesisFireHoseSqsEvent,
111+
type LambdaFunctionUrlEvent,
112+
type S3Event,
113+
type S3EventNotificationEventBridge,
114+
type S3SqsEventNotification,
115+
type SesEvent,
116+
type SnsEvent,
117+
type SqsEvent,
118+
type VpcLatticeEvent,
119+
type VpcLatticeEventV2,
120+
};

packages/parser/tests/unit/envelopes/apigwt.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

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

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

1717
testEvent.body = JSON.stringify(testCustomSchemaObject);
1818

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

2323
it('should throw no body provided', () => {
24-
const testEvent = TestEvents.apiGatewayProxyEvent as ApiGatewayProxyEvent;
24+
const testEvent = TestEvents.apiGatewayProxyEvent as APIGatewayProxyEvent;
2525
testEvent.body = undefined;
2626

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

0 commit comments

Comments
 (0)