-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify-web-api.js
137 lines (122 loc) · 4.2 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
import fetch from 'node-fetch';
import uuid4 from 'uuid/v4';
class ClientError extends Error {
constructor(name, code, value = null) {
super(`${name} (${code})`);
Error.captureStackTrace(this, ClientError);
this.name = name;
this.code = code;
this.value = value;
}
}
const queryStringify = query => Object.entries(query).map(pair => pair.join('=')).join('&');
const responseError = res => {
if (res.status > 299) {
throw new ClientError(...[
res.statusText,
res.status,
...(
res.status === 429 ? [ parseInt(res.headers.get('Retry-After')) ] : []
)
]);
}
return res;
};
const handleResponse = async res => {
if (res.status === 204) {
return { body: {}, no_content: true, res };
}
if (res.status === 202) {
return { body: {}, accepted: true, res };
}
try {
let body = await res.json();
return { body, res } ;
} catch (err) {
console.error(err);
return { body: {}, error: err.message, res };
}
};
const api = ({
client_id,
client_secret,
redirect_uri,
scope = []
}) => {
const endpoint = 'https://api.spotify.com/v1';
const request = new Proxy({}, {
get: (obj, prop) => ({ body, path, query }, { access_token, access_type = 'Bearer'}) => {
let url = `${endpoint}/${path}`;
if (query) {
url = `${url}?${queryStringify(query)}`;
}
//console.log(prop.toUpperCase(), url);
return fetch(url, {
method: prop.toUpperCase(),
headers: {
Authorization: `${access_type} ${access_token}`
},
...(body && { body: JSON.stringify(body) })
})
.then(responseError)
.then(handleResponse);
}
});
const token = ({ code, refresh_token, redirect_uri, grant_type = 'authorization_code' }) => {
return fetch('https://accounts.spotify.com/api/token', {
method: 'POST',
headers: {
Authorization: `Basic ${Buffer.from(`${client_id}:${client_secret}`).toString('base64')}`,
'Content-Type': 'application/x-www-form-urlencoded'
},
body: queryStringify({
grant_type,
...(grant_type === 'authorization_code' ? { code, redirect_uri } : { refresh_token } )
})
})
.then(responseError)
.then(res => res.json())
};
return {
init: ({ state } = {}) => {
state = state || uuid4();
const location = `https://accounts.spotify.com/authorize?${queryStringify({
response_type: 'code',
client_id,
scope: scope.join(' '),
redirect_uri,
state
})}`;
return {
uri: location,
header: { Location: location },
state
}
},
auth: {
token: (code) => token({ code, redirect_uri }),
refresh: (refresh_token) => token({ grant_type: 'refresh_token', refresh_token })
},
api: (auth) => ({
user: (user = null) => request.get({
path: user ? `users/${user}` : 'me'
}, auth),
player: {
play: ({ query, body } = {}) => request.put({ path: 'me/player/play', query, body }, auth),
pause: ({ query } = {}) => request.put({ path: 'me/player/pause', query, body: {} }, auth),
previous: ({ query } = {}) => request.post({ path: 'me/player/previous', query }, auth),
next: ({ query } = {}) => request.post({ path: 'me/player/next', query }, auth),
recent: ({ query } = {}) => request.get({ path: 'me/player/recently-played', query }, auth),
playing: ({ query } = {}) => request.get({ path: 'me/player/currently-playing', query }, auth),
player: ({ query } = {}) => request.get({ path: 'me/player', query }, auth),
shuffle: ({ query } = {}) => request.put({ path: 'me/player/shuffle', query, body: {} }, auth),
repeat: ({ query } = {}) => request.put({ path: 'me/player/repeat', query, body: {} }, auth),
volume: ({ query } = {}) => request.put({ path: 'me/player/volume', query, body: {} }, auth),
seek: ({ query } = {}) => request.put({ path: 'me/player/seek', query, body: {} }, auth),
transfer: ({ query } = {}) => request.put({ path: 'me/player', query, body: {} }, auth),
devices: () => request.get({ path: 'me/player/devices' }, auth),
}
})
}
};
export default api;