โ– humdrum codex / soft
1.5 KB raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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<Config, Payload> = {
  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(),
      })),
    };
  },
};