export type Response = | { type: "Success"; content: A } | { type: "Failure"; content: string } | { type: "Fatal"; content: string }; export interface TrackInfo { id: string; track_number?: number; duration?: number; name?: string; album?: string; artist?: string; } export const getTracks = (): Promise => fetch("/api/v1/tracks") .then((r) => r.json()) .then((result: Response) => { switch (result.type) { case "Success": return result.content || []; case "Failure": console.log("failed: ", result.content); return []; case "Fatal": console.log("fatal: ", result.content); return []; } }); export const playTrack = (id: string): Promise => fetch("/api/v1/play", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ id: id }), }) .then((r) => r.json()) .then((result: Response) => { console.log("result: ", result); });