diff --git a/src/index.tsx b/src/index.tsx index 51c5063..5118503 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -4,7 +4,6 @@ import isEqual from '@gilbarbara/deep-equal'; import memoize from 'memoize-one'; import { - getArrayOfStrings, getItemImage, getLocale, getMergedStyles, @@ -13,7 +12,7 @@ import { getSpotifyURIType, getTrackInfo, } from '~/modules/getters'; -import { loadSpotifyPlayer, parseVolume, round, validateURI } from '~/modules/helpers'; +import { loadSpotifyPlayer, parseIds, parseVolume, round, validateURI } from '~/modules/helpers'; import { getDevices, getPlaybackState, @@ -219,7 +218,7 @@ class SpotifyWebPlayer extends PureComponent { uris, } = this.props; const isReady = previousState.status !== STATUS.READY && status === STATUS.READY; - const playOptions = this.getPlayOptions(getArrayOfStrings(uris)); + const playOptions = this.getPlayOptions(parseIds(uris)); const canPlay = !!currentDeviceId && !!(playOptions.context_uri ?? playOptions.uris); const shouldPlay = isReady && (autoPlay || playProp); @@ -832,7 +831,7 @@ class SpotifyWebPlayer extends PureComponent { private toggleOffset = async () => { const { currentDeviceId } = this.state; const { offset, uris } = this.props; - const playOptions = this.getPlayOptions(getArrayOfStrings(uris)); + const playOptions = this.getPlayOptions(parseIds(uris)); if (typeof offset === 'number') { await play(this.token, { deviceId: currentDeviceId, offset, ...playOptions }); @@ -843,7 +842,7 @@ class SpotifyWebPlayer extends PureComponent { const { currentDeviceId, isPlaying, needsUpdate } = this.state; const { offset, uris } = this.props; const shouldInitialize = force || needsUpdate; - const playOptions = this.getPlayOptions(getArrayOfStrings(uris)); + const playOptions = this.getPlayOptions(parseIds(uris)); try { if (this.isExternalPlayer) { diff --git a/src/modules/getters.ts b/src/modules/getters.ts index 3f361a8..a126c91 100644 --- a/src/modules/getters.ts +++ b/src/modules/getters.ts @@ -1,5 +1,5 @@ /* eslint-disable camelcase */ -import { validateURI } from '~/modules/helpers'; +import { parseIds, validateURI } from '~/modules/helpers'; import { getAlbumTracks, getArtistTopTracks, @@ -73,7 +73,7 @@ export async function getPreloadData( uris: IDs, offset: number, ): Promise { - const parsedURIs = getArrayOfStrings(uris); + const parsedURIs = parseIds(uris); const uri = parsedURIs[offset]; if (!validateURI(uri)) { @@ -188,11 +188,3 @@ export function getTrackInfo(track: Spotify.Track | SpotifyApi.TrackObjectFull): uri, }; } - -export function getArrayOfStrings(ids: IDs): string[] { - if (!ids) { - return []; - } - - return Array.isArray(ids) ? ids : [ids]; -} diff --git a/src/modules/helpers.ts b/src/modules/helpers.ts index 85393b3..870bf14 100644 --- a/src/modules/helpers.ts +++ b/src/modules/helpers.ts @@ -1,4 +1,5 @@ import { SPOTIFY_CONTENT_TYPE } from '~/constants'; +import { IDs } from '~/types'; export function isNumber(value: unknown): value is number { return typeof value === 'number'; @@ -46,6 +47,14 @@ export function millisecondsToTime(input: number) { return parts.join(':'); } +export function parseIds(ids: IDs): string[] { + if (!ids) { + return []; + } + + return Array.isArray(ids) ? ids : [ids]; +} + export function parseVolume(value?: unknown): number { if (!isNumber(value)) { return 1; diff --git a/src/modules/spotify.ts b/src/modules/spotify.ts index 45da341..a3f118c 100644 --- a/src/modules/spotify.ts +++ b/src/modules/spotify.ts @@ -1,10 +1,10 @@ /* eslint-disable camelcase */ +import { parseIds } from '~/modules/helpers'; + import { IDs, RepeatState, SpotifyPlayOptions } from '~/types'; export async function checkTracksStatus(token: string, tracks: IDs): Promise { - const ids = Array.isArray(tracks) ? tracks : [tracks]; - - return fetch(`https://api.spotify.com/v1/me/tracks/contains?ids=${ids}`, { + return fetch(`https://api.spotify.com/v1/me/tracks/contains?ids=${parseIds(tracks)}`, { headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json', @@ -202,10 +202,8 @@ export async function previous(token: string, deviceId?: string): Promise } export async function removeTracks(token: string, tracks: IDs): Promise { - const ids = Array.isArray(tracks) ? tracks : [tracks]; - await fetch(`https://api.spotify.com/v1/me/tracks`, { - body: JSON.stringify({ids}), + body: JSON.stringify({ ids: parseIds(tracks) }), headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json', @@ -231,10 +229,8 @@ export async function repeat(token: string, state: RepeatState, deviceId?: strin } export async function saveTracks(token: string, tracks: IDs): Promise { - const ids = Array.isArray(tracks) ? tracks : [tracks]; - await fetch(`https://api.spotify.com/v1/me/tracks`, { - body: JSON.stringify({ ids }), + body: JSON.stringify({ ids: parseIds(tracks) }), headers: { Authorization: `Bearer ${token}`, 'Content-Type': 'application/json', diff --git a/test/modules/helpers.spec.ts b/test/modules/helpers.spec.ts index d0a44c0..6f7c36b 100644 --- a/test/modules/helpers.spec.ts +++ b/test/modules/helpers.spec.ts @@ -2,6 +2,7 @@ import { isNumber, loadSpotifyPlayer, millisecondsToTime, + parseIds, parseVolume, round, validateURI, @@ -41,6 +42,15 @@ describe('millisecondsToTime', () => { }); }); +describe('parseIds', () => { + it('should return properly', () => { + expect(parseIds('sek80pgtykoem9zr189zgyy9')).toEqual(['sek80pgtykoem9zr189zgyy9']); + expect(parseIds(['sek80pgtykoem9zr189zgyy9'])).toEqual(['sek80pgtykoem9zr189zgyy9']); + /* @ts-expect-error - missing parameter */ + expect(parseIds()).toEqual([]); + }); +}); + describe('parseVolume', () => { it.each([ [0.3, 0.3],