import io from "socket.io-client" class APIConnector { constructor(url) { this.url = url ?? "" this.socket = io(url) this.asset_index = 0 this.movement = 0 this.assets = null this.seekCallbacks = [] this.socket.on("seek", e => { this.assetIndex = e.asset_index this.movement = e.movement this.assets = e.assets for (const cb of this.seekCallbacks) cb() }) } fetch(endpoint, c) { return fetch(this.url + "/api" + endpoint, c ?? {}) .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) }) } post(endpoint, body) { return this.fetch(endpoint, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }) } seek(increment) { this.socket.emit("seek", increment) } fetchAlbums() { return this.fetch("/albums") } fetchConfig() { return this.fetch("/config") } updateConfig(config) { return this.post("/config/update", config) } albumSrc(key) { return `${this.url}/api/redirect/albums/${key}` } assetSrc(key) { return `${this.url}/api/asset/${key}` } assetThumbnailSrc(key) { return `${this.url}/api/asset/${key}/thumbnail` } } const apiConnector = new APIConnector("http://localhost:5000") export default apiConnector