/
index.ts
227 lines (207 loc) · 6.59 KB
/
index.ts
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
import axios from 'axios';
import qs from 'qs';
import { AlbumsApi } from './apis/AlbumsApi';
import { ArtistsApi } from './apis/ArtistsApi';
import { BrowseApi } from './apis/BrowseApi';
import { EpisodesApi } from './apis/EpisodesApi';
import { FollowApi } from './apis/FollowApi';
import { LibraryApi } from './apis/LibraryApi';
import { PersonalizationApi } from './apis/PersonalizationApi';
import { PlayerApi } from './apis/PlayerApi';
import { PlaylistsApi } from './apis/PlaylistsApi';
import { SearchApi } from './apis/SearchApi';
import { ShowsApi } from './apis/ShowsApi';
import { TracksApi } from './apis/TracksApi';
import { UsersApi } from './apis/UsersApi';
import { TOKEN_URL } from './constants';
import { encodeToBase64 } from './helpers/encodeToBase64';
import {
getAuthorizationUrl,
GetAuthorizationUrlOptions,
} from './helpers/getAuthorizationUrl';
import { Http } from './helpers/Http';
import {
GetRefreshableUserTokensResponse,
GetRefreshedAccessTokenResponse,
GetTemporaryAppTokensResponse,
} from './types/SpotifyAuthorization';
type SpotifyWebApiOptions = {
accessToken?: string;
clientId?: string;
clientSecret?: string;
redirectUri?: string;
};
export class SpotifyWebApi {
private clientId: string;
private clientSecret: string;
private redirectUri: string;
private http: Http;
public albums: AlbumsApi;
public artists: ArtistsApi;
public browse: BrowseApi;
public episodes: EpisodesApi;
public follow: FollowApi;
public library: LibraryApi;
public personalization: PersonalizationApi;
public player: PlayerApi;
public playlists: PlaylistsApi;
public search: SearchApi;
public shows: ShowsApi;
public tracks: TracksApi;
public users: UsersApi;
constructor(options?: SpotifyWebApiOptions) {
this.clientId = options?.clientId ?? '';
this.clientSecret = options?.clientSecret ?? '';
this.redirectUri = options?.redirectUri ?? '';
this.http = new Http(options?.accessToken ?? '');
this.albums = new AlbumsApi(this.http);
this.artists = new ArtistsApi(this.http);
this.browse = new BrowseApi(this.http);
this.episodes = new EpisodesApi(this.http);
this.follow = new FollowApi(this.http);
this.library = new LibraryApi(this.http);
this.personalization = new PersonalizationApi(this.http);
this.player = new PlayerApi(this.http);
this.playlists = new PlaylistsApi(this.http);
this.search = new SearchApi(this.http);
this.shows = new ShowsApi(this.http);
this.tracks = new TracksApi(this.http);
this.users = new UsersApi(this.http);
}
getAccessToken() {
return this.http.getAccessToken();
}
setAccessToken(accessToken: string) {
this.http.setAccessToken(accessToken);
}
getClientId() {
return this.clientId;
}
getClientSecret() {
return this.clientSecret;
}
getRedirectUri() {
return this.redirectUri;
}
// +--------------------+
// | Authorization URLs |
// +--------------------+
/**
* Get an authorization URL for use with the Authorization Code flow.
*
* @param options Optional URL parameters.
*/
getRefreshableAuthorizationUrl(options?: GetAuthorizationUrlOptions): string {
return getAuthorizationUrl(
this.clientId,
this.redirectUri,
'code',
options,
);
}
/**
* Get an authorization URL for use with the Implicit Grant and Client
* Credentials flows.
*
* @param options Optional URL parameters.
*/
getTemporaryAuthorizationUrl(options?: GetAuthorizationUrlOptions): string {
return getAuthorizationUrl(
this.clientId,
this.redirectUri,
'token',
options,
);
}
// +---------------------------+
// | Refreshable Authorization |
// +---------------------------+
/**
* Get refreshable authorization tokens using the Authorization Code flow.
*
* This flow is suitable for long-running applications in which the user
* grants permission only once. It provides an access token that can be
* refreshed. Since the token exchange involves sending your secret key,
* perform this on a secure location, like a backend service, and not from a
* client such as a browser or from a mobile app.
*
* @param code The authorization code returned from the initial request to
* the authorization endpoint.
*/
async getRefreshableUserTokens(
code: string,
): Promise<GetRefreshableUserTokensResponse> {
const response = await axios.post<GetRefreshableUserTokensResponse>(
TOKEN_URL,
qs.stringify({
code,
grant_type: 'authorization_code',
redirect_uri: this.redirectUri,
}),
{
headers: {
Authorization: `Basic ${encodeToBase64(
`${this.clientId}:${this.clientSecret}`,
)}`,
'Content-Type': 'application/x-www-form-urlencoded',
},
},
);
return response.data;
}
/**
* Obtain a refreshed access token given the original refresh token from the
* initial authorization code exchange.
*
* @param refreshToken The refresh token returned from the authorization code
* exchange.
*/
async getRefreshedAccessToken(
refreshToken: string,
): Promise<GetRefreshedAccessTokenResponse> {
const response = await axios.post<GetRefreshedAccessTokenResponse>(
TOKEN_URL,
qs.stringify({
grant_type: 'refresh_token',
refresh_token: refreshToken,
}),
{
headers: {
Authorization: `Basic ${encodeToBase64(
`${this.clientId}:${this.clientSecret}`,
)}`,
'Content-Type': 'application/x-www-form-urlencoded',
},
},
);
return response.data;
}
// +-------------------------+
// | Temporary Authorization |
// +-------------------------+
/**
* Get temporary authorization tokens using the Client Credentials flow.
*
* The Client Credentials flow is used in server-to-server authentication.
* Only endpoints that do not access user information can be accessed. The
* advantage here in comparison with requests to the Web API made without an
* access token, is that a higher rate limit is applied.
*/
async getTemporaryAppTokens(): Promise<GetTemporaryAppTokensResponse> {
const response = await axios.post<GetTemporaryAppTokensResponse>(
TOKEN_URL,
qs.stringify({
grant_type: 'client_credentials',
}),
{
headers: {
Authorization: `Basic ${encodeToBase64(
`${this.clientId}:${this.clientSecret}`,
)}`,
'Content-Type': 'application/x-www-form-urlencoded',
},
},
);
return response.data;
}
}