eve
Version:
Filesystem-first framework for durable backend AI agents that run anywhere.
1 lines • 1.71 kB
JavaScript
import{isObject}from"#shared/guards.js";import{parseFrontmatter}from"#internal/helpers/gray-matter.js";import{isArray}from"#runtime/connections/openapi-schema.js";function parseSpecDocument(e){try{return JSON.parse(e)}catch{}let t=e.replace(/^\uFEFF/,``),n=t.trimStart().startsWith(`---`)?t:`---\n${t}\n---`;return parseFrontmatter(n).data}function extractServerUrl(e,t){let n=extractOpenApiServerUrl(e,t);return n===void 0?extractSwaggerBaseUrl(e,t):n}function extractOpenApiServerUrl(e,t){let n=e.servers;if(isArray(n))for(let e of n){if(!isObject(e)||typeof e.url!=`string`||e.url.length===0)continue;let n=isObject(e.variables)?substituteServerVariables(e.url,e.variables):e.url;if(n.startsWith(`http://`)||n.startsWith(`https://`))return n;if(typeof t==`string`&&URL.canParse(t))try{return new URL(n,t).toString()}catch{}}}function extractSwaggerBaseUrl(e,t){let n=typeof e.basePath==`string`?e.basePath:``,r=typeof e.host==`string`&&e.host.length>0?e.host:``,i=typeof t==`string`&&URL.canParse(t)?new URL(t):void 0,a=extractSwaggerScheme(e)??i?.protocol.replace(/:$/,``)??`https`;if(r.length>0)return`${a}://${r}${normalizeBasePath(n)}`;if(i!==void 0){let e=new URL(normalizeBasePath(n)||`/`,i.origin).toString();return e.endsWith(`/`)&&normalizeBasePath(n).length===0?e.slice(0,-1):e}}function extractSwaggerScheme(e){let t=e.schemes;if(isArray(t)){for(let e of t)if(e===`https`||e===`http`)return e}}function normalizeBasePath(e){let t=e.trim();return t.length===0||t===`/`?``:t.startsWith(`/`)?t:`/${t}`}function substituteServerVariables(e,t){return e.replace(/\{([^}]+)\}/g,(e,n)=>{let r=t[n];return isObject(r)&&typeof r.default==`string`?r.default:e})}export{extractServerUrl,parseSpecDocument};