diff --git a/src/pages/api/spotify.ts b/src/pages/api/nowplaying.ts similarity index 92% rename from src/pages/api/spotify.ts rename to src/pages/api/nowplaying.ts index b672b33..6c64325 100644 --- a/src/pages/api/spotify.ts +++ b/src/pages/api/nowplaying.ts @@ -21,11 +21,13 @@ export default async function handler( ) { try { const song = await spotify.getCurrentSong() + if (!song || !song.is_playing) { return res.status(200).json({ is_playing: false, }) } + res.status(200).json({ is_playing: true, album: { @@ -43,8 +45,8 @@ export default async function handler( duration: song.duration, }) } catch (error) { - res - .status(500) - .json({ error: 'An error occurred while fetching the song.' }) + res.status(500).json({ + error: 'An error occurred while fetching the song.', + }) } } diff --git a/src/service/spotify.ts b/src/service/spotify.ts index 7fcc0a1..6839055 100644 --- a/src/service/spotify.ts +++ b/src/service/spotify.ts @@ -42,7 +42,7 @@ export class SpotifyService { } } - public async getCurrentSong(): Promise { + public async getCurrentSong(): Promise { try { if (!this.hasAccessToken()) { await this.getAccessToken() @@ -61,9 +61,8 @@ export class SpotifyService { }) return SongResultMap.parseSong(response.data) - } catch (error) { + } catch { await this.getAccessToken() - throw error } } }