web/src/utils/fetch-discord-info.ts

31 lines · 849 bytes

 1import { error } from './logger';
 2import { safeFetch } from './safe-fetch';
 3import { apiBase, enrichHeaders } from './api-config';
 4
 5export const fetchDiscordInfo = async (
 6  discordInvite: string,
 7): Promise<DiscordInfo | null> => {
 8  const endpoint = `${apiBase}/v1/enrich/discord/${discordInvite}`;
 9  try {
10    const res = await safeFetch(endpoint, { headers: enrichHeaders() });
11    if (!res.ok) {
12      error('Discord', `HTTP ${res.status} for ${discordInvite} (${endpoint})`);
13      return null;
14    }
15    return await res.json();
16  } catch (err) {
17    error('Discord', `Network error for ${discordInvite}: ${err}`);
18    return null;
19  }
20};
21
22export interface DiscordInfo {
23  inviteCode: string;
24  name: string;
25  memberCount: number;
26  memberOnlineCount: number;
27  channel: string;
28  icon: string;
29  banner: string;
30  inviter: string | null;
31}