UNPKG

eve

Version:

Filesystem-first framework for durable backend AI agents that run anywhere.

1 lines 1.7 kB
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 n=e.replace(/^\uFEFF/,``);return parseFrontmatter(n.trimStart().startsWith(`---`)?n:`---\n${n}\n---`).data}function extractServerUrl(e,t){let n=extractOpenApiServerUrl(e,t);return n===void 0?extractSwaggerBaseUrl(e,t):n}function extractOpenApiServerUrl(t,r){let i=t.servers;if(isArray(i))for(let t of i){if(!isObject(t)||typeof t.url!=`string`||t.url.length===0)continue;let n=isObject(t.variables)?substituteServerVariables(t.url,t.variables):t.url;if(n.startsWith(`http://`)||n.startsWith(`https://`))return n;if(typeof r==`string`&&URL.canParse(r))try{return new URL(n,r).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(t,n){return t.replace(/\{([^}]+)\}/g,(t,r)=>{let i=n[r];return isObject(i)&&typeof i.default==`string`?i.default:t})}export{extractServerUrl,parseSpecDocument};