|
| 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 | +}; |
0 commit comments