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

50 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-03-11 19:46:51 +00:00
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[]> =>
2023-03-11 19:46:51 +00:00
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 [];
}
});
2023-03-11 19:46:51 +00:00
export const playTrack = (id: string): Promise<void> =>
fetch("/api/v1/play", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ id: id }),
2023-03-11 19:46:51 +00:00
})
.then((r) => r.json())
.then((result: Response<null>) => {
console.log("result: ", result);
});
2023-03-11 20:08:39 +00:00
export const stopPlayback = (): Promise<void> =>
fetch("api/v1/stop", {
method: "POST",
})
.then((r) => r.json())
.then((result: Response<null>) => {
console.log("result: ", result);
});