mirror of
https://github.com/r-freeman/portfolio.git
synced 2024-11-22 18:15:41 +00:00
Converted js to typescript
This commit is contained in:
parent
83e11ce163
commit
914493f214
@ -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) {
|
@ -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
|
||||
})
|
||||
}
|
59
pages/api/spotify/last-played.ts
Normal file
59
pages/api/spotify/last-played.ts
Normal 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
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue
Block a user