Skip to content

Commit 2554800

Browse files
authored
feat(parser): add schemas for AppSync Events (#3907)
1 parent 1451364 commit 2554800

File tree

9 files changed

+365
-43
lines changed

9 files changed

+365
-43
lines changed

docs/features/parser.md

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,24 +64,28 @@ Both are also able to parse either an object or JSON string as an input.
6464
| **APIGatewayRequestAuthorizerEventSchema** | Lambda Event Source payload for Amazon API Gateway Request Authorizer |
6565
| **APIGatewayTokenAuthorizerEventSchema** | Lambda Event Source payload for Amazon API Gateway Token Authorizer |
6666
| **APIGatewayProxyEventV2Schema** | Lambda Event Source payload for Amazon API Gateway v2 payload |
67-
| **APIGatewayProxyWebsocketEventSchema** | Lambda Event Source payload for Amazon API Gateway WebSocket events |
67+
| **APIGatewayProxyWebsocketEventSchema** | Lambda Event Source payload for Amazon API Gateway WebSocket events |
6868
| **APIGatewayRequestAuthorizerEventV2Schema** | Lambda Event Source payload for Amazon API Gateway v2 Authorizer |
69+
| **AppSyncResolverSchema** | Lambda Event Source payload for AWS AppSync GraphQL API resolver |
70+
| **AppSyncBatchResolverSchema** | Lambda Event Source payload for AWS AppSync GraphQL API batch resolver |
71+
| **AppSyncEventsPublishSchema** | Lambda Event Source payload for AWS AppSync Events API `PUBLISH` operation |
72+
| **AppSyncEventsSubscribeSchema** | Lambda Event Source payload for AWS AppSync Events API `SUBSCRIBE` operation |
6973
| **CloudFormationCustomResourceCreateSchema** | Lambda Event Source payload for AWS CloudFormation `CREATE` operation |
7074
| **CloudFormationCustomResourceUpdateSchema** | Lambda Event Source payload for AWS CloudFormation `UPDATE` operation |
7175
| **CloudFormationCustomResourceDeleteSchema** | Lambda Event Source payload for AWS CloudFormation `DELETE` operation |
7276
| **CloudwatchLogsSchema** | Lambda Event Source payload for Amazon CloudWatch Logs |
73-
| **PreSignupTriggerSchema** | Lambda Event Source payload for Amazon Cognito Pre Sign-up trigger |
74-
| **PostConfirmationTriggerSchema** | Lambda Event Source payload for Amazon Cognito Post Confirmation trigger |
75-
| **PreTokenGenerationTriggerSchema** | Lambda Event Source payload for Amazon Cognito Pre Token Generation trigger |
76-
| **CustomMessageTriggerSchema** | Lambda Event Source payload for Amazon Cognito Custom Message trigger |
77-
| **MigrateUserTriggerSchema** | Lambda Event Source payload for Amazon Cognito User Migration trigger |
78-
| **CustomSMSTriggerSchema** | Lambda Event Source payload for Amazon Cognito Custom SMS trigger |
79-
| **CustomEmailTriggerSchema** | Lambda Event Source payload for Amazon Cognito Custom Email trigger |
80-
| **DefineAuthChallengeTriggerSchema** | Lambda Event Source payload for Amazon Cognito Define Auth Challenge trigger |
81-
| **CreateAuthChallengeTriggerSchema** | Lambda Event Source payload for Amazon Cognito Create Auth Challenge trigger |
82-
| **VerifyAuthChallengeResponseTriggerSchema** | Lambda Event Source payload for Amazon Cognito Verify Auth Challenge Response trigger |
83-
| **PreTokenGenerationTriggerSchemaV1** | Lambda Event Source payload for Amazon Cognito Pre Token Generation trigger v1 |
84-
| **PreTokenGenerationTriggerSchemaV2AndV3** | Lambda Event Source payload for Amazon Cognito Pre Token Generation trigger v2 and v3 |
77+
| **PreSignupTriggerSchema** | Lambda Event Source payload for Amazon Cognito Pre Sign-up trigger |
78+
| **PostConfirmationTriggerSchema** | Lambda Event Source payload for Amazon Cognito Post Confirmation trigger |
79+
| **PreTokenGenerationTriggerSchema** | Lambda Event Source payload for Amazon Cognito Pre Token Generation trigger |
80+
| **CustomMessageTriggerSchema** | Lambda Event Source payload for Amazon Cognito Custom Message trigger |
81+
| **MigrateUserTriggerSchema** | Lambda Event Source payload for Amazon Cognito User Migration trigger |
82+
| **CustomSMSTriggerSchema** | Lambda Event Source payload for Amazon Cognito Custom SMS trigger |
83+
| **CustomEmailTriggerSchema** | Lambda Event Source payload for Amazon Cognito Custom Email trigger |
84+
| **DefineAuthChallengeTriggerSchema** | Lambda Event Source payload for Amazon Cognito Define Auth Challenge trigger |
85+
| **CreateAuthChallengeTriggerSchema** | Lambda Event Source payload for Amazon Cognito Create Auth Challenge trigger |
86+
| **VerifyAuthChallengeResponseTriggerSchema** | Lambda Event Source payload for Amazon Cognito Verify Auth Challenge Response trigger |
87+
| **PreTokenGenerationTriggerSchemaV1** | Lambda Event Source payload for Amazon Cognito Pre Token Generation trigger v1 |
88+
| **PreTokenGenerationTriggerSchemaV2AndV3** | Lambda Event Source payload for Amazon Cognito Pre Token Generation trigger v2 and v3 |
8589
| **DynamoDBStreamSchema** | Lambda Event Source payload for Amazon DynamoDB Streams |
8690
| **EventBridgeSchema** | Lambda Event Source payload for Amazon EventBridge |
8791
| **KafkaMskEventSchema** | Lambda Event Source payload for AWS MSK payload |
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
import { z } from 'zod';
2+
import {
3+
AppSyncCognitoIdentity,
4+
AppSyncIamIdentity,
5+
AppSyncOidcIdentity,
6+
} from './appsync-shared.js';
7+
8+
/**
9+
* A zod schema for the AppSync Events `identity` object when using an AWS Lambda Authorizer.
10+
*/
11+
const AppSyncLambdaAuthIdentity = z.object({
12+
handlerContext: z.record(z.string(), z.unknown()),
13+
});
14+
15+
/**
16+
* A zod schema for AppSync Events request object.
17+
*
18+
* This schema is used when extending subscribe and publish events.
19+
*/
20+
const AppSyncEventsRequestSchema = z.object({
21+
headers: z.record(z.string(), z.string()).optional(),
22+
domainName: z.string().nullable(),
23+
});
24+
25+
/**
26+
* A zod schema for AppSync Events info object.
27+
*
28+
* This schema is used when extending subscribe and publish events.
29+
*/
30+
const AppSyncEventsInfoSchema = z.object({
31+
channel: z.object({
32+
path: z.string(),
33+
segments: z.array(z.string()),
34+
}),
35+
channelNamespace: z.object({
36+
name: z.string(),
37+
}),
38+
operation: z.union([z.literal('PUBLISH'), z.literal('SUBSCRIBE')]),
39+
});
40+
41+
/**
42+
* A zod schema for AppSync Events base events.
43+
*
44+
* This schema is used as a base for both publish and subscribe events.
45+
*/
46+
const AppSyncEventsBaseSchema = z.object({
47+
identity: z.union([
48+
z.null(),
49+
AppSyncCognitoIdentity,
50+
AppSyncIamIdentity,
51+
AppSyncLambdaAuthIdentity,
52+
AppSyncOidcIdentity,
53+
]),
54+
result: z.null(),
55+
request: AppSyncEventsRequestSchema,
56+
info: AppSyncEventsInfoSchema,
57+
error: z.null(),
58+
prev: z.null(),
59+
stash: z.object({}),
60+
outErrors: z.array(z.unknown()),
61+
events: z.null(),
62+
});
63+
64+
/**
65+
* A zod schema for AppSync Events publish events.
66+
*
67+
* @example
68+
* ```json
69+
* {
70+
* "identity": null,
71+
* "result": null,
72+
* "request": {
73+
* "headers": {
74+
* "header1": "value1",
75+
* },
76+
* "domainName": "example.com"
77+
* },
78+
* "info": {
79+
* "channel": {
80+
* "path": "/default/foo",
81+
* "segments": ["default", "foo"]
82+
* },
83+
* "channelNamespace": {
84+
* "name": "default"
85+
* },
86+
* "operation": "PUBLISH"
87+
* },
88+
* "error": null,
89+
* "prev": null,
90+
* "stash": {},
91+
* "outErrors": [],
92+
* "events": [
93+
* {
94+
* "payload": {
95+
* "key": "value"
96+
* },
97+
* "id": "12345"
98+
* },
99+
* {
100+
* "payload": {
101+
* "key2": "value2"
102+
* },
103+
* "id": "67890"
104+
* }
105+
* ]
106+
* }
107+
* ```
108+
*/
109+
const AppSyncEventsPublishSchema = AppSyncEventsBaseSchema.extend({
110+
info: AppSyncEventsInfoSchema.extend({
111+
operation: z.literal('PUBLISH'),
112+
}),
113+
events: z
114+
.array(
115+
z.object({
116+
payload: z.record(z.string(), z.unknown()),
117+
id: z.string(),
118+
})
119+
)
120+
.min(1),
121+
});
122+
123+
/**
124+
* A zod schema for AppSync Events subscribe events.
125+
*
126+
* @example
127+
* ```json
128+
* {
129+
* "identity": null,
130+
* "result": null,
131+
* "request": {
132+
* "headers": {
133+
* "header1": "value1",
134+
* },
135+
* "domainName": "example.com"
136+
* },
137+
* "info": {
138+
* "channel": {
139+
* "path": "/default/foo",
140+
* "segments": ["default", "foo"]
141+
* },
142+
* "channelNamespace": {
143+
* "name": "default"
144+
* },
145+
* "operation": "SUBSCRIBE"
146+
* },
147+
* "error": null,
148+
* "prev": null,
149+
* "stash": {},
150+
* "outErrors": [],
151+
* "events": null,
152+
* }
153+
* ```
154+
*/
155+
const AppSyncEventsSubscribeSchema = AppSyncEventsBaseSchema.extend({
156+
info: AppSyncEventsInfoSchema.extend({
157+
operation: z.literal('SUBSCRIBE'),
158+
}),
159+
events: z.null(),
160+
});
161+
162+
export {
163+
AppSyncEventsBaseSchema,
164+
AppSyncCognitoIdentity,
165+
AppSyncIamIdentity,
166+
AppSyncLambdaAuthIdentity,
167+
AppSyncOidcIdentity,
168+
AppSyncEventsRequestSchema,
169+
AppSyncEventsInfoSchema,
170+
AppSyncEventsPublishSchema,
171+
AppSyncEventsSubscribeSchema,
172+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.record(z.string(), z.unknown()),
19+
sourceIp: z.array(z.string().ip()),
20+
defaultAuthStrategy: z.string().nullable(),
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+
export { AppSyncCognitoIdentity, AppSyncIamIdentity, AppSyncOidcIdentity };

packages/parser/src/schemas/appsync.ts

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,9 @@
11
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-
});
2+
import {
3+
AppSyncCognitoIdentity,
4+
AppSyncIamIdentity,
5+
AppSyncOidcIdentity,
6+
} from './appsync-shared.js';
297

308
const AppSyncLambdaIdentity = z.object({
319
resolverContext: z.any(),
@@ -251,4 +229,11 @@ const AppSyncResolverSchema = z.object({
251229

252230
const AppSyncBatchResolverSchema = z.array(AppSyncResolverSchema);
253231

254-
export { AppSyncResolverSchema, AppSyncBatchResolverSchema };
232+
export {
233+
AppSyncResolverSchema,
234+
AppSyncBatchResolverSchema,
235+
AppSyncCognitoIdentity,
236+
AppSyncIamIdentity,
237+
AppSyncOidcIdentity,
238+
AppSyncLambdaIdentity,
239+
};

packages/parser/src/schemas/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@ export {
55
APIGatewayTokenAuthorizerEventSchema,
66
APIGatewayEventRequestContextSchema,
77
} from './api-gateway.js';
8-
export { APIGatewayProxyWebsocketEventSchema } from './api-gateway-websocket.js'
8+
export { APIGatewayProxyWebsocketEventSchema } from './api-gateway-websocket.js';
99
export {
1010
AppSyncResolverSchema,
1111
AppSyncBatchResolverSchema,
1212
} from './appsync.js';
13+
export {
14+
AppSyncEventsBaseSchema,
15+
AppSyncEventsRequestSchema,
16+
AppSyncEventsInfoSchema,
17+
AppSyncEventsPublishSchema,
18+
AppSyncEventsSubscribeSchema,
19+
} from './appsync-events.js';
1320
export {
1421
APIGatewayProxyEventV2Schema,
1522
APIGatewayRequestAuthorizerEventV2Schema,

packages/parser/src/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ export type {
1919
APIGatewayTokenAuthorizerEvent,
2020
AppSyncBatchResolverEvent,
2121
AppSyncResolverEvent,
22+
AppSyncEventsPublishEvent,
23+
AppSyncEventsSubscribeEvent,
2224
CloudFormationCustomResourceCreateEvent,
2325
CloudFormationCustomResourceDeleteEvent,
2426
CloudFormationCustomResourceUpdateEvent,

packages/parser/src/types/schema.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import type {
1111
AlbMultiValueHeadersSchema,
1212
AlbSchema,
1313
AppSyncBatchResolverSchema,
14+
AppSyncEventsPublishSchema,
15+
AppSyncEventsSubscribeSchema,
1416
AppSyncResolverSchema,
1517
CloudFormationCustomResourceCreateSchema,
1618
CloudFormationCustomResourceDeleteSchema,
@@ -69,7 +71,9 @@ type APIGatewayEventRequestContext = z.infer<
6971

7072
type APIGatewayProxyEventV2 = z.infer<typeof APIGatewayProxyEventV2Schema>;
7173

72-
type APIGatewayProxyWebsocketEvent = z.infer<typeof APIGatewayProxyWebsocketEventSchema>;
74+
type APIGatewayProxyWebsocketEvent = z.infer<
75+
typeof APIGatewayProxyWebsocketEventSchema
76+
>;
7377

7478
type APIGatewayRequestAuthorizerV2 = z.infer<
7579
typeof APIGatewayRequestAuthorizerV2Schema
@@ -83,6 +87,10 @@ type AppSyncResolverEvent = z.infer<typeof AppSyncResolverSchema>;
8387

8488
type AppSyncBatchResolverEvent = z.infer<typeof AppSyncBatchResolverSchema>;
8589

90+
type AppSyncEventsPublishEvent = z.infer<typeof AppSyncEventsPublishSchema>;
91+
92+
type AppSyncEventsSubscribeEvent = z.infer<typeof AppSyncEventsSubscribeSchema>;
93+
8694
type CloudWatchLogEvent = z.infer<typeof CloudWatchLogEventSchema>;
8795

8896
type CloudWatchLogsDecode = z.infer<typeof CloudWatchLogsDecodeSchema>;
@@ -176,6 +184,8 @@ export type {
176184
APIGatewayTokenAuthorizerEvent,
177185
AppSyncBatchResolverEvent,
178186
AppSyncResolverEvent,
187+
AppSyncEventsPublishEvent,
188+
AppSyncEventsSubscribeEvent,
179189
CloudFormationCustomResourceCreateEvent,
180190
CloudFormationCustomResourceDeleteEvent,
181191
CloudFormationCustomResourceUpdateEvent,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"identity": null,
3+
"result": null,
4+
"request": {
5+
"headers": {
6+
"key": "value"
7+
},
8+
"domainName": null
9+
},
10+
"info": {
11+
"channel": {
12+
"path": "/request/channel",
13+
"segments": [
14+
"request",
15+
"channel"
16+
]
17+
},
18+
"channelNamespace": {
19+
"name": "request"
20+
},
21+
"operation": "PUBLISH"
22+
},
23+
"error": null,
24+
"prev": null,
25+
"stash": {},
26+
"outErrors": [],
27+
"events": null
28+
}

0 commit comments

Comments
 (0)