Skip to content

Commit c49e4f7

Browse files
committed
docs: add parser example for built-in extension, remove event-handler missing code example reference
1 parent a9d4a9c commit c49e4f7

File tree

4 files changed

+149
-13
lines changed

4 files changed

+149
-13
lines changed

Diff for: docs/core/event-handler/api-gateway.md

+1-13
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,5 @@ This is the sample infrastructure for API Gateway and Lambda Function URLs we ar
3737

3838
???+ info "There is no additional permissions or dependencies required to use this utility."
3939

40-
=== "API Gateway SAM Template"
41-
42-
```yaml title="AWS Serverless Application Model (SAM) example"
43-
--8<-- "examples/snippets/event-handler/rest/templates/template.yaml"
44-
```
45-
46-
=== "Lambda Function URL SAM Template"
47-
48-
```yaml title="AWS Serverless Application Model (SAM) example"
49-
--8<-- "examples/event_handler_lambda_function_url/sam/template.yaml"
50-
```
51-
5240
<!-- remove line below while editing this doc & put it back until the doc has reached its first draft -->
53-
<!-- markdownlint-disable MD043 -->
41+
<!-- markdownlint-disable MD043 -->

Diff for: docs/utilities/parser.md

+17
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,23 @@ You can extend every built-in schema to include your own schema, and yet have al
109109
--8<-- "examples/snippets/parser/examplePayload.json"
110110
```
111111

112+
For scenarios where you have a stringiied JSON payload, you can extend the built-in schema using `.transform()` and `.pipe()` method.
113+
114+
=== "APIGatewayProxyEventSchema"
115+
116+
```typescript hl_lines="24-34"
117+
--8<-- "examples/snippets/parser/extendAPIGatewaySchema.ts"
118+
```
119+
120+
1. parse the `body` inside `transform` method
121+
2. chain your custom schema to `pipe` operation
122+
123+
=== "Example Payload for API Gateway Event"
124+
125+
```json
126+
--8<-- "examples/snippets/parser/exampleAPIGatewayPayload.json"
127+
```
128+
112129
## Envelopes
113130

114131
When trying to parse your payload you might encounter the following situations:
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"version": "1.0",
3+
"resource": "/my/path",
4+
"path": "/my/path",
5+
"httpMethod": "GET",
6+
"headers": {
7+
"Header1": "value1",
8+
"Header2": "value2",
9+
"Origin": "https://aws.amazon.com"
10+
},
11+
"multiValueHeaders": {
12+
"Header1": [
13+
"value1"
14+
],
15+
"Header2": [
16+
"value1",
17+
"value2"
18+
]
19+
},
20+
"queryStringParameters": {
21+
"parameter1": "value1",
22+
"parameter2": "value"
23+
},
24+
"multiValueQueryStringParameters": {
25+
"parameter1": [
26+
"value1",
27+
"value2"
28+
],
29+
"parameter2": [
30+
"value"
31+
]
32+
},
33+
"requestContext": {
34+
"accountId": "123456789012",
35+
"apiId": "id",
36+
"authorizer": {
37+
"claims": null,
38+
"scopes": null
39+
},
40+
"domainName": "id.execute-api.us-east-1.amazonaws.com",
41+
"domainPrefix": "id",
42+
"extendedRequestId": "request-id",
43+
"httpMethod": "GET",
44+
"identity": {
45+
"accessKey": null,
46+
"accountId": null,
47+
"caller": null,
48+
"cognitoAuthenticationProvider": null,
49+
"cognitoAuthenticationType": null,
50+
"cognitoIdentityId": null,
51+
"cognitoIdentityPoolId": null,
52+
"principalOrgId": null,
53+
"sourceIp": "192.168.0.1",
54+
"user": null,
55+
"userAgent": "user-agent",
56+
"userArn": null,
57+
"clientCert": {
58+
"clientCertPem": "CERT_CONTENT",
59+
"subjectDN": "www.example.com",
60+
"issuerDN": "Example issuer",
61+
"serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1",
62+
"validity": {
63+
"notBefore": "May 28 12:30:02 2019 GMT",
64+
"notAfter": "Aug 5 09:36:04 2021 GMT"
65+
}
66+
}
67+
},
68+
"path": "/my/path",
69+
"protocol": "HTTP/1.1",
70+
"requestId": "id=",
71+
"requestTime": "04/Mar/2020:19:15:17 +0000",
72+
"requestTimeEpoch": 1583349317135,
73+
"resourceId": null,
74+
"resourcePath": "/my/path",
75+
"stage": "$default"
76+
},
77+
"pathParameters": null,
78+
"stageVariables": null,
79+
"body": "{\"id\":10876546789,\"description\":\"My order\",\"items\":[{\"id\":1015938732,\"quantity\":1,\"description\":\"item xpto\"}]}",
80+
"isBase64Encoded": false
81+
}

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

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
2+
import { Logger } from '@aws-lambda-powertools/logger';
3+
import { parser } from '@aws-lambda-powertools/parser';
4+
import { APIGatewayProxyEventSchema } from '@aws-lambda-powertools/parser/schemas';
5+
import type { Context } from 'aws-lambda';
6+
import { z } from 'zod';
7+
8+
const logger = new Logger();
9+
10+
const orderSchema = z.object({
11+
id: z.number().positive(),
12+
description: z.string(),
13+
items: z.array(
14+
z.object({
15+
id: z.number().positive(),
16+
quantity: z.number(),
17+
description: z.string(),
18+
})
19+
),
20+
});
21+
22+
const orderEventSchema = APIGatewayProxyEventSchema.extend({
23+
body: z
24+
.string()
25+
.transform((str, ctx) => {
26+
try {
27+
return JSON.parse(str); // (1)!
28+
} catch (err) {
29+
ctx.addIssue({
30+
code: 'custom',
31+
message: 'Invalid JSON',
32+
});
33+
}
34+
})
35+
.pipe(orderSchema), // (2)!
36+
});
37+
38+
type OrderEvent = z.infer<typeof orderEventSchema>;
39+
40+
class Lambda implements LambdaInterface {
41+
@parser({ schema: orderEventSchema })
42+
public async handler(event: OrderEvent, _context: Context): Promise<void> {
43+
for (const item of event.body.items) {
44+
// process OrderItem
45+
logger.info('Processing item', { item });
46+
}
47+
}
48+
}
49+
const myFunction = new Lambda();
50+
export const handler = myFunction.handler.bind(myFunction);

0 commit comments

Comments
 (0)