UNPKG

1.3 kBJavaScriptView Raw
1import {AlbumAPI, PlaybackAPI, PlaylistAPI, SearchAPI, TrackAPI} from './API.js';
2import * as analysis from './analysis.js';
3
4export {
5 AlbumAPI,
6 PlaybackAPI,
7 PlaylistAPI,
8 SearchAPI,
9 TrackAPI,
10 analysis,
11};
12
13const playerPromise = new Promise(resolve => {
14 window.onSpotifyWebPlaybackSDKReady = () => {
15 console.log('onSpotifyWebPlaybackSDKReady');
16 resolve(window.Spotify);
17 };
18});
19
20export function getSpotify() {
21 return playerPromise;
22}
23
24export function createPlayer(token, options) {
25 return getSpotify()
26 .then(Spotify => {
27 const player = new Spotify.Player(Object.assign({
28 getOAuthToken: callback => callback(token),
29 }, options));
30
31 return player;
32 });
33}
34
35export function createAuthorizationURL(clientId, callbackURL) {
36 return (
37 'https://accounts.spotify.com/authorize?' +
38 [
39 ['client_id', clientId],
40 ['redirect_uri', callbackURL],
41 ['response_type', 'token'],
42 [
43 'scope',
44 [
45 'user-read-private',
46 'user-read-playback-state',
47 'user-modify-playback-state',
48 'playlist-read-private',
49 'playlist-read-collaborative',
50 'streaming',
51 ].join(' '),
52 ],
53 ]
54 .map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
55 .join('&')
56 );
57}