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