aboutsummaryrefslogtreecommitdiff
path: root/web/src/lib/api.ts
blob: 032cbd454560a8d0fdb4e824eb6f35ba131e5825 (plain)
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
export type Song = {
	title: string;
	artist: string;
	source: string;
	uri: string;
	weight: number;
};

export type WsMessage = {
	cmd: 'add' | 'update' | 'pause' | 'play' | 'upvoted' | 'downvoted';
	songs: Array<Song>;
};
export type SearchResponse = {
	cmd: 'search';
	songs: Array<Song>;
};

const HEADERS = new Headers([
	['Accept', 'application/json'],
	['Content-Type', 'application/json']
]);
const API = {
	baseUrl: '',
	get(url: string, body?: Record<string, string>) {
		if (body !== undefined) {
			url += '?' + new URLSearchParams(body);
		}
		return fetch(this.baseUrl + url, {
			method: 'GET'
		});
	},
	post(url: string, body?: object) {
		return fetch(this.baseUrl + url, {
			method: 'POST',
			body: body && JSON.stringify(body),
			headers: HEADERS
		});
	}
};
export default API;