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 = { 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, })), }; }, };