feat(github): usar o ultimo retrato da contagem quando nao ha token ou a API falha

data/github-stats.json guarda 68 repositorios proprios (25 publicos e 43
privados, sem forks) conferidos em 2026-10-08. Com token valido a contagem
ao vivo vale mais; o retrato evita que o hero fique sem o numero.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
Evandro Fonseca JuniorandClaude Sonnet 5.5 committed 2026-10-08 15:26:57 -03:00
1 parent 942f6bb45c
commit c0493f1e77
3 files changed
+82 -11

No files matched your search

+30 -3
View File
@@ -1,7 +1,12 @@
import { readFile } from "node:fs/promises";
import { resolve } from "node:path";
/**
* Contagem de repositórios próprios no GitHub (públicos + privados), consultada só no servidor.
* O token (GITHUB_STATS_TOKEN, fine-grained, somente leitura de metadados) nunca recebe prefixo NEXT_PUBLIC_
* e este módulo só é importado por componentes de servidor: ele não chega ao navegador.
*
* Sem token, ou se a API falhar, vale o último retrato conhecido em data/github-stats.json.
*/
export const GITHUB_STATS_REVALIDATE_SECONDS = 24 * 60 * 60;
@@ -42,9 +47,9 @@ export function countOwnRepos(repos: RepoSummary[]): GithubRepoCounts {
};
}
export async function loadGithubRepoCounts(
token: string | undefined = process.env.GITHUB_STATS_TOKEN,
fetchImpl: FetchLike = fetch,
async function fetchGithubRepoCounts(
token: string | undefined,
fetchImpl: FetchLike,
): Promise<GithubRepoCounts | null> {
if (!token) return null;
@@ -80,3 +85,25 @@ export async function loadGithubRepoCounts(
return null;
}
}
/** Último retrato conhecido (data/github-stats.json); `null` se o arquivo faltar ou estiver incoerente. */
export async function loadGithubStatsSnapshot(): Promise<GithubRepoCounts | null> {
try {
const path = resolve(process.cwd(), "data/github-stats.json");
const raw = JSON.parse(await readFile(path, "utf8")) as GithubRepoCounts;
const counts = [raw.total, raw.public, raw.private];
if (!counts.every((n) => Number.isInteger(n) && n >= 0)) return null;
if (raw.total !== raw.public + raw.private) return null;
return { total: raw.total, public: raw.public, private: raw.private };
} catch {
return null;
}
}
export async function loadGithubRepoCounts(
token: string | undefined = process.env.GITHUB_STATS_TOKEN,
fetchImpl: FetchLike = fetch,
readSnapshot: () => Promise<GithubRepoCounts | null> = loadGithubStatsSnapshot,
): Promise<GithubRepoCounts | null> {
return (await fetchGithubRepoCounts(token, fetchImpl)) ?? readSnapshot();
}