|
1 | 1 | import { z } from 'zod';
|
| 2 | +import { |
| 3 | + APIGatewayCert, |
| 4 | + APIGatewayRecord, |
| 5 | + APIGatewayStringArray, |
| 6 | + APIGatewayHttpMethod, |
| 7 | +} from './apigw-proxy.js'; |
2 | 8 |
|
3 |
| -const APIGatewayCert = z.object({ |
4 |
| - clientCertPem: z.string(), |
5 |
| - subjectDN: z.string(), |
6 |
| - issuerDN: z.string(), |
7 |
| - serialNumber: z.string(), |
8 |
| - validity: z.object({ |
9 |
| - notBefore: z.string(), |
10 |
| - notAfter: z.string(), |
11 |
| - }), |
12 |
| -}); |
13 |
| - |
| 9 | +/** |
| 10 | + * A zod schema for an API Gateway Event Identity |
| 11 | + * |
| 12 | + * @see {@link https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html} |
| 13 | + */ |
14 | 14 | const APIGatewayEventIdentity = z.object({
|
15 | 15 | accessKey: z.string().nullish(),
|
16 | 16 | accountId: z.string().nullish(),
|
@@ -38,15 +38,27 @@ const APIGatewayEventIdentity = z.object({
|
38 | 38 | clientCert: APIGatewayCert.nullish(),
|
39 | 39 | });
|
40 | 40 |
|
| 41 | +/** |
| 42 | + * A zod schema for an API Gateway Event Request Context |
| 43 | + * |
| 44 | + * @see {@link https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#context-variable-reference} |
| 45 | + */ |
41 | 46 | const APIGatewayEventRequestContext = z
|
42 | 47 | .object({
|
43 | 48 | accountId: z.string(),
|
44 | 49 | apiId: z.string(),
|
| 50 | + deploymentId: z.string().nullish(), |
45 | 51 | authorizer: z
|
46 |
| - .object({ |
47 |
| - claims: z.record(z.string(), z.any()).nullish(), |
48 |
| - scopes: z.array(z.string()).nullish(), |
49 |
| - }) |
| 52 | + .union([ |
| 53 | + z.object({ |
| 54 | + integrationLatency: z.number(), |
| 55 | + principalId: z.string(), |
| 56 | + }), |
| 57 | + z.object({ |
| 58 | + claims: z.record(z.string(), z.any()), |
| 59 | + scopes: APIGatewayStringArray.optional(), |
| 60 | + }), |
| 61 | + ]) |
50 | 62 | .nullish(),
|
51 | 63 | stage: z.string(),
|
52 | 64 | protocol: z.string(),
|
@@ -88,32 +100,165 @@ const APIGatewayEventRequestContext = z
|
88 | 100 | }
|
89 | 101 | );
|
90 | 102 |
|
| 103 | +/** |
| 104 | + * A zod schema for an API Gateway Proxy event |
| 105 | + * |
| 106 | + * @example |
| 107 | + * ```json |
| 108 | + * { |
| 109 | + * "type": "REQUEST", |
| 110 | + * "methodArn": "arn:aws:execute-api:us-east-1:123456789012:abcdef123/test/GET/request", |
| 111 | + * "resource": "/request", |
| 112 | + * "path": "/request", |
| 113 | + * "httpMethod": "GET", |
| 114 | + * "headers": { |
| 115 | + * "X-AMZ-Date": "20170718T062915Z", |
| 116 | + * "Accept": "application/json", |
| 117 | + * "HeaderAuth1": "headerValue1" |
| 118 | + * }, |
| 119 | + * "queryStringParameters": { |
| 120 | + * "QueryString1": "queryValue1" |
| 121 | + * }, |
| 122 | + * "pathParameters": {}, |
| 123 | + * "stageVariables": null, |
| 124 | + * "requestContext": { |
| 125 | + * "path": "/request", |
| 126 | + * "accountId": "123456789012", |
| 127 | + * "resourceId": "05c7jb", |
| 128 | + * "stage": "test", |
| 129 | + * "requestId": "...", |
| 130 | + * "identity": { |
| 131 | + * "cognitoIdentityPoolId": null, |
| 132 | + * "accountId": null, |
| 133 | + * "cognitoIdentityId": null, |
| 134 | + * "caller": null, |
| 135 | + * "sourceIp": "192.168.1.1", |
| 136 | + * "principalOrgId": null, |
| 137 | + * "accessKey": null, |
| 138 | + * "cognitoAuthenticationType": null, |
| 139 | + * "cognitoAuthenticationProvider": null, |
| 140 | + * "userArn": null, |
| 141 | + * "userAgent": "HTTPie/3.2.2", |
| 142 | + * "user": null |
| 143 | + * } |
| 144 | + * }, |
| 145 | + * "resourcePath": "/request", |
| 146 | + * "httpMethod": "GET", |
| 147 | + * "apiId": "abcdef123" |
| 148 | + * } |
| 149 | + * ``` |
| 150 | + * |
| 151 | + * @see {@link https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html} |
| 152 | + */ |
91 | 153 | const APIGatewayProxyEventSchema = z.object({
|
92 |
| - version: z.string().optional(), |
93 |
| - authorizationToken: z.string().optional(), |
94 |
| - identitySource: z.string().optional(), |
95 |
| - methodArn: z.string().optional(), |
96 |
| - type: z.enum(['TOKEN', 'REQUEST']).optional(), |
97 | 154 | resource: z.string(),
|
98 | 155 | path: z.string(),
|
99 |
| - httpMethod: z.enum([ |
100 |
| - 'GET', |
101 |
| - 'POST', |
102 |
| - 'PUT', |
103 |
| - 'PATCH', |
104 |
| - 'DELETE', |
105 |
| - 'HEAD', |
106 |
| - 'OPTIONS', |
107 |
| - ]), |
108 |
| - headers: z.record(z.string()).optional(), |
109 |
| - queryStringParameters: z.record(z.string()).nullable(), |
110 |
| - multiValueHeaders: z.record(z.array(z.string())).optional(), |
111 |
| - multiValueQueryStringParameters: z.record(z.array(z.string())).nullable(), |
| 156 | + httpMethod: APIGatewayHttpMethod, |
| 157 | + headers: APIGatewayRecord.nullish(), |
| 158 | + multiValueHeaders: z.record(APIGatewayStringArray).nullish(), |
| 159 | + queryStringParameters: APIGatewayRecord.nullable(), |
| 160 | + multiValueQueryStringParameters: z.record(APIGatewayStringArray).nullable(), |
| 161 | + pathParameters: APIGatewayRecord.nullish(), |
| 162 | + stageVariables: APIGatewayRecord.nullish(), |
112 | 163 | requestContext: APIGatewayEventRequestContext,
|
113 |
| - pathParameters: z.record(z.string()).optional().nullish(), |
114 |
| - stageVariables: z.record(z.string()).optional().nullish(), |
115 |
| - isBase64Encoded: z.boolean().optional(), |
116 | 164 | body: z.string().nullable(),
|
| 165 | + isBase64Encoded: z.boolean(), |
| 166 | +}); |
| 167 | + |
| 168 | +/** |
| 169 | + * A zod schema for an API Gateway Request Authorizer event |
| 170 | + * |
| 171 | + * @example |
| 172 | + * ```json |
| 173 | + * { |
| 174 | + * "type": "REQUEST", |
| 175 | + * "methodArn": "arn:aws:execute-api:us-west-2:123456789012:ymy8tbxw7b/prod/GET/", |
| 176 | + * "resource": "/{proxy+}", |
| 177 | + * "path": "/hello/world", |
| 178 | + * "httpMethod": "GET", |
| 179 | + * "headers": { |
| 180 | + * "X-AMZ-Date": "20170718T062915Z", |
| 181 | + * "Accept": "application/json", |
| 182 | + * "HeaderAuth1": "headerValue1" |
| 183 | + * }, |
| 184 | + * "multiValueHeaders": { |
| 185 | + * "X-AMZ-Date": ["20170718T062915Z"], |
| 186 | + * "Accept": ["application/json"], |
| 187 | + * "HeaderAuth1": ["headerValue1"] |
| 188 | + * }, |
| 189 | + * "queryStringParameters": {}, |
| 190 | + * "multiValueQueryStringParameters": {}, |
| 191 | + * "pathParameters": {}, |
| 192 | + * "stageVariables": {}, |
| 193 | + * "requestContext": { |
| 194 | + * "path": "/request", |
| 195 | + * "accountId": "123456789012", |
| 196 | + * "resourceId": "05c7jb", |
| 197 | + * "stage": "test", |
| 198 | + * "requestId": "...", |
| 199 | + * "identity": { |
| 200 | + * "cognitoIdentityPoolId": null, |
| 201 | + * "accountId": null, |
| 202 | + * "cognitoIdentityId": null, |
| 203 | + * "caller": null, |
| 204 | + * "sourceIp": "192.168.1.1", |
| 205 | + * "principalOrgId": null, |
| 206 | + * "accessKey": null, |
| 207 | + * "cognitoAuthenticationType": null, |
| 208 | + * "cognitoAuthenticationProvider": null, |
| 209 | + * "userArn": null, |
| 210 | + * "userAgent": "HTTPie/3.2.2", |
| 211 | + * "user": null |
| 212 | + * } |
| 213 | + * }, |
| 214 | + * "domainName": "id.execute-api.us-west-2.amazonaws.com", |
| 215 | + * "deploymentId": "lle82z", |
| 216 | + * "apiId": "ymy8tbxw7b" |
| 217 | + * } |
| 218 | + * ``` |
| 219 | + * |
| 220 | + * @see {@link https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-lambda-authorizer-input.html#w76aac15b9c21c25c21b5} |
| 221 | + */ |
| 222 | +const APIGatewayRequestAuthorizerEventSchema = z.object({ |
| 223 | + type: z.literal('REQUEST'), |
| 224 | + methodArn: z.string(), |
| 225 | + resource: z.string(), |
| 226 | + path: z.string(), |
| 227 | + httpMethod: APIGatewayHttpMethod, |
| 228 | + headers: APIGatewayRecord, |
| 229 | + multiValueHeaders: z.record(APIGatewayStringArray), |
| 230 | + queryStringParameters: APIGatewayRecord, |
| 231 | + multiValueQueryStringParameters: z.record(APIGatewayStringArray), |
| 232 | + pathParameters: APIGatewayRecord, |
| 233 | + stageVariables: APIGatewayRecord, |
| 234 | + requestContext: APIGatewayEventRequestContext, |
| 235 | + domainName: z.string().optional(), |
| 236 | + deploymentId: z.string().optional(), |
| 237 | + apiId: z.string().optional(), |
| 238 | +}); |
| 239 | + |
| 240 | +/** |
| 241 | + * A zod schema for an API Gateway Token Authorizer event |
| 242 | + * |
| 243 | + * @example |
| 244 | + * ```json |
| 245 | + * { |
| 246 | + * "type": "TOKEN", |
| 247 | + * "authorizationToken": "Bearer abcd1234", |
| 248 | + * "methodArn": "arn:aws:execute-api:us-west-2:123456789012:ymy8tbxw7b/prod/GET/" |
| 249 | + * } |
| 250 | + * ``` |
| 251 | + * |
| 252 | + * @see {@link https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-lambda-authorizer-input.html#w76aac15b9c21c25c21b3} |
| 253 | + */ |
| 254 | +const APIGatewayTokenAuthorizerEventSchema = z.object({ |
| 255 | + type: z.literal('TOKEN'), |
| 256 | + authorizationToken: z.string(), |
| 257 | + methodArn: z.string(), |
117 | 258 | });
|
118 | 259 |
|
119 |
| -export { APIGatewayProxyEventSchema, APIGatewayCert }; |
| 260 | +export { |
| 261 | + APIGatewayProxyEventSchema, |
| 262 | + APIGatewayRequestAuthorizerEventSchema, |
| 263 | + APIGatewayTokenAuthorizerEventSchema, |
| 264 | +}; |
0 commit comments