UNPKG

test-openapi

Version:
68 lines (39 loc) 1.59 kB
"use strict";require("core-js/modules/es.string.replace");Object.defineProperty(exports,"__esModule",{value:true});exports.getConstants=void 0;var _error=require("../../../../../errors/error.js"); const getConstants=function({spec,operation,method,path}){ const serverParam=getServerParam({spec,operation}); const methodParam=getMethodParam({method}); const pathParam=getPathParam({path}); return{...serverParam,...methodParam,...pathParam}; };exports.getConstants=getConstants; const getServerParam=function({ spec:{schemes:specSchemes=DEFAULT_SCHEMES,host:hostname,basePath}, operation:{schemes=specSchemes}}) { if(hostname===undefined){ return; } const servers=schemes.map(scheme=>`${scheme}://${hostname}${basePath}`); return{server:{type:"string",enum:servers}}; }; const DEFAULT_SCHEMES=["http"]; const getMethodParam=function({method}){ return{method:{type:"string",enum:[method]}}; }; const getPathParam=function({path}){ const pathA=getExpressPath({path}); return{path:{type:"string",enum:[pathA]}}; }; const getExpressPath=function({path}){ return path.replace(URL_PARAM_REGEXP,(match,name)=> getExpressVariable({name,path})); }; const URL_PARAM_REGEXP=/\{([\0-\|~-\u{10FFFF}]+)\}/gu; const getExpressVariable=function({name,path}){ if(VALID_EXPRESS_PATH_NAME.test(name)){ return`:${name}`; } throw new _error.TestOpenApiError( `In OpenAPI 'path' '${path}', variable name '${name}' is invalid: it must only contain letters, digits and underscores`); }; const VALID_EXPRESS_PATH_NAME=/^[A-Z_a-z][0-9A-Z_a-z]*$/u; //# sourceMappingURL=constants.js.map