import type { SourceModule } from "../registry"; import { VercelConfig, VercelPayload, type VercelConfig as Config, type VercelPayload as Payload, } from "@/lib/schemas/sources/vercel"; export const vercelModule: SourceModule = { kind: "vercel", label: "Vercel", keyless: false, defaultRefreshSeconds: 600, configSchema: VercelConfig, payloadSchema: VercelPayload, isConfigured: (_c, env) => Boolean(env.VERCEL_TOKEN), async fetch({ config, env, signal }) { const token = env.VERCEL_TOKEN; if (!token) throw new Error("Set VERCEL_TOKEN in .env.local"); const url = new URL("https://api.vercel.com/v6/deployments"); url.searchParams.set("limit", String(config.limit)); if (env.VERCEL_TEAM_ID) url.searchParams.set("teamId", env.VERCEL_TEAM_ID); const res = await fetch(url, { headers: { Authorization: `Bearer ${token}` }, signal, }); if (!res.ok) throw new Error(`Vercel ${res.status}`); const j = (await res.json()) as { deployments: Array<{ name: string; state?: string; readyState?: string; url?: string; target?: string | null; created: number; }>; }; return { deployments: (j.deployments ?? []).map((d) => ({ name: d.name, state: d.state ?? d.readyState ?? "—", url: d.url ? `https://${d.url}` : null, target: d.target ?? "preview", createdAt: new Date(d.created).toISOString(), })), }; }, };