|
1 | 1 | import { query, getRequestEvent } from '$app/server'; |
| 2 | +import { env } from '$env/dynamic/private'; |
| 3 | +import { api } from './api'; |
2 | 4 |
|
3 | 5 | export const getStats = query(async () => { |
4 | 6 | const { fetch } = getRequestEvent(); |
5 | | - const [githubRes, npmWeeklyRes, npmMonthlyRes, npmLifetimeRes, discordRes, bskyRes] = |
| 7 | + |
| 8 | + const githubHeaders: Record<string, string> = { Accept: 'application/vnd.github.v3+json' }; |
| 9 | + if (env.GITHUB_API_TOKEN) { |
| 10 | + const prefix = env.GITHUB_API_TOKEN.startsWith('ghp_') ? 'token' : 'Bearer'; |
| 11 | + githubHeaders['Authorization'] = `${prefix} ${env.GITHUB_API_TOKEN}`; |
| 12 | + } |
| 13 | + |
| 14 | + const [githubData, npmWeeklyData, npmMonthlyData, npmLifetimeData, discordData, bskyData] = |
6 | 15 | await Promise.all([ |
7 | | - fetch('https://api.github.com/repos/techniq/layerchart', { |
8 | | - headers: { Accept: 'application/vnd.github.v3+json' } |
| 16 | + api('https://api.github.com', 'repos/techniq/layerchart', { |
| 17 | + fetch, |
| 18 | + headers: githubHeaders |
| 19 | + }), |
| 20 | + api('https://api.npmjs.org', 'downloads/point/last-week/layerchart', { fetch }), |
| 21 | + api('https://api.npmjs.org', 'downloads/point/last-month/layerchart', { fetch }), |
| 22 | + api('https://api.npmjs.org', 'downloads/point/2020-01-01:2099-12-31/layerchart', { |
| 23 | + fetch |
9 | 24 | }), |
10 | | - fetch('https://api.npmjs.org/downloads/point/last-week/layerchart'), |
11 | | - fetch('https://api.npmjs.org/downloads/point/last-month/layerchart'), |
12 | | - fetch('https://api.npmjs.org/downloads/point/2020-01-01:2099-12-31/layerchart'), |
13 | | - fetch('https://discord.com/api/v9/invites/697JhMPD3t?with_counts=true'), |
14 | | - fetch('https://public.api.bsky.app/xrpc/app.bsky.actor.getProfile?actor=techniq.dev') |
| 25 | + api('https://discord.com', 'api/v9/invites/697JhMPD3t?with_counts=true', { fetch }), |
| 26 | + api('https://public.api.bsky.app', 'xrpc/app.bsky.actor.getProfile?actor=techniq.dev', { |
| 27 | + fetch |
| 28 | + }) |
15 | 29 | ]); |
16 | 30 |
|
17 | | - const githubStars = githubRes.ok ? ((await githubRes.json()).stargazers_count as number) : null; |
18 | | - const npmWeekly = npmWeeklyRes.ok ? ((await npmWeeklyRes.json()).downloads as number) : null; |
19 | | - const npmMonthly = npmMonthlyRes.ok ? ((await npmMonthlyRes.json()).downloads as number) : null; |
20 | | - const npmLifetime = npmLifetimeRes.ok |
21 | | - ? ((await npmLifetimeRes.json()).downloads as number) |
22 | | - : null; |
23 | | - const bskyFollowers = bskyRes.ok ? ((await bskyRes.json()).followersCount as number) : null; |
24 | | - const discordMembers = discordRes.ok |
25 | | - ? ((await discordRes.json()).approximate_member_count as number) |
26 | | - : null; |
| 31 | + const githubStars = (githubData?.stargazers_count as number) ?? null; |
| 32 | + const npmWeekly = (npmWeeklyData?.downloads as number) ?? null; |
| 33 | + const npmMonthly = (npmMonthlyData?.downloads as number) ?? null; |
| 34 | + const npmLifetime = (npmLifetimeData?.downloads as number) ?? null; |
| 35 | + const bskyFollowers = (bskyData?.followersCount as number) ?? null; |
| 36 | + const discordMembers = (discordData?.approximate_member_count as number) ?? null; |
27 | 37 |
|
28 | 38 | const npmDownloads: [number | null, number | null, number | null] = [ |
29 | 39 | npmWeekly, |
|
0 commit comments