forked from FreekBes/spotify_web_controller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify-web-api.js
1770 lines (1669 loc) · 82.8 KB
/
spotify-web-api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Modified from JMPerez/spotify-web-api-js
* https://github.com/JMPerez/spotify-web-api-js/
*/
/* 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);
}
if (requestData.contentType) {
req.setRequestHeader('Content-Type', requestData.contentType)
}
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) {
postData = requestData.contentType === 'image/jpeg' ? requestData.postData : JSON.stringify(requestData.postData)
}
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} ownerId The id of the playlist owner. If you know the Spotify URI of
* the playlist, it is easy to find the owner's user id
* (e.g. spotify:user:<here_is_the_owner_id>:playlist:xxxx)
* @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(ownerId, playlistId, options, callback) {
var requestData = {
url: _baseUri + '/users/' + encodeURIComponent(ownerId) + '/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} ownerId The id of the playlist owner. If you know the Spotify URI of
* the playlist, it is easy to find the owner's user id
* (e.g. spotify:user:<here_is_the_owner_id>:playlist:xxxx)
* @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(ownerId, playlistId, callback) {
var requestData = {
url: _baseUri + '/users/' + encodeURIComponent(ownerId) + '/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} ownerId The id of the playlist owner. If you know the Spotify URI of
* the playlist, it is easy to find the owner's user id
* (e.g. spotify:user:<here_is_the_owner_id>:playlist:xxxx)
* @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(ownerId, playlistId, userIds, callback) {
var requestData = {
url: _baseUri + '/users/' + encodeURIComponent(ownerId) + '/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);
};
/**
* 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. You may want to user the "getMe" function to
* find out the id of the current logged in user
* @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} userId The id of the user. You may want to user the "getMe" function to
* find out the id of the current logged in user
* @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(userId, playlistId, data, callback) {
var requestData = {
url: _baseUri + '/users/' + encodeURIComponent(userId) + '/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} userId The id of the user. If you know the Spotify URI it is easy
* to find the user id (e.g. spotify:user:<here_is_the_user_id>:playlist:xxxx)
* @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(userId, playlistId, uris, options, callback) {
var requestData = {
url: _baseUri + '/users/' + encodeURIComponent(userId) + '/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} userId The id of the user. If you know the Spotify URI it is easy
* to find the user id (e.g. spotify:user:<here_is_the_user_id>:playlist:xxxx)
* @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(userId, playlistId, uris, callback) {
var requestData = {
url: _baseUri + '/users/' + encodeURIComponent(userId) + '/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} userId The id of the user. If you know the Spotify URI it is easy
* to find the user id (e.g. spotify:user:<here_is_the_user_id>:playlist:xxxx)
* @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(userId, playlistId, rangeStart, insertBefore, options, callback) {
/* eslint-disable camelcase */
var requestData = {
url: _baseUri + '/users/' + encodeURIComponent(userId) + '/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} userId The id of the user. If you know the Spotify URI it is easy
* to find the user id (e.g. spotify:user:<here_is_the_user_id>:playlist:xxxx)
* @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(userId, playlistId, uris, callback) {
var dataToBeSent = uris.map(function(uri) {
if (typeof uri === 'string') {
return { uri: uri };
} else {
return uri;
}
});
var requestData = {
url: _baseUri + '/users/' + encodeURIComponent(userId) + '/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} userId The id of the user. If you know the Spotify URI it is easy
* to find the user id (e.g. spotify:user:<here_is_the_user_id>:playlist:xxxx)
* @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(userId, 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 + '/users/' + encodeURIComponent(userId) + '/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} userId The id of the user. If you know the Spotify URI it is easy
* to find the user id (e.g. spotify:user:<here_is_the_user_id>:playlist:xxxx)
* @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(userId, playlistId, positions, snapshotId, callback) {
/* eslint-disable camelcase */
var requestData = {
url: _baseUri + '/users/' + encodeURIComponent(userId) + '/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} userId The id of the user. If you know the Spotify URI it is easy
* to find the user id (e.g. spotify:user:<here_is_the_user_id>:playlist:xxxx)
* @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(userId, playlistId, imageData, callback) {
var requestData = {
url: _baseUri + '/users/' + encodeURIComponent(userId) + '/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