-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathapi.ts
More file actions
164 lines (149 loc) · 4.23 KB
/
api.ts
File metadata and controls
164 lines (149 loc) · 4.23 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
export class APIError extends Error {
status: number;
constructor(message: string, status: number) {
super(message);
this.name = "APIError";
this.status = status;
}
}
export async function fetchAboutInfo(): Promise<string> {
const res = await fetch("/api/about");
if (!res.ok) {
throw new Error(`Failed to fetch: ${res.status}`);
}
const r = await res.json();
return r.data.message;
}
export async function fetchLeaderBoard(id: string): Promise<any> {
const res = await fetch(`/api/leaderboard/${id}`);
if (!res.ok) {
const json = await res.json();
const message = json?.message || "Unknown error";
throw new APIError(`Failed to fetch leaderboard: ${message}`, res.status);
}
const r = await res.json();
return r.data;
}
export async function fetchCodes(
leaderboardId: number | string,
submissionIds: (number | string)[],
): Promise<any> {
const res = await fetch("/api/codes", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
leaderboard_id: leaderboardId,
submission_ids: submissionIds,
}),
});
if (!res.ok) {
const json = await res.json();
const message = json?.message || "Unknown error";
throw new APIError(`Failed to fetch news contents: ${message}`, res.status);
}
const r = await res.json();
return r.data;
}
export async function onDeleteSubmission(
submissionId?: string | number,
): Promise<void> {
if (!submissionId) {
throw new Error("No submission ID provided for deletion");
}
const res = await fetch(`/api/submission/${submissionId}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
});
if (!res.ok) {
const text = await res.text();
throw new Error(`Failed to delete submission: ${text}`);
}
}
export async function fetchAllNews(): Promise<any> {
const res = await fetch("/api/news");
if (!res.ok) {
const json = await res.json();
const message = json?.message || "Unknown error";
throw new APIError(`Failed to fetch news contents: ${message}`, res.status);
}
const r = await res.json();
return r.data;
}
export async function fetchLeaderboardSummaries(): Promise<any> {
const res = await fetch("/api/leaderboard-summaries");
if (!res.ok) {
const json = await res.json();
const message = json?.message || "Unknown error";
throw new APIError(
`Failed to fetch leaderboard summaries: ${message}`,
res.status,
);
}
const r = await res.json();
return r.data;
}
export async function getMe(): Promise<any> {
const res = await fetch("/api/me");
if (!res.ok) {
const json = await res.json();
const message = json?.message || "Unknown error";
throw new APIError(`Failed to fetch news contents: ${message}`, res.status);
}
const r = await res.json();
return r.data;
}
export async function logout(): Promise<any> {
const res = await fetch("/api/logout");
if (!res.ok) {
const json = await res.json();
const message = json?.message || "Unknown error";
throw new APIError(`Failed to fetch news contents: ${message}`, res.status);
}
const r = await res.json();
return r.data;
}
export async function submitFile(form: FormData) {
const resp = await fetch("/api/submission", {
method: "POST",
body: form,
});
const text = await resp.text();
let data: any;
try {
data = JSON.parse(text);
} catch {
data = { raw: text };
}
if (!resp.ok) {
const msg = data?.detail || data?.message || "Submission failed";
throw new Error(msg);
}
return data; // e.g. { submission_id, message, ... }
}
export async function fetchUserSubmissions(
leaderboardId: number | string,
userId: number | string,
page: number = 1,
pageSize: number = 10,
): Promise<any> {
const offset = (page - 1) * pageSize;
const res = await fetch(
`/api/submissions?leaderboard_id=${leaderboardId}&offset=${offset}&limit=${pageSize}`,
);
if (!res.ok) {
let message = "Unknown error";
try {
const json = await res.json();
message = json?.detail || json?.message || message;
} catch {
/* ignore */
}
throw new APIError(`Failed to fetch submissions: ${message}`, res.status);
}
const r = await res.json();
return r.data;
}