|
| 1 | +/* eslint-disable import/no-anonymous-default-export */ |
| 2 | +import { |
| 3 | + getTopUsersFromDb, |
| 4 | + storeTopUsersInDb, |
| 5 | +} from "../../utils/fetchTopUsersFromDb"; |
1 | 6 | import { fetchTopUsersByPullRequests } from "../../utils/fetchTopUsersByPullRequests"; |
2 | | -import { getTopUsersFromDb, storeTopUsersInDb } from "../../utils/fetchTopUsersFromDb"; |
3 | 7 |
|
4 | | -export default async function handler(req, res) { |
5 | | - // Add CORS headers |
6 | | - res.setHeader('Cache-Control', 'public, s-maxage=300, stale-while-revalidate=599'); |
| 8 | +export default async (req, res) => { |
| 9 | + res.setHeader('Access-Control-Allow-Origin', '*'); |
| 10 | + res.setHeader('Access-Control-Allow-Methods', 'GET'); |
| 11 | + res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); |
7 | 12 |
|
8 | 13 | try { |
9 | | - console.log('API: Checking DB for cached top users...'); |
10 | | - let data = await getTopUsersFromDb(); |
11 | | - |
12 | | - // If we have cached data, return it immediately |
13 | | - if (data && data.length > 0) { |
14 | | - console.log('API: Using cached data from DB'); |
15 | | - res.status(200).json(data); |
| 14 | + const message = 'API: Checking DB for cached top users...'; |
| 15 | + console.log(message); |
| 16 | + res.setHeader('X-Debug-Message', message); |
16 | 17 |
|
17 | | - // Optionally refresh cache in background if data is old |
18 | | - const cacheAge = Date.now() - new Date(data[0].timestamp).getTime(); |
19 | | - if (cacheAge > 3600000) { // 1 hour |
20 | | - refreshCache().catch(console.error); |
21 | | - } |
22 | | - return; |
23 | | - } |
| 18 | + let data = await getTopUsersFromDb(); |
24 | 19 |
|
25 | | - // If no cached data, fetch new data with timeout |
26 | | - console.log('API: No cached data found, fetching from GitHub...'); |
27 | | - const controller = new AbortController(); |
28 | | - const timeout = setTimeout(() => controller.abort(), 50000); // 50 second timeout |
| 20 | + if (!data) { |
| 21 | + const message2 = 'API: No cached data found, fetching from GitHub...'; |
| 22 | + console.log(message2); |
| 23 | + res.setHeader('X-Debug-Message-2', message2); |
29 | 24 |
|
30 | | - try { |
31 | 25 | data = await fetchTopUsersByPullRequests("fork-commit-merge/fork-commit-merge"); |
32 | | - clearTimeout(timeout); |
33 | 26 |
|
34 | 27 | if (data && data.length > 0) { |
35 | | - console.log('API: Storing new data in DB...'); |
| 28 | + const message3 = 'API: Storing new data in DB...'; |
| 29 | + console.log(message3); |
| 30 | + res.setHeader('X-Debug-Message-3', message3); |
| 31 | + |
36 | 32 | await storeTopUsersInDb(data); |
37 | | - res.status(200).json(data); |
38 | | - } else { |
39 | | - res.status(404).json({ error: 'No data available' }); |
40 | 33 | } |
41 | | - } catch (error) { |
42 | | - clearTimeout(timeout); |
43 | | - throw error; |
| 34 | + } else { |
| 35 | + const message4 = 'API: Using cached data from DB'; |
| 36 | + console.log(message4); |
| 37 | + res.setHeader('X-Debug-Message-4', message4); |
44 | 38 | } |
45 | | - } catch (error) { |
46 | | - console.error('API route error:', error); |
47 | | - res.status(500).json({ error: 'Internal Server Error' }); |
48 | | - } |
49 | | -} |
50 | 39 |
|
51 | | -// Background cache refresh |
52 | | -async function refreshCache() { |
53 | | - try { |
54 | | - const data = await fetchTopUsersByPullRequests("fork-commit-merge/fork-commit-merge"); |
55 | | - if (data && data.length > 0) { |
56 | | - await storeTopUsersInDb(data); |
| 40 | + if (!data || data.length === 0) { |
| 41 | + const message5 = 'API: No data available'; |
| 42 | + console.log(message5); |
| 43 | + res.setHeader('X-Debug-Message-5', message5); |
| 44 | + return res.status(404).json({ error: 'No data available' }); |
57 | 45 | } |
| 46 | + |
| 47 | + return res.status(200).json(data); |
58 | 48 | } catch (error) { |
59 | | - console.error('Failed to refresh cache:', error); |
| 49 | + console.error('API route error:', error); |
| 50 | + console.error('Error details:', error.response?.data || error.message); |
| 51 | + return res.status(500).json({ error: 'Internal Server Error' }); |
60 | 52 | } |
61 | | -} |
| 53 | +}; |
62 | 54 |
|
63 | 55 |
|
64 | 56 |
|
0 commit comments