▍ humdrum codex / soft
1.6 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
54
55
56
57
58
import type { SourceModule } from "../registry";
import {
  GitHubConfig,
  GitHubPayload,
  type GitHubConfig as Config,
  type GitHubPayload as Payload,
} from "@/lib/schemas/sources/github";

const API = "https://api.github.com";

export const githubModule: SourceModule<Config, Payload> = {
  kind: "github",
  label: "GitHub",
  keyless: false,
  defaultRefreshSeconds: 900,
  configSchema: GitHubConfig,
  payloadSchema: GitHubPayload,

  isConfigured: (_c, env) => Boolean(env.GITHUB_TOKEN),

  async fetch({ config, env, signal }) {
    const token = env.GITHUB_TOKEN;
    if (!token) throw new Error("Set GITHUB_TOKEN in .env.local");
    const headers = {
      Authorization: `Bearer ${token}`,
      Accept: "application/vnd.github+json",
      "User-Agent": "PersonalDashboard",
    };

    const me = await fetch(`${API}/user`, { headers, signal });
    if (!me.ok) throw new Error(`GitHub ${me.status}`);
    const user = (await me.json()) as {
      login: string;
      public_repos: number;
      followers: number;
    };
    const login = config.user || env.GITHUB_USER || user.login;

    const evRes = await fetch(`${API}/users/${login}/events?per_page=${config.limit}`, {
      headers,
      signal,
    });
    const events = evRes.ok
      ? ((await evRes.json()) as Array<{ type: string; repo?: { name: string }; created_at: string }>)
      : [];

    return {
      login,
      publicRepos: user.public_repos,
      followers: user.followers,
      events: events.map((e) => ({
        type: e.type.replace(/Event$/, ""),
        repo: e.repo?.name ?? "",
        createdAt: e.created_at,
      })),
    };
  },
};