import{join as f}from"path";import{REDOCLY_ROUTE_RBAC as c}from"@redocly/config";import{removeTrailingSlash as d}from"../../../utils/url/remove-trailing-slash.js";import{removeLeadingSlash as m}from"../../../utils/url/remove-leading-slash.js";import{USER_DEFINED_API_FUNCTIONS_COUNTER_KEY as h}from"../../../server/store.js";import{parseRouteFsPathForHono as g}from"./helpers/parse-route-fs-path-for-hono.js";import{telemetryTraceStep as F}from"../../../cli/telemetry/helpers/trace-step.js";const R="api-functions";async function S(){return{id:"ApiFunctions",requiredEntitlements:["apiFunctions"],async processContent(n,i){await F("build.plugin.api_functions",async r=>{const s=await i.getConfig(),a=(s.apiFunctions?.folders||[]).map(e=>`^${m(d(e))}`);r?.setAttribute("config",JSON.stringify(s.apiFunctions||{})),a.push(".*@api");const l=new RegExp(`(${a.join("|")})/.*.(ts|js)$`);let o=0;for(const e of await i.fs.scan(l)){if(e.isVirtual)continue;const t=g(e.relativePath);if(!t)continue;const u=`${R}:${t.handler}`,p=f(n.contentDir,t.handler);n.createRequestHandler(u,p),n.addApiRoute({requestHandlerId:u,slug:t.path,fsPath:e.relativePath,httpMethod:t.method,[c]:{fsPath:e.relativePath,slug:t.path}}),o++}r?.setAttribute("definedApiFunctions",String(o)),n.setGlobalConfig({[h]:o})})}}}export{R as API_FUNCTIONS_REQUEST_HANDLER_ID,S as apiFunctionsPlugin};
