@@ -4,7 +4,7 @@ import type { KinesisEnvelope } from '../envelopes/kinesis.js';
44import type { DynamoDBMarshalled } from '../helpers/dynamodb.js' ;
55import type { DynamoDBStreamEvent } from '../types/schema.js' ;
66
7- const DynamoDBStreamChangeRecordBase = z . object ( {
7+ const DynamoDBStreamChangeRecordBaseSchema = z . object ( {
88 ApproximateCreationDateTime : z . number ( ) . optional ( ) ,
99 Keys : z . record ( z . string ( ) , z . record ( z . string ( ) , z . any ( ) ) ) ,
1010 NewImage : z . record ( z . string ( ) , z . any ( ) ) . optional ( ) ,
@@ -19,19 +19,28 @@ const DynamoDBStreamChangeRecordBase = z.object({
1919 ] ) ,
2020} ) ;
2121
22- const DynamoDBStreamToKinesisChangeRecord = DynamoDBStreamChangeRecordBase . omit (
23- {
22+ type DynamoDBStreamChangeRecordBase = z . infer <
23+ typeof DynamoDBStreamChangeRecordBaseSchema
24+ > ;
25+
26+ const DynamoDBStreamToKinesisChangeRecordSchema =
27+ DynamoDBStreamChangeRecordBaseSchema . omit ( {
2428 SequenceNumber : true ,
2529 StreamViewType : true ,
26- }
27- ) ;
30+ } ) ;
31+
32+ type DynamoDBStreamToKinesisChangeRecord = z . infer <
33+ typeof DynamoDBStreamToKinesisChangeRecordSchema
34+ > ;
2835
29- const unmarshallDynamoDBTransform = (
30- object :
31- | z . infer < typeof DynamoDBStreamChangeRecordBase >
32- | z . infer < typeof DynamoDBStreamToKinesisChangeRecord > ,
36+ const unmarshallDynamoDBTransform = <
37+ T extends
38+ | DynamoDBStreamChangeRecordBase
39+ | DynamoDBStreamToKinesisChangeRecord ,
40+ > (
41+ object : T ,
3342 ctx : z . RefinementCtx
34- ) => {
43+ ) : T => {
3544 const result = { ...object } ;
3645
3746 const unmarshallAttributeValue = (
@@ -73,24 +82,27 @@ const unmarshallDynamoDBTransform = (
7382 return result ;
7483} ;
7584
76- const DynamoDBStreamChangeRecord = DynamoDBStreamChangeRecordBase . transform (
77- unmarshallDynamoDBTransform
78- ) ;
85+ const DynamoDBStreamChangeRecordSchema =
86+ DynamoDBStreamChangeRecordBaseSchema . transform (
87+ unmarshallDynamoDBTransform <
88+ DynamoDBStreamChangeRecordBase | DynamoDBStreamToKinesisChangeRecord
89+ >
90+ ) ;
7991
80- const UserIdentity = z . object ( {
92+ const UserIdentitySchema = z . object ( {
8193 type : z . enum ( [ 'Service' ] ) ,
8294 principalId : z . literal ( 'dynamodb.amazonaws.com' ) ,
8395} ) ;
8496
85- const DynamoDBStreamRecord = z . object ( {
97+ const DynamoDBStreamRecordSchema = z . object ( {
8698 eventID : z . string ( ) ,
8799 eventName : z . enum ( [ 'INSERT' , 'MODIFY' , 'REMOVE' ] ) ,
88100 eventVersion : z . string ( ) ,
89101 eventSource : z . literal ( 'aws:dynamodb' ) ,
90102 awsRegion : z . string ( ) ,
91103 eventSourceARN : z . string ( ) ,
92- dynamodb : DynamoDBStreamChangeRecord ,
93- userIdentity : UserIdentity . optional ( ) ,
104+ dynamodb : DynamoDBStreamChangeRecordSchema ,
105+ userIdentity : UserIdentitySchema . optional ( ) ,
94106} ) ;
95107
96108/**
@@ -135,12 +147,12 @@ const DynamoDBStreamRecord = z.object({
135147 * type CustomEvent = z.infer<typeof CustomSchema>;
136148 * ```
137149 */
138- const DynamoDBStreamToKinesisRecord = DynamoDBStreamRecord . extend ( {
150+ const DynamoDBStreamToKinesisRecordSchema = DynamoDBStreamRecordSchema . extend ( {
139151 recordFormat : z . literal ( 'application/json' ) ,
140152 tableName : z . string ( ) ,
141- userIdentity : UserIdentity . nullish ( ) ,
142- dynamodb : DynamoDBStreamToKinesisChangeRecord . transform (
143- unmarshallDynamoDBTransform
153+ userIdentity : UserIdentitySchema . nullish ( ) ,
154+ dynamodb : DynamoDBStreamToKinesisChangeRecordSchema . transform (
155+ unmarshallDynamoDBTransform < DynamoDBStreamToKinesisChangeRecord >
144156 ) ,
145157} ) . omit ( {
146158 eventVersion : true ,
@@ -256,8 +268,8 @@ const DynamoDBStreamToKinesisRecord = DynamoDBStreamRecord.extend({
256268 * @see {@link DynamoDBStreamEvent | DynamoDBStreamEvent }
257269 * @see {@link https://docs.aws.amazon.com/lambda/latest/dg/with-ddb.html }
258270 */
259- const DynamoDBStreamSchema = z . object ( {
260- Records : z . array ( DynamoDBStreamRecord ) . nonempty ( ) ,
271+ const DynamoDBStreamSchemaSchema = z . object ( {
272+ Records : z . array ( DynamoDBStreamRecordSchema ) . nonempty ( ) ,
261273 window : z
262274 . object ( {
263275 start : z . iso . datetime ( ) ,
@@ -272,11 +284,11 @@ const DynamoDBStreamSchema = z.object({
272284} ) ;
273285
274286export {
275- DynamoDBStreamToKinesisRecord ,
276- DynamoDBStreamToKinesisChangeRecord ,
277- DynamoDBStreamSchema ,
278- DynamoDBStreamRecord ,
279- DynamoDBStreamChangeRecord ,
280- DynamoDBStreamChangeRecordBase ,
281- UserIdentity ,
287+ DynamoDBStreamToKinesisRecordSchema as DynamoDBStreamToKinesisRecord ,
288+ DynamoDBStreamToKinesisChangeRecordSchema as DynamoDBStreamToKinesisChangeRecord ,
289+ DynamoDBStreamSchemaSchema as DynamoDBStreamSchema ,
290+ DynamoDBStreamRecordSchema as DynamoDBStreamRecord ,
291+ DynamoDBStreamChangeRecordSchema as DynamoDBStreamChangeRecord ,
292+ DynamoDBStreamChangeRecordBaseSchema as DynamoDBStreamChangeRecordBase ,
293+ UserIdentitySchema as UserIdentity ,
282294} ;
0 commit comments