-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathgithubStars.ts
More file actions
49 lines (42 loc) · 1.26 KB
/
githubStars.ts
File metadata and controls
49 lines (42 loc) · 1.26 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/** JabRef desktop app repo — fixed to avoid open proxy / rate-limit abuse */
const JABREF_REPO = 'JabRef/jabref'
export default defineCachedEventHandler(
async (event) => {
event.node.res.setHeader(
'Cache-Control',
'public, s-maxage=300, stale-while-revalidate=600',
)
const config = useRuntimeConfig()
const githubToken = config.githubRepoToken as string | undefined
const headers: Record<string, string> = {
Accept: 'application/vnd.github.v3+json',
'User-Agent': 'jabref-online',
}
if (githubToken) {
headers.Authorization = `Bearer ${githubToken}`
}
try {
const response = await fetch(
`https://api.github.com/repos/${JABREF_REPO}`,
{ headers },
)
if (!response.ok) {
throw new Error(`GitHub API responded with ${response.status}`)
}
const data = (await response.json()) as { stargazers_count?: number }
return {
stars: data.stargazers_count ?? 0,
}
} catch (error) {
console.debug('Failed to fetch GitHub stars for repo', JABREF_REPO, error)
throw createError({
statusCode: 500,
message: 'Failed to fetch GitHub stars',
})
}
},
{
name: 'github-stars-jabref',
maxAge: 300,
},
)