summaryrefslogtreecommitdiff
path: root/src/connector.js
blob: 1fa513bf600ebc9f014ce32694701c68f66f85f3 (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
class APIConnector {
	constructor(url) {
		this.url = url ?? ""
	}

	#fetch(endpoint) {
		return fetch(this.url + "/api" + endpoint)
	}

	fetch(endpoint) {
		return this.#fetch(endpoint)
			.then(response => {
				if (!response.ok)
					throw new Error(`HTTP error! Status: ${response.status}`)
				return response.json()
			})
			.then(data => {
				return data
			})
			.catch(error => {
				console.error("Fetch error:", error)
			})
	}

	seek(increment) {
		return this.fetch(`/seek?increment=${increment}`)
	}

	fetchAlbums() {
		return this.fetch("/albums/get")
	}

	albumThumbSrc(key) {
		return `${this.url}/api/albums/thumb/${key}`
	}
}

const apiConnector = new APIConnector("http://localhost:5000")
export default apiConnector