-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.ts
53 lines (48 loc) · 1.07 KB
/
api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import fetch from 'node-fetch'
export const playlistIDs: string[] = [
'spotify:playlist:1IoSlQbiD7OZ7APX3tviuX',
'spotify:playlist:0Qf5El4jvjpUnsJ4YZdTKo',
'spotify:playlist:1EOj4DNc4LJD6ujTkFu6N7',
]
export interface PlaylistData {
id: string
images: {
height: number
url: string
}[]
name: string
external_urls: {
spotify: string
}
}
const base = 'https://api.spotify.com/v1'
function formatPlaylistId(id: string): string {
const parts = id.split(':')
if (parts.length === 1) {
return id
}
if (parts.length === 3 && parts[1] === 'playlist') {
return parts[2]
}
throw new Error(`unknown id: ${id}`)
}
export async function getPlaylist(
token: string,
id: string
): Promise<PlaylistData> {
id = formatPlaylistId(id)
try {
const response = await fetch(`${base}/playlists/${id}`, {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
},
})
if (!response.ok) {
throw new Error(await response.text())
}
return await response.json()
} catch (err) {
console.error(err)
}
}