forked from roadmapsh/deprecated-version
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.ts
34 lines (27 loc) · 832 Bytes
/
github.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const formatter = Intl.NumberFormat('en-US', {
notation: 'compact',
});
const defaultStarCount = 224000;
let starCount: number | undefined = undefined;
export async function countStars(
repo = 'kamranahmedse/developer-roadmap'
): Promise<number> {
if (starCount) {
return starCount;
}
try {
const repoData = await fetch(`https://api.github.com/repos/${repo}`);
const json = await repoData.json();
starCount = json.stargazers_count * 1 || defaultStarCount;
} catch (e) {
console.log('Failed to fetch stars', e);
starCount = defaultStarCount;
}
return starCount;
}
export async function getFormattedStars(
repo = 'kamranahmedse/developer-roadmap'
): Promise<string> {
const stars = import.meta.env.DEV ? defaultStarCount : await countStars(repo);
return formatter.format(stars);
}