mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 08:03:30 +01:00
fix: handle github api errors gracefully (#11357)
* fix: handle github api errors gracefully * fix: remove forks and improve log
This commit is contained in:
@@ -5,14 +5,7 @@ import { Star } from 'lucide-react';
|
||||
import { type AnchorHTMLAttributes } from 'react';
|
||||
import { twMerge as cn } from 'tailwind-merge';
|
||||
|
||||
async function getRepoStarsAndForks(
|
||||
owner: string,
|
||||
repo: string,
|
||||
token?: string,
|
||||
): Promise<{
|
||||
forks: number;
|
||||
stars: number;
|
||||
}> {
|
||||
async function getRepoStars(owner: string, repo: string, token?: string): Promise<{ stars: number } | null> {
|
||||
const endpoint = `https://api.github.com/repos/${owner}/${repo}`;
|
||||
const headers = new Headers({
|
||||
'Content-Type': 'application/json',
|
||||
@@ -24,21 +17,18 @@ async function getRepoStarsAndForks(
|
||||
const response = await fetch(endpoint, {
|
||||
headers,
|
||||
next: {
|
||||
revalidate: 60,
|
||||
revalidate: 24 * 60 * 60,
|
||||
},
|
||||
} as RequestInit);
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const message = await response.text();
|
||||
|
||||
throw new Error(`Failed to fetch repository data: ${message}`);
|
||||
console.warn(`Failed to fetch repository data (${response.status}):`, message);
|
||||
return null;
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
return {
|
||||
stars: data.stargazers_count,
|
||||
forks: data.forks_count,
|
||||
};
|
||||
return { stars: data.stargazers_count };
|
||||
}
|
||||
|
||||
export async function GithubInfo({
|
||||
@@ -51,8 +41,7 @@ export async function GithubInfo({
|
||||
readonly repo: string;
|
||||
readonly token?: string;
|
||||
}) {
|
||||
const { stars } = await getRepoStarsAndForks(owner, repo, token);
|
||||
const humanizedStars = humanizeNumber(stars);
|
||||
const repoData = await getRepoStars(owner, repo, token);
|
||||
|
||||
return (
|
||||
<a
|
||||
@@ -72,10 +61,12 @@ export async function GithubInfo({
|
||||
</svg>
|
||||
{owner}/{repo}
|
||||
</p>
|
||||
<p className="text-fd-muted-foreground flex items-center gap-1 text-xs">
|
||||
<Star className="size-3" />
|
||||
{humanizedStars}
|
||||
</p>
|
||||
{repoData ? (
|
||||
<p className="text-fd-muted-foreground flex items-center gap-1 text-xs">
|
||||
<Star className="size-3" />
|
||||
{humanizeNumber(repoData.stars)}
|
||||
</p>
|
||||
) : null}
|
||||
</a>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user