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) { 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}`) this.socket.emit("seek", increment) } fetchAlbums() { return this.fetch("/albums") } 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