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