monorepo/music-player/client/src/client.ts

50 lines
1.2 KiB
TypeScript

export type Response<A> =
| { 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<TrackInfo[]> =>
fetch("/api/v1/tracks")
.then((r) => r.json())
.then((result: Response<TrackInfo[]>) => {
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<void> =>
fetch("/api/v1/play", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ id: id }),
})
.then((r) => r.json())
.then((result: Response<null>) => {
console.log("result: ", result);
});
export const stopPlayback = (): Promise<void> =>
fetch("api/v1/stop", {
method: "POST",
})
.then((r) => r.json())
.then((result: Response<null>) => {
console.log("result: ", result);
});