Skip to content

Commit

Permalink
Merge pull request #131 from VictorOffspring/fix/fix_getPreview
Browse files Browse the repository at this point in the history
check null getImages function which is call on getPreview
  • Loading branch information
Kikobeats authored Nov 17, 2024
2 parents 3c70dd8 + 2e81e5d commit acec3e2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ function getParsedUrl (url) {
}
}

const getImages = data => data.coverArt?.sources || data.images
const getImages = data =>
data.coverArt?.sources || data.images || data.visualIdentity.image

const getDate = data => data.releaseDate?.isoString || data.release_date

Expand Down Expand Up @@ -123,7 +124,7 @@ function getPreview (data) {
track: track.name,
description: data.description || data.subtitle || track.description,
artist: track.artist,
image: getImages(data).reduce((a, b) => (a.width > b.width ? a : b)).url,
image: getImages(data)?.reduce((a, b) => (a.width > b.width ? a : b))?.url,
audio: track.previewUrl,
link: getLink(data),
embed: `https://embed.spotify.com/?uri=${data.uri}`
Expand Down
6 changes: 6 additions & 0 deletions test/get-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,9 @@ test('list multiple artists as one', async t => {
const preview = await getPreview(url)
t.is(preview.artist, 'C. Tangana, Niño de Elche & La Húngara')
})

test('get preview for spotify track with no cover', async t => {
const url = 'https://open.spotify.com/track/4AjDdThsTlHF90gZTYVZzR'
const preview = await getPreview(url)
t.true(preview.image.includes('://'))
})

0 comments on commit acec3e2

Please sign in to comment.