refactor: Track class

This commit is contained in:
Mateusz Pstrucha committed 2021-09-13 21:44:45 +02:00
1 parent 27e76f7270
commit af9934d476
2 files changed
+19 -17

No files matched your search

+5 -15
View File
@@ -1,25 +1,15 @@
class Track {
constructor(data) {
this.id = data.id
this.title = data.name
this.title = data.title
this.artists = data.artists.map((a) => a.name)
this.artists = data.artists
this.album = {
name: data.album.name,
art: data.album.images[0].url,
year: data.album.release_date.slice(0, 4),
}
this.album = data.album
this.duration = data.duration_ms
this.duration = data.duration
this.explicit = data.explicit
this.banned = false
this.votes = {
up: 100,
down: 50,
rank: 1,
}
this.banned = data.banned ?? false
}
}
+14 -2
View File
@@ -23,11 +23,23 @@ function byId(id) {
return new Promise((resolve, reject) => {
if (typeof id != 'string')
return reject(new Error('Incorrect id, must by type String'))
if (id.length < 22) return reject(new Error('Id too short'))
spotify
.getTrack(id)
.then((data) => {
resolve(new Track(data.body))
.then(({ body }) => {
let tdata = {
...body,
title: body.name,
artists: body.artists.map((a) => a.name),
album: {
name: body.album.name,
art: body.album.images[0].url,
year: body.album.release_date.slice(0, 4),
},
duration: body.duration_ms,
}
resolve(new Track(tdata))
})
.catch((err) => {
reject(err)