-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Spotify): Add support for Spotify (#29)
* feat: add support for spotify * Add docs & fix export * fix: remove invalid URL * feat: add support for embedding spotify track * fix: add separate method for fetching Iframe url and more support of spotify url * Cleanup docs * Cleanup code * Update + add tests * fix: tests and refactor code * Update snapshots * fix: requested change * fix: requested changes
- Loading branch information
1 parent
5cdb453
commit 55aa248
Showing
8 changed files
with
198 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { URL } from 'url'; | ||
|
||
const includesSomeOfArray = (string, array) => | ||
array.some(item => string.includes(item)); | ||
|
||
export const shouldTransform = url => { | ||
const { host, pathname } = new URL(url); | ||
|
||
return ( | ||
host === 'open.spotify.com' && | ||
!includesSomeOfArray(pathname, ['embed', 'embed-podcast']) && | ||
includesSomeOfArray(pathname, [ | ||
'/album/', | ||
'/artist/', | ||
'/episode/', | ||
'/show/', | ||
'/track/', | ||
'/playlist/', | ||
]) | ||
); | ||
}; | ||
|
||
export const getSpotifyIFrameSrc = urlString => { | ||
const { pathname } = new URL(urlString); | ||
const type = pathname.split('/')[1].toLowerCase(); | ||
|
||
const podcastTypes = ['episode', 'show']; | ||
if (podcastTypes.includes(type)) { | ||
return urlString.replace(type, `embed-podcast/${type}`); | ||
} | ||
|
||
return urlString.replace(type, `embed/${type}`); | ||
}; | ||
|
||
export const getHTML = url => { | ||
const iframeSrc = getSpotifyIFrameSrc(url); | ||
|
||
return `<iframe src="${iframeSrc}" width="100%" height="380" frameborder="0" allowtransparency="true" allow="encrypted-media"></iframe>`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import cases from 'jest-in-case'; | ||
|
||
import { getHTML, getSpotifyIFrameSrc, shouldTransform } from '../Spotify'; | ||
|
||
cases( | ||
'url validation', | ||
({ url, valid }) => { | ||
expect(shouldTransform(url)).toBe(valid); | ||
}, | ||
{ | ||
'non-Spotify url': { | ||
url: 'https://not-a-spotify-url.com', | ||
valid: false, | ||
}, | ||
"non-Spotify url ending with 'spotify.com'": { | ||
url: 'https://this-is-not-spotify.com', | ||
valid: false, | ||
}, | ||
"api url having '/album/'": { | ||
url: 'https://api.spotify.com/album/1DFixLWuPkv3KT3TnV35m3', | ||
valid: false, | ||
}, | ||
'embed album url': { | ||
url: 'https://open.spotify.com/embed/album/254Y0CD07dB40q84db89EB', | ||
valid: false, | ||
}, | ||
'embed artist url': { | ||
url: 'https://open.spotify.com/embed/artist/0QaSiI5TLA4N7mcsdxShDO', | ||
valid: false, | ||
}, | ||
'embed episode url': { | ||
url: | ||
'https://open.spotify.com/embed-podcast/episode/0j9RE1H47GSmBnRqOtf1dx', | ||
valid: false, | ||
}, | ||
'embed playlist url': { | ||
url: 'https://open.spotify.com/embed/playlist/37i9dQZF1DX5wDmLW735Yd', | ||
valid: false, | ||
}, | ||
'embed show url': { | ||
url: 'https://open.spotify.com/embed-podcast/show/7GkO2poedjbltWT5lduL5w', | ||
valid: false, | ||
}, | ||
'embed track url': { | ||
url: 'https://open.spotify.com/embed/track/0It2bnTdLl2vyymzOkBI3L', | ||
valid: false, | ||
}, | ||
'album url': { | ||
url: 'https://open.spotify.com/album/254Y0CD07dB40q84db89EB', | ||
valid: true, | ||
}, | ||
'artist url': { | ||
url: 'https://open.spotify.com/artist/0QaSiI5TLA4N7mcsdxShDO', | ||
valid: true, | ||
}, | ||
'episode url': { | ||
url: 'https://open.spotify.com/episode/0j9RE1H47GSmBnRqOtf1dx', | ||
valid: true, | ||
}, | ||
'playlist url': { | ||
url: 'https://open.spotify.com/playlist/37i9dQZF1DX5wDmLW735Yd', | ||
valid: true, | ||
}, | ||
'show url': { | ||
url: 'https://open.spotify.com/show/7GkO2poedjbltWT5lduL5w', | ||
valid: true, | ||
}, | ||
'track url': { | ||
url: 'https://open.spotify.com/track/0It2bnTdLl2vyymzOkBI3L', | ||
valid: true, | ||
}, | ||
} | ||
); | ||
|
||
cases( | ||
'get spotify iframe', | ||
({ url, iframe }) => { | ||
expect(getSpotifyIFrameSrc(url)).toBe(iframe); | ||
}, | ||
{ | ||
'album url': { | ||
url: 'https://open.spotify.com/album/254Y0CD07dB40q84db89EB', | ||
iframe: 'https://open.spotify.com/embed/album/254Y0CD07dB40q84db89EB', | ||
}, | ||
'artist url': { | ||
url: 'https://open.spotify.com/artist/0QaSiI5TLA4N7mcsdxShDO', | ||
iframe: 'https://open.spotify.com/embed/artist/0QaSiI5TLA4N7mcsdxShDO', | ||
}, | ||
'episode url': { | ||
url: 'https://open.spotify.com/episode/0j9RE1H47GSmBnRqOtf1dx', | ||
iframe: | ||
'https://open.spotify.com/embed-podcast/episode/0j9RE1H47GSmBnRqOtf1dx', | ||
}, | ||
'playlist url': { | ||
url: 'https://open.spotify.com/playlist/37i9dQZF1DX5wDmLW735Yd', | ||
iframe: 'https://open.spotify.com/embed/playlist/37i9dQZF1DX5wDmLW735Yd', | ||
}, | ||
'show url': { | ||
url: 'https://open.spotify.com/show/7GkO2poedjbltWT5lduL5w', | ||
iframe: | ||
'https://open.spotify.com/embed-podcast/show/7GkO2poedjbltWT5lduL5w', | ||
}, | ||
'track url': { | ||
url: 'https://open.spotify.com/track/0It2bnTdLl2vyymzOkBI3L', | ||
iframe: 'https://open.spotify.com/embed/track/0It2bnTdLl2vyymzOkBI3L', | ||
}, | ||
} | ||
); | ||
|
||
test('Gets the correct Spotify iframe for Track', () => { | ||
const html = getHTML('https://open.spotify.com/track/0It2bnTdLl2vyymzOkBI3L'); | ||
|
||
expect(html).toMatchInlineSnapshot( | ||
`"<iframe src=\\"https://open.spotify.com/embed/track/0It2bnTdLl2vyymzOkBI3L\\" width=\\"100%\\" height=\\"380\\" frameborder=\\"0\\" allowtransparency=\\"true\\" allow=\\"encrypted-media\\"></iframe>"` | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
https://open.spotify.com/track/0It2bnTdLl2vyymzOkBI3L |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters