tangled
alpha
login
or
join now
flo-bit.dev
/
blento
your personal website on atproto - mirror
blento.app
20
fork
atom
overview
issues
pulls
pipelines
add npmx-leaderboard api
Florian
1 day ago
6e19d606
0705fd4d
+44
1 changed file
expand all
collapse all
unified
split
src
routes
api
npmx-leaderboard
+server.ts
+44
src/routes/api/npmx-leaderboard/+server.ts
···
1
1
+
import { json } from '@sveltejs/kit';
2
2
+
import type { RequestHandler } from './$types';
3
3
+
4
4
+
const LEADERBOARD_API_URL =
5
5
+
'https://npmx-likes-leaderboard-api-production.up.railway.app/api/leaderboard/likes';
6
6
+
7
7
+
export const GET: RequestHandler = async ({ platform }) => {
8
8
+
const cacheKey = '#npmx-leaderboard:likes';
9
9
+
const cachedData = await platform?.env?.USER_DATA_CACHE?.get(cacheKey);
10
10
+
11
11
+
if (cachedData) {
12
12
+
const parsedCache = JSON.parse(cachedData);
13
13
+
14
14
+
const TWELVE_HOURS = 12 * 60 * 60 * 1000;
15
15
+
const now = Date.now();
16
16
+
17
17
+
if (now - (parsedCache.updatedAt || 0) < TWELVE_HOURS) {
18
18
+
return json(parsedCache.data);
19
19
+
}
20
20
+
}
21
21
+
22
22
+
try {
23
23
+
const response = await fetch(LEADERBOARD_API_URL);
24
24
+
25
25
+
if (!response.ok) {
26
26
+
return json(
27
27
+
{ error: 'Failed to fetch npmx leaderboard ' + response.statusText },
28
28
+
{ status: response.status }
29
29
+
);
30
30
+
}
31
31
+
32
32
+
const data = await response.json();
33
33
+
34
34
+
await platform?.env?.USER_DATA_CACHE?.put(
35
35
+
cacheKey,
36
36
+
JSON.stringify({ data, updatedAt: Date.now() })
37
37
+
);
38
38
+
39
39
+
return json(data);
40
40
+
} catch (error) {
41
41
+
console.error('Error fetching npmx leaderboard:', error);
42
42
+
return json({ error: 'Failed to fetch npmx leaderboard' }, { status: 500 });
43
43
+
}
44
44
+
};