Skip to content

Commit 318f34b

Browse files
svozzadreamorosi
andauthored
feat(parser): Add appsync resolver event Zod schemas (#3301)
Co-authored-by: Andrea Amorosi <[email protected]>
1 parent 0a270b9 commit 318f34b

17 files changed

+1051
-7
lines changed

Diff for: packages/parser/package.json

+10-7
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@
6060
"require": "./lib/cjs/schemas/apigwv2.js",
6161
"import": "./lib/esm/schemas/apigwv2.js"
6262
},
63+
"./schemas/appsync": {
64+
"require": "./lib/cjs/schemas/appsync.js",
65+
"import": "./lib/esm/schemas/appsync.js"
66+
},
6367
"./schemas/cloudformation-custom-resources": {
6468
"require": "./lib/cjs/schemas/cloudformation-custom-resources.js",
6569
"import": "./lib/esm/schemas/cloudformation-custom-resources.js"
@@ -183,10 +187,7 @@
183187
},
184188
"typesVersions": {
185189
"*": {
186-
"types": [
187-
"./lib/cjs/types/index.d.ts",
188-
"./lib/esm/types/index.d.ts"
189-
],
190+
"types": ["./lib/cjs/types/index.d.ts", "./lib/esm/types/index.d.ts"],
190191
"middleware": [
191192
"./lib/cjs/middleware/parser.d.ts",
192193
"./lib/esm/middleware/parser.d.ts"
@@ -207,6 +208,10 @@
207208
"./lib/cjs/schemas/apigwv2.d.ts",
208209
"./lib/esm/schemas/apigwv2.d.ts"
209210
],
211+
"schemas/appsync": [
212+
"./lib/cjs/schemas/appsync.d.ts",
213+
"./lib/esm/schemas/appsync.d.ts"
214+
],
210215
"schemas/cloudformation-custom-resources": [
211216
"./lib/cjs/schemas/cloudformation-custom-resources.d.ts",
212217
"./lib/esm/schemas/cloudformation-custom-resources.d.ts"
@@ -327,9 +332,7 @@
327332
},
328333
"main": "./lib/cjs/index.js",
329334
"types": "./lib/cjs/index.d.ts",
330-
"files": [
331-
"lib"
332-
],
335+
"files": ["lib"],
333336
"repository": {
334337
"type": "git",
335338
"url": "git+https://github.com/aws-powertools/powertools-lambda-typescript.git"

Diff for: packages/parser/src/schemas/appsync.ts

+254
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
import { z } from 'zod';
2+
3+
const AppSyncIamIdentity = z.object({
4+
accountId: z.string(),
5+
cognitoIdentityPoolId: z.string().nullable(),
6+
cognitoIdentityId: z.string().nullable(),
7+
sourceIp: z.array(z.string()),
8+
username: z.string(),
9+
userArn: z.string(),
10+
cognitoIdentityAuthType: z.string().nullable(),
11+
cognitoIdentityAuthProvider: z.string().nullable(),
12+
});
13+
14+
const AppSyncCognitoIdentity = z.object({
15+
sub: z.string(),
16+
issuer: z.string(),
17+
username: z.string(),
18+
claims: z.any(),
19+
sourceIp: z.array(z.string()),
20+
defaultAuthStrategy: z.string(),
21+
groups: z.array(z.string()).nullable(),
22+
});
23+
24+
const AppSyncOidcIdentity = z.object({
25+
claims: z.any(),
26+
issuer: z.string(),
27+
sub: z.string(),
28+
});
29+
30+
const AppSyncLambdaIdentity = z.object({
31+
resolverContext: z.any(),
32+
});
33+
34+
const AppSyncIdentity = z.union([
35+
AppSyncCognitoIdentity,
36+
AppSyncIamIdentity,
37+
AppSyncOidcIdentity,
38+
AppSyncLambdaIdentity,
39+
]);
40+
41+
/**
42+
* A zod schema for an AppSync resolver event
43+
*
44+
* @example
45+
* ```json
46+
* {
47+
* "arguments": {
48+
* "id": "1973493"
49+
* },
50+
* "source": null,
51+
* "identity": {
52+
* "accountId": "012345678901",
53+
* "cognitoIdentityAuthProvider": null,
54+
* "cognitoIdentityAuthType": null,
55+
* "cognitoIdentityId": null,
56+
* "cognitoIdentityPoolId": null,
57+
* "sourceIp": ["10.10.10.10"],
58+
* "userArn": "arn:aws:sts::012345678901:assumed-role/role",
59+
* "username": "AROAXYKJUOW6FHGUSK5FA:username"
60+
* },
61+
* "request": {
62+
* "headers": {
63+
* "x-forwarded-for": "1.1.1.1, 2.2.2.2",
64+
* "cloudfront-viewer-country": "US",
65+
* "cloudfront-is-tablet-viewer": "false",
66+
* "via": "2.0 xxxxxxxxxxxxxxxx.cloudfront.net (CloudFront)",
67+
* "cloudfront-forwarded-proto": "https",
68+
* "origin": "https://us-west-1.console.aws.amazon.com",
69+
* "content-length": "217",
70+
* "accept-language": "en-US,en;q=0.9",
71+
* "host": "xxxxxxxxxxxxxxxx.appsync-api.us-west-1.amazonaws.com",
72+
* "x-forwarded-proto": "https",
73+
* "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36",
74+
* "accept": "*!/!*",
75+
* "cloudfront-is-mobile-viewer": "false",
76+
* "cloudfront-is-smarttv-viewer": "false",
77+
* "accept-encoding": "gzip, deflate, br",
78+
* "referer": "https://us-west-1.console.aws.amazon.com/appsync/home?region=us-west-1",
79+
* "content-type": "application/json",
80+
* "sec-fetch-mode": "cors",
81+
* "x-amz-cf-id": "3aykhqlUwQeANU-HGY7E_guV5EkNeMMtwyOgiA==",
82+
* "x-amzn-trace-id": "Root=1-5f512f51-fac632066c5e848ae714",
83+
* "authorization": "eyJraWQiOiJScWFCSlJqYVJlM0hrSnBTUFpIcVRXazNOW...",
84+
* "sec-fetch-dest": "empty",
85+
* "x-amz-user-agent": "AWS-Console-AppSync/",
86+
* "cloudfront-is-desktop-viewer": "true",
87+
* "sec-fetch-site": "cross-site",
88+
* "x-forwarded-port": "443"
89+
* }
90+
* },
91+
* "prev": {
92+
* "result": {}
93+
* },
94+
* "info": {
95+
* "selectionSetList": ["id", "field1", "field2"],
96+
* "selectionSetGraphQL": "{\n id\n field1\n field2\n}",
97+
* "parentTypeName": "Mutation",
98+
* "fieldName": "createSomething",
99+
* "variables": {}
100+
* },
101+
* "stash": {}
102+
* }
103+
* ```
104+
*
105+
* @see {@link https://docs.aws.amazon.com/appsync/latest/devguide/resolver-context-reference-js.html}
106+
*/
107+
108+
const AppSyncResolverSchema = z.object({
109+
arguments: z.record(z.any()),
110+
identity: z.optional(AppSyncIdentity),
111+
source: z.record(z.any()).nullable(),
112+
request: z.object({
113+
domainName: z.string().nullable(),
114+
headers: z.record(z.string()),
115+
}),
116+
info: z.object({
117+
selectionSetList: z.array(z.string()),
118+
selectionSetGraphQL: z.string(),
119+
parentTypeName: z.string(),
120+
fieldName: z.string(),
121+
variables: z.record(z.any()),
122+
}),
123+
prev: z
124+
.object({
125+
result: z.record(z.any()),
126+
})
127+
.nullable(),
128+
stash: z.record(z.any()),
129+
});
130+
131+
/**
132+
* A zod schema for a batch AppSync resolver event
133+
*
134+
* @example
135+
* ```json
136+
* [{
137+
* "arguments": {
138+
* "id": "1973493"
139+
* },
140+
* "source": null,
141+
* "identity": {
142+
* "accountId": "012345678901",
143+
* "cognitoIdentityAuthProvider": "cognitoIdentityAuthProvider",
144+
* "cognitoIdentityAuthType": "cognitoIdentityAuthType",
145+
* "cognitoIdentityId": "cognitoIdentityId",
146+
* "cognitoIdentityPoolId": "cognitoIdentityPoolId",
147+
* "sourceIp": ["10.10.10.10"],
148+
* "userArn": "arn:aws:sts::012345678901:assumed-role/role",
149+
* "username": "AROAXYKJUOW6FHGUSK5FA:username"
150+
* },
151+
* "request": {
152+
* "headers": {
153+
* "x-forwarded-for": "1.1.1.1, 2.2.2.2",
154+
* "cloudfront-viewer-country": "US",
155+
* "cloudfront-is-tablet-viewer": "false",
156+
* "via": "2.0 xxxxxxxxxxxxxxxx.cloudfront.net (CloudFront)",
157+
* "cloudfront-forwarded-proto": "https",
158+
* "origin": "https://us-west-1.console.aws.amazon.com",
159+
* "content-length": "217",
160+
* "accept-language": "en-US,en;q=0.9",
161+
* "host": "xxxxxxxxxxxxxxxx.appsync-api.us-west-1.amazonaws.com",
162+
* "x-forwarded-proto": "https",
163+
* "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36",
164+
* "accept": "*!/!*",
165+
* "cloudfront-is-mobile-viewer": "false",
166+
* "cloudfront-is-smarttv-viewer": "false",
167+
* "accept-encoding": "gzip, deflate, br",
168+
* "referer": "https://us-west-1.console.aws.amazon.com/appsync/home?region=us-west-1",
169+
* "content-type": "application/json",
170+
* "sec-fetch-mode": "cors",
171+
* "x-amz-cf-id": "3aykhqlUwQeANU-HGY7E_guV5EkNeMMtwyOgiA==",
172+
* "x-amzn-trace-id": "Root=1-5f512f51-fac632066c5e848ae714",
173+
* "authorization": "eyJraWQiOiJScWFCSlJqYVJlM0hrSnBTUFpIcVRXazNOW...",
174+
* "sec-fetch-dest": "empty",
175+
* "x-amz-user-agent": "AWS-Console-AppSync/",
176+
* "cloudfront-is-desktop-viewer": "true",
177+
* "sec-fetch-site": "cross-site",
178+
* "x-forwarded-port": "443"
179+
* }
180+
* },
181+
* "prev": {
182+
* "result": {}
183+
* },
184+
* "info": {
185+
* "selectionSetList": ["id", "field1", "field2"],
186+
* "selectionSetGraphQL": "{\n id\n field1\n field2\n}",
187+
* "parentTypeName": "Mutation",
188+
* "fieldName": "createSomething",
189+
* "variables": {}
190+
* },
191+
* "stash": {}
192+
* },
193+
* {
194+
* "arguments": {
195+
* "id": "1987311"
196+
* },
197+
* "source": null,
198+
* "identity": {
199+
* "claims": {
200+
* "sub": "sub"
201+
* },
202+
* "issuer": "issuer",
203+
* "sub": "sub
204+
* },
205+
* "request": {
206+
* "headers": {
207+
* "x-forwarded-for": "1.1.1.1, 2.2.2.2",
208+
* "cloudfront-viewer-country": "US",
209+
* "cloudfront-is-tablet-viewer": "false",
210+
* "via": "2.0 xxxxxxxxxxxxxxxx.cloudfront.net (CloudFront)",
211+
* "cloudfront-forwarded-proto": "https",
212+
* "origin": "https://us-west-1.console.aws.amazon.com",
213+
* "content-length": "217",
214+
* "accept-language": "en-US,en;q=0.9",
215+
* "host": "xxxxxxxxxxxxxxxx.appsync-api.us-west-1.amazonaws.com",
216+
* "x-forwarded-proto": "https",
217+
* "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36",
218+
* "accept": "*!/!*",
219+
* "cloudfront-is-mobile-viewer": "false",
220+
* "cloudfront-is-smarttv-viewer": "false",
221+
* "accept-encoding": "gzip, deflate, br",
222+
* "referer": "https://us-west-1.console.aws.amazon.com/appsync/home?region=us-west-1",
223+
* "content-type": "application/json",
224+
* "sec-fetch-mode": "cors",
225+
* "x-amz-cf-id": "3aykhqlUwQeANU-HGY7E_guV5EkNeMMtwyOgiA==",
226+
* "x-amzn-trace-id": "Root=1-5f512f51-fac632066c5e848ae714",
227+
* "authorization": "eyJraWQiOiJScWFCSlJqYVJlM0hrSnBTUFpIcVRXazNOW...",
228+
* "sec-fetch-dest": "empty",
229+
* "x-amz-user-agent": "AWS-Console-AppSync/",
230+
* "cloudfront-is-desktop-viewer": "true",
231+
* "sec-fetch-site": "cross-site",
232+
* "x-forwarded-port": "443"
233+
* }
234+
* },
235+
* "prev": {
236+
* "result": {}
237+
* },
238+
* "info": {
239+
* "selectionSetList": ["id", "field1", "field2"],
240+
* "selectionSetGraphQL": "{\n id\n field1\n field2\n}",
241+
* "parentTypeName": "Mutation",
242+
* "fieldName": "createSomething",
243+
* "variables": {}
244+
* },
245+
* "stash": {}
246+
* }]
247+
* ```
248+
*
249+
* @see {@link https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-lambda-resolvers.html#advanced-use-case-batching}
250+
*/
251+
252+
const AppSyncBatchResolverSchema = z.array(AppSyncResolverSchema);
253+
254+
export { AppSyncResolverSchema, AppSyncBatchResolverSchema };

Diff for: packages/parser/src/schemas/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ export {
44
APIGatewayRequestAuthorizerEventSchema,
55
APIGatewayTokenAuthorizerEventSchema,
66
} from './apigw.js';
7+
export {
8+
AppSyncResolverSchema,
9+
AppSyncBatchResolverSchema,
10+
} from './appsync.js';
711
export {
812
APIGatewayProxyEventV2Schema,
913
APIGatewayRequestAuthorizerEventV2Schema,

Diff for: packages/parser/src/types/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ export type {
1313
APIGatewayProxyEventV2,
1414
APIGatewayRequestContextV2,
1515
APIGatewayRequestAuthorizerV2,
16+
AppSyncResolverEvent,
17+
AppSyncBatchResolverEvent,
1618
S3Event,
1719
S3EventNotificationEventBridge,
1820
S3SqsEventNotification,

Diff for: packages/parser/src/types/schema.ts

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import type {
66
APIGatewayRequestContextV2Schema,
77
AlbMultiValueHeadersSchema,
88
AlbSchema,
9+
AppSyncBatchResolverSchema,
10+
AppSyncResolverSchema,
911
CloudFormationCustomResourceCreateSchema,
1012
CloudFormationCustomResourceDeleteSchema,
1113
CloudFormationCustomResourceUpdateSchema,
@@ -55,6 +57,10 @@ type APIGatewayRequestContextV2 = z.infer<
5557
typeof APIGatewayRequestContextV2Schema
5658
>;
5759

60+
type AppSyncResolverEvent = z.infer<typeof AppSyncResolverSchema>;
61+
62+
type AppSyncBatchResolverEvent = z.infer<typeof AppSyncBatchResolverSchema>;
63+
5864
type CloudFormationCustomResourceCreateEvent = z.infer<
5965
typeof CloudFormationCustomResourceCreateSchema
6066
>;
@@ -134,6 +140,8 @@ export type {
134140
APIGatewayProxyEventV2,
135141
APIGatewayRequestAuthorizerV2,
136142
APIGatewayRequestContextV2,
143+
AppSyncResolverEvent,
144+
AppSyncBatchResolverEvent,
137145
CloudFormationCustomResourceCreateEvent,
138146
CloudFormationCustomResourceDeleteEvent,
139147
CloudFormationCustomResourceUpdateEvent,

0 commit comments

Comments
 (0)