const a=(r,e)=>r.secureMethods?.includes(e)??!1;function d(r={}){return async(e,s)=>{const o=e.req.method;if(!a(r,o))return await s();const n=e.req.header("apiKey");if(!n)return e.json({message:"API key is required"},401);try{if(!process.env.BH_API_URL)return e.json({message:"API key validation service not configured"},500);const i=new URL("api-keys-verify",process.env.BH_API_URL).toString();if(!(await fetch(i,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({apiKey:n})})).ok)return e.json({message:"Invalid API key"},401);await s()}catch{return e.json({message:"API key validation failed"},400)}}}export{d as apiKeyMiddleware};
