web/src/utils/fetch-repo-info.ts
72 lines · 1740 bytes
1import { error } from './logger';
2import { safeFetch } from './safe-fetch';
3import { apiBase, enrichHeaders } from './api-config';
4
5const normalizeRepo = (github: string): string =>
6 github.replace(/^https?:\/\/github\.com\//, '').replace(/\/+$/, '');
7
8export const fetchGitHubStats = async (
9 github: string,
10): Promise<GitHubStatsResponse | null> => {
11 const repo = normalizeRepo(github);
12 const endpoint = `${apiBase}/v1/enrich/github/${repo}`;
13 try {
14 const res = await safeFetch(endpoint, { headers: enrichHeaders() });
15 if (!res.ok) {
16 error('GitHub Stats', `HTTP ${res.status} for ${repo} (${endpoint})`);
17 return null;
18 }
19 return await res.json();
20 } catch (err) {
21 error('GitHub Stats', `Network error for ${repo}: ${err}`);
22 return null;
23 }
24};
25
26export interface GitHubStatsResponse {
27 info: {
28 ownerUsername: string;
29 ownerAvatar: string;
30 description: string;
31 url: string;
32 homepage: string;
33 language: string;
34 topics: string[];
35 license: string;
36 licenseName: string;
37 isFork: boolean;
38 isArchived: boolean;
39 forkParent: string;
40 createdAt: string;
41 updatedAt: string;
42 pushedAt: string;
43 size: number;
44 starCount: number;
45 forksCount: number;
46 watchersCount: number;
47 openIssues: number;
48 };
49 languages: {
50 [key: string]: number;
51 };
52 versions: Array<{
53 name: string;
54 commit: string;
55 zipball: string;
56 tarball: string;
57 }>;
58 contributors: Array<{
59 username: string;
60 avatar: string;
61 url: string;
62 contributions: number;
63 }>;
64 commits: Array<{
65 sha: string;
66 authorName: string;
67 authorDate: string;
68 message: string;
69 authorUsername: string;
70 authorAvatar: string;
71 }>;
72}