From 414627555af76f0ba63e56857a94dfbb940eed21 Mon Sep 17 00:00:00 2001 From: Savanni D'Gerinel Date: Sat, 11 Mar 2023 15:03:06 -0500 Subject: [PATCH] Handle the result of server calls --- music-player/client/src/client.ts | 28 ++++++++++++++++++++++++--- music-player/server/src/bin/server.rs | 2 ++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/music-player/client/src/client.ts b/music-player/client/src/client.ts index c39a0ea..315d320 100644 --- a/music-player/client/src/client.ts +++ b/music-player/client/src/client.ts @@ -1,3 +1,8 @@ +export type Response = + | { type: "Success"; content: A } + | { type: "Failure"; content: string } + | { type: "Fatal"; content: string }; + export interface TrackInfo { id: string; track_number?: number; @@ -8,11 +13,28 @@ export interface TrackInfo { } export const getTracks = (): Promise => - fetch("/api/v1/tracks").then((r) => r.json()); + 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 => +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); + }); diff --git a/music-player/server/src/bin/server.rs b/music-player/server/src/bin/server.rs index ca69e4a..34c5c47 100644 --- a/music-player/server/src/bin/server.rs +++ b/music-player/server/src/bin/server.rs @@ -16,11 +16,13 @@ use std::{ use warp::Filter; #[derive(Clone, Debug, Deserialize)] +#[serde(rename_all = "camelCase")] struct TrackRequest { id: String, } #[derive(Serialize)] +#[serde(tag = "type", content = "content")] enum Response { Success(A), Failure(String),