@aws-lambda-powertools/parser
Version:
The parser package for the Powertools for AWS Lambda (TypeScript) library.
244 lines (243 loc) • 8.21 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.APIGatewayRequestContextV2Schema = exports.APIGatewayRequestAuthorizerV2Schema = exports.APIGatewayRequestAuthorizerEventV2Schema = exports.APIGatewayProxyEventV2Schema = void 0;
const zod_1 = require("zod");
const apigw_proxy_js_1 = require("./apigw-proxy.js");
/**
* A zod schema for an API Gateway HTTP API Request Authorizer
*
* If Lambda authorizer is used, the `lambda` property will be set to an object
* containing the `context` object returned by the Lambda authorizer function.
* If no `context` object is returned, the `lambda` property will be set to `null`.
*
* If JWT authorizer is used, the `jwt` property will be set to an object
* containing the `claims` object returned by the JWT authorizer function. Optionally,
* the `scopes` property will be set to an array of scopes returned by the JWT authorizer.
*
* @example
* ```json
* {
* "jwt": {
* "claims": {
* "claim1": "value1",
* "claim2": "value2"
* },
* "scopes": [
* "scope1",
* "scope2"
* ]
* }
* }
* ```
*
* If IAM authorizer is used, the `iam` property will be set to an object
* containing the details of the IAM user making the request.
*
* @see {@link https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-access-control.html}
*/
const APIGatewayRequestAuthorizerV2Schema = zod_1.z.object({
jwt: zod_1.z
.object({
claims: zod_1.z.record(zod_1.z.string(), zod_1.z.any()),
scopes: apigw_proxy_js_1.APIGatewayStringArray.nullable(),
})
.optional(),
iam: zod_1.z
.object({
accessKey: zod_1.z.string().optional(),
accountId: zod_1.z.string().optional(),
callerId: zod_1.z.string().optional(),
principalOrgId: zod_1.z.string().nullish(),
userArn: zod_1.z.string().optional(),
userId: zod_1.z.string().optional(),
cognitoIdentity: zod_1.z
.object({
amr: apigw_proxy_js_1.APIGatewayStringArray,
identityId: zod_1.z.string(),
identityPoolId: zod_1.z.string(),
})
.nullish(),
})
.optional(),
lambda: zod_1.z.record(zod_1.z.string(), zod_1.z.any()).nullish(),
});
exports.APIGatewayRequestAuthorizerV2Schema = APIGatewayRequestAuthorizerV2Schema;
/**
* A zod schema for an API Gateway HTTP API Request Context
*
* @example
* ```json
* {
* "accountId": "123456789012",
* "apiId": "api-id",
* "authentication": {}
* "domainName": "id.execute-api.us-east-1.amazonaws.com",
* "domainPrefix": "id",
* "http": {
* "method": "POST",
* "path": "/my/path",
* "protocol": "HTTP/1.1",
* "sourceIp": "...",
* "userAgent": "..."
* },
* "requestId": "...",
* "routeKey": "$default",
* "stage": "$default",
* "time": "12/Mar/2020:19:03:58 +0000",
* "timeEpoch": 1583348638390
* }
* ```
*/
const APIGatewayRequestContextV2Schema = zod_1.z.object({
accountId: zod_1.z.string(),
apiId: zod_1.z.string(),
authorizer: APIGatewayRequestAuthorizerV2Schema.optional(),
authentication: zod_1.z
.object({
clientCert: apigw_proxy_js_1.APIGatewayCert.optional(),
})
.nullish(),
domainName: zod_1.z.string(),
domainPrefix: zod_1.z.string(),
http: zod_1.z.object({
method: apigw_proxy_js_1.APIGatewayHttpMethod,
path: zod_1.z.string(),
protocol: zod_1.z.string(),
sourceIp: zod_1.z.ipv4(),
userAgent: zod_1.z.string(),
}),
requestId: zod_1.z.string(),
routeKey: zod_1.z.string(),
stage: zod_1.z.string(),
time: zod_1.z.string(),
timeEpoch: zod_1.z.number(),
});
exports.APIGatewayRequestContextV2Schema = APIGatewayRequestContextV2Schema;
/**
* A zod schema for an API Gateway HTTP API Proxy event
*
* @example
* ```json
* {
* "version": "2.0",
* "routeKey": "$default",
* "rawPath": "/my/path",
* "rawQueryString": "parameter1=value1¶meter1=value2¶meter2=value",
* "cookies": ["cookie1", "cookie2"],
* "headers": {
* "header1": "value1",
* "header2": "value1,value2"
* },
* "queryStringParameters": {
* "parameter1": "value1,value2",
* "parameter2": "value"
* },
* "requestContext": {
* "accountId": "123456789012",
* "apiId": "api-id",
* "authentication": {}
* "domainName": "id.execute-api.us-east-1.amazonaws.com",
* "domainPrefix": "id",
* "http": {
* "method": "POST",
* "path": "/my/path",
* "protocol": "HTTP/1.1",
* "sourceIp": "...",
* "userAgent": "..."
* },
* "requestId": "...",
* "routeKey": "$default",
* "stage": "$default",
* "time": "12/Mar/2020:19:03:58 +0000",
* "timeEpoch": 1583348638390
* },
* "body": "Hello from Lambda",
* "pathParameters": {},
* "isBase64Encoded": false,
* "stageVariables": {}
* }
* ```
*
* @see {@link APIGatewayProxyEventV2 | `APIGatewayProxyEventV2`}
* @see {@link https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-lambda.html}
*/
const APIGatewayProxyEventV2Schema = zod_1.z.object({
version: zod_1.z.string(),
routeKey: zod_1.z.string(),
rawPath: zod_1.z.string(),
rawQueryString: zod_1.z.string(),
cookies: apigw_proxy_js_1.APIGatewayStringArray.optional(),
headers: apigw_proxy_js_1.APIGatewayRecord,
queryStringParameters: apigw_proxy_js_1.APIGatewayRecord.optional(),
requestContext: APIGatewayRequestContextV2Schema,
body: zod_1.z.string().optional(),
pathParameters: apigw_proxy_js_1.APIGatewayRecord.nullish(),
isBase64Encoded: zod_1.z.boolean(),
stageVariables: apigw_proxy_js_1.APIGatewayRecord.nullish(),
});
exports.APIGatewayProxyEventV2Schema = APIGatewayProxyEventV2Schema;
/**
* A zod schema for an API Gateway HTTP API Request Authorizer event
*
* @example
* ```json
* {
* "version": "2.0",
* "type": "REQUEST",
* "routeArn": "arn:aws:execute-api:us-east-1:123456789012:api-id/stage-name/GET/mydemoresource",
* "identitySource": ["user1", "123"],
* "routeKey": "$default",
* "rawPath": "/mydemoresource",
* "rawQueryString": "parameter1=value1¶meter1=value2¶meter2=value",
* "cookies": ["cookie1", "cookie2"],
* "headers": {
* "header1": "value1",
* "header2": "value1,value2"
* },
* "queryStringParameters": {
* "parameter1": "value1,value2",
* "parameter2": "value"
* },
* "requestContext": {
* "accountId": "123456789012",
* "apiId": "api-id",
* "authentication": {}
* "domainName": "id.execute-api.us-east-1.amazonaws.com",
* "domainPrefix": "id",
* "http": {
* "method": "POST",
* "path": "/my/path",
* "protocol": "HTTP/1.1",
* "sourceIp": "...",
* "userAgent": "..."
* },
* "requestId": "...",
* "routeKey": "$default",
* "stage": "$default",
* "time": "12/Mar/2020:19:03:58 +0000",
* "timeEpoch": 1583348638390
* },
* "pathParameters": {},
* "stageVariables": {}
* }
* ```
*
* @see {@link APIGatewayRequestAuthorizerEventV2 | `APIGatewayRequestAuthorizerEventV2`}
* @see {@link https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-lambda-authorizer.html}
*/
const APIGatewayRequestAuthorizerEventV2Schema = zod_1.z.object({
version: zod_1.z.literal('2.0'),
type: zod_1.z.literal('REQUEST'),
routeArn: zod_1.z.string(),
identitySource: apigw_proxy_js_1.APIGatewayStringArray.nullish(),
routeKey: zod_1.z.string(),
rawPath: zod_1.z.string(),
rawQueryString: zod_1.z.string(),
cookies: apigw_proxy_js_1.APIGatewayStringArray.optional(),
headers: apigw_proxy_js_1.APIGatewayRecord.optional(),
queryStringParameters: apigw_proxy_js_1.APIGatewayRecord.optional(),
requestContext: APIGatewayRequestContextV2Schema,
pathParameters: apigw_proxy_js_1.APIGatewayRecord.nullish(),
stageVariables: apigw_proxy_js_1.APIGatewayRecord.nullish(),
});
exports.APIGatewayRequestAuthorizerEventV2Schema = APIGatewayRequestAuthorizerEventV2Schema;