import type { Env } from "./types"; export function authenticate(request: Request, env: Env): Response | null { const header = request.headers.get("Authorization"); if (!header) { return json(401, { error: "Missing Authorization header" }); } const [scheme, token] = header.split(" ", 2); if (scheme !== "Bearer" || !token) { return json(401, { error: "Invalid Authorization format. Use: Bearer " }); } if (token !== env.API_KEY) { return json(403, { error: "Invalid API key" }); } return null; // authenticated } export function json(status: number, body: unknown): Response { return new Response(JSON.stringify(body), { status, headers: { "Content-Type": "application/json" }, }); }