Skip to content

Commit 87e0acd

Browse files
committed
Add method to delete tracks from playlist given positions - Fixes #14
1 parent de27fdb commit 87e0acd

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/spotify-web-api.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,35 @@ var SpotifyWebApi = (function() {
670670
};
671671
return _checkParamsAndPerformRequest(requestData, {}, callback);
672672
};
673+
674+
/**
675+
* Remove tracks from a playlist, specifying the positions of the tracks to be removed.
676+
* See [Remove Tracks from a Playlist](https://developer.spotify.com/web-api/remove-tracks-playlist/) on
677+
* the Spotify Developer site for more information about the endpoint.
678+
* @param {string} userId The id of the user. If you know the Spotify URI it is easy
679+
* to find the user id (e.g. spotify:user:<here_is_the_user_id>:playlist:xxxx)
680+
* @param {string} playlistId The id of the playlist. If you know the Spotify URI it is easy
681+
* to find the playlist id (e.g. spotify:user:xxxx:playlist:<here_is_the_playlist_id>)
682+
* @param {Array<number>} positions array of integers containing the positions of the tracks to remove
683+
* from the playlist.
684+
* @param {string} snapshotId The playlist's snapshot ID against which you want to make the changes
685+
* @param {function(Object, Object)} callback An optional callback that receives 2 parameters. The first
686+
* one is the error object (null if no error), and the second is the value if the request succeeded.
687+
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
688+
*/
689+
Constr.prototype.removeTracksFromPlaylistInPositions = function(userId, playlistId, positions, snapshotId, callback) {
690+
/*jshint camelcase: false */
691+
var requestData = {
692+
url: _baseUri + '/users/' + userId + '/playlists/' + playlistId + '/tracks',
693+
type: 'DELETE',
694+
postData: {
695+
positions: positions,
696+
snapshot_id: snapshotId
697+
}
698+
};
699+
return _checkParamsAndPerformRequest(requestData, {}, callback);
700+
};
701+
673702
/**
674703
* Fetches an album from the Spotify catalog.
675704
* See [Get an Album](https://developer.spotify.com/web-api/get-album/) on

tests/js/spec/test.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@
552552
var callback = sinon.spy();
553553
var api = new SpotifyWebApi();
554554
api.setAccessToken('<example_access_token>');
555-
api.removeTracksFromPlaylistWithSnapshotId('jmperezperez', '7Kud0O2IdWLbEGgvBkW9di', [{uri: 'spotify:track:2Oehrcv4Kov0SuIgWyQY9e', position: 6}], 'AsNaPsHoTiD', callback);
555+
api.removeTracksFromPlaylistWithSnapshotId('jmperezperez', '7Kud0O2IdWLbEGgvBkW9di', [{uri: 'spotify:track:2Oehrcv4Kov0SuIgWyQY9e', positions: [6]}], 'AsNaPsHoTiD', callback);
556556
that.requests[0].respond(200,
557557
{'Content-Type':'application/json'},
558558
''
@@ -565,12 +565,32 @@
565565
expect(that.requests[0].requestBody).to.equal(JSON.stringify({
566566
tracks: [{
567567
uri: 'spotify:track:2Oehrcv4Kov0SuIgWyQY9e',
568-
position: 6
568+
positions: [6]
569569
}],
570570
snapshot_id: 'AsNaPsHoTiD'
571571
}));
572572
});
573573

574+
it('should remove tracks from a playlist specifying just their positions and snapshot id', function() {
575+
var callback = sinon.spy();
576+
var api = new SpotifyWebApi();
577+
api.setAccessToken('<example_access_token>');
578+
api.removeTracksFromPlaylistInPositions('jmperezperez', '7Kud0O2IdWLbEGgvBkW9di', [0,1,3,9], 'AsNaPsHoTiD', callback);
579+
that.requests[0].respond(200,
580+
{'Content-Type':'application/json'},
581+
''
582+
);
583+
expect(callback.calledWith(null, '')).to.be.ok;
584+
expect(that.requests).to.have.length(1);
585+
expect(that.requests[0].url).to.equal('https://api.spotify.com/v1/users/jmperezperez/playlists/7Kud0O2IdWLbEGgvBkW9di/tracks');
586+
expect(that.requests[0].method).to.equal('DELETE');
587+
expect(that.requests[0].status).to.equal(200);
588+
expect(that.requests[0].requestBody).to.equal(JSON.stringify({
589+
positions: [0,1,3,9],
590+
snapshot_id: 'AsNaPsHoTiD'
591+
}));
592+
});
593+
574594
it('should replace the tracks in a playlist', function() {
575595
var callback = sinon.spy();
576596
var api = new SpotifyWebApi();

0 commit comments

Comments
 (0)