diff --git a/pages/api/spotify/currently-playing.js b/pages/api/spotify/currently-playing.ts similarity index 58% rename from pages/api/spotify/currently-playing.js rename to pages/api/spotify/currently-playing.ts index 208096d..62c212b 100644 --- a/pages/api/spotify/currently-playing.js +++ b/pages/api/spotify/currently-playing.ts @@ -1,6 +1,30 @@ import {getCurrentlyPlaying} from '@/lib/spotify' +import {NextApiRequest, NextApiResponse} from 'next' -export default async function handler(req, res) { +type Song = { + item: { + album: { + name: string + images: [ + { + url: string + } + ] + } + artists: [ + { + name: string + } + ] + external_urls: { + spotify: string + } + name: string + } + is_playing: boolean +} + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { const response = await getCurrentlyPlaying() if (response.status === 204 || response.status > 400) { @@ -9,7 +33,7 @@ export default async function handler(req, res) { }) } - const song = await response.json() + const song = await response.json() as Song const {item} = song if (item === null) { diff --git a/pages/api/spotify/last-played.js b/pages/api/spotify/last-played.js deleted file mode 100644 index 79efa17..0000000 --- a/pages/api/spotify/last-played.js +++ /dev/null @@ -1,31 +0,0 @@ -import {getRecentlyPlayed} from '@/lib/spotify' - -export default async function handler(req, res) { - const response = await getRecentlyPlayed() - - if (response.status > 400) { - return res.status(200).json({}) - } - - const tracks = await response.json(); - - if (tracks === null) { - return res.status(200).json({}) - } - - const {track} = tracks.items.reduce((r, a) => r.played_at > a.played_at ? r : a) - - const title = track.name; - const artist = track.artists.map(artist => artist.name).join(', ') - const songUrl = track.external_urls.spotify - const album = track.album.name - const albumImageUrl = track.album.images[0].url - - return res.status(200).json({ - artist, - title, - songUrl, - album, - albumImageUrl - }) -} \ No newline at end of file diff --git a/pages/api/spotify/last-played.ts b/pages/api/spotify/last-played.ts new file mode 100644 index 0000000..88d611e --- /dev/null +++ b/pages/api/spotify/last-played.ts @@ -0,0 +1,59 @@ +import {getRecentlyPlayed} from '@/lib/spotify' +import {NextApiRequest, NextApiResponse} from 'next' + +type Tracks = { + items: [ + { + track: { + name: string + artists: [ + { + name: string + } + ] + external_urls: { + spotify: string + } + album: { + name: string + images: [ + { + url: string + } + ] + } + } + played_at: string + } + ] +} + +export default async function handler(req: NextApiRequest, res: NextApiResponse) { + const response = await getRecentlyPlayed() + + if (response.status > 400) { + return res.status(200).json({}) + } + + const tracks = await response.json() as Tracks + + if (tracks === null) { + return res.status(200).json({}) + } + + const {track} = tracks.items.reduce((r, a) => r.played_at > a.played_at ? r : a) + + const title = track.name; + const artist = track.artists.map(artist => artist.name).join(', ') + const songUrl = track.external_urls.spotify + const album = track.album.name + const albumImageUrl = track.album.images[0].url + + return res.status(200).json({ + artist, + title, + songUrl, + album, + albumImageUrl + }) +} \ No newline at end of file