1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
4 |
|
5 | require("core-js/shim");
|
6 |
|
7 | require("regenerator-runtime/runtime");
|
8 |
|
9 | require("core-js/fn/regexp/escape");
|
10 |
|
11 | if (global._babelPolyfill) {
|
12 | throw new Error("only one instance of babel-polyfill is allowed");
|
13 | }
|
14 | global._babelPolyfill = true;
|
15 |
|
16 | var DEFINE_PROPERTY = "defineProperty";
|
17 | function define(O, key, value) {
|
18 | O[key] || Object[DEFINE_PROPERTY](O, key, {
|
19 | writable: true,
|
20 | configurable: true,
|
21 | value: value
|
22 | });
|
23 | }
|
24 |
|
25 | define(String.prototype, "padLeft", "".padStart);
|
26 | define(String.prototype, "padRight", "".padEnd);
|
27 |
|
28 | "pop,reverse,shift,keys,values,entries,indexOf,every,some,forEach,map,filter,find,findIndex,includes,join,slice,concat,push,splice,unshift,sort,lastIndexOf,reduce,reduceRight,copyWithin,fill".split(",").forEach(function (key) {
|
29 | [][key] && define(Array, key, Function.call.bind([][key]));
|
30 | });
|
31 |
|
32 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
|
33 | return typeof obj;
|
34 | } : function (obj) {
|
35 | return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
|
36 | };
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | var asyncToGenerator = function (fn) {
|
47 | return function () {
|
48 | var gen = fn.apply(this, arguments);
|
49 | return new Promise(function (resolve, reject) {
|
50 | function step(key, arg) {
|
51 | try {
|
52 | var info = gen[key](arg);
|
53 | var value = info.value;
|
54 | } catch (error) {
|
55 | reject(error);
|
56 | return;
|
57 | }
|
58 |
|
59 | if (info.done) {
|
60 | resolve(value);
|
61 | } else {
|
62 | return Promise.resolve(value).then(function (value) {
|
63 | step("next", value);
|
64 | }, function (err) {
|
65 | step("throw", err);
|
66 | });
|
67 | }
|
68 | }
|
69 |
|
70 | return step("next");
|
71 | });
|
72 | };
|
73 | };
|
74 |
|
75 | var classCallCheck = function (instance, Constructor) {
|
76 | if (!(instance instanceof Constructor)) {
|
77 | throw new TypeError("Cannot call a class as a function");
|
78 | }
|
79 | };
|
80 |
|
81 | var createClass = function () {
|
82 | function defineProperties(target, props) {
|
83 | for (var i = 0; i < props.length; i++) {
|
84 | var descriptor = props[i];
|
85 | descriptor.enumerable = descriptor.enumerable || false;
|
86 | descriptor.configurable = true;
|
87 | if ("value" in descriptor) descriptor.writable = true;
|
88 | Object.defineProperty(target, descriptor.key, descriptor);
|
89 | }
|
90 | }
|
91 |
|
92 | return function (Constructor, protoProps, staticProps) {
|
93 | if (protoProps) defineProperties(Constructor.prototype, protoProps);
|
94 | if (staticProps) defineProperties(Constructor, staticProps);
|
95 | return Constructor;
|
96 | };
|
97 | }();
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 | var defineProperty = function (obj, key, value) {
|
104 | if (key in obj) {
|
105 | Object.defineProperty(obj, key, {
|
106 | value: value,
|
107 | enumerable: true,
|
108 | configurable: true,
|
109 | writable: true
|
110 | });
|
111 | } else {
|
112 | obj[key] = value;
|
113 | }
|
114 |
|
115 | return obj;
|
116 | };
|
117 |
|
118 |
|
119 |
|
120 | var inherits = function (subClass, superClass) {
|
121 | if (typeof superClass !== "function" && superClass !== null) {
|
122 | throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
|
123 | }
|
124 |
|
125 | subClass.prototype = Object.create(superClass && superClass.prototype, {
|
126 | constructor: {
|
127 | value: subClass,
|
128 | enumerable: false,
|
129 | writable: true,
|
130 | configurable: true
|
131 | }
|
132 | });
|
133 | if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
|
134 | };
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | var possibleConstructorReturn = function (self, call) {
|
147 | if (!self) {
|
148 | throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
|
149 | }
|
150 |
|
151 | return call && (typeof call === "object" || typeof call === "function") ? call : self;
|
152 | };
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | var slicedToArray = function () {
|
159 | function sliceIterator(arr, i) {
|
160 | var _arr = [];
|
161 | var _n = true;
|
162 | var _d = false;
|
163 | var _e = undefined;
|
164 |
|
165 | try {
|
166 | for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
|
167 | _arr.push(_s.value);
|
168 |
|
169 | if (i && _arr.length === i) break;
|
170 | }
|
171 | } catch (err) {
|
172 | _d = true;
|
173 | _e = err;
|
174 | } finally {
|
175 | try {
|
176 | if (!_n && _i["return"]) _i["return"]();
|
177 | } finally {
|
178 | if (_d) throw _e;
|
179 | }
|
180 | }
|
181 |
|
182 | return _arr;
|
183 | }
|
184 |
|
185 | return function (arr, i) {
|
186 | if (Array.isArray(arr)) {
|
187 | return arr;
|
188 | } else if (Symbol.iterator in Object(arr)) {
|
189 | return sliceIterator(arr, i);
|
190 | } else {
|
191 | throw new TypeError("Invalid attempt to destructure non-iterable instance");
|
192 | }
|
193 | };
|
194 | }();
|
195 |
|
196 |
|
197 | !function (e) {
|
198 | function t(s) {
|
199 | if (n[s]) return n[s].exports;var r = n[s] = { i: s, l: !1, exports: {} };return e[s].call(r.exports, r, r.exports, t), r.l = !0, r.exports;
|
200 | }var n = {};t.m = e, t.c = n, t.d = function (e, n, s) {
|
201 | t.o(e, n) || Object.defineProperty(e, n, { configurable: !1, enumerable: !0, get: s });
|
202 | }, t.n = function (e) {
|
203 | var n = e && e.__esModule ? function () {
|
204 | return e.default;
|
205 | } : function () {
|
206 | return e;
|
207 | };return t.d(n, "a", n), n;
|
208 | }, t.o = function (e, t) {
|
209 | return Object.prototype.hasOwnProperty.call(e, t);
|
210 | }, t.p = "", t(t.s = 2);
|
211 | }([function (e, t, n) {
|
212 | var s = { SPOTIFY_MESSAGE: "SP_MESSAGE", ACCOUNT_ERROR: "ACCOUNT_ERROR", AUTH_ERROR: "AUTH_ERROR", CONNECT: "CONNECT", CONNECTED: "CONNECTED", CURRENT_STATE: "CURRENT_STATE", DISCONNECT: "DISCONNECT", EVENT: "EVENT", GET_CURRENT_STATE: "GET_CURRENT_STATE", GET_TOKEN: "GET_TOKEN", GET_VOLUME: "GET_VOLUME", INIT: "INIT", LOADED: "LOADED", NEXT_TRACK: "NEXT_TRACK", PAUSE: "PAUSE", PLAYBACK_ERROR: "PLAYBACK_ERROR", PLAYER_INIT_ERROR: "PLAYER_INIT_ERROR", PLAYER_READY: "PLAYER_READY", PLAYER_STATE_CHANGED: "PLAYER_STATE_CHANGED", PREV_TRACK: "PREV_TRACK", RESUME: "RESUME", SEEK: "SEEK", SET_VOLUME: "SET_VOLUME", TOGGLE_PLAY: "TOGGLE_PLAY", TOKEN: "TOKEN", VOLUME: "VOLUME", accountError: function accountError(e) {
|
213 | return this._createEventMessage(s.ACCOUNT_ERROR, { message: e });
|
214 | }, authError: function authError(e) {
|
215 | return this._createEventMessage(s.AUTH_ERROR, e);
|
216 | }, playbackError: function playbackError(e) {
|
217 | return this._createEventMessage(s.PLAYBACK_ERROR, e);
|
218 | }, playerReady: function playerReady(e) {
|
219 | return this._createEventMessage(s.PLAYER_READY, e);
|
220 | }, connect: function connect() {
|
221 | return this._createMessage(s.CONNECT);
|
222 | }, connected: function connected(e, t) {
|
223 | return this._createMessage(s.CONNECTED, { connected: e, ref: t });
|
224 | }, disconnect: function disconnect() {
|
225 | return this._createMessage(s.DISCONNECT);
|
226 | }, init: function init(e) {
|
227 | return this._createMessage(s.INIT, e);
|
228 | }, playerInitError: function playerInitError(e) {
|
229 | return this._createEventMessage(s.PLAYER_INIT_ERROR, e);
|
230 | }, getToken: function getToken() {
|
231 | return this._createMessage(s.GET_TOKEN);
|
232 | }, token: function token(e, t) {
|
233 | return this._createMessage(s.TOKEN, { token: e, ref: t });
|
234 | }, pause: function pause() {
|
235 | return this._createMessage(s.PAUSE);
|
236 | }, resume: function resume() {
|
237 | return this._createMessage(s.RESUME);
|
238 | }, togglePlay: function togglePlay() {
|
239 | return this._createMessage(s.TOGGLE_PLAY);
|
240 | }, seek: function seek(e) {
|
241 | return this._createMessage(s.SEEK, e);
|
242 | }, nextTrack: function nextTrack(e) {
|
243 | return this._createMessage(s.NEXT_TRACK, e);
|
244 | }, previousTrack: function previousTrack(e) {
|
245 | return this._createMessage(s.PREV_TRACK, e);
|
246 | }, getCurrentState: function getCurrentState() {
|
247 | return this._createMessage(s.GET_CURRENT_STATE);
|
248 | }, currentState: function currentState(e, t) {
|
249 | return this._createMessage(s.CURRENT_STATE, { state: e, ref: t });
|
250 | }, playerStateChanged: function playerStateChanged(e) {
|
251 | return this._createEventMessage(s.PLAYER_STATE_CHANGED, e);
|
252 | }, getVolume: function getVolume() {
|
253 | return this._createMessage(s.GET_VOLUME);
|
254 | }, volume: function volume(e, t) {
|
255 | return this._createMessage(s.VOLUME, { volume: e, ref: t });
|
256 | }, setVolume: function setVolume(e) {
|
257 | return this._createMessage(s.SET_VOLUME, e);
|
258 | }, embeddedLoaded: function embeddedLoaded() {
|
259 | return this._createMessage(s.LOADED);
|
260 | }, _createEventMessage: function _createEventMessage(e, t) {
|
261 | return this._createMessage(s.EVENT, { name: e, eventData: t });
|
262 | }, _createMessage: function _createMessage(e, t) {
|
263 | return { type: s.SPOTIFY_MESSAGE, body: { topic: e, data: t ? JSON.parse(JSON.stringify(t)) : null } };
|
264 | } };e.exports = s;
|
265 | }, function (e, t) {
|
266 | var n;n = function () {
|
267 | return this;
|
268 | }();try {
|
269 | n = n || Function("return this")() || (eval)("this");
|
270 | } catch (e) {
|
271 | "object" == (typeof window === "undefined" ? "undefined" : _typeof(window)) && (n = window);
|
272 | }e.exports = n;
|
273 | }, function (e, t, n) {
|
274 | function s() {
|
275 | var e = n(3);if (!document.body) throw new Error("Document doesn't have a body");if (window.Spotify = { Player: e.setupPlayerEnv(window) }, window.onSpotifyWebPlaybackSDKReady) window.onSpotifyWebPlaybackSDKReady();else {
|
276 | if (!window.onSpotifyPlayerAPIReady) throw new Error("onSpotifyWebPlaybackSDKReady is not defined");window.onSpotifyPlayerAPIReady();
|
277 | }
|
278 | }"complete" === document.readyState ? s() : window.addEventListener("load", s);
|
279 | }, function (e, t, n) {
|
280 | var s = n(4),
|
281 | r = n(5),
|
282 | o = n(0),
|
283 | i = n(6),
|
284 | a = function a(e, t) {
|
285 | var n,
|
286 | a = "https://sdk.scdn.co/embedded/index.html",
|
287 | u = r.defer(),
|
288 | c = new s(),
|
289 | _ = t || function (t) {
|
290 | var n = e.document.createElement("iframe");return n.src = t, n.style.setProperty("display", "none", "important"), n.allow = "encrypted-media", e.document.body.appendChild(n), n.contentWindow;
|
291 | },
|
292 | h = function h(t) {
|
293 | t === o.LOADED && (c.stopListening(e, h), u.resolve());
|
294 | };c.listen(e, h), n = _(a);var d = function d(t) {
|
295 | this._options = this._sanitizeOptions(t), this._handleMessages = this._handleMessages.bind(this), this._messageHandlers = {}, this._eventListeners = {}, this._setupMessageHandlers(), this._connectionRequests = {}, this._getCurrentStateRequests = {}, this._getVolumeRequests = {}, this.isLoaded = u.promise.then(function () {
|
296 | c.listen(e, this._handleMessages), this._sendMessage(o.init(this._options));
|
297 | }.bind(this));
|
298 | };return d.prototype._sanitizeOptions = function (t) {
|
299 | var n = e && e.location && e.location.hostname || "";return { name: t.name || n, id: i.get(), getOAuthToken: t.getOAuthToken || t.getOauthToken, volume: t.volume || 1 };
|
300 | }, d.prototype._setupMessageHandlers = function () {
|
301 | this._messageHandlers[o.GET_TOKEN] = this._onGetToken.bind(this), this._messageHandlers[o.EVENT] = this._onEvent.bind(this), this._messageHandlers[o.CONNECTED] = this._onConnected.bind(this), this._messageHandlers[o.CURRENT_STATE] = this._onCurrentState.bind(this), this._messageHandlers[o.VOLUME] = this._onVolume.bind(this);
|
302 | }, d.prototype.connect = function () {
|
303 | return this.isLoaded.then(function () {
|
304 | var e = this._sendMessage(o.connect());return this._connectionRequests[e] = r.defer(), this._connectionRequests[e].promise;
|
305 | }.bind(this));
|
306 | }, d.prototype.on = function (e, t) {
|
307 | this._eventListeners[e] = this._eventListeners[e] || [], this._eventListeners[e].push(t);
|
308 | }, d.prototype.getCurrentState = function () {
|
309 | return this.isLoaded.then(function () {
|
310 | var e = this._sendMessage(o.getCurrentState());return this._getCurrentStateRequests[e] = r.defer(), this._getCurrentStateRequests[e].promise;
|
311 | }.bind(this));
|
312 | }, d.prototype.getVolume = function () {
|
313 | return this.isLoaded.then(function () {
|
314 | var e = this._sendMessage(o.getVolume());return this._getVolumeRequests[e] = r.defer(), this._getVolumeRequests[e].promise;
|
315 | }.bind(this));
|
316 | }, d.prototype.setVolume = function (e) {
|
317 | return this._sendMessageWhenLoaded(o.setVolume(e));
|
318 | }, d.prototype.pause = function () {
|
319 | return this._sendMessageWhenLoaded(o.pause());
|
320 | }, d.prototype.resume = function () {
|
321 | return this._sendMessageWhenLoaded(o.resume());
|
322 | }, d.prototype.togglePlay = function () {
|
323 | return this._sendMessageWhenLoaded(o.togglePlay());
|
324 | }, d.prototype.seek = function (e) {
|
325 | return this._sendMessageWhenLoaded(o.seek(e));
|
326 | }, d.prototype.previousTrack = function (e) {
|
327 | return this._sendMessageWhenLoaded(o.previousTrack(e));
|
328 | }, d.prototype.nextTrack = function (e) {
|
329 | return this._sendMessageWhenLoaded(o.nextTrack(e));
|
330 | }, d.prototype.disconnect = function () {
|
331 | return this._sendMessageWhenLoaded(o.disconnect());
|
332 | }, d.prototype._getListeners = function () {
|
333 | var e = {};return e[o.ACCOUNT_ERROR] = "account_error", e[o.AUTH_ERROR] = "authentication_error", e[o.PLAYBACK_ERROR] = "playback_error", e[o.PLAYER_INIT_ERROR] = "initialization_error", e[o.PLAYER_READY] = "ready", e[o.PLAYER_STATE_CHANGED] = "player_state_changed", function (t) {
|
334 | return this._eventListeners[e[t]] || [];
|
335 | };
|
336 | }(), d.prototype._onEvent = function (e) {
|
337 | this._getListeners(e.name).forEach(function (t) {
|
338 | t(e.eventData);
|
339 | });
|
340 | }, d.prototype._onGetToken = function (e, t) {
|
341 | var n = this._options.getOAuthToken;if ("function" != typeof n) {
|
342 | var s = "getOAuthToken is not a function";if (this._getListeners(o.PLAYER_INIT_ERROR).length) return void this._onEvent({ name: o.PLAYER_INIT_ERROR, eventData: { message: s } });throw new Error(s);
|
343 | }new r(n).then(function (e) {
|
344 | this._sendMessage(o.token(e, t));
|
345 | }.bind(this));
|
346 | }, d.prototype._onConnected = function (e) {
|
347 | e.ref in this._connectionRequests && (this._connectionRequests[e.ref].resolve(e.connected), delete this._connectionRequests[e.ref]);
|
348 | }, d.prototype._onCurrentState = function (e) {
|
349 | e.ref in this._getCurrentStateRequests && (this._getCurrentStateRequests[e.ref].resolve(e.state), delete this._getCurrentStateRequests[e.ref]);
|
350 | }, d.prototype._onVolume = function (e) {
|
351 | e.ref in this._getVolumeRequests && (this._getVolumeRequests[e.ref].resolve(e.volume), delete this._getVolumeRequests[e.ref]);
|
352 | }, d.prototype._handleMessages = function (e, t, n) {
|
353 | e in this._messageHandlers && this._messageHandlers[e](t, n);
|
354 | }, d.prototype._sendMessage = function (e) {
|
355 | return c.send(n, e, a);
|
356 | }, d.prototype._sendMessageWhenLoaded = function (e) {
|
357 | return this.isLoaded.then(this._sendMessage.bind(this, e));
|
358 | }, d;
|
359 | };e.exports.setupPlayerEnv = a;
|
360 | }, function (e, t, n) {
|
361 | var s = n(0),
|
362 | r = function r() {
|
363 | this._onMessageCallback = function () {}, this._receiveMessage = this._receiveMessage.bind(this), this._messageId = 0;
|
364 | };r.prototype.listen = function (e, t) {
|
365 | this._onMessageCallback = t, e.addEventListener("message", this._receiveMessage);
|
366 | }, r.prototype.stopListening = function (e) {
|
367 | e.removeEventListener("message", this._receiveMessage);
|
368 | }, r.prototype.send = function (e, t, n) {
|
369 | return e.postMessage(this._addMessageId(t), n || "*"), t.seq;
|
370 | }, r.prototype._addMessageId = function (e) {
|
371 | return e.seq = this._messageId++, e;
|
372 | }, r.prototype._receiveMessage = function (e) {
|
373 | var t = e.data;t.type === s.SPOTIFY_MESSAGE && t.body && t.body.topic && this._onMessageCallback(t.body.topic, t.body.data, t.seq);
|
374 | }, e.exports = r;
|
375 | }, function (e, t, n) {
|
376 | (function (t) {
|
377 | var n = t.Promise;if (!n) throw new Error("Fatal: no Promise implementation available.");var s;s = Object.defineProperty ? function (e, t, n) {
|
378 | return e[t] ? e : (Object.defineProperty(e, t, { value: n, configurable: !0, writable: !0 }), e);
|
379 | } : function (e, t, n) {
|
380 | return e[t] ? e : (e[t] = n, e);
|
381 | }, s(n, "defer", function () {
|
382 | var e = {};return e.promise = new n(function (t, n) {
|
383 | e.resolve = t, e.reject = n;
|
384 | }), e;
|
385 | }), s(n.prototype, "spread", function (e, t) {
|
386 | return this.then(function (e) {
|
387 | return n.all(e);
|
388 | }).then(function (t) {
|
389 | return 1 === t.length ? e(t[0]) : e.apply(this, t);
|
390 | }, t);
|
391 | }), e.exports = n;
|
392 | }).call(t, n(1));
|
393 | }, function (e, t, n) {
|
394 | (function (t) {
|
395 | function s() {
|
396 | var e = new t.Uint8Array(16);return t.crypto.getRandomValues(e), i.toHex(e.join(""), 40).slice(0, 40);
|
397 | }function r() {
|
398 | for (var e = new Array(27), t = e.length; t--;) {
|
399 | e[t] = Math.floor(8 * Math.random());
|
400 | }return i.toHex(e.join(""), 40);
|
401 | }function o(e, n) {
|
402 | if (e && "string" == typeof e) return e;if (n) return u();var s = t.localStorage.getItem(c);return s || (s = u(), t.localStorage.setItem(c, s)), s;
|
403 | }var i = n(7),
|
404 | a = "function" == typeof t.Uint8Array && void 0 !== t.crypto && "function" == typeof t.crypto.getRandomValues,
|
405 | u = a ? s : r,
|
406 | c = "_spharmony_device_id";e.exports = { get: o, generate: u, generateWithCrypto: s, generateWithRandom: r };
|
407 | }).call(t, n(1));
|
408 | }, function (e, t, n) {
|
409 | function s(e, t, n) {
|
410 | for (var s = 0, r = 0; r < e.length; ++r) {
|
411 | var o = e[r] * t + s;e[r] = o % n, s = ~~(o / n);
|
412 | }for (; s;) {
|
413 | e.push(s % n), s = ~~(s / n);
|
414 | }
|
415 | }function r(e, t, n, s) {
|
416 | var r,
|
417 | o,
|
418 | i = 0;for (o = 0; o < t.length; ++o) {
|
419 | r = ~~e[o] + t[o] * n + i, e[o] = r % s, i = ~~(r / s);
|
420 | }for (; i;) {
|
421 | r = ~~e[o] + i, e[o] = r % s, i = ~~(r / s), ++o;
|
422 | }
|
423 | }function o(e, t, n) {
|
424 | for (var o = [0], i = [1], a = 0; a < e.length; ++a) {
|
425 | r(o, i, e[a], n), s(i, t, n);
|
426 | }return o;
|
427 | }function i(e, t) {
|
428 | for (var n = [], s = 0; s < e.length; ++s) {
|
429 | n.push(t[e[s]]);
|
430 | }return n.reverse();
|
431 | }function a(e, t) {
|
432 | for (; e.length < t;) {
|
433 | e.push(0);
|
434 | }return e;
|
435 | }var u = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
436 | c = {},
|
437 | _ = {};!function () {
|
438 | var e, t;for (e = 0, t = u.length; e < t; ++e) {
|
439 | _[u[e]] = e;
|
440 | }for (e = 0; e < 16; ++e) {
|
441 | c["0123456789abcdef"[e]] = e;
|
442 | }for (e = 0; e < 16; ++e) {
|
443 | c["0123456789ABCDEF"[e]] = e;
|
444 | }
|
445 | }(), e.exports = { fromBytes: function fromBytes(e, t) {
|
446 | return i(a(o(e.slice(0).reverse(), 256, 62), t), u).join("");
|
447 | }, toBytes: function toBytes(e, t) {
|
448 | return a(o(i(e, _), 62, 256), t).reverse();
|
449 | }, toHex: function toHex(e, t) {
|
450 | return i(a(o(i(e, _), 62, 16), t), u).join("");
|
451 | }, fromHex: function fromHex(e, t) {
|
452 | return i(a(o(i(e, c), 16, 62), t), u).join("");
|
453 | } };
|
454 | }]);
|
455 |
|
456 | var CoreAPI = function () {
|
457 | function CoreAPI(token) {
|
458 | classCallCheck(this, CoreAPI);
|
459 |
|
460 | this.base = 'https://api.spotify.com/';
|
461 | this.token = token;
|
462 | }
|
463 |
|
464 | createClass(CoreAPI, [{
|
465 | key: 'consume',
|
466 | value: function () {
|
467 | var _ref = asyncToGenerator( regeneratorRuntime.mark(function _callee(request, callback) {
|
468 | var result;
|
469 | return regeneratorRuntime.wrap(function _callee$(_context) {
|
470 | while (1) {
|
471 | switch (_context.prev = _context.next) {
|
472 | case 0:
|
473 | _context.next = 2;
|
474 | return request;
|
475 |
|
476 | case 2:
|
477 | result = _context.sent;
|
478 |
|
479 | request = null;
|
480 |
|
481 | if (result.items) {
|
482 | callback(result.items);
|
483 | }
|
484 |
|
485 | if (result.next) {
|
486 | request = this.request(result.next);
|
487 | }
|
488 |
|
489 | case 6:
|
490 | if (request) {
|
491 | _context.next = 0;
|
492 | break;
|
493 | }
|
494 |
|
495 | case 7:
|
496 | case 'end':
|
497 | return _context.stop();
|
498 | }
|
499 | }
|
500 | }, _callee, this);
|
501 | }));
|
502 |
|
503 | function consume(_x, _x2) {
|
504 | return _ref.apply(this, arguments);
|
505 | }
|
506 |
|
507 | return consume;
|
508 | }()
|
509 | }, {
|
510 | key: 'request',
|
511 | value: function request(url) {
|
512 | var body = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
513 | var method = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'GET';
|
514 |
|
515 | var headers = new Headers({
|
516 | 'Content-Type': 'application/json'
|
517 | });
|
518 |
|
519 | if (this.token) {
|
520 | headers.append('Authorization', 'Bearer ' + this.token);
|
521 | }
|
522 |
|
523 | var req = new Request(url, {
|
524 | method: method,
|
525 | headers: headers,
|
526 | body: body ? JSON.stringify(body) : null
|
527 | });
|
528 |
|
529 | return fetch(req).then(function (response) {
|
530 | var contentType = response.headers.get('content-type');
|
531 |
|
532 | if (contentType && contentType.startsWith('application/json')) {
|
533 | return response.json();
|
534 | }
|
535 |
|
536 | return response.text();
|
537 | });
|
538 | }
|
539 | }, {
|
540 | key: 'url',
|
541 | value: function url(path) {
|
542 | var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
543 |
|
544 | var url = this.base;
|
545 |
|
546 | url += path;
|
547 |
|
548 | if (params) {
|
549 | url += '?' + params.map(function (pair) {
|
550 | return pair.map(encodeURIComponent).join('=');
|
551 | }).join('&');
|
552 | }
|
553 |
|
554 | return url;
|
555 | }
|
556 | }]);
|
557 | return CoreAPI;
|
558 | }();
|
559 |
|
560 | var AlbumAPI = function (_CoreAPI) {
|
561 | inherits(AlbumAPI, _CoreAPI);
|
562 |
|
563 | function AlbumAPI() {
|
564 | classCallCheck(this, AlbumAPI);
|
565 | return possibleConstructorReturn(this, (AlbumAPI.__proto__ || Object.getPrototypeOf(AlbumAPI)).apply(this, arguments));
|
566 | }
|
567 |
|
568 | createClass(AlbumAPI, [{
|
569 | key: 'getAlbum',
|
570 | value: function getAlbum(albumId) {
|
571 | return this.request(this.url('v1/albums/' + albumId));
|
572 | }
|
573 | }]);
|
574 | return AlbumAPI;
|
575 | }(CoreAPI);
|
576 |
|
577 | var PlaylistAPI = function (_CoreAPI) {
|
578 | inherits(PlaylistAPI, _CoreAPI);
|
579 |
|
580 | function PlaylistAPI() {
|
581 | classCallCheck(this, PlaylistAPI);
|
582 | return possibleConstructorReturn(this, (PlaylistAPI.__proto__ || Object.getPrototypeOf(PlaylistAPI)).apply(this, arguments));
|
583 | }
|
584 |
|
585 | createClass(PlaylistAPI, [{
|
586 | key: 'getPlaylists',
|
587 | value: function getPlaylists() {
|
588 | return this.request(this.url('v1/me/playlists'));
|
589 | }
|
590 | }, {
|
591 | key: 'getPlaylist',
|
592 | value: function getPlaylist(userId, playlistId) {
|
593 | return this.request(this.url('v1/users/' + userId + '/playlists/' + playlistId));
|
594 | }
|
595 | }, {
|
596 | key: 'getPlaylistTracks',
|
597 | value: function getPlaylistTracks(userId, playlistId) {
|
598 | return this.request(this.url('v1/users/' + userId + '/playlists/' + playlistId + '/tracks'));
|
599 | }
|
600 | }]);
|
601 | return PlaylistAPI;
|
602 | }(CoreAPI);
|
603 |
|
604 | var PlaybackAPI = function (_CoreAPI) {
|
605 | inherits(PlaybackAPI, _CoreAPI);
|
606 |
|
607 | function PlaybackAPI() {
|
608 | classCallCheck(this, PlaybackAPI);
|
609 | return possibleConstructorReturn(this, (PlaybackAPI.__proto__ || Object.getPrototypeOf(PlaybackAPI)).apply(this, arguments));
|
610 | }
|
611 |
|
612 | createClass(PlaybackAPI, [{
|
613 | key: 'setDevice',
|
614 | value: function setDevice(deviceId) {
|
615 | this.deviceId = deviceId;
|
616 | }
|
617 | }, {
|
618 | key: 'urlWithDevice',
|
619 | value: function urlWithDevice(path, device_id) {
|
620 | if (!device_id) {
|
621 | throw new TypeError('Device id argument missing');
|
622 | }
|
623 |
|
624 | return this.url(path + '?device_id=' + device_id);
|
625 | }
|
626 | }, {
|
627 | key: 'playAlbum',
|
628 | value: function playAlbum(albumURI, trackURI) {
|
629 | return this.request(this.urlWithDevice('v1/me/player/play', this.deviceId), {
|
630 | context_uri: albumURI,
|
631 | offset: defineProperty({}, trackURI ? 'uri' : 'position', trackURI || 1)
|
632 | }, 'PUT');
|
633 | }
|
634 | }, {
|
635 | key: 'playContext',
|
636 | value: function playContext(contextURI) {
|
637 | return this.request(this.urlWithDevice('v1/me/player/play', this.deviceId), {
|
638 | context_uri: contextURI
|
639 | }, 'PUT');
|
640 | }
|
641 | }, {
|
642 | key: 'playTracks',
|
643 | value: function playTracks(trackURIs, trackURI) {
|
644 | return this.request(this.urlWithDevice('v1/me/player/play', this.deviceId), {
|
645 | uris: trackURIs,
|
646 | offset: {
|
647 | uri: trackURI
|
648 | }
|
649 | }, 'PUT');
|
650 | }
|
651 | }, {
|
652 | key: 'resume',
|
653 | value: function resume() {
|
654 | return this.request(this.url('v1/me/player/play'), null, 'PUT');
|
655 | }
|
656 | }, {
|
657 | key: 'pause',
|
658 | value: function pause() {
|
659 | return this.request(this.url('v1/me/player/pause'), null, 'PUT');
|
660 | }
|
661 | }, {
|
662 | key: 'next',
|
663 | value: function next() {
|
664 | return this.request(this.url('v1/me/player/next'), null, 'POST');
|
665 | }
|
666 | }, {
|
667 | key: 'prev',
|
668 | value: function prev() {
|
669 | return this.request(this.url('v1/me/player/previous'), null, 'POST');
|
670 | }
|
671 | }, {
|
672 | key: 'seek',
|
673 | value: function seek(ms) {
|
674 | return this.request(this.url('v1/me/player/seek', [['position_ms', ms]]), null, 'PUT');
|
675 | }
|
676 | }]);
|
677 | return PlaybackAPI;
|
678 | }(CoreAPI);
|
679 |
|
680 | var SearchAPI = function (_CoreAPI) {
|
681 | inherits(SearchAPI, _CoreAPI);
|
682 |
|
683 | function SearchAPI() {
|
684 | classCallCheck(this, SearchAPI);
|
685 | return possibleConstructorReturn(this, (SearchAPI.__proto__ || Object.getPrototypeOf(SearchAPI)).apply(this, arguments));
|
686 | }
|
687 |
|
688 | createClass(SearchAPI, [{
|
689 | key: 'search',
|
690 | value: function search(type, query) {
|
691 | var limit = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 50;
|
692 |
|
693 | var params = [['type', type], ['limit', limit], ['q', query]];
|
694 | return this.request(this.url('v1/search', params));
|
695 | }
|
696 | }]);
|
697 | return SearchAPI;
|
698 | }(CoreAPI);
|
699 |
|
700 | var TrackAPI = function (_CoreAPI) {
|
701 | inherits(TrackAPI, _CoreAPI);
|
702 |
|
703 | function TrackAPI() {
|
704 | classCallCheck(this, TrackAPI);
|
705 | return possibleConstructorReturn(this, (TrackAPI.__proto__ || Object.getPrototypeOf(TrackAPI)).apply(this, arguments));
|
706 | }
|
707 |
|
708 | createClass(TrackAPI, [{
|
709 | key: 'getAudioAnalysis',
|
710 | value: function getAudioAnalysis(trackId) {
|
711 | return this.request(this.url('v1/audio-analysis/' + trackId));
|
712 | }
|
713 | }]);
|
714 | return TrackAPI;
|
715 | }(CoreAPI);
|
716 |
|
717 | var playerPromise = new Promise(function (resolve) {
|
718 | window.onSpotifyWebPlaybackSDKReady = function () {
|
719 | console.log('onSpotifyWebPlaybackSDKReady');
|
720 | resolve(window.Spotify);
|
721 | };
|
722 | });
|
723 |
|
724 | var getSpotify = function () {
|
725 | var _ref = asyncToGenerator( regeneratorRuntime.mark(function _callee() {
|
726 | return regeneratorRuntime.wrap(function _callee$(_context) {
|
727 | while (1) {
|
728 | switch (_context.prev = _context.next) {
|
729 | case 0:
|
730 | return _context.abrupt('return', playerPromise);
|
731 |
|
732 | case 1:
|
733 | case 'end':
|
734 | return _context.stop();
|
735 | }
|
736 | }
|
737 | }, _callee, this);
|
738 | }));
|
739 |
|
740 | return function getSpotify() {
|
741 | return _ref.apply(this, arguments);
|
742 | };
|
743 | }();
|
744 |
|
745 | var createPlayer = function () {
|
746 | var _ref2 = asyncToGenerator( regeneratorRuntime.mark(function _callee2(token, options) {
|
747 | var Spotify, player;
|
748 | return regeneratorRuntime.wrap(function _callee2$(_context2) {
|
749 | while (1) {
|
750 | switch (_context2.prev = _context2.next) {
|
751 | case 0:
|
752 | _context2.next = 2;
|
753 | return getSpotify();
|
754 |
|
755 | case 2:
|
756 | Spotify = _context2.sent;
|
757 | player = new Spotify.Player(Object.assign({
|
758 | getOAuthToken: function getOAuthToken(callback) {
|
759 | return callback(token);
|
760 | }
|
761 | }, options));
|
762 | return _context2.abrupt('return', player);
|
763 |
|
764 | case 5:
|
765 | case 'end':
|
766 | return _context2.stop();
|
767 | }
|
768 | }
|
769 | }, _callee2, this);
|
770 | }));
|
771 |
|
772 | return function createPlayer(_x, _x2) {
|
773 | return _ref2.apply(this, arguments);
|
774 | };
|
775 | }();
|
776 |
|
777 | function createAuthorizationURL(clientId, callbackURL) {
|
778 | return 'https://accounts.spotify.com/authorize?' + [['client_id', clientId], ['redirect_uri', callbackURL], ['response_type', 'token'], ['scope', ['user-read-private', 'user-read-playback-state', 'user-modify-playback-state', 'playlist-read-private', 'playlist-read-collaborative', 'streaming'].join(' ')]].map(function (_ref3) {
|
779 | var _ref4 = slicedToArray(_ref3, 2),
|
780 | key = _ref4[0],
|
781 | value = _ref4[1];
|
782 |
|
783 | return key + '=' + encodeURIComponent(value);
|
784 | }).join('&');
|
785 | }
|
786 |
|
787 | exports.AlbumAPI = AlbumAPI;
|
788 | exports.PlaybackAPI = PlaybackAPI;
|
789 | exports.PlaylistAPI = PlaylistAPI;
|
790 | exports.SearchAPI = SearchAPI;
|
791 | exports.TrackAPI = TrackAPI;
|
792 | exports.getSpotify = getSpotify;
|
793 | exports.createPlayer = createPlayer;
|
794 | exports.createAuthorizationURL = createAuthorizationURL;
|