-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.ts
213 lines (177 loc) · 5.07 KB
/
client.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
import type { SearchParams } from "./shared.ts";
/**
* @see https://developer.spotify.com/documentation/web-api/concepts/api-calls#regular-error-object
*/
export type RegularErrorObject = {
error: {
message: string;
status: number;
reason?: string;
};
};
export class SpotifyError extends Error {
name = "SpotifyError";
constructor(
message: string,
public readonly response: Response,
public readonly body: RegularErrorObject | string | null,
options?: ErrorOptions,
) {
super(message, options);
}
get url() {
return this.response.url;
}
get status() {
return this.response.status;
}
}
const APP_JSON = "application/json";
const CONTENT_TYPE = "Content-Type";
const getBodyMessage = (body: RegularErrorObject | string | null) => {
if (body === null) return null;
if (typeof body === "string") return body;
return (
body.error.message + (body.error.reason ? ` (${body.error.reason})` : "")
);
};
const createSpotifyError = async (
response: Response,
options?: ErrorOptions,
) => {
let message = response.statusText
? `${response.status} ${response.statusText}`
: response.status.toString();
const urlWithoutQuery = response.url.split("?")[0];
if (urlWithoutQuery) {
message += ` (${urlWithoutQuery})`;
}
let body: RegularErrorObject | string | null = null;
if (response.body && response.type !== "opaque") {
try {
body = await response.text();
const contentType = response.headers.get(CONTENT_TYPE);
if (
contentType &&
(contentType === APP_JSON || contentType.split(";")[0] === APP_JSON)
) {
body = JSON.parse(body);
}
} catch (_) {
/* Ignore errors */
}
const bodyMessage = getBodyMessage(body);
if (bodyMessage) {
message += " : " + bodyMessage;
}
}
return new SpotifyError(message, response, body, options);
};
export interface FetchLikeOptions extends Omit<RequestInit, "body"> {
query?: SearchParams;
body?: BodyInit | null | Record<string, unknown> | unknown[];
}
type FetchLike = (
resource: URL,
options: FetchLikeOptions,
) => Promise<Response>;
export type Middleware = (next: FetchLike) => FetchLike;
/**
* Interface for making HTTP requests to the Spotify API.
* All Soundify endpoint functions expect the client to implement this interface.
* You can create a custom client by implementing this interface.
*/
export interface HTTPClient {
fetch: (path: string, options?: FetchLikeOptions) => Promise<Response>;
}
const isPlainObject = (obj: unknown): obj is Record<PropertyKey, unknown> => {
return (
typeof obj === "object" &&
obj !== null &&
Object.prototype.toString.call(obj) === "[object Object]"
);
};
export type SpotifyClinetOptions = {
/**
* Use this option to provide a custom fetch function.
*/
fetch?: (input: URL, init?: RequestInit) => Promise<Response>;
/**
* @default "https://api.spotify.com/"
*/
baseUrl?: string;
/**
* @returns new access token
*/
refresher?: () => Promise<string>;
/**
* @default false
*/
waitForRateLimit?: boolean | ((retryAfter: number) => boolean);
middlewares?: Middleware[];
};
export class SpotifyClient implements HTTPClient {
private readonly baseUrl: string;
constructor(
private accessToken: string,
private readonly options: SpotifyClinetOptions = {},
) {
this.baseUrl = options.baseUrl
? options.baseUrl
: "https://api.spotify.com/";
}
fetch(path: string, opts: FetchLikeOptions = {}) {
const url = new URL(path, this.baseUrl);
if (opts.query) {
for (const key in opts.query) {
const value = opts.query[key];
if (typeof value !== "undefined") {
url.searchParams.set(key, value.toString());
}
}
}
const headers = new Headers(opts.headers);
headers.set("Accept", APP_JSON);
const isBodyJSON = !!opts.body &&
(isPlainObject(opts.body) || Array.isArray(opts.body));
if (isBodyJSON) {
headers.set(CONTENT_TYPE, APP_JSON);
}
const body = isBodyJSON
? JSON.stringify(opts.body)
: (opts.body as BodyInit | null | undefined);
let isRefreshed = false;
const wrappedFetch = (this.options.middlewares || []).reduceRight(
(next, mw) => mw(next),
(this.options.fetch || globalThis.fetch) as FetchLike,
);
const recursiveFetch = async (): Promise<Response> => {
headers.set("Authorization", "Bearer " + this.accessToken);
const res = await wrappedFetch(url, { ...opts, body, headers });
if (res.ok) return res;
if (res.status === 401 && this.options.refresher && !isRefreshed) {
this.accessToken = await this.options.refresher();
isRefreshed = true;
return recursiveFetch();
}
if (res.status === 429) {
// time in seconds
const retryAfter = Number(res.headers.get("Retry-After")) || undefined;
if (retryAfter) {
const waitForRateLimit =
typeof this.options.waitForRateLimit === "function"
? this.options.waitForRateLimit(retryAfter)
: this.options.waitForRateLimit;
if (waitForRateLimit) {
await new Promise((resolve) =>
setTimeout(resolve, retryAfter * 1000)
);
}
return recursiveFetch();
}
}
throw await createSpotifyError(res);
};
return recursiveFetch();
}
}