Skip to content

Commit f886004

Browse files
Harden GitHub fetch
1 parent 315f28e commit f886004

1 file changed

Lines changed: 42 additions & 32 deletions

File tree

src/lib/github.ts

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,48 @@ export async function getGithubProjects(): Promise<GithubProject[]> {
2626
headers.Authorization = `Bearer ${process.env.GITHUB_TOKEN}`;
2727
}
2828

29-
const response = await fetch(
30-
`https://api.github.com/users/${username}/repos?per_page=100&sort=pushed`,
31-
{
32-
headers,
33-
next: { revalidate: REVALIDATE_SECONDS },
34-
},
35-
);
36-
37-
if (!response.ok) {
29+
const controller = new AbortController();
30+
const timeout = setTimeout(() => controller.abort(), 3000);
31+
32+
try {
33+
const response = await fetch(
34+
`https://api.github.com/users/${username}/repos?per_page=100&sort=pushed`,
35+
{
36+
headers,
37+
next: { revalidate: REVALIDATE_SECONDS },
38+
signal: controller.signal,
39+
},
40+
);
41+
42+
if (!response.ok) {
43+
return [];
44+
}
45+
46+
const repos = (await response.json()) as Array<{
47+
name: string;
48+
description: string | null;
49+
html_url: string;
50+
updated_at: string;
51+
language: string | null;
52+
stargazers_count: number;
53+
fork: boolean;
54+
archived: boolean;
55+
}>;
56+
57+
return repos
58+
.filter((repo) => !repo.fork && !repo.archived)
59+
.slice(0, 6)
60+
.map((repo) => ({
61+
name: repo.name,
62+
description: repo.description,
63+
url: repo.html_url,
64+
updatedAt: repo.updated_at,
65+
language: repo.language,
66+
stars: repo.stargazers_count,
67+
}));
68+
} catch {
3869
return [];
70+
} finally {
71+
clearTimeout(timeout);
3972
}
40-
41-
const repos = (await response.json()) as Array<{
42-
name: string;
43-
description: string | null;
44-
html_url: string;
45-
updated_at: string;
46-
language: string | null;
47-
stargazers_count: number;
48-
fork: boolean;
49-
archived: boolean;
50-
}>;
51-
52-
return repos
53-
.filter((repo) => !repo.fork && !repo.archived)
54-
.slice(0, 6)
55-
.map((repo) => ({
56-
name: repo.name,
57-
description: repo.description,
58-
url: repo.html_url,
59-
updatedAt: repo.updated_at,
60-
language: repo.language,
61-
stars: repo.stargazers_count,
62-
}));
6373
}

0 commit comments

Comments
 (0)