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