UNPKG

aws-cdk-lib

Version:

Version 2 of the AWS Cloud Development Kit library

2 lines (1 loc) 5.13 kB
"use strict";var __createBinding=exports&&exports.__createBinding||(Object.create?(function(o,m,k,k2){k2===void 0&&(k2=k);var desc=Object.getOwnPropertyDescriptor(m,k);(!desc||("get"in desc?!m.__esModule:desc.writable||desc.configurable))&&(desc={enumerable:!0,get:function(){return m[k]}}),Object.defineProperty(o,k2,desc)}):(function(o,m,k,k2){k2===void 0&&(k2=k),o[k2]=m[k]})),__setModuleDefault=exports&&exports.__setModuleDefault||(Object.create?(function(o,v){Object.defineProperty(o,"default",{enumerable:!0,value:v})}):function(o,v){o.default=v}),__importStar=exports&&exports.__importStar||(function(){var ownKeys=function(o){return ownKeys=Object.getOwnPropertyNames||function(o2){var ar=[];for(var k in o2)Object.prototype.hasOwnProperty.call(o2,k)&&(ar[ar.length]=k);return ar},ownKeys(o)};return function(mod){if(mod&&mod.__esModule)return mod;var result={};if(mod!=null)for(var k=ownKeys(mod),i=0;i<k.length;i++)k[i]!=="default"&&__createBinding(result,mod,k[i]);return __setModuleDefault(result,mod),result}})();Object.defineProperty(exports,"__esModule",{value:!0}),exports.JsonSchemaMapper=exports.ALL_METHODS=void 0,exports.validateHttpMethod=validateHttpMethod,exports.parseMethodOptionsPath=parseMethodOptionsPath,exports.parseAwsApiCall=parseAwsApiCall,exports.validateInteger=validateInteger,exports.validateDouble=validateDouble;var url_1=()=>{var tmp=require("url");return url_1=()=>tmp,tmp},jsonSchema=()=>{var tmp=__importStar(require("./json-schema"));return jsonSchema=()=>tmp,tmp},errors_1=()=>{var tmp=require("../../core/lib/errors");return errors_1=()=>tmp,tmp},literal_string_1=()=>{var tmp=require("../../core/lib/private/literal-string");return literal_string_1=()=>tmp,tmp};exports.ALL_METHODS=["OPTIONS","GET","PUT","POST","DELETE","PATCH","HEAD"];const ALLOWED_METHODS=["ANY",...exports.ALL_METHODS];function validateHttpMethod(method,messagePrefix=""){if(!ALLOWED_METHODS.includes(method))throw new(errors_1()).UnscopedValidationError((0,literal_string_1().lit)`InvalidHttpMethod`,`${messagePrefix}Invalid HTTP method "${method}". Allowed methods: ${ALLOWED_METHODS.join(",")}`)}function parseMethodOptionsPath(originalPath){if(!originalPath.startsWith("/"))throw new(errors_1()).UnscopedValidationError((0,literal_string_1().lit)`MethodOptionsPathStart`,`Method options path must start with '/': ${originalPath}`);const path=originalPath.slice(1),components=path.split("/");if(components.length<2)throw new(errors_1()).UnscopedValidationError((0,literal_string_1().lit)`MethodOptionsPathInclude`,`Method options path must include at least two components: /{resource}/{method} (i.e. /foo/bar/GET): ${path}`);const httpMethod=components.pop().toUpperCase();httpMethod!=="*"&&validateHttpMethod(httpMethod,`${originalPath}: `);let resourcePath="/~1"+components.join("~1");return components.length===1&&components[0]==="*"?resourcePath="/*":components.length===1&&components[0]===""&&(resourcePath="/"),{httpMethod,resourcePath}}function parseAwsApiCall(path,action,actionParams){if(actionParams&&!action)throw new(errors_1()).UnscopedValidationError((0,literal_string_1().lit)`RequiresActionparamsRequiresAction`,'"actionParams" requires that "action" will be set');if(path&&action)throw new(errors_1()).UnscopedValidationError((0,literal_string_1().lit)`PathActionMutuallyExclusive`,`"path" and "action" are mutually exclusive (path="${path}", action="${action}")`);if(path)return{apiType:"path",apiValue:path};if(action)return actionParams&&(action+="&"+(0,url_1().format)({query:actionParams}).slice(1)),{apiType:"action",apiValue:action};throw new(errors_1()).UnscopedValidationError((0,literal_string_1().lit)`EitherPathActionRequired`,'Either "path" or "action" are required')}function validateInteger(property,messagePrefix){if(property&&!Number.isInteger(property))throw new(errors_1()).UnscopedValidationError((0,literal_string_1().lit)`ShouldBeShouldInteger`,`${messagePrefix} should be an integer`)}function validateDouble(property,messagePrefix){if(property&&isNaN(property)&&isNaN(parseFloat(property.toString())))throw new(errors_1()).UnscopedValidationError((0,literal_string_1().lit)`ShouldBeShouldDouble`,`${messagePrefix} should be an double`)}class JsonSchemaMapper{static toCfnJsonSchema(schema){const result=JsonSchemaMapper._toCfnJsonSchema(schema);return"$schema"in result||(result.$schema=jsonSchema().JsonSchemaVersion.DRAFT4),result}static SchemaPropsWithPrefix={schema:"$schema",ref:"$ref"};static SchemaPropsWithUserDefinedChildren={definitions:!0,properties:!0,patternProperties:!0,dependencies:!0};static _toCfnJsonSchema(schema,preserveKeys=!1){return schema==null||typeof schema!="object"?schema:Array.isArray(schema)?schema.map(entry=>JsonSchemaMapper._toCfnJsonSchema(entry)):Object.assign({},...Object.entries(schema).map(([key,value])=>{const newKey=!preserveKeys&&key in JsonSchemaMapper.SchemaPropsWithPrefix?JsonSchemaMapper.SchemaPropsWithPrefix[key]:key,newValue=JsonSchemaMapper._toCfnJsonSchema(value,!preserveKeys&&JsonSchemaMapper.SchemaPropsWithUserDefinedChildren[key]);return{[newKey]:newValue}}))}}exports.JsonSchemaMapper=JsonSchemaMapper;