diff options
| author | gammelalf <gammelalf@hopfen.space> | 2023-01-23 03:12:21 +0100 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2023-02-24 12:47:40 +0100 |
| commit | 7b9d908d2c1612843990dd39decbdbf40e51dbd7 (patch) | |
| tree | 540f2c6aaf25f9251e4666dc56f0afbf85b2944c /web/src/lib/api.ts | |
| parent | 95f95783383a58af5250bcb66155a04971385b75 (diff) | |
| download | WRMS-7b9d908d2c1612843990dd39decbdbf40e51dbd7.tar.gz WRMS-7b9d908d2c1612843990dd39decbdbf40e51dbd7.zip | |
Reimplemented current frontend in svelte
Diffstat (limited to 'web/src/lib/api.ts')
| -rw-r--r-- | web/src/lib/api.ts | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/web/src/lib/api.ts b/web/src/lib/api.ts new file mode 100644 index 0000000..032cbd4 --- /dev/null +++ b/web/src/lib/api.ts @@ -0,0 +1,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; |
