spotify-web-api-js
Version:
A client-side JS wrapper for the Spotify Web API
1,141 lines (1,078 loc) • 96 kB
JavaScript
/* global module */
'use strict';
/**
* Class representing the API
*/
var SpotifyWebApi = (function () {
var _baseUri = 'https://api.spotify.com/v1';
var _accessToken = null;
var _promiseImplementation = null;
var WrapPromiseWithAbort = function (promise, onAbort) {
promise.abort = onAbort;
return promise;
};
var _promiseProvider = function (promiseFunction, onAbort) {
var returnedPromise;
if (_promiseImplementation !== null) {
var deferred = _promiseImplementation.defer();
promiseFunction(
function (resolvedResult) {
deferred.resolve(resolvedResult);
},
function (rejectedResult) {
deferred.reject(rejectedResult);
}
);
returnedPromise = deferred.promise;
} else {
if (window.Promise) {
returnedPromise = new window.Promise(promiseFunction);
}
}
if (returnedPromise) {
return new WrapPromiseWithAbort(returnedPromise, onAbort);
} else {
return null;
}
};
var _extend = function () {
var args = Array.prototype.slice.call(arguments);
var target = args[0];
var objects = args.slice(1);
target = target || {};
objects.forEach(function (object) {
for (var j in object) {
if (object.hasOwnProperty(j)) {
target[j] = object[j];
}
}
});
return target;
};
var _buildUrl = function (url, parameters) {
var qs = '';
for (var key in parameters) {
if (parameters.hasOwnProperty(key)) {
var value = parameters[key];
qs += encodeURIComponent(key) + '=' + encodeURIComponent(value) + '&';
}
}
if (qs.length > 0) {
// chop off last '&'
qs = qs.substring(0, qs.length - 1);
url = url + '?' + qs;
}
return url;
};
var _performRequest = function (requestData, callback) {
var req = new XMLHttpRequest();
var promiseFunction = function (resolve, reject) {
function success(data) {
if (resolve) {
resolve(data);
}
if (callback) {
callback(null, data);
}
}
function failure() {
if (reject) {
reject(req);
}
if (callback) {
callback(req, null);
}
}
var type = requestData.type || 'GET';
req.open(type, _buildUrl(requestData.url, requestData.params));
if (_accessToken) {
req.setRequestHeader('Authorization', 'Bearer ' + _accessToken);
}
req.onreadystatechange = function () {
if (req.readyState === 4) {
var data = null;
try {
data = req.responseText ? JSON.parse(req.responseText) : '';
} catch (e) {
console.error(e);
}
if (req.status >= 200 && req.status < 300) {
success(data);
} else {
failure();
}
}
};
if (type === 'GET') {
req.send(null);
} else {
var postData = null;
if (requestData.postData) {
if (requestData.contentType === 'image/jpeg') {
postData = requestData.postData;
req.setRequestHeader('Content-Type', requestData.contentType);
} else {
postData = JSON.stringify(requestData.postData);
req.setRequestHeader('Content-Type', 'application/json');
}
}
req.send(postData);
}
};
if (callback) {
promiseFunction();
return null;
} else {
return _promiseProvider(promiseFunction, function () {
req.abort();
});
}
};
var _checkParamsAndPerformRequest = function (
requestData,
options,
callback,
optionsAlwaysExtendParams
) {
var opt = {};
var cb = null;
if (typeof options === 'object') {
opt = options;
cb = callback;
} else if (typeof options === 'function') {
cb = options;
}
// options extend postData, if any. Otherwise they extend parameters sent in the url
var type = requestData.type || 'GET';
if (type !== 'GET' && requestData.postData && !optionsAlwaysExtendParams) {
requestData.postData = _extend(requestData.postData, opt);
} else {
requestData.params = _extend(requestData.params, opt);
}
return _performRequest(requestData, cb);
};
/**
* Creates an instance of the wrapper
* @constructor
*/
var Constr = function () {};
Constr.prototype = {
constructor: SpotifyWebApi
};
/**
* Fetches a resource through a generic GET request.
*
* @param {string} url The URL to be fetched
* @param {function(Object,Object)} callback An optional callback
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.getGeneric = function (url, callback) {
var requestData = {
url: url
};
return _checkParamsAndPerformRequest(requestData, callback);
};
/**
* Fetches information about the current user.
* See [Get Current User's Profile](https://developer.spotify.com/web-api/get-current-users-profile/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {Object} options A JSON object with options that can be passed
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.getMe = function (options, callback) {
var requestData = {
url: _baseUri + '/me'
};
return _checkParamsAndPerformRequest(requestData, options, callback);
};
/**
* Fetches current user's saved tracks.
* See [Get Current User's Saved Tracks](https://developer.spotify.com/web-api/get-users-saved-tracks/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {Object} options A JSON object with options that can be passed
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.getMySavedTracks = function (options, callback) {
var requestData = {
url: _baseUri + '/me/tracks'
};
return _checkParamsAndPerformRequest(requestData, options, callback);
};
/**
* Adds a list of tracks to the current user's saved tracks.
* See [Save Tracks for Current User](https://developer.spotify.com/web-api/save-tracks-user/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {Array<string>} trackIds The ids of the tracks. If you know their Spotify URI it is easy
* to find their track id (e.g. spotify:track:<here_is_the_track_id>)
* @param {Object} options A JSON object with options that can be passed
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.addToMySavedTracks = function (trackIds, options, callback) {
var requestData = {
url: _baseUri + '/me/tracks',
type: 'PUT',
postData: trackIds
};
return _checkParamsAndPerformRequest(requestData, options, callback);
};
/**
* Remove a list of tracks from the current user's saved tracks.
* See [Remove Tracks for Current User](https://developer.spotify.com/web-api/remove-tracks-user/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {Array<string>} trackIds The ids of the tracks. If you know their Spotify URI it is easy
* to find their track id (e.g. spotify:track:<here_is_the_track_id>)
* @param {Object} options A JSON object with options that can be passed
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.removeFromMySavedTracks = function (
trackIds,
options,
callback
) {
var requestData = {
url: _baseUri + '/me/tracks',
type: 'DELETE',
postData: trackIds
};
return _checkParamsAndPerformRequest(requestData, options, callback);
};
/**
* Checks if the current user's saved tracks contains a certain list of tracks.
* See [Check Current User's Saved Tracks](https://developer.spotify.com/web-api/check-users-saved-tracks/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {Array<string>} trackIds The ids of the tracks. If you know their Spotify URI it is easy
* to find their track id (e.g. spotify:track:<here_is_the_track_id>)
* @param {Object} options A JSON object with options that can be passed
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.containsMySavedTracks = function (
trackIds,
options,
callback
) {
var requestData = {
url: _baseUri + '/me/tracks/contains',
params: { ids: trackIds.join(',') }
};
return _checkParamsAndPerformRequest(requestData, options, callback);
};
/**
* Get a list of the albums saved in the current Spotify user's "Your Music" library.
* See [Get Current User's Saved Albums](https://developer.spotify.com/web-api/get-users-saved-albums/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {Object} options A JSON object with options that can be passed
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.getMySavedAlbums = function (options, callback) {
var requestData = {
url: _baseUri + '/me/albums'
};
return _checkParamsAndPerformRequest(requestData, options, callback);
};
/**
* Save one or more albums to the current user's "Your Music" library.
* See [Save Albums for Current User](https://developer.spotify.com/web-api/save-albums-user/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {Array<string>} albumIds The ids of the albums. If you know their Spotify URI, it is easy
* to find their album id (e.g. spotify:album:<here_is_the_album_id>)
* @param {Object} options A JSON object with options that can be passed
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.addToMySavedAlbums = function (albumIds, options, callback) {
var requestData = {
url: _baseUri + '/me/albums',
type: 'PUT',
postData: albumIds
};
return _checkParamsAndPerformRequest(requestData, options, callback);
};
/**
* Remove one or more albums from the current user's "Your Music" library.
* See [Remove Albums for Current User](https://developer.spotify.com/web-api/remove-albums-user/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {Array<string>} albumIds The ids of the albums. If you know their Spotify URI, it is easy
* to find their album id (e.g. spotify:album:<here_is_the_album_id>)
* @param {Object} options A JSON object with options that can be passed
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.removeFromMySavedAlbums = function (
albumIds,
options,
callback
) {
var requestData = {
url: _baseUri + '/me/albums',
type: 'DELETE',
postData: albumIds
};
return _checkParamsAndPerformRequest(requestData, options, callback);
};
/**
* Check if one or more albums is already saved in the current Spotify user's "Your Music" library.
* See [Check User's Saved Albums](https://developer.spotify.com/web-api/check-users-saved-albums/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {Array<string>} albumIds The ids of the albums. If you know their Spotify URI, it is easy
* to find their album id (e.g. spotify:album:<here_is_the_album_id>)
* @param {Object} options A JSON object with options that can be passed
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.containsMySavedAlbums = function (
albumIds,
options,
callback
) {
var requestData = {
url: _baseUri + '/me/albums/contains',
params: { ids: albumIds.join(',') }
};
return _checkParamsAndPerformRequest(requestData, options, callback);
};
/**
* Get the current user’s top artists based on calculated affinity.
* See [Get a User’s Top Artists](https://developer.spotify.com/web-api/get-users-top-artists-and-tracks/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {Object} options A JSON object with options that can be passed
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.getMyTopArtists = function (options, callback) {
var requestData = {
url: _baseUri + '/me/top/artists'
};
return _checkParamsAndPerformRequest(requestData, options, callback);
};
/**
* Get the current user’s top tracks based on calculated affinity.
* See [Get a User’s Top Tracks](https://developer.spotify.com/web-api/get-users-top-artists-and-tracks/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {Object} options A JSON object with options that can be passed
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.getMyTopTracks = function (options, callback) {
var requestData = {
url: _baseUri + '/me/top/tracks'
};
return _checkParamsAndPerformRequest(requestData, options, callback);
};
/**
* Get tracks from the current user’s recently played tracks.
* See [Get Current User’s Recently Played Tracks](https://developer.spotify.com/web-api/web-api-personalization-endpoints/get-recently-played/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {Object} options A JSON object with options that can be passed
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.getMyRecentlyPlayedTracks = function (options, callback) {
var requestData = {
url: _baseUri + '/me/player/recently-played'
};
return _checkParamsAndPerformRequest(requestData, options, callback);
};
/**
* Adds the current user as a follower of one or more other Spotify users.
* See [Follow Artists or Users](https://developer.spotify.com/web-api/follow-artists-users/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {Array<string>} userIds The ids of the users. If you know their Spotify URI it is easy
* to find their user id (e.g. spotify:user:<here_is_the_user_id>)
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is an empty value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.followUsers = function (userIds, callback) {
var requestData = {
url: _baseUri + '/me/following/',
type: 'PUT',
params: {
ids: userIds.join(','),
type: 'user'
}
};
return _checkParamsAndPerformRequest(requestData, callback);
};
/**
* Adds the current user as a follower of one or more artists.
* See [Follow Artists or Users](https://developer.spotify.com/web-api/follow-artists-users/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {Array<string>} artistIds The ids of the artists. If you know their Spotify URI it is easy
* to find their artist id (e.g. spotify:artist:<here_is_the_artist_id>)
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is an empty value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.followArtists = function (artistIds, callback) {
var requestData = {
url: _baseUri + '/me/following/',
type: 'PUT',
params: {
ids: artistIds.join(','),
type: 'artist'
}
};
return _checkParamsAndPerformRequest(requestData, callback);
};
/**
* Add the current user as a follower of one playlist.
* See [Follow a Playlist](https://developer.spotify.com/web-api/follow-playlist/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {string} playlistId The id of the playlist. If you know the Spotify URI it is easy
* to find the playlist id (e.g. spotify:user:xxxx:playlist:<here_is_the_playlist_id>)
* @param {Object} options A JSON object with options that can be passed. For instance,
* whether you want the playlist to be followed privately ({public: false})
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is an empty value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.followPlaylist = function (playlistId, options, callback) {
var requestData = {
url: _baseUri + '/playlists/' + playlistId + '/followers',
type: 'PUT',
postData: {}
};
return _checkParamsAndPerformRequest(requestData, options, callback);
};
/**
* Removes the current user as a follower of one or more other Spotify users.
* See [Unfollow Artists or Users](https://developer.spotify.com/web-api/unfollow-artists-users/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {Array<string>} userIds The ids of the users. If you know their Spotify URI it is easy
* to find their user id (e.g. spotify:user:<here_is_the_user_id>)
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is an empty value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.unfollowUsers = function (userIds, callback) {
var requestData = {
url: _baseUri + '/me/following/',
type: 'DELETE',
params: {
ids: userIds.join(','),
type: 'user'
}
};
return _checkParamsAndPerformRequest(requestData, callback);
};
/**
* Removes the current user as a follower of one or more artists.
* See [Unfollow Artists or Users](https://developer.spotify.com/web-api/unfollow-artists-users/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {Array<string>} artistIds The ids of the artists. If you know their Spotify URI it is easy
* to find their artist id (e.g. spotify:artist:<here_is_the_artist_id>)
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is an empty value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.unfollowArtists = function (artistIds, callback) {
var requestData = {
url: _baseUri + '/me/following/',
type: 'DELETE',
params: {
ids: artistIds.join(','),
type: 'artist'
}
};
return _checkParamsAndPerformRequest(requestData, callback);
};
/**
* Remove the current user as a follower of one playlist.
* See [Unfollow a Playlist](https://developer.spotify.com/web-api/unfollow-playlist/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {string} playlistId The id of the playlist. If you know the Spotify URI it is easy
* to find the playlist id (e.g. spotify:user:xxxx:playlist:<here_is_the_playlist_id>)
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is an empty value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.unfollowPlaylist = function (playlistId, callback) {
var requestData = {
url: _baseUri + '/playlists/' + playlistId + '/followers',
type: 'DELETE'
};
return _checkParamsAndPerformRequest(requestData, callback);
};
/**
* Checks to see if the current user is following one or more other Spotify users.
* See [Check if Current User Follows Users or Artists](https://developer.spotify.com/web-api/check-current-user-follows/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {Array<string>} userIds The ids of the users. If you know their Spotify URI it is easy
* to find their user id (e.g. spotify:user:<here_is_the_user_id>)
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is an array of boolean values that indicate
* whether the user is following the users sent in the request.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.isFollowingUsers = function (userIds, callback) {
var requestData = {
url: _baseUri + '/me/following/contains',
type: 'GET',
params: {
ids: userIds.join(','),
type: 'user'
}
};
return _checkParamsAndPerformRequest(requestData, callback);
};
/**
* Checks to see if the current user is following one or more artists.
* See [Check if Current User Follows](https://developer.spotify.com/web-api/check-current-user-follows/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {Array<string>} artistIds The ids of the artists. If you know their Spotify URI it is easy
* to find their artist id (e.g. spotify:artist:<here_is_the_artist_id>)
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is an array of boolean values that indicate
* whether the user is following the artists sent in the request.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.isFollowingArtists = function (artistIds, callback) {
var requestData = {
url: _baseUri + '/me/following/contains',
type: 'GET',
params: {
ids: artistIds.join(','),
type: 'artist'
}
};
return _checkParamsAndPerformRequest(requestData, callback);
};
/**
* Check to see if one or more Spotify users are following a specified playlist.
* See [Check if Users Follow a Playlist](https://developer.spotify.com/web-api/check-user-following-playlist/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {string} playlistId The id of the playlist. If you know the Spotify URI it is easy
* to find the playlist id (e.g. spotify:user:xxxx:playlist:<here_is_the_playlist_id>)
* @param {Array<string>} userIds The ids of the users. If you know their Spotify URI it is easy
* to find their user id (e.g. spotify:user:<here_is_the_user_id>)
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is an array of boolean values that indicate
* whether the users are following the playlist sent in the request.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.areFollowingPlaylist = function (
playlistId,
userIds,
callback
) {
var requestData = {
url: _baseUri + '/playlists/' + playlistId + '/followers/contains',
type: 'GET',
params: {
ids: userIds.join(',')
}
};
return _checkParamsAndPerformRequest(requestData, callback);
};
/**
* Get the current user's followed artists.
* See [Get User's Followed Artists](https://developer.spotify.com/web-api/get-followed-artists/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {Object} [options] Options, being after and limit.
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is an object with a paged object containing
* artists.
* @returns {Promise|undefined} A promise that if successful, resolves to an object containing a paging object which contains
* artists objects. Not returned if a callback is given.
*/
Constr.prototype.getFollowedArtists = function (options, callback) {
var requestData = {
url: _baseUri + '/me/following',
type: 'GET',
params: {
type: 'artist'
}
};
return _checkParamsAndPerformRequest(requestData, options, callback);
};
/**
* Fetches information about a specific user.
* See [Get a User's Profile](https://developer.spotify.com/web-api/get-users-profile/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {string} userId The id of the user. If you know the Spotify URI it is easy
* to find the id (e.g. spotify:user:<here_is_the_id>)
* @param {Object} options A JSON object with options that can be passed
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.getUser = function (userId, options, callback) {
var requestData = {
url: _baseUri + '/users/' + encodeURIComponent(userId)
};
return _checkParamsAndPerformRequest(requestData, options, callback);
};
/**
* Fetches a list of the current user's playlists.
* See [Get a List of a User's Playlists](https://developer.spotify.com/web-api/get-list-users-playlists/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {string} userId An optional id of the user. If you know the Spotify URI it is easy
* to find the id (e.g. spotify:user:<here_is_the_id>). If not provided, the id of the user that granted
* the permissions will be used.
* @param {Object} options A JSON object with options that can be passed
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.getUserPlaylists = function (userId, options, callback) {
var requestData;
if (typeof userId === 'string') {
requestData = {
url: _baseUri + '/users/' + encodeURIComponent(userId) + '/playlists'
};
} else {
requestData = {
url: _baseUri + '/me/playlists'
};
callback = options;
options = userId;
}
return _checkParamsAndPerformRequest(requestData, options, callback);
};
/**
* Fetches a specific playlist.
* See [Get a Playlist](https://developer.spotify.com/web-api/get-playlist/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {string} playlistId The id of the playlist. If you know the Spotify URI it is easy
* to find the playlist id (e.g. spotify:user:xxxx:playlist:<here_is_the_playlist_id>)
* @param {Object} options A JSON object with options that can be passed
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.getPlaylist = function (playlistId, options, callback) {
var requestData = {
url: _baseUri + '/playlists/' + playlistId
};
return _checkParamsAndPerformRequest(requestData, options, callback);
};
/**
* Fetches the tracks from a specific playlist.
* See [Get a Playlist's Tracks](https://developer.spotify.com/web-api/get-playlists-tracks/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {string} playlistId The id of the playlist. If you know the Spotify URI it is easy
* to find the playlist id (e.g. spotify:user:xxxx:playlist:<here_is_the_playlist_id>)
* @param {Object} options A JSON object with options that can be passed
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.getPlaylistTracks = function (
playlistId,
options,
callback
) {
var requestData = {
url: _baseUri + '/playlists/' + playlistId + '/tracks'
};
return _checkParamsAndPerformRequest(requestData, options, callback);
};
/**
* Gets the current image associated with a specific playlist.
* See [Get a Playlist Cover Image](https://developer.spotify.com/documentation/web-api/reference/playlists/get-playlist-cover/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {string} playlistId The id of the playlist. If you know the Spotify URI it is easy
* to find the playlist id (e.g. spotify:playlist:<here_is_the_playlist_id>)
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.getPlaylistCoverImage = function (playlistId, callback) {
var requestData = {
url: _baseUri + '/playlists/' + playlistId + '/images'
};
return _checkParamsAndPerformRequest(requestData, callback);
};
/**
* Creates a playlist and stores it in the current user's library.
* See [Create a Playlist](https://developer.spotify.com/web-api/create-playlist/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {string} userId The id of the user. If you know the Spotify URI it is easy
* to find the id (e.g. spotify:user:<here_is_the_id>)
* @param {Object} options A JSON object with options that can be passed
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.createPlaylist = function (userId, options, callback) {
var requestData = {
url: _baseUri + '/users/' + encodeURIComponent(userId) + '/playlists',
type: 'POST',
postData: options
};
return _checkParamsAndPerformRequest(requestData, options, callback);
};
/**
* Change a playlist's name and public/private state
* See [Change a Playlist's Details](https://developer.spotify.com/web-api/change-playlist-details/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {string} playlistId The id of the playlist. If you know the Spotify URI it is easy
* to find the playlist id (e.g. spotify:user:xxxx:playlist:<here_is_the_playlist_id>)
* @param {Object} data A JSON object with the data to update. E.g. {name: 'A new name', public: true}
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.changePlaylistDetails = function (
playlistId,
data,
callback
) {
var requestData = {
url: _baseUri + '/playlists/' + playlistId,
type: 'PUT',
postData: data
};
return _checkParamsAndPerformRequest(requestData, data, callback);
};
/**
* Add tracks to a playlist.
* See [Add Tracks to a Playlist](https://developer.spotify.com/web-api/add-tracks-to-playlist/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {string} playlistId The id of the playlist. If you know the Spotify URI it is easy
* to find the playlist id (e.g. spotify:user:xxxx:playlist:<here_is_the_playlist_id>)
* @param {Array<string>} uris An array of Spotify URIs for the tracks
* @param {Object} options A JSON object with options that can be passed
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.addTracksToPlaylist = function (
playlistId,
uris,
options,
callback
) {
var requestData = {
url: _baseUri + '/playlists/' + playlistId + '/tracks',
type: 'POST',
postData: {
uris: uris
}
};
return _checkParamsAndPerformRequest(requestData, options, callback, true);
};
/**
* Replace the tracks of a playlist
* See [Replace a Playlist's Tracks](https://developer.spotify.com/web-api/replace-playlists-tracks/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {string} playlistId The id of the playlist. If you know the Spotify URI it is easy
* to find the playlist id (e.g. spotify:user:xxxx:playlist:<here_is_the_playlist_id>)
* @param {Array<string>} uris An array of Spotify URIs for the tracks
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.replaceTracksInPlaylist = function (
playlistId,
uris,
callback
) {
var requestData = {
url: _baseUri + '/playlists/' + playlistId + '/tracks',
type: 'PUT',
postData: { uris: uris }
};
return _checkParamsAndPerformRequest(requestData, {}, callback);
};
/**
* Reorder tracks in a playlist
* See [Reorder a Playlist’s Tracks](https://developer.spotify.com/web-api/reorder-playlists-tracks/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {string} playlistId The id of the playlist. If you know the Spotify URI it is easy
* to find the playlist id (e.g. spotify:user:xxxx:playlist:<here_is_the_playlist_id>)
* @param {number} rangeStart The position of the first track to be reordered.
* @param {number} insertBefore The position where the tracks should be inserted. To reorder the tracks to
* the end of the playlist, simply set insert_before to the position after the last track.
* @param {Object} options An object with optional parameters (range_length, snapshot_id)
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.reorderTracksInPlaylist = function (
playlistId,
rangeStart,
insertBefore,
options,
callback
) {
/* eslint-disable camelcase */
var requestData = {
url: _baseUri + '/playlists/' + playlistId + '/tracks',
type: 'PUT',
postData: {
range_start: rangeStart,
insert_before: insertBefore
}
};
/* eslint-enable camelcase */
return _checkParamsAndPerformRequest(requestData, options, callback);
};
/**
* Remove tracks from a playlist
* See [Remove Tracks from a Playlist](https://developer.spotify.com/web-api/remove-tracks-playlist/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {string} playlistId The id of the playlist. If you know the Spotify URI it is easy
* to find the playlist id (e.g. spotify:user:xxxx:playlist:<here_is_the_playlist_id>)
* @param {Array<Object>} uris An array of tracks to be removed. Each element of the array can be either a
* string, in which case it is treated as a URI, or an object containing the properties `uri` (which is a
* string) and `positions` (which is an array of integers).
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.removeTracksFromPlaylist = function (
playlistId,
uris,
callback
) {
var dataToBeSent = uris.map(function (uri) {
if (typeof uri === 'string') {
return { uri: uri };
} else {
return uri;
}
});
var requestData = {
url: _baseUri + '/playlists/' + playlistId + '/tracks',
type: 'DELETE',
postData: { tracks: dataToBeSent }
};
return _checkParamsAndPerformRequest(requestData, {}, callback);
};
/**
* Remove tracks from a playlist, specifying a snapshot id.
* See [Remove Tracks from a Playlist](https://developer.spotify.com/web-api/remove-tracks-playlist/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {string} playlistId The id of the playlist. If you know the Spotify URI it is easy
* to find the playlist id (e.g. spotify:user:xxxx:playlist:<here_is_the_playlist_id>)
* @param {Array<Object>} uris An array of tracks to be removed. Each element of the array can be either a
* string, in which case it is treated as a URI, or an object containing the properties `uri` (which is a
* string) and `positions` (which is an array of integers).
* @param {string} snapshotId The playlist's snapshot ID against which you want to make the changes
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.removeTracksFromPlaylistWithSnapshotId = function (
playlistId,
uris,
snapshotId,
callback
) {
var dataToBeSent = uris.map(function (uri) {
if (typeof uri === 'string') {
return { uri: uri };
} else {
return uri;
}
});
/* eslint-disable camelcase */
var requestData = {
url: _baseUri + '/playlists/' + playlistId + '/tracks',
type: 'DELETE',
postData: {
tracks: dataToBeSent,
snapshot_id: snapshotId
}
};
/* eslint-enable camelcase */
return _checkParamsAndPerformRequest(requestData, {}, callback);
};
/**
* Remove tracks from a playlist, specifying the positions of the tracks to be removed.
* See [Remove Tracks from a Playlist](https://developer.spotify.com/web-api/remove-tracks-playlist/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {string} playlistId The id of the playlist. If you know the Spotify URI it is easy
* to find the playlist id (e.g. spotify:user:xxxx:playlist:<here_is_the_playlist_id>)
* @param {Array<number>} positions array of integers containing the positions of the tracks to remove
* from the playlist.
* @param {string} snapshotId The playlist's snapshot ID against which you want to make the changes
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.removeTracksFromPlaylistInPositions = function (
playlistId,
positions,
snapshotId,
callback
) {
/* eslint-disable camelcase */
var requestData = {
url: _baseUri + '/playlists/' + playlistId + '/tracks',
type: 'DELETE',
postData: {
positions: positions,
snapshot_id: snapshotId
}
};
/* eslint-enable camelcase */
return _checkParamsAndPerformRequest(requestData, {}, callback);
};
/**
* Upload a custom playlist cover image.
* See [Upload A Custom Playlist Cover Image](https://developer.spotify.com/web-api/upload-a-custom-playlist-cover-image/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {string} playlistId The id of the playlist. If you know the Spotify URI it is easy
* to find the playlist id (e.g. spotify:user:xxxx:playlist:<here_is_the_playlist_id>)
* @param {string} imageData Base64 encoded JPEG image data, maximum payload size is 256 KB.
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.uploadCustomPlaylistCoverImage = function (
playlistId,
imageData,
callback
) {
var requestData = {
url: _baseUri + '/playlists/' + playlistId + '/images',
type: 'PUT',
postData: imageData.replace(/^data:image\/jpeg;base64,/, ''),
contentType: 'image/jpeg'
};
return _checkParamsAndPerformRequest(requestData, {}, callback);
};
/**
* Fetches an album from the Spotify catalog.
* See [Get an Album](https://developer.spotify.com/web-api/get-album/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {string} albumId The id of the album. If you know the Spotify URI it is easy
* to find the album id (e.g. spotify:album:<here_is_the_album_id>)
* @param {Object} options A JSON object with options that can be passed
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.getAlbum = function (albumId, options, callback) {
var requestData = {
url: _baseUri + '/albums/' + albumId
};
return _checkParamsAndPerformRequest(requestData, options, callback);
};
/**
* Fetches the tracks of an album from the Spotify catalog.
* See [Get an Album's Tracks](https://developer.spotify.com/web-api/get-albums-tracks/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {string} albumId The id of the album. If you know the Spotify URI it is easy
* to find the album id (e.g. spotify:album:<here_is_the_album_id>)
* @param {Object} options A JSON object with options that can be passed
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.getAlbumTracks = function (albumId, options, callback) {
var requestData = {
url: _baseUri + '/albums/' + albumId + '/tracks'
};
return _checkParamsAndPerformRequest(requestData, options, callback);
};
/**
* Fetches multiple albums from the Spotify catalog.
* See [Get Several Albums](https://developer.spotify.com/web-api/get-several-albums/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {Array<string>} albumIds The ids of the albums. If you know their Spotify URI it is easy
* to find their album id (e.g. spotify:album:<here_is_the_album_id>)
* @param {Object} options A JSON object with options that can be passed
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.getAlbums = function (albumIds, options, callback) {
var requestData = {
url: _baseUri + '/albums/',
params: { ids: albumIds.join(',') }
};
return _checkParamsAndPerformRequest(requestData, options, callback);
};
/**
* Fetches a track from the Spotify catalog.
* See [Get a Track](https://developer.spotify.com/web-api/get-track/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {string} trackId The id of the track. If you know the Spotify URI it is easy
* to find the track id (e.g. spotify:track:<here_is_the_track_id>)
* @param {Object} options A JSON object with options that can be passed
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.getTrack = function (trackId, options, callback) {
var requestData = {};
requestData.url = _baseUri + '/tracks/' + trackId;
return _checkParamsAndPerformRequest(requestData, options, callback);
};
/**
* Fetches multiple tracks from the Spotify catalog.
* See [Get Several Tracks](https://developer.spotify.com/web-api/get-several-tracks/) on
* the Spotify Developer site for more information about the endpoint.
*
* @param {Array<string>} trackIds The ids of the tracks. If you know their Spotify URI it is easy
* to find their track id (e.g. spotify:track:<here_is_the_track_id>)
* @param {Object} options A JSON object with options that can be passed
* @param {function(Object,Object)} callback An optional callback that receives 2 parameters. The first
* one is the error object (null if no error), and the second is the value if the request succeeded.
* @return {Object} Null if a callback is provided, a `Promise` object otherwise
*/
Constr.prototype.getTracks = function (trackIds, options, callback) {
var requestData = {
url: _baseUri + '/tracks/',
params: { ids: trackIds.join(',')