File size: 906 Bytes
f91a684
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { apiFetch } from './authClient';

export type LeetCodeSummary = {
  username: string;
  profileURL: string;
  totalSolved: number;
};

async function parseJson(res: Response) {
  const text = await res.text();
  try {
    return text ? JSON.parse(text) : {};
  } catch {
    return { error: 'Invalid server response' };
  }
}

export async function fetchLeetCodeSummary(profileLink: string): Promise<LeetCodeSummary> {
  const res = await apiFetch('/api/integrations/leetcode-summary', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ profileLink }),
  });

  const data = (await parseJson(res)) as LeetCodeSummary & { error?: string };
  if (!res.ok) {
    throw new Error(data.error || 'Failed to fetch LeetCode profile');
  }

  return {
    username: data.username,
    profileURL: data.profileURL,
    totalSolved: data.totalSolved,
  };
}