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