--- import FontAwesome from '@components/form/FontAwesome.svelte'; import { error } from '@utils/logger'; import { safeFetch } from '@utils/safe-fetch'; import { fetchGitHubStats } from '@utils/fetch-repo-info'; const { github } = Astro.props; interface RepoMetrics { stars: number; forks: number; openIssues: number; language: string; licenseSpdx: string; licenseName: string; } const normalizeRepo = (repo: string) => repo.replace(/^https?:\/\/github\.com\//, '').replace(/\/+$/, ''); // Fallback straight to the GitHub API when our API is unavailable. const fromGitHub = async (repo: string): Promise => { const headers: Record = { 'User-Agent': 'awesome-privacy' }; const apiKey = import.meta.env.GITHUB_API_KEY; if (apiKey) headers['Authorization'] = `token ${apiKey}`; try { const res = await safeFetch(`https://api.github.com/repos/${repo}`, { headers }); if (!res.ok) { error('GitHub API', `HTTP ${res.status} for ${repo}`); return null; } const data = await res.json(); return { stars: data.stargazers_count ?? 0, forks: data.forks_count ?? 0, openIssues: data.open_issues_count ?? 0, language: data.language ?? '', licenseSpdx: data.license?.spdx_id ?? '', licenseName: data.license?.name ?? '', }; } catch (e) { error('GitHub API', `Network error for ${repo}: ${e}`); return null; } }; // Prefer our unified API, then fall back to the GitHub API directly. const getMetrics = async (repo: string): Promise => { const stats = await fetchGitHubStats(repo); if (stats) { const { info } = stats; return { stars: info.starCount, forks: info.forksCount, openIssues: info.openIssues, language: info.language, licenseSpdx: info.license, licenseName: info.licenseName, }; } return fromGitHub(normalizeRepo(repo)); }; const formatLicense = (spdx: string) => { if (!spdx) return 'Unknown'; if (spdx === 'NOASSERTION') return 'No License'; return spdx; }; const formatBigNumber = (num: number | undefined) => { if (num == null) return 0; if (num > 1000) { return `${(num / 1000).toFixed(1)}k`; } return num; }; const metrics = await getMetrics(github); ---
{github} { metrics && ( {formatBigNumber(metrics.stars)} {formatBigNumber(metrics.forks)} {formatBigNumber(metrics.openIssues)} {formatLicense(metrics.licenseSpdx)} {metrics.language} )}