@@ -140,103 +140,107 @@ class BatchProcessor extends BasePartialBatchProcessor {
140
140
}
141
141
142
142
/**
143
- * Parse the record according to the schema passed.
144
- *
145
- * If the passed schema is already an extended schema,
146
- * it directly uses the schema to parse the record
147
- *
148
- * If the passed schema is an internal payload schema,
149
- * it checks whether it is a zod schema and
150
- * then extends the zod schema according to the passed event type for parsing
143
+ * Create an extended schema according to the event type passed.
151
144
*
152
- * @param record The record to be parsed
153
- * @param eventType The type of event to process
145
+ * @param eventType The type of event to process (SQS, Kinesis, DynamoDB)
154
146
* @param schema The StandardSchema to be used for parsing
155
147
*/
156
- private async parseRecord (
157
- record : EventSourceDataClassTypes ,
148
+ private async createExtendedSchema (
158
149
eventType : keyof typeof EventType ,
159
150
schema : StandardSchemaV1
160
- ) : Promise < SQSRecord | KinesisStreamRecord | DynamoDBRecord > {
161
- const { parse } = await import ( '@aws-lambda-powertools/parser' ) ;
162
- if ( eventType === EventType . SQS ) {
163
- const extendedSchemaParsing = parse ( record , undefined , schema , true ) ;
164
- if ( extendedSchemaParsing . success )
165
- return extendedSchemaParsing . data as SQSRecord ;
166
- if ( schema [ '~standard' ] . vendor === SchemaType . Zod ) {
167
- const { JSONStringified } = await import (
168
- '@aws-lambda-powertools/parser/helpers'
169
- ) ;
170
- const { SqsRecordSchema } = await import (
171
- '@aws-lambda-powertools/parser/schemas/sqs'
172
- ) ;
173
- const extendedSchema = SqsRecordSchema . extend ( {
174
- // biome-ignore lint/suspicious/noExplicitAny: The vendor field in the schema is verified that the schema is a Zod schema
151
+ ) {
152
+ switch ( eventType ) {
153
+ case EventType . SQS : {
154
+ const [ { JSONStringified } , { SqsRecordSchema } ] = await Promise . all ( [
155
+ import ( '@aws-lambda-powertools/parser/helpers' ) ,
156
+ import ( '@aws-lambda-powertools/parser/schemas/sqs' ) ,
157
+ ] ) ;
158
+ return SqsRecordSchema . extend ( {
175
159
body : JSONStringified ( schema as any ) ,
176
160
} ) ;
177
- return parse ( record , undefined , extendedSchema ) ;
178
161
}
179
- console . warn (
180
- 'The schema provided is not supported. Only Zod schemas are supported for extension.'
181
- ) ;
182
- throw new Error ( 'Unsupported schema type' ) ;
183
- }
184
- if ( eventType === EventType . KinesisDataStreams ) {
185
- const extendedSchemaParsing = parse ( record , undefined , schema , true ) ;
186
- if ( extendedSchemaParsing . success )
187
- return extendedSchemaParsing . data as KinesisStreamRecord ;
188
- if ( schema [ '~standard' ] . vendor === SchemaType . Zod ) {
189
- const { Base64Encoded } = await import (
190
- '@aws-lambda-powertools/parser/helpers'
191
- ) ;
192
- const { KinesisDataStreamRecord, KinesisDataStreamRecordPayload } =
193
- await import ( '@aws-lambda-powertools/parser/schemas/kinesis' ) ;
194
- const extendedSchema = KinesisDataStreamRecord . extend ( {
162
+ case EventType . KinesisDataStreams : {
163
+ const [
164
+ { Base64Encoded } ,
165
+ { KinesisDataStreamRecord, KinesisDataStreamRecordPayload } ,
166
+ ] = await Promise . all ( [
167
+ import ( '@aws-lambda-powertools/parser/helpers' ) ,
168
+ import ( '@aws-lambda-powertools/parser/schemas/kinesis' ) ,
169
+ ] ) ;
170
+ return KinesisDataStreamRecord . extend ( {
195
171
kinesis : KinesisDataStreamRecordPayload . extend ( {
196
- // biome-ignore lint/suspicious/noExplicitAny: The vendor field in the schema is verified that the schema is a Zod schema
197
172
data : Base64Encoded ( schema as any ) ,
198
173
} ) ,
199
174
} ) ;
200
- return parse ( record , undefined , extendedSchema ) ;
201
175
}
202
- console . warn (
203
- 'The schema provided is not supported. Only Zod schemas are supported for extension.'
204
- ) ;
205
- throw new Error ( 'Unsupported schema type' ) ;
206
- }
207
- if ( eventType === EventType . DynamoDBStreams ) {
208
- const extendedSchemaParsing = parse ( record , undefined , schema , true ) ;
209
- if ( extendedSchemaParsing . success )
210
- return extendedSchemaParsing . data as DynamoDBRecord ;
211
- if ( schema [ '~standard' ] . vendor === SchemaType . Zod ) {
212
- const { DynamoDBMarshalled } = await import (
213
- '@aws-lambda-powertools/parser/helpers/dynamodb'
214
- ) ;
215
- const { DynamoDBStreamRecord, DynamoDBStreamChangeRecordBase } =
216
- await import ( '@aws-lambda-powertools/parser/schemas/dynamodb' ) ;
217
- const extendedSchema = DynamoDBStreamRecord . extend ( {
176
+ case EventType . DynamoDBStreams : {
177
+ const [
178
+ { DynamoDBMarshalled } ,
179
+ { DynamoDBStreamRecord, DynamoDBStreamChangeRecordBase } ,
180
+ ] = await Promise . all ( [
181
+ import ( '@aws-lambda-powertools/parser/helpers/dynamodb' ) ,
182
+ import ( '@aws-lambda-powertools/parser/schemas/dynamodb' ) ,
183
+ ] ) ;
184
+ return DynamoDBStreamRecord . extend ( {
218
185
dynamodb : DynamoDBStreamChangeRecordBase . extend ( {
219
- // biome-ignore lint/suspicious/noExplicitAny: The vendor field in the schema is verified that the schema is a Zod schema
220
186
OldImage : DynamoDBMarshalled < StreamRecord [ 'OldImage' ] > (
221
187
schema as any
222
188
) . optional ( ) ,
223
- // biome-ignore lint/suspicious/noExplicitAny: The vendor field in the schema is verified that the schema is a Zod schema
224
189
NewImage : DynamoDBMarshalled < StreamRecord [ 'NewImage' ] > (
225
190
schema as any
226
191
) . optional ( ) ,
227
192
} ) ,
228
193
} ) ;
229
- return parse ( record , undefined , extendedSchema ) ;
230
194
}
195
+ default :
196
+ console . warn (
197
+ `The event type provided is not supported. Supported events: ${ Object . values ( EventType ) . join ( ',' ) } `
198
+ ) ;
199
+ throw new Error ( 'Unsupported event type' ) ;
200
+ }
201
+ }
202
+
203
+ /**
204
+ * Parse the record according to the schema passed.
205
+ *
206
+ * If the passed schema is already an extended schema,
207
+ * it directly uses the schema to parse the record
208
+ *
209
+ * If the passed schema is an internal payload schema,
210
+ * it checks whether it is a zod schema and
211
+ * then extends the zod schema according to the passed event type for parsing
212
+ *
213
+ * @param record The record to be parsed
214
+ * @param eventType The type of event to process
215
+ * @param schema The StandardSchema to be used for parsing
216
+ */
217
+ private async parseRecord (
218
+ record : EventSourceDataClassTypes ,
219
+ eventType : keyof typeof EventType ,
220
+ schema : StandardSchemaV1
221
+ ) : Promise < SQSRecord | KinesisStreamRecord | DynamoDBRecord > {
222
+ const { parse } = await import ( '@aws-lambda-powertools/parser' ) ;
223
+ // Try parsing with the original schema first
224
+ const extendedSchemaParsing = parse ( record , undefined , schema , true ) ;
225
+ if ( extendedSchemaParsing . success ) {
226
+ return extendedSchemaParsing . data as
227
+ | SQSRecord
228
+ | KinesisStreamRecord
229
+ | DynamoDBRecord ;
230
+ }
231
+ // Only proceed with schema extension if it's a Zod schema
232
+ if ( schema [ '~standard' ] . vendor !== SchemaType . Zod ) {
231
233
console . warn (
232
234
'The schema provided is not supported. Only Zod schemas are supported for extension.'
233
235
) ;
234
236
throw new Error ( 'Unsupported schema type' ) ;
235
237
}
236
- console . warn (
237
- `The event type provided is not supported. Supported events: ${ Object . values ( EventType ) . join ( ',' ) } `
238
- ) ;
239
- throw new Error ( 'Unsupported event type' ) ;
238
+ // Handle schema extension based on event type
239
+ const extendedSchema = await this . createExtendedSchema ( eventType , schema ) ;
240
+ return parse ( record , undefined , extendedSchema ) as
241
+ | SQSRecord
242
+ | KinesisStreamRecord
243
+ | DynamoDBRecord ;
240
244
}
241
245
}
242
246
0 commit comments