Skip to content

Commit 51a3410

Browse files
dreamorosiam29d
andauthored
fix(parser): ddb base schema + other exports (#3741)
Co-authored-by: Alexander Schueren <[email protected]>
1 parent c28e45e commit 51a3410

37 files changed

+157
-300
lines changed

Diff for: docs/utilities/parser.md

+14-16
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ Both are also able to parse either an object or JSON string as an input.
4444
Be cautious when using multiple decorators that expect event to have a specific structure, the order of evaluation for decorators is from bottom to top.
4545

4646
=== "Middy middleware"
47-
```typescript hl_lines="34"
47+
```typescript hl_lines="22"
4848
--8<-- "examples/snippets/parser/middy.ts"
4949
```
5050

@@ -157,7 +157,7 @@ If you want to extend a schema and transform a JSON stringified payload to an ob
157157
If you want to parse a DynamoDB stream event with unmarshalling, you can use the helper function `DynamoDBMarshalled`:
158158

159159
=== "DynamoDBStreamSchema with DynamoDBMarshalled"
160-
```typescript hl_lines="17"
160+
```typescript hl_lines="18"
161161
--8<-- "examples/snippets/parser/extendDynamoDBStreamSchema.ts"
162162
```
163163

@@ -180,12 +180,12 @@ Envelopes can be used via envelope parameter available in middy and decorator.
180180
Here's an example of parsing a custom schema in an event coming from EventBridge, where all you want is what's inside the detail key.
181181

182182
=== "Middy middleware"
183-
```typescript hl_lines="5 36"
183+
```typescript hl_lines="23"
184184
--8<-- "examples/snippets/parser/envelopeMiddy.ts"
185185
```
186186

187187
=== "Decorator"
188-
```typescript hl_lines="5 26 30"
188+
```typescript hl_lines="26"
189189
--8<-- "examples/snippets/parser/envelopeDecorator.ts"
190190
```
191191

@@ -230,26 +230,24 @@ The `ParsedResult` object will have `success`, `data`, or `error` and `original
230230
If the parsing is successful, the `data` field will contain the parsed event, otherwise you can access the `error` field and the `originalEvent` to handle the error and recover the original event.
231231

232232
=== "Middy middleware"
233-
```typescript hl_lines="32 35 38 39 44"
233+
```typescript hl_lines="23 28 32-33"
234234
--8<-- "examples/snippets/parser/safeParseMiddy.ts"
235235
```
236236

237237
1. Use `safeParse` option to parse the event without throwing an error
238-
2. Check if the result is successful or not and handle the error accordingly
239-
3. Use `data` to access the parsed event
240-
4. Use `error` to handle the error message
241-
5. Use `originalEvent` to get the original event and recover
238+
2. Use `data` to access the parsed event when successful
239+
3. Use `error` to handle the error message
240+
4. Use `originalEvent` to get the original event and recover
242241

243242
=== "Decorator"
244-
```typescript hl_lines="29 35 37 40 41"
243+
```typescript hl_lines="33 41 45-46"
245244
--8<-- "examples/snippets/parser/safeParseDecorator.ts"
246245
```
247246

248247
1. Use `safeParse` option to parse the event without throwing an error
249-
2. Check if the result is successful or not and handle the error accordingly
250-
3. Use `data` to access the parsed event
251-
4. Use `error` to handle the error message
252-
5. Use `originalEvent` to get the original event and recover
248+
2. Use `data` to access the parsed event when successful
249+
3. Use `error` to handle the error message
250+
4. Use `originalEvent` to get the original event and recover
253251

254252
## Manual parsing
255253

@@ -316,7 +314,7 @@ If you are you use middy middleware, you don't need to do this.
316314

317315
=== "handlerDecorator.test.ts"
318316

319-
```typescript hl_lines="26"
317+
```typescript hl_lines="27"
320318
--8<-- "examples/snippets/parser/unitTestDecorator.ts"
321319
```
322320

@@ -338,7 +336,7 @@ This also works when using `safeParse` option.
338336

339337
=== "handlerSafeParse.test.ts"
340338

341-
```typescript hl_lines="21-29 35 45"
339+
```typescript hl_lines="21-30 36 46"
342340
--8<-- "examples/snippets/parser/unitTestSafeParse.ts"
343341
```
344342

Diff for: examples/snippets/parser/envelopeDecorator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
22
import { Logger } from '@aws-lambda-powertools/logger';
33
import { parser } from '@aws-lambda-powertools/parser';
4-
import { EventBridgeEnvelope } from '@aws-lambda-powertools/parser/envelopes';
4+
import { EventBridgeEnvelope } from '@aws-lambda-powertools/parser/envelopes/eventbridge';
55
import type { Context } from 'aws-lambda';
66
import { z } from 'zod';
77

Diff for: examples/snippets/parser/envelopeMiddy.ts

+9-17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { Logger } from '@aws-lambda-powertools/logger';
2-
import { EventBridgeEnvelope } from '@aws-lambda-powertools/parser/envelopes';
2+
import { EventBridgeEnvelope } from '@aws-lambda-powertools/parser/envelopes/eventbridge';
33
import { parser } from '@aws-lambda-powertools/parser/middleware';
44
import middy from '@middy/core';
5-
import type { Context } from 'aws-lambda';
65
import { z } from 'zod';
76

87
const logger = new Logger();
@@ -20,18 +19,11 @@ const orderSchema = z.object({
2019
optionalField: z.string().optional(),
2120
});
2221

23-
type Order = z.infer<typeof orderSchema>;
24-
25-
const lambdaHandler = async (
26-
event: Order,
27-
_context: Context
28-
): Promise<void> => {
29-
for (const item of event.items) {
30-
// item is parsed as OrderItem
31-
logger.info('Processing item', { item });
32-
}
33-
};
34-
35-
export const handler = middy(lambdaHandler).use(
36-
parser({ schema: orderSchema, envelope: EventBridgeEnvelope })
37-
);
22+
export const handler = middy()
23+
.use(parser({ schema: orderSchema, envelope: EventBridgeEnvelope }))
24+
.handler(async (event): Promise<void> => {
25+
for (const item of event.items) {
26+
// item is parsed as OrderItem
27+
logger.info('Processing item', { item });
28+
}
29+
});

Diff for: examples/snippets/parser/extend.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
22
import { Logger } from '@aws-lambda-powertools/logger';
33
import { parser } from '@aws-lambda-powertools/parser';
4-
import { EventBridgeSchema } from '@aws-lambda-powertools/parser/schemas';
4+
import { EventBridgeSchema } from '@aws-lambda-powertools/parser/schemas/eventbridge';
55
import type { Context } from 'aws-lambda';
66
import { z } from 'zod';
77

Diff for: examples/snippets/parser/extendDynamoDBStreamSchema.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { DynamoDBMarshalled } from '@aws-lambda-powertools/parser/helpers/dynamodb';
22
import {
3+
DynamoDBStreamChangeRecordBase,
34
DynamoDBStreamRecord,
45
DynamoDBStreamSchema,
56
} from '@aws-lambda-powertools/parser/schemas/dynamodb';
@@ -13,7 +14,7 @@ const customSchema = z.object({
1314
const extendedSchema = DynamoDBStreamSchema.extend({
1415
Records: z.array(
1516
DynamoDBStreamRecord.extend({
16-
dynamodb: z.object({
17+
dynamodb: DynamoDBStreamChangeRecordBase.extend({
1718
NewImage: DynamoDBMarshalled(customSchema).optional(),
1819
}),
1920
})

Diff for: examples/snippets/parser/handlerSafeParseDecorator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
22
import { Logger } from '@aws-lambda-powertools/logger';
33
import { parser } from '@aws-lambda-powertools/parser';
4-
import { EventBridgeEnvelope } from '@aws-lambda-powertools/parser/envelopes';
4+
import { EventBridgeEnvelope } from '@aws-lambda-powertools/parser/envelopes/eventbridge';
55
import type {
66
EventBridgeEvent,
77
ParsedResult,

Diff for: examples/snippets/parser/manual.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Logger } from '@aws-lambda-powertools/logger';
2-
import { EventBridgeEnvelope } from '@aws-lambda-powertools/parser/envelopes';
3-
import { EventBridgeSchema } from '@aws-lambda-powertools/parser/schemas';
2+
import { EventBridgeEnvelope } from '@aws-lambda-powertools/parser/envelopes/eventbridge';
3+
import { EventBridgeSchema } from '@aws-lambda-powertools/parser/schemas/eventbridge';
44
import type { EventBridgeEvent } from '@aws-lambda-powertools/parser/types';
55
import type { Context } from 'aws-lambda';
66
import { z } from 'zod';

Diff for: examples/snippets/parser/manualSafeParse.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Logger } from '@aws-lambda-powertools/logger';
2-
import { EventBridgeEnvelope } from '@aws-lambda-powertools/parser/envelopes';
3-
import { EventBridgeSchema } from '@aws-lambda-powertools/parser/schemas';
2+
import { EventBridgeEnvelope } from '@aws-lambda-powertools/parser/envelopes/eventbridge';
3+
import { EventBridgeSchema } from '@aws-lambda-powertools/parser/schemas/eventbridge';
44
import type { EventBridgeEvent } from '@aws-lambda-powertools/parser/types';
55
import type { Context } from 'aws-lambda';
66
import { z } from 'zod';

Diff for: examples/snippets/parser/middy.ts

+8-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Logger } from '@aws-lambda-powertools/logger';
22
import { parser } from '@aws-lambda-powertools/parser/middleware';
33
import middy from '@middy/core';
4-
import type { Context } from 'aws-lambda';
54
import { z } from 'zod';
65

76
const logger = new Logger();
@@ -19,18 +18,11 @@ const orderSchema = z.object({
1918
optionalField: z.string().optional(),
2019
});
2120

22-
type Order = z.infer<typeof orderSchema>;
23-
24-
const lambdaHandler = async (
25-
event: Order,
26-
_context: Context
27-
): Promise<void> => {
28-
for (const item of event.items) {
29-
// item is parsed as OrderItem
30-
logger.info('Processing item', { item });
31-
}
32-
};
33-
34-
export const handler = middy(lambdaHandler).use(
35-
parser({ schema: orderSchema })
36-
);
21+
export const handler = middy()
22+
.use(parser({ schema: orderSchema }))
23+
.handler(async (event): Promise<void> => {
24+
for (const item of event.items) {
25+
// item is parsed as OrderItem
26+
logger.info('Processing item', { item });
27+
}
28+
});

Diff for: examples/snippets/parser/safeParseDecorator.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
22
import { Logger } from '@aws-lambda-powertools/logger';
33
import { parser } from '@aws-lambda-powertools/parser';
4-
import { EventBridgeEnvelope } from '@aws-lambda-powertools/parser/envelopes';
4+
import { EventBridgeEnvelope } from '@aws-lambda-powertools/parser/envelopes/eventbridge';
55
import type {
66
EventBridgeEvent,
77
ParsedResult,
@@ -30,20 +30,19 @@ class Lambda implements LambdaInterface {
3030
@parser({
3131
schema: orderSchema,
3232
envelope: EventBridgeEnvelope,
33-
safeParse: true,
34-
}) // (1)!
33+
safeParse: true, // (1)!
34+
})
3535
public async handler(
3636
event: ParsedResult<EventBridgeEvent, Order>,
3737
_context: Context
3838
): Promise<void> {
3939
if (event.success) {
40-
// (2)!
4140
for (const item of event.data.items) {
42-
logger.info('Processing item', { item }); // (3)!
41+
logger.info('Processing item', { item }); // (2)!
4342
}
4443
} else {
45-
logger.error('Failed to parse event', event.error); // (4)!
46-
logger.error('Original event is: ', event.originalEvent); // (5)!
44+
logger.error('Failed to parse event', { error: event.error }); // (3)!
45+
logger.error('Original event is ', { original: event.originalEvent }); // (4)!
4746
}
4847
}
4948
}

Diff for: examples/snippets/parser/safeParseMiddy.ts

+13-24
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import { Logger } from '@aws-lambda-powertools/logger';
22
import { parser } from '@aws-lambda-powertools/parser/middleware';
3-
import type {
4-
EventBridgeEvent,
5-
ParsedResult,
6-
} from '@aws-lambda-powertools/parser/types';
73
import middy from '@middy/core';
8-
import type { Context } from 'aws-lambda';
94
import { z } from 'zod';
105

116
const logger = new Logger();
@@ -23,23 +18,17 @@ const orderSchema = z.object({
2318
optionalField: z.string().optional(),
2419
});
2520

26-
type Order = z.infer<typeof orderSchema>;
27-
28-
const lambdaHandler = async (
29-
event: ParsedResult<EventBridgeEvent, Order>,
30-
_context: Context
31-
): Promise<void> => {
32-
if (event.success) {
33-
// (2)!
34-
for (const item of event.data.items) {
35-
logger.info('Processing item', { item }); // (3)!
21+
export const handler = middy()
22+
.use(
23+
parser({ schema: orderSchema, safeParse: true }) // (1)!
24+
)
25+
.handler(async (event): Promise<void> => {
26+
if (event.success) {
27+
for (const item of event.data.items) {
28+
logger.info('Processing item', { item }); // (2)!
29+
}
30+
} else {
31+
logger.error('Error parsing event', { event: event.error }); // (3)!
32+
logger.error('Original event', { event: event.originalEvent }); // (4)!
3633
}
37-
} else {
38-
logger.error('Error parsing event', { event: event.error }); // (4)!
39-
logger.error('Original event', { event: event.originalEvent }); // (5)!
40-
}
41-
};
42-
43-
export const handler = middy(lambdaHandler).use(
44-
parser({ schema: orderSchema, safeParse: true }) // (1)!
45-
);
34+
});

Diff for: examples/snippets/parser/unitTestDecorator.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Context } from 'aws-lambda';
2+
import { describe, expect, it } from 'vitest';
23
import { handler } from './decorator.js';
34
import type { Order } from './schema.js';
45

Diff for: examples/snippets/parser/unitTestSafeParse.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {
33
ParsedResult,
44
} from '@aws-lambda-powertools/parser/types';
55
import type { Context } from 'aws-lambda';
6+
import { describe, expect, it } from 'vitest';
67
import { handler } from './safeParseDecorator.js';
78
import type { Order } from './schema.js';
89

0 commit comments

Comments
 (0)