-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathClient.ts
More file actions
290 lines (254 loc) · 9.13 KB
/
Copy pathClient.ts
File metadata and controls
290 lines (254 loc) · 9.13 KB
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
import axios, { AxiosError } from "axios";
import { SpotifyAPIError } from "./Error";
import { AuthManager } from "./managers/Auth";
import { UserManager } from "./managers/User";
import { ArtistManager } from "./managers/Artist";
import { BrowseManager } from "./managers/Browse";
import { AlbumManager } from "./managers/Album";
import { EpisodeManager } from "./managers/Episode";
import { PlaylistManager } from "./managers/Playlist";
import { ShowManager } from "./managers/Show";
import { TrackManager } from "./managers/Track";
import { UserClient } from "./managers/UserClient";
import { createCacheStructArray } from "./Cache";
import type {
ClientOptions,
FetchOptions,
ClientRefreshMeta,
GetUserTokenOptions,
CacheSettings,
ClientSearchOptions,
SearchContent,
} from "./Interface";
const NOOP = () => {};
/**
* The basic client to interact with the Spotify Web API.
*/
export class Client {
/**
* The token of the spotify web client.
*/
public token!: string;
/**
* The manager to perform actions regarding the authorization to the web api.
*/
public auth!: AuthManager;
/**
* A manager to perform actions which belongs to the spotify user web api.
*/
public users!: UserManager;
/**
* A manager to perform actions which belongs to the spotify artist web api.
*/
public artists!: ArtistManager;
/**
* A manager to perform actions which belongs to the spotify browse web api.
*/
public browse!: BrowseManager;
/**
* A manager to perform actions which belongs to the spotify album web api.
*/
public albums!: AlbumManager;
/**
* A manager to perform actions which belongs to the spotify episode web api.
*/
public episodes!: EpisodeManager;
/**
* A manager to perform actions which belongs to the spotify playlist web api.
*/
public playlists!: PlaylistManager;
/**
* A manager to perform actions which belongs to the spotify show web api.
*/
public shows!: ShowManager;
/**
* A manager to perform actions which belongs to the spotify track web api.
*/
public tracks!: TrackManager;
/**
* The client which handles all the current user api endpoints and with the details of the current user.
*/
public user!: UserClient;
/**
* The version of spotify web api. For future purposes.
*/
public version: `v${number}` = "v1";
/**
* The refresh event of the client.
*/
public onRefresh: () => void = NOOP;
/**
* The metadata for continous refresh of token.
*/
public refreshMeta?: ClientRefreshMeta;
/**
* Boolean stating should the client retry when the request is rate limited or not by default it is true.
*/
public retryOnRateLimit?: boolean = true;
/**
* Cache settings for the client.
*/
public cacheSettings: CacheSettings = {};
/**
* The basic client to interact with the Spotify Web API.
*
* @param options The options necessary for the client.
* @example const client = new Client({ token: "someToken" });
*/
public constructor(options: ClientOptions) {
this.onRefresh = options.onRefresh || NOOP;
this.retryOnRateLimit = options.retryOnRateLimit ?? true;
this.auth = new AuthManager(this.token);
this.users = new UserManager(this);
this.artists = new ArtistManager(this);
this.browse = new BrowseManager(this);
this.albums = new AlbumManager(this);
this.episodes = new EpisodeManager(this);
this.playlists = new PlaylistManager(this);
this.shows = new ShowManager(this);
this.tracks = new TrackManager(this);
this._init(options);
if (typeof options.cacheSettings == "object") this.cacheSettings = options.cacheSettings;
else if (options.cacheSettings == true)
this.cacheSettings = {
users: true,
artists: true,
tracks: true,
episodes: true,
shows: true,
albums: true,
playlists: true,
};
}
/**
* Creates a client and returns it as promise when its ready.
*
* @param options The same client options provided for the constructor but "onReady" and "onFail" fields should not be provided.
* @example const client = await Client.create({ token: "token" });
*/
public static create(options: Omit<ClientOptions, "onReady" | "onFail">): Promise<Client> {
return new Promise((onReady, onFail) => new Client({ ...options, onReady, onFail }));
}
/**
* Search a query in spotify through web api across various types.
*
* @param query The query to search.
* @param options The types, limit, offset, market query paramaters.
* @example const { tracks, albums } = await client.search('some query', { types: ['track', 'album'] });
*/
public async search(query: string, options: ClientSearchOptions): Promise<SearchContent> {
const response: SearchContent = {};
const fetchedData = await this.fetch("/search", {
params: {
q: query,
type: options.types.join(","),
market: options.market,
limit: options.limit,
offset: options.offset,
include_external: options.includeExternalAudio ? "audio" : undefined,
},
});
if (fetchedData.albums) response.albums = createCacheStructArray("albums", this, fetchedData.albums.items);
if (fetchedData.tracks) response.tracks = createCacheStructArray("tracks", this, fetchedData.tracks.items);
if (fetchedData.episodes)
response.episodes = createCacheStructArray("episodes", this, fetchedData.episodes.items);
if (fetchedData.shows) response.shows = createCacheStructArray("shows", this, fetchedData.shows.items);
if (fetchedData.artists) response.artists = createCacheStructArray("artists", this, fetchedData.artists.items);
return response;
}
/**
* Used to fetch data from spotify rest api.
*
* @param url The path from spotify api to fetch!
* @param options The additional options required to fetch from the api.
* @example await client.fetch('/users/id');
*/
public fetch(url: string, options: FetchOptions = {}) {
return axios({
url: `https://api.spotify.com/${this.version}${url}`,
method: options.method || "GET",
params: options.params,
headers: {
Authorization: "Bearer " + this.token,
Accept: "application/json",
...options.headers,
},
data: options.body,
}).then(
response => response.data,
async (error: AxiosError) => {
if (!error.response) throw new SpotifyAPIError(error);
else if (error.response.status == 404) return null;
else if (error.response.status == 429 && this.retryOnRateLimit) {
const retryAfter = error.response.headers["Retry-After"];
if (typeof retryAfter == "number") await new Promise(r => setTimeout(r, retryAfter * 1000));
} else if (
// @ts-expect-error - Token can be invalid.
error.response.data?.error?.message == "Invalid access token" ||
// @ts-expect-error - Token can be expired.
(error.response.data?.error?.message == "The access token expired" && this.refreshMeta)
) {
await this.refreshFromMeta();
return;
} else throw new SpotifyAPIError(error);
return this.fetch(url, options);
}
);
}
/**
* Refreshes the token from meta.
*/
public async refreshFromMeta() {
if (!this.refreshMeta) return;
if ("refreshToken" in this.refreshMeta!) {
this.auth.getUserToken(this.refreshMeta as GetUserTokenOptions).then(context => {
this.token = context.accessToken;
if (context.refreshToken) this.refreshMeta!.refreshToken = context.refreshToken;
new UserClient(this).patchInfo().then(x => {
this.user = x;
this.onRefresh();
});
});
} else {
this.auth.getApiToken(this.refreshMeta!.clientID, this.refreshMeta!.clientSecret).then(token => {
this.token = token;
this.onRefresh();
});
}
this.auth = new AuthManager(this.token);
}
/**
* A function to initate the client through options and client options.
*/
private async _init(options: ClientOptions) {
if (!options.token) throw new SpotifyAPIError("No token was provided in [ClientOptions]");
try {
if (typeof options.token == "string") {
if (options.refreshToken)
console.trace(
"[SpotifyClientWarn]: You have provided a token and used `refreshToken` option. Try to provide clientID, clientSecret or user authenication details."
);
this.token = options.token;
if (options.userAuthorizedToken) this.user = await new UserClient(this).patchInfo();
} else if ("token" in options.token) {
this.token = options.token.token;
this.refreshMeta = options.token;
if (options.userAuthorizedToken) this.user = await new UserClient(this).patchInfo();
} else if ("redirectURL" in options.token || "refreshToken" in options.token) {
const context = await this.auth.getUserToken(options.token as GetUserTokenOptions);
this.refreshMeta = options.token;
if (context.refreshToken) this.refreshMeta.refreshToken = context.refreshToken;
this.token = context.accessToken;
this.user = await new UserClient(this).patchInfo();
} else if ("clientID" in options.token) {
this.refreshMeta = options.token;
this.token = await this.auth.getApiToken(options.token.clientID, options.token.clientSecret);
} else throw new SpotifyAPIError("Improper [ClientOptions] provided!.");
options.onReady?.(this);
} catch (originalError) {
const spotifyError = new SpotifyAPIError(originalError as AxiosError);
if (options.onFail) options.onFail(spotifyError);
else throw spotifyError;
}
}
}