Converted js to typescript

This commit is contained in:
r-freeman 2023-02-12 18:41:17 +00:00
parent 83e11ce163
commit 914493f214
3 changed files with 85 additions and 33 deletions

View File

@ -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) {

View File

@ -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
})
}

View File

@ -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
})
}