1
- import { z , type ZodSchema } from 'zod' ;
1
+ import type { ZodSchema , z } from 'zod' ;
2
+ import { ParseError } from '../errors.js' ;
2
3
import { DynamoDBStreamSchema } from '../schemas/index.js' ;
4
+ import type { DynamoDBStreamEnvelopeResponse } from '../types/envelope.js' ;
3
5
import type { ParsedResult , ParsedResultError } from '../types/index.js' ;
4
6
import { Envelope } from './envelope.js' ;
5
- import { ParseError } from '../errors.js' ;
6
- import type { DynamoDBStreamEnvelopeResponse } from '../types/envelope.js' ;
7
7
8
8
/**
9
9
* DynamoDB Stream Envelope to extract data within NewImage/OldImage
10
10
*
11
11
* Note: Values are the parsed models. Images' values can also be None, and
12
12
* length of the list is the record's amount in the original event.
13
13
*/
14
- export class DynamoDBStreamEnvelope extends Envelope {
15
- public static parse < T extends ZodSchema > (
14
+ export const DynamoDBStreamEnvelope = {
15
+ parse < T extends ZodSchema > (
16
16
data : unknown ,
17
17
schema : T
18
18
) : DynamoDBStreamEnvelopeResponse < z . infer < T > > [ ] {
19
19
const parsedEnvelope = DynamoDBStreamSchema . parse ( data ) ;
20
20
21
21
return parsedEnvelope . Records . map ( ( record ) => {
22
22
return {
23
- NewImage : super . parse ( record . dynamodb . NewImage , schema ) ,
24
- OldImage : super . parse ( record . dynamodb . OldImage , schema ) ,
23
+ NewImage : Envelope . parse ( record . dynamodb . NewImage , schema ) ,
24
+ OldImage : Envelope . parse ( record . dynamodb . OldImage , schema ) ,
25
25
} ;
26
26
} ) ;
27
- }
27
+ } ,
28
28
29
- public static safeParse < T extends ZodSchema > (
30
- data : unknown ,
31
- schema : T
32
- ) : ParsedResult {
29
+ safeParse < T extends ZodSchema > ( data : unknown , schema : T ) : ParsedResult {
33
30
const parsedEnvelope = DynamoDBStreamSchema . safeParse ( data ) ;
34
31
35
32
if ( ! parsedEnvelope . success ) {
@@ -44,8 +41,14 @@ export class DynamoDBStreamEnvelope extends Envelope {
44
41
const parsedLogEvents : DynamoDBStreamEnvelopeResponse < z . infer < T > > [ ] = [ ] ;
45
42
46
43
for ( const record of parsedEnvelope . data . Records ) {
47
- const parsedNewImage = super . safeParse ( record . dynamodb . NewImage , schema ) ;
48
- const parsedOldImage = super . safeParse ( record . dynamodb . OldImage , schema ) ;
44
+ const parsedNewImage = Envelope . safeParse (
45
+ record . dynamodb . NewImage ,
46
+ schema
47
+ ) ;
48
+ const parsedOldImage = Envelope . safeParse (
49
+ record . dynamodb . OldImage ,
50
+ schema
51
+ ) ;
49
52
if ( ! parsedNewImage . success || ! parsedOldImage . success ) {
50
53
return {
51
54
success : false ,
@@ -58,17 +61,16 @@ export class DynamoDBStreamEnvelope extends Envelope {
58
61
} ) ,
59
62
originalEvent : data ,
60
63
} ;
61
- } else {
62
- parsedLogEvents . push ( {
63
- NewImage : parsedNewImage . data ,
64
- OldImage : parsedOldImage . data ,
65
- } ) ;
66
64
}
65
+ parsedLogEvents . push ( {
66
+ NewImage : parsedNewImage . data ,
67
+ OldImage : parsedOldImage . data ,
68
+ } ) ;
67
69
}
68
70
69
71
return {
70
72
success : true ,
71
73
data : parsedLogEvents ,
72
74
} ;
73
- }
74
- }
75
+ } ,
76
+ } ;
0 commit comments