Skip to content

Commit bc745e4

Browse files
committed
feat(parser): add appsync resolver event Zod scemas
1 parent e73b575 commit bc745e4

15 files changed

+973
-15
lines changed

Diff for: packages/parser/package.json

+4-15
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,7 @@
182182
},
183183
"typesVersions": {
184184
"*": {
185-
"types": [
186-
"./lib/cjs/types/index.d.ts",
187-
"./lib/esm/types/index.d.ts"
188-
],
185+
"types": ["./lib/cjs/types/index.d.ts", "./lib/esm/types/index.d.ts"],
189186
"middleware": [
190187
"./lib/cjs/middleware/parser.d.ts",
191188
"./lib/esm/middleware/parser.d.ts"
@@ -238,10 +235,7 @@
238235
"./lib/cjs/schemas/lambda.d.ts",
239236
"./lib/esm/schemas/lambda.d.ts"
240237
],
241-
"schemas/s3": [
242-
"./lib/cjs/schemas/s3.d.ts",
243-
"./lib/esm/schemas/s3.d.ts"
244-
],
238+
"schemas/s3": ["./lib/cjs/schemas/s3.d.ts", "./lib/esm/schemas/s3.d.ts"],
245239
"schemas/ses": [
246240
"./lib/cjs/schemas/ses.d.ts",
247241
"./lib/esm/schemas/ses.d.ts"
@@ -318,17 +312,12 @@
318312
"./lib/cjs/envelopes/vpc-latticev2.d.ts",
319313
"./lib/esm/envelopes/vpc-latticev2.d.ts"
320314
],
321-
"helpers": [
322-
"./lib/cjs/helpers.d.ts",
323-
"./lib/esm/helpers.d.ts"
324-
]
315+
"helpers": ["./lib/cjs/helpers.d.ts", "./lib/esm/helpers.d.ts"]
325316
}
326317
},
327318
"main": "./lib/cjs/index.js",
328319
"types": "./lib/cjs/index.d.ts",
329-
"files": [
330-
"lib"
331-
],
320+
"files": ["lib"],
332321
"repository": {
333322
"type": "git",
334323
"url": "git+https://github.com/aws-powertools/powertools-lambda-typescript.git"

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

+253
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
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+
headers: z.record(z.string()),
114+
}),
115+
info: z.object({
116+
selectionSetList: z.array(z.string()),
117+
selectionSetGraphQL: z.string(),
118+
parentTypeName: z.string(),
119+
fieldName: z.string(),
120+
variables: z.record(z.any()),
121+
}),
122+
prev: z
123+
.object({
124+
result: z.record(z.any()),
125+
})
126+
.nullable(),
127+
stash: z.record(z.any()),
128+
});
129+
130+
/**
131+
* A zod schema for a batch AppSync resolver event
132+
*
133+
* @example
134+
* /*
135+
* [{
136+
* "arguments": {
137+
* "id": "1973493"
138+
* },
139+
* "source": null,
140+
* "identity": {
141+
* "accountId": "012345678901",
142+
* "cognitoIdentityAuthProvider": "cognitoIdentityAuthProvider",
143+
* "cognitoIdentityAuthType": "cognitoIdentityAuthType",
144+
* "cognitoIdentityId": "cognitoIdentityId",
145+
* "cognitoIdentityPoolId": "cognitoIdentityPoolId",
146+
* "sourceIp": ["10.10.10.10"],
147+
* "userArn": "arn:aws:sts::012345678901:assumed-role/role",
148+
* "username": "AROAXYKJUOW6FHGUSK5FA:username"
149+
* },
150+
* "request": {
151+
* "headers": {
152+
* "x-forwarded-for": "1.1.1.1, 2.2.2.2",
153+
* "cloudfront-viewer-country": "US",
154+
* "cloudfront-is-tablet-viewer": "false",
155+
* "via": "2.0 xxxxxxxxxxxxxxxx.cloudfront.net (CloudFront)",
156+
* "cloudfront-forwarded-proto": "https",
157+
* "origin": "https://us-west-1.console.aws.amazon.com",
158+
* "content-length": "217",
159+
* "accept-language": "en-US,en;q=0.9",
160+
* "host": "xxxxxxxxxxxxxxxx.appsync-api.us-west-1.amazonaws.com",
161+
* "x-forwarded-proto": "https",
162+
* "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",
163+
* "accept": "*!/!*",
164+
* "cloudfront-is-mobile-viewer": "false",
165+
* "cloudfront-is-smarttv-viewer": "false",
166+
* "accept-encoding": "gzip, deflate, br",
167+
* "referer": "https://us-west-1.console.aws.amazon.com/appsync/home?region=us-west-1",
168+
* "content-type": "application/json",
169+
* "sec-fetch-mode": "cors",
170+
* "x-amz-cf-id": "3aykhqlUwQeANU-HGY7E_guV5EkNeMMtwyOgiA==",
171+
* "x-amzn-trace-id": "Root=1-5f512f51-fac632066c5e848ae714",
172+
* "authorization": "eyJraWQiOiJScWFCSlJqYVJlM0hrSnBTUFpIcVRXazNOW...",
173+
* "sec-fetch-dest": "empty",
174+
* "x-amz-user-agent": "AWS-Console-AppSync/",
175+
* "cloudfront-is-desktop-viewer": "true",
176+
* "sec-fetch-site": "cross-site",
177+
* "x-forwarded-port": "443"
178+
* }
179+
* },
180+
* "prev": {
181+
* "result": {}
182+
* },
183+
* "info": {
184+
* "selectionSetList": ["id", "field1", "field2"],
185+
* "selectionSetGraphQL": "{\n id\n field1\n field2\n}",
186+
* "parentTypeName": "Mutation",
187+
* "fieldName": "createSomething",
188+
* "variables": {}
189+
* },
190+
* "stash": {}
191+
* },
192+
* {
193+
* "arguments": {
194+
* "id": "1987311"
195+
* },
196+
* "source": null,
197+
* "identity": {
198+
* "claims": {
199+
* "sub": "sub"
200+
* },
201+
* "issuer": "issuer",
202+
* "sub": "sub
203+
* },
204+
* "request": {
205+
* "headers": {
206+
* "x-forwarded-for": "1.1.1.1, 2.2.2.2",
207+
* "cloudfront-viewer-country": "US",
208+
* "cloudfront-is-tablet-viewer": "false",
209+
* "via": "2.0 xxxxxxxxxxxxxxxx.cloudfront.net (CloudFront)",
210+
* "cloudfront-forwarded-proto": "https",
211+
* "origin": "https://us-west-1.console.aws.amazon.com",
212+
* "content-length": "217",
213+
* "accept-language": "en-US,en;q=0.9",
214+
* "host": "xxxxxxxxxxxxxxxx.appsync-api.us-west-1.amazonaws.com",
215+
* "x-forwarded-proto": "https",
216+
* "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",
217+
* "accept": "*!/!*",
218+
* "cloudfront-is-mobile-viewer": "false",
219+
* "cloudfront-is-smarttv-viewer": "false",
220+
* "accept-encoding": "gzip, deflate, br",
221+
* "referer": "https://us-west-1.console.aws.amazon.com/appsync/home?region=us-west-1",
222+
* "content-type": "application/json",
223+
* "sec-fetch-mode": "cors",
224+
* "x-amz-cf-id": "3aykhqlUwQeANU-HGY7E_guV5EkNeMMtwyOgiA==",
225+
* "x-amzn-trace-id": "Root=1-5f512f51-fac632066c5e848ae714",
226+
* "authorization": "eyJraWQiOiJScWFCSlJqYVJlM0hrSnBTUFpIcVRXazNOW...",
227+
* "sec-fetch-dest": "empty",
228+
* "x-amz-user-agent": "AWS-Console-AppSync/",
229+
* "cloudfront-is-desktop-viewer": "true",
230+
* "sec-fetch-site": "cross-site",
231+
* "x-forwarded-port": "443"
232+
* }
233+
* },
234+
* "prev": {
235+
* "result": {}
236+
* },
237+
* "info": {
238+
* "selectionSetList": ["id", "field1", "field2"],
239+
* "selectionSetGraphQL": "{\n id\n field1\n field2\n}",
240+
* "parentTypeName": "Mutation",
241+
* "fieldName": "createSomething",
242+
* "variables": {}
243+
* },
244+
* "stash": {}
245+
* }]
246+
* ```
247+
*
248+
* @see {@link https://docs.aws.amazon.com/appsync/latest/devguide/tutorial-lambda-resolvers.html#advanced-use-case-batching}
249+
*/
250+
251+
const AppSyncBatchResolverSchema = z.array(AppSyncResolverSchema);
252+
253+
export { AppSyncResolverSchema, AppSyncBatchResolverSchema };

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

+10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import type { z } from 'zod';
2+
import type {
3+
AppSyncBatchResolverSchema,
4+
AppSyncResolverSchema,
5+
} from '../schemas/appsync';
26
import type {
37
APIGatewayProxyEventSchema,
48
APIGatewayProxyEventV2Schema,
@@ -53,6 +57,10 @@ type APIGatewayRequestContextV2 = z.infer<
5357
typeof APIGatewayRequestContextV2Schema
5458
>;
5559

60+
type AppSyncResolverEvent = z.infer<typeof AppSyncResolverSchema>;
61+
62+
type AppSyncBatchResolverEvent = z.infer<typeof AppSyncBatchResolverSchema>;
63+
5664
type CloudFormationCustomResourceCreateEvent = z.infer<
5765
typeof CloudFormationCustomResourceCreateSchema
5866
>;
@@ -126,6 +134,8 @@ export type {
126134
APIGatewayProxyEventV2,
127135
APIGatewayRequestAuthorizerV2,
128136
APIGatewayRequestContextV2,
137+
AppSyncResolverEvent,
138+
AppSyncBatchResolverEvent,
129139
CloudFormationCustomResourceCreateEvent,
130140
CloudFormationCustomResourceDeleteEvent,
131141
CloudFormationCustomResourceUpdateEvent,

0 commit comments

Comments
 (0)