fix: handle github api errors gracefully (#11357)

* fix: handle github api errors gracefully

* fix: remove forks and improve log
This commit is contained in:
Almeida
2025-12-10 22:03:32 +00:00
committed by GitHub
parent 01f31f374a
commit 451a6380c6

View File

@@ -5,14 +5,7 @@ import { Star } from 'lucide-react';
import { type AnchorHTMLAttributes } from 'react'; import { type AnchorHTMLAttributes } from 'react';
import { twMerge as cn } from 'tailwind-merge'; import { twMerge as cn } from 'tailwind-merge';
async function getRepoStarsAndForks( async function getRepoStars(owner: string, repo: string, token?: string): Promise<{ stars: number } | null> {
owner: string,
repo: string,
token?: string,
): Promise<{
forks: number;
stars: number;
}> {
const endpoint = `https://api.github.com/repos/${owner}/${repo}`; const endpoint = `https://api.github.com/repos/${owner}/${repo}`;
const headers = new Headers({ const headers = new Headers({
'Content-Type': 'application/json', 'Content-Type': 'application/json',
@@ -24,21 +17,18 @@ async function getRepoStarsAndForks(
const response = await fetch(endpoint, { const response = await fetch(endpoint, {
headers, headers,
next: { next: {
revalidate: 60, revalidate: 24 * 60 * 60,
}, },
} as RequestInit); });
if (!response.ok) { if (!response.ok) {
const message = await response.text(); const message = await response.text();
console.warn(`Failed to fetch repository data (${response.status}):`, message);
throw new Error(`Failed to fetch repository data: ${message}`); return null;
} }
const data = await response.json(); const data = await response.json();
return { return { stars: data.stargazers_count };
stars: data.stargazers_count,
forks: data.forks_count,
};
} }
export async function GithubInfo({ export async function GithubInfo({
@@ -51,8 +41,7 @@ export async function GithubInfo({
readonly repo: string; readonly repo: string;
readonly token?: string; readonly token?: string;
}) { }) {
const { stars } = await getRepoStarsAndForks(owner, repo, token); const repoData = await getRepoStars(owner, repo, token);
const humanizedStars = humanizeNumber(stars);
return ( return (
<a <a
@@ -72,10 +61,12 @@ export async function GithubInfo({
</svg> </svg>
{owner}/{repo} {owner}/{repo}
</p> </p>
{repoData ? (
<p className="text-fd-muted-foreground flex items-center gap-1 text-xs"> <p className="text-fd-muted-foreground flex items-center gap-1 text-xs">
<Star className="size-3" /> <Star className="size-3" />
{humanizedStars} {humanizeNumber(repoData.stars)}
</p> </p>
) : null}
</a> </a>
); );
} }