-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrending.js
63 lines (59 loc) · 2.03 KB
/
trending.js
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
export async function fetchTrendingRepos(): Promise<RepoType[]> {
try {
const response = await fetch('/backend/api/trending');
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const repos: RepoType[] = await response.json();
return repos;
} catch (error) {
console.error('Error fetching trending repositories:', error);
throw error;
}
}
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
export async function fetchTrendingRepos() {
try {
const responsePromise = fetch('/backend/api/trending');
const timeoutPromise = timeout(5000); // 5 seconds timeout
const response = await Promise.race([responsePromise, timeoutPromise]);
if (!response || !response.ok) {
throw new Error(`Request failed or timed out.`);
}
const repos = await response.json();
return repos;
} catch (error) {
console.error('Error fetching trending repositories:', error);
throw error;
}
}
async function fetchWithRetry(url, options = {}, retries = 3, backoff = 300) {
try {
const response = await fetch(url, options);
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
return response;
} catch (error) {
if (retries > 0) {
console.warn(`Retrying... attempts left: ${retries}`);
await new Promise(resolve => setTimeout(resolve, backoff));
return fetchWithRetry(url, options, retries - 1, backoff * 2);
} else {
throw error;
}
}
}
export async function fetchTrendingRepos() {
try {
const response = await fetchWithRetry('/backend/api/trending');
const repos = await response.json();
return repos;
} catch (error) {
console.error('Error fetching trending repositories:', error);
throw error;
}
}