File size: 670 Bytes
161cb23
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Pages below the site root (the leaderboard) set data-root="../" so API calls still reach /api.
const SITE_ROOT = new URL(document.documentElement.dataset.root ?? "./", document.baseURI);

export function apiUrl(path) {
  return new URL(`api/${path}`, SITE_ROOT).href;
}

async function getJson(path, label) {
  const response = await fetch(apiUrl(path), { cache: "no-store" });
  if (!response.ok) {
    throw new Error(`${label} failed (${response.status})`);
  }
  return response.json();
}

export function fetchStats() {
  return getJson("stats", "Stats request");
}

export function fetchLeaderboard() {
  return getJson("leaderboard", "Leaderboard request");
}