Personal Site

Create types for use on the client to type data from sse

vielle.dev b6e76820 a98cf368

verified
+41 -2
+41 -2
src/components/home/playing/spotify/client.ts
··· 1 - /** 1 + /** 2 2 * types & type guards for front end logic when received from now-playing-sse 3 - */ 3 + */ 4 + 5 + import { isObj } from "/utils"; 6 + 7 + export type nowPlaying = { 8 + id: string; 9 + name: string; 10 + href: string; 11 + album: string; 12 + art: string; 13 + artists: { 14 + name: string; 15 + href: string; 16 + }[]; 17 + } | null; 18 + 19 + export const isNowPlaying = (playing: any): playing is nowPlaying => 20 + playing === null || 21 + (isObj(playing) && 22 + "id" in playing && 23 + typeof playing.id === "string" && 24 + "name" in playing && 25 + typeof playing.name === "string" && 26 + "href" in playing && 27 + typeof playing.href === "string" && 28 + "album" in playing && 29 + typeof playing.album === "string" && 30 + "art" in playing && 31 + typeof playing.art === "string" && 32 + "artists" in playing && 33 + Array.isArray(playing.artists) && 34 + playing.artists.reduce( 35 + (acc, curr) => 36 + acc && 37 + "name" in curr && 38 + typeof curr.name === "string" && 39 + "href" in curr && 40 + typeof curr.href === "string", 41 + ), 42 + true);