portfolio/app/api/spotify/currently-playing/route.ts

54 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-07-29 22:40:36 +00:00
import {NextResponse} from 'next/server'
2022-12-06 12:54:34 +00:00
import {getCurrentlyPlaying} from '@/lib/spotify'
2023-02-12 18:41:17 +00:00
type Song = {
item: {
album: {
name: string
images: [
{
url: string
}
]
}
artists: [
{
name: string
}
]
external_urls: {
spotify: string
}
name: string
}
is_playing: boolean
}
2023-07-29 22:40:36 +00:00
export async function GET(request: Request) {
2022-12-06 12:54:34 +00:00
const response = await getCurrentlyPlaying()
if (response.status === 204 || response.status > 400) {
2023-07-29 22:40:36 +00:00
return new Response(JSON.stringify({isPlaying: false}), {
status: 200
2022-12-06 12:54:34 +00:00
})
}
2023-02-12 18:41:17 +00:00
const song = await response.json() as Song
2022-12-06 12:54:34 +00:00
const {item} = song
const artist = item.artists.map(artist => artist.name).join(', ')
const title = item.name;
const songUrl = item.external_urls.spotify
const album = item.album.name
const albumImageUrl = item.album.images[0].url
const isPlaying = song.is_playing;
2023-07-29 22:40:36 +00:00
return NextResponse.json({
2022-12-06 12:54:34 +00:00
artist,
title,
songUrl,
album,
albumImageUrl,
isPlaying
})
}