-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.ts
123 lines (104 loc) · 3.38 KB
/
api.ts
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
import {
API_ScheduleItem,
days,
Now_Playing_API,
Schedule_API,
Show as ShowType,
Show,
ShowSchedule
} from "@/lib/types";
import {NOW_PLAYING_ENDPOINT, SCHEDULE_ENDPOINT} from "@/lib/endpoints";
// Forms show object given a ScheduleItem from the API
function formShow(show: API_ScheduleItem): Show {
const start = new Date();
const end = new Date();
const duration = new Date();
let [h, m, s] = show.start_time.split(':').map(Number);
start.setHours(h, m, s);
[h, m, s] = show.end_time.split(':').map(Number);
end.setHours(h, m, s);
end.setSeconds(end.getSeconds() + 1);
[h, m, s] = show.duration.split(':').map(Number);
duration.setHours(h, m, s);
duration.setSeconds(duration.getSeconds() + 1);
return {
id: show.id,
day: show.day,
title: show.title,
description: show.description,
img: show.photo ? "http://api.burnfm.com/schedule_img/" + show.photo : "",
duration: duration,
start_time: start,
end_time: end,
hosts: show.hosts
};
}
// Gets a list of shows from the schedule. If a day is specified, it only gets that day's shows.
export async function getSchedule(day?: number): Promise<Show[]> {
let endpoint;
if (day !== undefined) {
endpoint = SCHEDULE_ENDPOINT + "?day=" + days[day];
} else {
endpoint = SCHEDULE_ENDPOINT;
}
const json = await fetchClient<Schedule_API>(endpoint);
return json.schedule
.map(scheduleItem => formShow(scheduleItem))
.toSorted((a, b) => a.start_time.getTime() < b.start_time.getTime() ? -1 : 1);
}
// Returns the current_show as well as a list of next_shows.
export async function getNowPlaying(): Promise<ShowSchedule> {
let json = await fetchClient<Now_Playing_API>(NOW_PLAYING_ENDPOINT);
const upNext = json.up_next.map(formShow)
const BurnOut: ShowType = {
id: -1,
day: days[new Date().getDay()],
duration: new Date(),
title: "BurnOut",
description: "This is the pulse of Birmingham's campus with nonstop tunes 24/7.",
start_time: new Date(),
end_time: upNext? upNext[0].start_time : new Date(),
img: "",
hosts: ["Burn FM"]
}
return {
current_show: json.now_playing.length ? formShow(json.now_playing[0]) : BurnOut,
next_shows: upNext
}
}
// Retrieve a show from the API.
export async function getShow(id: number) {
const shows = await getSchedule();
const occurrences = shows.filter(show => show.id === id);
if (occurrences.length == 0)
throw Error("404 - not found");
return occurrences[0] // right now only gets first, worth editing for if a show runs multiple times a week
}
interface FetchOptions extends RequestInit {
headers?: Record<string, string>;
}
export async function fetchClient<T>(url: string, options?: FetchOptions): Promise<T> {
try {
const response = await fetch(url, {
...options,
headers: {
...options?.headers,
},
});
if (!response.ok) {
// Parse error response
const errorMessage = await response.text();
throw new Error(
`Error: ${response.status} - ${response.statusText} - ${errorMessage}`
);
}
// Safely parse JSON if content exists
if (response.status !== 204) {
return await response.json();
}
return {} as T; // Return empty object for no content
} catch (error: any) {
// Handle network or parsing errors
throw new Error(error.message || 'An unexpected error occurred');
}
}