-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathapi.ts
More file actions
49 lines (45 loc) · 1.37 KB
/
api.ts
File metadata and controls
49 lines (45 loc) · 1.37 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
import { IEvent } from '@/components/Events/events';
import { transformApiDates } from '@/utils/dateutils';
import axios from 'axios';
const baseUrl = process.env.NEXT_PUBLIC_CORE_API_BASE_URL || '';
export async function fetchUpcomingEvents() {
try {
const response = await fetch(
`${baseUrl}/api/v1/events?upcomingOnly=true&featuredOnly=true&includeMetadata=true`,
);
const rawDates = (await response.json()) as IEvent[];
return transformApiDates(rawDates);
} catch (err: any) {
return [];
}
}
export const syncIdentity = async (
accessToken: string,
force: boolean = false,
) => {
// If this fails we don't care its just best effort.
if (!force) {
const syncRequired = await checkIfSyncNeeded(accessToken);
if (!syncRequired) {
return;
}
}
const url = `${baseUrl}/api/v1/syncIdentity`;
return await axios
.post(url, {}, { headers: { 'x-uiuc-token': accessToken } })
.then(() => {
console.log('Synced user identity');
})
.catch((error) => {
console.error(`Failed to sync user identity: ${error}`);
});
};
export const checkIfSyncNeeded = async (
accessToken: string,
): Promise<boolean> => {
const url = `${baseUrl}/api/v1/syncIdentity/isRequired`;
const response = await axios.get(url, {
headers: { 'x-uiuc-token': accessToken },
});
return response.data.syncRequired ?? false;
};