-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi-client.js
More file actions
60 lines (49 loc) · 1.67 KB
/
api-client.js
File metadata and controls
60 lines (49 loc) · 1.67 KB
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
export class APIClient {
constructor(baseURL = 'https://infra-runner-leaderboard-api.onrender.com/') {
this.baseURL = baseURL;
}
async request(endpoint, options = {}) {
const url = `${this.baseURL}${endpoint}`;
const config = {
headers: {
'Content-Type': 'application/json',
},
...options,
};
if (config.body && typeof config.body === 'object') {
config.body = JSON.stringify(config.body);
}
try {
const response = await fetch(url, config);
const data = await response.json();
if (!response.ok) {
throw new Error(data.message || `HTTP error! status: ${response.status}`);
}
return data;
} catch (error) {
// API request failed
throw error;
}
}
async submitScore(playerData) {
return this.request('api/leaderboard/submit', {
method: 'POST',
body: playerData,
});
}
async getTopScores(limit = 10) {
return this.request(`api/leaderboard/top?limit=${limit}`);
}
async getPlayerBestScore(email) {
return this.request(`api/leaderboard/player/${encodeURIComponent(email)}`);
}
async getOrganizationLeaderboard(organizationName, limit = 10) {
return this.request(`api/leaderboard/organization/${encodeURIComponent(organizationName)}?limit=${limit}`);
}
async getRecentScores(limit = 10) {
return this.request(`api/leaderboard/recent?limit=${limit}`);
}
async getStatistics() {
return this.request('api/leaderboard/statistics');
}
}