UNPKG

@aws-lambda-powertools/parser

Version:
251 lines (250 loc) 9.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.APIGatewayEventRequestContextSchema = exports.APIGatewayTokenAuthorizerEventSchema = exports.APIGatewayRequestAuthorizerEventSchema = exports.APIGatewayProxyEventSchema = void 0; const zod_1 = require("zod"); const apigw_proxy_js_1 = require("./apigw-proxy.js"); /** * A zod schema for an API Gateway Event Identity * * @see {@link https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html} */ const APIGatewayEventIdentity = zod_1.z.object({ accessKey: zod_1.z.string().nullish(), accountId: zod_1.z.string().nullish(), apiKey: zod_1.z.string().nullish(), apiKeyId: zod_1.z.string().nullish(), caller: zod_1.z.string().nullish(), cognitoAuthenticationProvider: zod_1.z.string().nullish(), cognitoAuthenticationType: zod_1.z.string().nullish(), cognitoIdentityId: zod_1.z.string().nullish(), cognitoIdentityPoolId: zod_1.z.string().nullish(), principalOrgId: zod_1.z.string().nullish(), /** * When invoking the API Gateway REST API using the Test Invoke feature, * the sourceIp is hardcoded to `test-invoke-source-ip`. This is a stopgap * solution to allow customers to test their API and have successful parsing. * * See aws-powertools/powertools-lambda-python#1562 for more information. */ sourceIp: zod_1.z.union([zod_1.z.ipv4(), zod_1.z.literal('test-invoke-source-ip')]).optional(), user: zod_1.z.string().nullish(), userAgent: zod_1.z.string().nullish(), userArn: zod_1.z.string().nullish(), clientCert: apigw_proxy_js_1.APIGatewayCert.nullish(), }); /** * A zod schema for an API Gateway Event Request Context * * @see {@link https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#context-variable-reference} */ const APIGatewayEventRequestContextSchema = zod_1.z .object({ accountId: zod_1.z.string(), apiId: zod_1.z.string(), deploymentId: zod_1.z.string().nullish(), authorizer: zod_1.z .union([ zod_1.z.object({ integrationLatency: zod_1.z.number(), principalId: zod_1.z.string(), }), zod_1.z.object({ claims: zod_1.z.record(zod_1.z.string(), zod_1.z.any()), scopes: apigw_proxy_js_1.APIGatewayStringArray.optional(), }), ]) .nullish(), stage: zod_1.z.string(), protocol: zod_1.z.string(), identity: APIGatewayEventIdentity, requestId: zod_1.z.string(), requestTime: zod_1.z.string(), requestTimeEpoch: zod_1.z.number(), resourceId: zod_1.z.string().nullish(), resourcePath: zod_1.z.string(), domainName: zod_1.z.string().nullish(), domainPrefix: zod_1.z.string().nullish(), extendedRequestId: zod_1.z.string().nullish(), httpMethod: zod_1.z.enum([ 'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS', ]), path: zod_1.z.string(), connectedAt: zod_1.z.number().nullish(), connectionId: zod_1.z.string().nullish(), eventType: zod_1.z.enum(['CONNECT', 'MESSAGE', 'DISCONNECT']).nullish(), messageDirection: zod_1.z.string().nullish(), messageId: zod_1.z.string().nullish(), routeKey: zod_1.z.string().nullish(), operationName: zod_1.z.string().nullish(), }) .refine((input) => { return (!input.messageId || (input.messageId && input.eventType === 'MESSAGE')); }, { message: 'messageId is available only when `eventType` is MESSAGE', }); exports.APIGatewayEventRequestContextSchema = APIGatewayEventRequestContextSchema; /** * A zod schema for an API Gateway Proxy event * * @example * ```json * { * "type": "REQUEST", * "methodArn": "arn:aws:execute-api:us-east-1:123456789012:abcdef123/test/GET/request", * "resource": "/request", * "path": "/request", * "httpMethod": "GET", * "headers": { * "X-AMZ-Date": "20170718T062915Z", * "Accept": "application/json", * "HeaderAuth1": "headerValue1" * }, * "queryStringParameters": { * "QueryString1": "queryValue1" * }, * "pathParameters": {}, * "stageVariables": null, * "requestContext": { * "path": "/request", * "accountId": "123456789012", * "resourceId": "05c7jb", * "stage": "test", * "requestId": "...", * "identity": { * "cognitoIdentityPoolId": null, * "accountId": null, * "cognitoIdentityId": null, * "caller": null, * "sourceIp": "192.168.1.1", * "principalOrgId": null, * "accessKey": null, * "cognitoAuthenticationType": null, * "cognitoAuthenticationProvider": null, * "userArn": null, * "userAgent": "HTTPie/3.2.2", * "user": null * } * }, * "resourcePath": "/request", * "httpMethod": "GET", * "apiId": "abcdef123" * } * ``` * @see {@link APIGatewayProxyEvent | `APIGatewayProxyEvent`} * @see {@link https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html} */ const APIGatewayProxyEventSchema = zod_1.z.object({ resource: zod_1.z.string(), path: zod_1.z.string(), httpMethod: apigw_proxy_js_1.APIGatewayHttpMethod, headers: apigw_proxy_js_1.APIGatewayRecord.nullish(), multiValueHeaders: zod_1.z.record(zod_1.z.string(), apigw_proxy_js_1.APIGatewayStringArray).nullish(), queryStringParameters: apigw_proxy_js_1.APIGatewayRecord.nullable(), multiValueQueryStringParameters: zod_1.z .record(zod_1.z.string(), apigw_proxy_js_1.APIGatewayStringArray) .nullable(), pathParameters: apigw_proxy_js_1.APIGatewayRecord.nullish(), stageVariables: apigw_proxy_js_1.APIGatewayRecord.nullish(), requestContext: APIGatewayEventRequestContextSchema, body: zod_1.z.string().nullable(), isBase64Encoded: zod_1.z.boolean(), }); exports.APIGatewayProxyEventSchema = APIGatewayProxyEventSchema; /** * A zod schema for an API Gateway Request Authorizer event * * @example * ```json * { * "type": "REQUEST", * "methodArn": "arn:aws:execute-api:us-west-2:123456789012:ymy8tbxw7b/prod/GET/", * "resource": "/{proxy+}", * "path": "/hello/world", * "httpMethod": "GET", * "headers": { * "X-AMZ-Date": "20170718T062915Z", * "Accept": "application/json", * "HeaderAuth1": "headerValue1" * }, * "multiValueHeaders": { * "X-AMZ-Date": ["20170718T062915Z"], * "Accept": ["application/json"], * "HeaderAuth1": ["headerValue1"] * }, * "queryStringParameters": {}, * "multiValueQueryStringParameters": {}, * "pathParameters": {}, * "stageVariables": {}, * "requestContext": { * "path": "/request", * "accountId": "123456789012", * "resourceId": "05c7jb", * "stage": "test", * "requestId": "...", * "identity": { * "cognitoIdentityPoolId": null, * "accountId": null, * "cognitoIdentityId": null, * "caller": null, * "sourceIp": "192.168.1.1", * "principalOrgId": null, * "accessKey": null, * "cognitoAuthenticationType": null, * "cognitoAuthenticationProvider": null, * "userArn": null, * "userAgent": "HTTPie/3.2.2", * "user": null * } * }, * "domainName": "id.execute-api.us-west-2.amazonaws.com", * "deploymentId": "lle82z", * "apiId": "ymy8tbxw7b" * } * ``` * * @see {@link https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-lambda-authorizer-input.html#w76aac15b9c21c25c21b5} */ const APIGatewayRequestAuthorizerEventSchema = zod_1.z.object({ type: zod_1.z.literal('REQUEST'), methodArn: zod_1.z.string(), resource: zod_1.z.string(), path: zod_1.z.string(), httpMethod: apigw_proxy_js_1.APIGatewayHttpMethod, headers: apigw_proxy_js_1.APIGatewayRecord, multiValueHeaders: zod_1.z.record(zod_1.z.string(), apigw_proxy_js_1.APIGatewayStringArray), queryStringParameters: apigw_proxy_js_1.APIGatewayRecord, multiValueQueryStringParameters: zod_1.z.record(zod_1.z.string(), apigw_proxy_js_1.APIGatewayStringArray), pathParameters: apigw_proxy_js_1.APIGatewayRecord, stageVariables: apigw_proxy_js_1.APIGatewayRecord, requestContext: APIGatewayEventRequestContextSchema, domainName: zod_1.z.string().optional(), deploymentId: zod_1.z.string().optional(), apiId: zod_1.z.string().optional(), }); exports.APIGatewayRequestAuthorizerEventSchema = APIGatewayRequestAuthorizerEventSchema; /** * A zod schema for an API Gateway Token Authorizer event * * @example * ```json * { * "type": "TOKEN", * "authorizationToken": "Bearer abcd1234", * "methodArn": "arn:aws:execute-api:us-west-2:123456789012:ymy8tbxw7b/prod/GET/" * } * ``` * * @see {@link https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-lambda-authorizer-input.html#w76aac15b9c21c25c21b3} */ const APIGatewayTokenAuthorizerEventSchema = zod_1.z.object({ type: zod_1.z.literal('TOKEN'), authorizationToken: zod_1.z.string(), methodArn: zod_1.z.string(), }); exports.APIGatewayTokenAuthorizerEventSchema = APIGatewayTokenAuthorizerEventSchema;