Personal Site

Make server sent events follow the new type

vielle.dev 5bf8049e 0cf6c7d8

verified
+20 -13
+20 -13
src/pages/now-playing-sse.ts
··· 1 - import { spotifyNowPlaying, SpotifyError } from "/components/playing/spotify"; 1 + import { 2 + spotifyNowPlaying, 3 + SpotifyError, 4 + type nowPlaying, 5 + } from "/components/playing/spotify"; 2 6 3 7 export async function GET() { 4 8 const update = async (): Promise<string> => { 5 9 // extract a subset to reduce size for client 6 10 // not huge savings but tesco yk 7 - const playing = await spotifyNowPlaying() 11 + // + reduces chance of leaking extra data to client 12 + const playing: SpotifyError | nowPlaying = await spotifyNowPlaying() 8 13 .then((playing) => 9 14 playing 10 15 ? { 11 - type: "track", 12 - id: playing.id, 16 + type: "track" as const, 13 17 name: playing.name, 18 + external_urls: { 19 + spotify: playing.external_urls.spotify, 20 + }, 14 21 album: { 15 - images: playing.album.images.map((x) => ({ 16 - url: x.url, 17 - width: x.width, 18 - height: x.height, 19 - })), 22 + name: playing.album.name, 23 + external_urls: { 24 + spotify: playing.album.external_urls.spotify, 25 + }, 26 + images: [playing.album.images[0]], 20 27 }, 21 - artists: playing.artists.map((x) => ({ 22 - name: x.name, 23 - id: x.id, 28 + artists: playing.artists.map((artist) => ({ 29 + external_urls: { spotify: artist.external_urls.spotify }, 30 + name: artist.name, 24 31 })), 25 32 } 26 33 : null, ··· 29 36 if (!(err instanceof SpotifyError)) 30 37 throw new Error("Unhandled exception"); 31 38 if (err.code === "NO_CONTENT") return null; 32 - 39 + 33 40 console.error(err.code, err.human, err.details); 34 41 return err; 35 42 });