import o from"node:path";import{readFile as x,existsSync as h}from"node:fs";import{promisify as N}from"node:util";import S from"js-yaml";import{withoutPathPrefix as _}from"@redocly/theme/core/utils";import{DEPRECATED_PUBLIC_API_DEFINITIONS_FOLDER as P,PUBLIC_API_DEFINITIONS_FOLDER as F}from"../../constants/common.js";import{canDownloadApiDefinition as L,filterDataByAccessDeep as O}from"../../utils/rbac.js";import{replaceFileExtension as y}from"./store-definition-bundles.js";const U=N(x),j=async(I,s,n)=>{const{isAuthenticated:e,teams:a,claims:{email:i}}=s.user,A=new URL(I.url),r=_(A.pathname),t=decodeURIComponent(r.replace(F,"").replace(P,"")),{rbac:c={},requiresLogin:p=!1}=s.config;if(!L(r,c,p,{isAuthenticated:e,email:i,teams:a}))return new Response("Unauthorized",{status:e?403:401});const l=o.join(n.props?.outdir||"",F,y(t,".json")),m=o.join(n.props?.outdir||"",P,y(t,".json")),f=h(l),D=h(m);if(!f&&!D)return new Response("Not found",{status:404});const E=await U(f?l:m,"utf-8"),R=JSON.parse(E),u=O(R,{isAuthenticated:e,email:i,teams:a},c,p),d=o.extname(t),w=d===".yaml"?S.dump(u):JSON.stringify(u,null,2);return new Response(w,{headers:{"Content-Type":d===".yaml"?"application/yaml":"application/json"}})};var H=j;export{H as default};
