|
| 1 | +export const DEFAULT_COMMUNITY_STATS = { |
| 2 | + discordMembers: 1650, |
| 3 | + githubStars: 1050, |
| 4 | +}; |
| 5 | + |
| 6 | +export const DEFAULT_DISCORD_INVITE_CODE = 'HvQugYx'; |
| 7 | +export const DEFAULT_GITHUB_REPO = 'minekube/gate'; |
| 8 | + |
| 9 | +const isValidCount = (value) => Number.isInteger(value) && value >= 0; |
| 10 | + |
| 11 | +export function parseGitHubRepoStats(payload) { |
| 12 | + const count = payload?.stargazers_count; |
| 13 | + if (!isValidCount(count)) { |
| 14 | + throw new Error('GitHub API response did not include stargazers_count'); |
| 15 | + } |
| 16 | + return count; |
| 17 | +} |
| 18 | + |
| 19 | +export function parseDiscordInviteStats(payload) { |
| 20 | + const count = payload?.approximate_member_count; |
| 21 | + if (!isValidCount(count)) { |
| 22 | + throw new Error( |
| 23 | + 'Discord invite API response did not include approximate_member_count' |
| 24 | + ); |
| 25 | + } |
| 26 | + return count; |
| 27 | +} |
| 28 | + |
| 29 | +async function fetchJson(url, { fetchImpl = fetch, headers = {} } = {}) { |
| 30 | + const response = await fetchImpl(url, { |
| 31 | + headers: { |
| 32 | + Accept: 'application/json', |
| 33 | + ...headers, |
| 34 | + }, |
| 35 | + signal: AbortSignal.timeout(10_000), |
| 36 | + }); |
| 37 | + |
| 38 | + if (!response.ok) { |
| 39 | + throw new Error(`Request failed with ${response.status} for ${url}`); |
| 40 | + } |
| 41 | + |
| 42 | + return response.json(); |
| 43 | +} |
| 44 | + |
| 45 | +export async function fetchGitHubStars({ |
| 46 | + fetchImpl, |
| 47 | + repo = process.env.GATE_GITHUB_REPO || DEFAULT_GITHUB_REPO, |
| 48 | +} = {}) { |
| 49 | + const payload = await fetchJson(`https://api.github.com/repos/${repo}`, { |
| 50 | + fetchImpl, |
| 51 | + headers: { |
| 52 | + 'X-GitHub-Api-Version': '2022-11-28', |
| 53 | + 'User-Agent': 'gate-docs-build', |
| 54 | + }, |
| 55 | + }); |
| 56 | + return parseGitHubRepoStats(payload); |
| 57 | +} |
| 58 | + |
| 59 | +export async function fetchDiscordMembers({ |
| 60 | + fetchImpl, |
| 61 | + inviteCode = process.env.GATE_DISCORD_INVITE_CODE || |
| 62 | + DEFAULT_DISCORD_INVITE_CODE, |
| 63 | +} = {}) { |
| 64 | + const payload = await fetchJson( |
| 65 | + `https://discord.com/api/v10/invites/${inviteCode}?with_counts=true`, |
| 66 | + { fetchImpl } |
| 67 | + ); |
| 68 | + return parseDiscordInviteStats(payload); |
| 69 | +} |
| 70 | + |
| 71 | +export async function fetchCommunityStats({ fetchImpl, logger = console } = {}) { |
| 72 | + const stats = {}; |
| 73 | + |
| 74 | + try { |
| 75 | + stats.discordMembers = await fetchDiscordMembers({ fetchImpl }); |
| 76 | + } catch (error) { |
| 77 | + logger.warn( |
| 78 | + `[community-stats] Discord member count unavailable, using fallback: ${error.message}` |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + try { |
| 83 | + stats.githubStars = await fetchGitHubStars({ fetchImpl }); |
| 84 | + } catch (error) { |
| 85 | + logger.warn( |
| 86 | + `[community-stats] GitHub star count unavailable, using fallback: ${error.message}` |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + return stats; |
| 91 | +} |
| 92 | + |
| 93 | +export function buildStatsEnv(stats = {}) { |
| 94 | + const discordMembers = isValidCount(stats.discordMembers) |
| 95 | + ? stats.discordMembers |
| 96 | + : DEFAULT_COMMUNITY_STATS.discordMembers; |
| 97 | + const githubStars = isValidCount(stats.githubStars) |
| 98 | + ? stats.githubStars |
| 99 | + : DEFAULT_COMMUNITY_STATS.githubStars; |
| 100 | + |
| 101 | + return { |
| 102 | + GATE_DOCS_DISCORD_MEMBERS: String(discordMembers), |
| 103 | + GATE_DOCS_GITHUB_STARS: String(githubStars), |
| 104 | + }; |
| 105 | +} |
0 commit comments