import { Context } from "netlify:edge"; const schemaVersion = 1; const label = "OPA"; const releases = "https://api.github.com/repos/open-policy-agent/opa/releases/latest"; // OPA logo SVG const logoSvg = ""; export default async (req: Request, context: Context) => { const url = new URL(req.url); const host = req.headers.get("host"); const pathParts = url.pathname.split("/").filter(Boolean); // remove empty parts const query = url.searchParams; // Handle /badge/config/:tag if (pathParts.length === 3 && pathParts[0] === "badge" && pathParts[1] === "config") { const tag = pathParts[2]; const latest = await fetch(releases) .then((response) => response.json()) .then((data) => data.tag_name) .catch(() => "unknown"); const res = { schemaVersion, label, logoSvg, message: tag, color: latest === tag ? "green" : "yellow", }; return context.json(res); } // Handle /badge/:tag if (pathParts.length === 2 && pathParts[0] === "badge") { const tag = pathParts[1]; const style = query.get("style"); // Build dynamic badge endpoint URL using current host const base = `https://${host}/badge/config/${tag}`; const encodedUrl = encodeURIComponent(base); let redirectUrl = `https://img.shields.io/endpoint?url=${encodedUrl}`; if (style) { redirectUrl += `&style=${encodeURIComponent(style)}`; } return Response.redirect(redirectUrl, 302); } return new Response("Not Found", { status: 404 }); };