-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathapi.ts
More file actions
65 lines (52 loc) · 2.04 KB
/
api.ts
File metadata and controls
65 lines (52 loc) · 2.04 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
import { Competition } from '@wca/helpers';
import { WCA_ORIGIN } from './wca-env';
export const wcaApiFetch = async <T>(path: string, fetchOptions: RequestInit = {}) => {
const res = await fetch(`${WCA_ORIGIN}${path}`, fetchOptions);
if (!res.ok) {
const error = await res.text();
throw new Error(error);
}
return (await res.json()) as T;
};
export const fetchMe = async (
accessToken: string,
params: {
upcoming_competitions?: boolean;
ongoing_competitions?: boolean;
} = {},
) => {
const urlParams = new URLSearchParams({
upcoming_competitions: params.upcoming_competitions ? 'true' : 'false',
ongoing_competitions: params.ongoing_competitions ? 'true' : 'false',
});
return wcaApiFetch<{
me: User;
ongoing_competitions?: ApiCompetition[];
upcoming_competitions?: ApiCompetition[];
}>(`/me?${urlParams}`, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
});
};
export const fetchUser = async (userId: string) => wcaApiFetch<{ user: User }>(`/users/${userId}`);
const fetchUserWithCompetitionsParams = new URLSearchParams({
upcoming_competitions: 'true',
ongoing_competitions: 'true',
});
export interface UserCompsResponse {
user: User;
upcoming_competitions: ApiCompetition[];
ongoing_competitions: ApiCompetition[];
}
export const fetchUserWithCompetitions = async (userId: string) =>
wcaApiFetch<UserCompsResponse>(`/users/${userId}?${fetchUserWithCompetitionsParams.toString()}`);
export const fetchWcif = async (competitionId: string) =>
wcaApiFetch<Competition>(`/competitions/${competitionId}/wcif/public`);
export const fetchCompetition = async (competitionId: string) =>
await wcaApiFetch<ApiCompetition>(`/competitions/${competitionId}`);
export const fetchCompetitionTabs = async (competitionId: string) =>
await wcaApiFetch<ApiCompetitionTab[]>(`/competitions/${competitionId}/tabs`);
export const fetchSearchCompetition = (search: string) =>
wcaApiFetch<{ result: ApiCompetition[] }>(`/search/competitions?q=${search}`);