@borealise/api 2.0.0-alpha.8 → 2.1.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -9
- package/dist/index.d.mts +148 -22
- package/dist/index.d.ts +148 -22
- package/dist/index.js +159 -119
- package/dist/index.mjs +159 -118
- package/package.json +50 -51
package/dist/index.js
CHANGED
|
@@ -30,7 +30,6 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
-
Api: () => Api,
|
|
34
33
|
ApiError: () => ApiError,
|
|
35
34
|
Logger: () => Logger,
|
|
36
35
|
createAdminResource: () => createAdminResource,
|
|
@@ -48,8 +47,55 @@ __export(index_exports, {
|
|
|
48
47
|
});
|
|
49
48
|
module.exports = __toCommonJS(index_exports);
|
|
50
49
|
|
|
51
|
-
// src/
|
|
52
|
-
var
|
|
50
|
+
// src/api/errors.ts
|
|
51
|
+
var import_ky = require("ky");
|
|
52
|
+
var ApiError = class extends Error {
|
|
53
|
+
constructor(message, status, code, response) {
|
|
54
|
+
super(message);
|
|
55
|
+
this.name = "ApiError";
|
|
56
|
+
this.status = status;
|
|
57
|
+
this.code = code;
|
|
58
|
+
this.response = response;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
var tryParseErrorResponse = async (response) => {
|
|
62
|
+
const contentType = response.headers.get("content-type") ?? "";
|
|
63
|
+
if (contentType.includes("application/json")) {
|
|
64
|
+
return await response.json();
|
|
65
|
+
}
|
|
66
|
+
const text = await response.text();
|
|
67
|
+
return text || void 0;
|
|
68
|
+
};
|
|
69
|
+
var parseApiError = async (error) => {
|
|
70
|
+
if (error instanceof import_ky.HTTPError) {
|
|
71
|
+
const responseData = await tryParseErrorResponse(error.response);
|
|
72
|
+
const isObjectResponse = typeof responseData === "object" && responseData !== null;
|
|
73
|
+
let message;
|
|
74
|
+
let backendResponse;
|
|
75
|
+
if (isObjectResponse && "error" in responseData && responseData.error) {
|
|
76
|
+
message = responseData.error;
|
|
77
|
+
backendResponse = responseData;
|
|
78
|
+
} else if (isObjectResponse && "message" in responseData && responseData.message) {
|
|
79
|
+
message = responseData.message;
|
|
80
|
+
} else if (typeof responseData === "string" && responseData) {
|
|
81
|
+
message = responseData;
|
|
82
|
+
} else {
|
|
83
|
+
message = error.message;
|
|
84
|
+
}
|
|
85
|
+
return new ApiError(message, error.response.status, `HTTP_${error.response.status}`, backendResponse);
|
|
86
|
+
}
|
|
87
|
+
if (error instanceof import_ky.TimeoutError) {
|
|
88
|
+
return new ApiError("Request timed out", void 0, "TIMEOUT_ERROR");
|
|
89
|
+
}
|
|
90
|
+
if (error instanceof Error) {
|
|
91
|
+
return new ApiError(error.message, void 0, "NETWORK_ERROR");
|
|
92
|
+
}
|
|
93
|
+
return new ApiError("Unknown request error", void 0, "UNKNOWN_ERROR");
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// src/api/client.ts
|
|
97
|
+
var import_ky2 = __toESM(require("ky"));
|
|
98
|
+
var import_node_url = require("url");
|
|
53
99
|
|
|
54
100
|
// src/Logger.ts
|
|
55
101
|
var Logger = class _Logger {
|
|
@@ -99,132 +145,114 @@ var Logger = class _Logger {
|
|
|
99
145
|
}
|
|
100
146
|
};
|
|
101
147
|
|
|
102
|
-
// src/
|
|
103
|
-
var
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
this.status = status;
|
|
108
|
-
this.code = code;
|
|
109
|
-
this.response = response;
|
|
148
|
+
// src/api/client.ts
|
|
149
|
+
var resolveUrl = (baseURL, url) => new import_node_url.URL(url, baseURL).toString();
|
|
150
|
+
var parseQueryParams = (params) => {
|
|
151
|
+
if (!params) {
|
|
152
|
+
return void 0;
|
|
110
153
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
constructor(config) {
|
|
114
|
-
this.config = config;
|
|
115
|
-
this.logger = new Logger("Api");
|
|
116
|
-
if (config.logging === false) {
|
|
117
|
-
this.logger.disable();
|
|
118
|
-
}
|
|
119
|
-
this.axios = import_axios.default.create({
|
|
120
|
-
baseURL: config.baseURL,
|
|
121
|
-
timeout: config.timeout ?? 3e4,
|
|
122
|
-
headers: {
|
|
123
|
-
"Content-Type": "application/json",
|
|
124
|
-
...config.headers
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
this.setupInterceptors();
|
|
128
|
-
this.logger.success(`initialized (baseURL: ${config.baseURL})`);
|
|
154
|
+
if (params instanceof import_node_url.URLSearchParams) {
|
|
155
|
+
return params;
|
|
129
156
|
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
return config;
|
|
135
|
-
},
|
|
136
|
-
(error) => {
|
|
137
|
-
this.logger.error("Request error", error);
|
|
138
|
-
return Promise.reject(error);
|
|
139
|
-
}
|
|
140
|
-
);
|
|
141
|
-
this.axios.interceptors.response.use(
|
|
142
|
-
(response) => {
|
|
143
|
-
this.logger.debug(`\u2190 ${response.status} ${response.config.url}`);
|
|
144
|
-
return response;
|
|
145
|
-
},
|
|
146
|
-
(error) => {
|
|
147
|
-
const apiError = this.parseError(error);
|
|
148
|
-
const status = apiError.status ?? apiError.code ?? "ERR";
|
|
149
|
-
this.logger.error(`\u2190 ${status} ${error.config?.url}: ${apiError.message}`);
|
|
150
|
-
return Promise.reject(apiError);
|
|
151
|
-
}
|
|
152
|
-
);
|
|
153
|
-
}
|
|
154
|
-
parseError(error) {
|
|
155
|
-
if (error.response) {
|
|
156
|
-
const responseData = error.response.data;
|
|
157
|
-
let message;
|
|
158
|
-
let backendResponse;
|
|
159
|
-
if (responseData && "error" in responseData && responseData.error) {
|
|
160
|
-
message = responseData.error;
|
|
161
|
-
backendResponse = responseData;
|
|
162
|
-
} else if (responseData && "message" in responseData && responseData.message) {
|
|
163
|
-
message = responseData.message;
|
|
164
|
-
} else {
|
|
165
|
-
message = error.message;
|
|
166
|
-
}
|
|
167
|
-
return new ApiError(message, error.response.status, error.code, backendResponse);
|
|
168
|
-
}
|
|
169
|
-
if (error.request) {
|
|
170
|
-
return new ApiError("No response received from server", void 0, "NETWORK_ERROR");
|
|
157
|
+
const searchParams = new import_node_url.URLSearchParams();
|
|
158
|
+
for (const [key, value] of Object.entries(params)) {
|
|
159
|
+
if (value === void 0 || value === null) {
|
|
160
|
+
continue;
|
|
171
161
|
}
|
|
172
|
-
|
|
173
|
-
}
|
|
174
|
-
setAuthToken(token) {
|
|
175
|
-
if (token) {
|
|
176
|
-
this.axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
|
177
|
-
this.logger.debug("Auth token set");
|
|
178
|
-
} else {
|
|
179
|
-
delete this.axios.defaults.headers.common["Authorization"];
|
|
180
|
-
this.logger.debug("Auth token cleared");
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
setHeader(key, value) {
|
|
184
|
-
this.axios.defaults.headers.common[key] = value;
|
|
162
|
+
searchParams.set(key, String(value));
|
|
185
163
|
}
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
async put(url, data, config) {
|
|
198
|
-
const response = await this.axios.put(url, data, config);
|
|
199
|
-
return this.wrapResponse(response);
|
|
200
|
-
}
|
|
201
|
-
async patch(url, data, config) {
|
|
202
|
-
const response = await this.axios.patch(url, data, config);
|
|
203
|
-
return this.wrapResponse(response);
|
|
204
|
-
}
|
|
205
|
-
async delete(url, config) {
|
|
206
|
-
const response = await this.axios.delete(url, config);
|
|
207
|
-
return this.wrapResponse(response);
|
|
208
|
-
}
|
|
209
|
-
wrapResponse(response) {
|
|
210
|
-
return {
|
|
211
|
-
data: response.data,
|
|
212
|
-
status: response.status,
|
|
213
|
-
headers: response.headers
|
|
214
|
-
};
|
|
164
|
+
return searchParams;
|
|
165
|
+
};
|
|
166
|
+
var wrapResponse = async (response) => {
|
|
167
|
+
const contentType = response.headers.get("content-type") ?? "";
|
|
168
|
+
let data;
|
|
169
|
+
if (response.status === 204) {
|
|
170
|
+
data = void 0;
|
|
171
|
+
} else if (contentType.includes("application/json")) {
|
|
172
|
+
data = await response.json();
|
|
173
|
+
} else {
|
|
174
|
+
data = await response.text();
|
|
215
175
|
}
|
|
216
|
-
|
|
217
|
-
|
|
176
|
+
return {
|
|
177
|
+
data,
|
|
178
|
+
status: response.status,
|
|
179
|
+
headers: Object.fromEntries(response.headers.entries())
|
|
180
|
+
};
|
|
181
|
+
};
|
|
182
|
+
var buildOptions = (defaultHeaders, config) => ({
|
|
183
|
+
headers: {
|
|
184
|
+
...defaultHeaders,
|
|
185
|
+
...config?.headers
|
|
186
|
+
},
|
|
187
|
+
searchParams: parseQueryParams(config?.params),
|
|
188
|
+
timeout: config?.timeout,
|
|
189
|
+
credentials: config?.credentials
|
|
190
|
+
});
|
|
191
|
+
var createApi = (config) => {
|
|
192
|
+
const logger = new Logger("Api");
|
|
193
|
+
if (config.logging === false) {
|
|
194
|
+
logger.disable();
|
|
218
195
|
}
|
|
196
|
+
const defaultHeaders = {
|
|
197
|
+
"Content-Type": "application/json",
|
|
198
|
+
...config.headers
|
|
199
|
+
};
|
|
200
|
+
const client = import_ky2.default.create({
|
|
201
|
+
timeout: config.timeout ?? 3e4,
|
|
202
|
+
credentials: config.withCredentials ? "include" : "same-origin",
|
|
203
|
+
retry: 0,
|
|
204
|
+
throwHttpErrors: true
|
|
205
|
+
});
|
|
206
|
+
const request = async (method, url, data, requestConfig) => {
|
|
207
|
+
logger.debug(`\u2192 ${method.toUpperCase()} ${url}`);
|
|
208
|
+
try {
|
|
209
|
+
const options = buildOptions(defaultHeaders, requestConfig);
|
|
210
|
+
if (data !== void 0) {
|
|
211
|
+
options.json = data;
|
|
212
|
+
}
|
|
213
|
+
const response = await client(resolveUrl(config.baseURL, url), {
|
|
214
|
+
...options,
|
|
215
|
+
method
|
|
216
|
+
});
|
|
217
|
+
logger.debug(`\u2190 ${response.status} ${url}`);
|
|
218
|
+
return wrapResponse(response);
|
|
219
|
+
} catch (error) {
|
|
220
|
+
const apiError = await parseApiError(error);
|
|
221
|
+
const status = apiError.status ?? apiError.code ?? "ERR";
|
|
222
|
+
logger.error(`\u2190 ${status} ${url}: ${apiError.message}`);
|
|
223
|
+
throw apiError;
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
return {
|
|
227
|
+
setAuthToken: (token) => {
|
|
228
|
+
if (token) {
|
|
229
|
+
defaultHeaders["Authorization"] = `Bearer ${token}`;
|
|
230
|
+
logger.debug("Auth token set");
|
|
231
|
+
} else {
|
|
232
|
+
delete defaultHeaders["Authorization"];
|
|
233
|
+
logger.debug("Auth token cleared");
|
|
234
|
+
}
|
|
235
|
+
},
|
|
236
|
+
setHeader: (key, value) => {
|
|
237
|
+
defaultHeaders[key] = value;
|
|
238
|
+
},
|
|
239
|
+
removeHeader: (key) => {
|
|
240
|
+
delete defaultHeaders[key];
|
|
241
|
+
},
|
|
242
|
+
get: (url, requestConfig) => request("get", url, void 0, requestConfig),
|
|
243
|
+
post: (url, data, requestConfig) => request("post", url, data, requestConfig),
|
|
244
|
+
put: (url, data, requestConfig) => request("put", url, data, requestConfig),
|
|
245
|
+
patch: (url, data, requestConfig) => request("patch", url, data, requestConfig),
|
|
246
|
+
delete: (url, requestConfig) => request("delete", url, void 0, requestConfig)
|
|
247
|
+
};
|
|
219
248
|
};
|
|
220
|
-
var createApi = (config) => new Api(config);
|
|
221
249
|
|
|
222
250
|
// src/resources/auth.ts
|
|
223
251
|
var endpoint = "/auth";
|
|
224
252
|
var createAuthResource = (api) => ({
|
|
225
253
|
login: (credentials) => api.post(`${endpoint}/login`, credentials),
|
|
226
254
|
register: (data) => api.post(`${endpoint}/register`, data),
|
|
227
|
-
refresh: (refreshToken) => api.post(`${endpoint}/refresh`, { refreshToken }),
|
|
255
|
+
refresh: (refreshToken) => api.post(`${endpoint}/refresh`, refreshToken ? { refreshToken } : {}),
|
|
228
256
|
logout: () => api.post(`${endpoint}/logout`),
|
|
229
257
|
me: () => api.get(`${endpoint}/me`),
|
|
230
258
|
forgotPassword: (email) => api.post(`${endpoint}/forgot-password`, { email }),
|
|
@@ -247,6 +275,7 @@ var createUserResource = (api) => ({
|
|
|
247
275
|
var endpoint3 = "/rooms";
|
|
248
276
|
var createRoomResource = (api) => ({
|
|
249
277
|
list: () => api.get(endpoint3),
|
|
278
|
+
mine: (limit = 20) => api.get(`${endpoint3}/mine`, { params: { limit } }),
|
|
250
279
|
featured: () => api.get(`${endpoint3}/featured`),
|
|
251
280
|
getBySlug: (slug) => api.get(`${endpoint3}/${slug}`),
|
|
252
281
|
create: (data) => api.post(endpoint3, data),
|
|
@@ -284,7 +313,8 @@ var createRoomResource = (api) => ({
|
|
|
284
313
|
vote: (slug, type) => api.post(`${endpoint3}/${slug}/booth/vote`, { type }),
|
|
285
314
|
grabTrack: (slug, playlistId) => api.post(`${endpoint3}/${slug}/booth/grab`, playlistId ? { playlistId } : {}),
|
|
286
315
|
getHistory: (slug, page = 1, limit = 20) => api.get(`${endpoint3}/${slug}/history`, { params: { page, limit } }),
|
|
287
|
-
getAuditLog: (slug, limit = 50, before) => api.get(`${endpoint3}/${slug}/audit`, { params: before ? { limit, before } : { limit } })
|
|
316
|
+
getAuditLog: (slug, limit = 50, before) => api.get(`${endpoint3}/${slug}/audit`, { params: before ? { limit, before } : { limit } }),
|
|
317
|
+
activity: (limit = 12) => api.get(`${endpoint3}/activity`, { params: { limit } })
|
|
288
318
|
});
|
|
289
319
|
|
|
290
320
|
// src/resources/chat.ts
|
|
@@ -298,6 +328,7 @@ var createChatResource = (api) => ({
|
|
|
298
328
|
}
|
|
299
329
|
return api.get(`${endpoint4}/${slug}/chat`, { params });
|
|
300
330
|
},
|
|
331
|
+
editMessage: (slug, messageId, data) => api.patch(`${endpoint4}/${slug}/chat/${messageId}`, data),
|
|
301
332
|
deleteMessage: (slug, messageId) => api.delete(`${endpoint4}/${slug}/chat/${messageId}`)
|
|
302
333
|
});
|
|
303
334
|
|
|
@@ -364,7 +395,17 @@ var createSubscriptionResource = (api) => ({
|
|
|
364
395
|
getStatus: () => api.get(`${endpoint8}/status`),
|
|
365
396
|
createIntent: (plan) => api.post(`${endpoint8}/create-intent`, { plan }),
|
|
366
397
|
cancelIntent: (subscriptionId) => api.post(`${endpoint8}/cancel-intent`, { subscriptionId }),
|
|
367
|
-
createPortal: () => api.post(`${endpoint8}/portal`)
|
|
398
|
+
createPortal: () => api.post(`${endpoint8}/portal`),
|
|
399
|
+
createGiftCheckout: (recipientUsername, quantity, isAnonymous = false) => api.post(`${endpoint8}/create-gift-checkout`, {
|
|
400
|
+
recipientUsername,
|
|
401
|
+
quantity,
|
|
402
|
+
isAnonymous
|
|
403
|
+
}),
|
|
404
|
+
getGiftsHistory: () => api.get(`${endpoint8}/gifts-history`),
|
|
405
|
+
retryGiftAssignment: (purchaseId, recipientUsername) => api.post(`${endpoint8}/retry-gift-assignment`, {
|
|
406
|
+
purchaseId,
|
|
407
|
+
recipientUsername
|
|
408
|
+
})
|
|
368
409
|
});
|
|
369
410
|
|
|
370
411
|
// src/resources/friend.ts
|
|
@@ -424,7 +465,6 @@ var createApiClient = (config) => {
|
|
|
424
465
|
};
|
|
425
466
|
// Annotate the CommonJS export names for ESM import in node:
|
|
426
467
|
0 && (module.exports = {
|
|
427
|
-
Api,
|
|
428
468
|
ApiError,
|
|
429
469
|
Logger,
|
|
430
470
|
createAdminResource,
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,52 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
import
|
|
1
|
+
// src/api/errors.ts
|
|
2
|
+
import { HTTPError, TimeoutError } from "ky";
|
|
3
|
+
var ApiError = class extends Error {
|
|
4
|
+
constructor(message, status, code, response) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = "ApiError";
|
|
7
|
+
this.status = status;
|
|
8
|
+
this.code = code;
|
|
9
|
+
this.response = response;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
var tryParseErrorResponse = async (response) => {
|
|
13
|
+
const contentType = response.headers.get("content-type") ?? "";
|
|
14
|
+
if (contentType.includes("application/json")) {
|
|
15
|
+
return await response.json();
|
|
16
|
+
}
|
|
17
|
+
const text = await response.text();
|
|
18
|
+
return text || void 0;
|
|
19
|
+
};
|
|
20
|
+
var parseApiError = async (error) => {
|
|
21
|
+
if (error instanceof HTTPError) {
|
|
22
|
+
const responseData = await tryParseErrorResponse(error.response);
|
|
23
|
+
const isObjectResponse = typeof responseData === "object" && responseData !== null;
|
|
24
|
+
let message;
|
|
25
|
+
let backendResponse;
|
|
26
|
+
if (isObjectResponse && "error" in responseData && responseData.error) {
|
|
27
|
+
message = responseData.error;
|
|
28
|
+
backendResponse = responseData;
|
|
29
|
+
} else if (isObjectResponse && "message" in responseData && responseData.message) {
|
|
30
|
+
message = responseData.message;
|
|
31
|
+
} else if (typeof responseData === "string" && responseData) {
|
|
32
|
+
message = responseData;
|
|
33
|
+
} else {
|
|
34
|
+
message = error.message;
|
|
35
|
+
}
|
|
36
|
+
return new ApiError(message, error.response.status, `HTTP_${error.response.status}`, backendResponse);
|
|
37
|
+
}
|
|
38
|
+
if (error instanceof TimeoutError) {
|
|
39
|
+
return new ApiError("Request timed out", void 0, "TIMEOUT_ERROR");
|
|
40
|
+
}
|
|
41
|
+
if (error instanceof Error) {
|
|
42
|
+
return new ApiError(error.message, void 0, "NETWORK_ERROR");
|
|
43
|
+
}
|
|
44
|
+
return new ApiError("Unknown request error", void 0, "UNKNOWN_ERROR");
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// src/api/client.ts
|
|
48
|
+
import ky from "ky";
|
|
49
|
+
import { URL, URLSearchParams as URLSearchParams2 } from "url";
|
|
3
50
|
|
|
4
51
|
// src/Logger.ts
|
|
5
52
|
var Logger = class _Logger {
|
|
@@ -49,132 +96,114 @@ var Logger = class _Logger {
|
|
|
49
96
|
}
|
|
50
97
|
};
|
|
51
98
|
|
|
52
|
-
// src/
|
|
53
|
-
var
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
this.status = status;
|
|
58
|
-
this.code = code;
|
|
59
|
-
this.response = response;
|
|
99
|
+
// src/api/client.ts
|
|
100
|
+
var resolveUrl = (baseURL, url) => new URL(url, baseURL).toString();
|
|
101
|
+
var parseQueryParams = (params) => {
|
|
102
|
+
if (!params) {
|
|
103
|
+
return void 0;
|
|
60
104
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
constructor(config) {
|
|
64
|
-
this.config = config;
|
|
65
|
-
this.logger = new Logger("Api");
|
|
66
|
-
if (config.logging === false) {
|
|
67
|
-
this.logger.disable();
|
|
68
|
-
}
|
|
69
|
-
this.axios = axios.create({
|
|
70
|
-
baseURL: config.baseURL,
|
|
71
|
-
timeout: config.timeout ?? 3e4,
|
|
72
|
-
headers: {
|
|
73
|
-
"Content-Type": "application/json",
|
|
74
|
-
...config.headers
|
|
75
|
-
}
|
|
76
|
-
});
|
|
77
|
-
this.setupInterceptors();
|
|
78
|
-
this.logger.success(`initialized (baseURL: ${config.baseURL})`);
|
|
105
|
+
if (params instanceof URLSearchParams2) {
|
|
106
|
+
return params;
|
|
79
107
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
return config;
|
|
85
|
-
},
|
|
86
|
-
(error) => {
|
|
87
|
-
this.logger.error("Request error", error);
|
|
88
|
-
return Promise.reject(error);
|
|
89
|
-
}
|
|
90
|
-
);
|
|
91
|
-
this.axios.interceptors.response.use(
|
|
92
|
-
(response) => {
|
|
93
|
-
this.logger.debug(`\u2190 ${response.status} ${response.config.url}`);
|
|
94
|
-
return response;
|
|
95
|
-
},
|
|
96
|
-
(error) => {
|
|
97
|
-
const apiError = this.parseError(error);
|
|
98
|
-
const status = apiError.status ?? apiError.code ?? "ERR";
|
|
99
|
-
this.logger.error(`\u2190 ${status} ${error.config?.url}: ${apiError.message}`);
|
|
100
|
-
return Promise.reject(apiError);
|
|
101
|
-
}
|
|
102
|
-
);
|
|
103
|
-
}
|
|
104
|
-
parseError(error) {
|
|
105
|
-
if (error.response) {
|
|
106
|
-
const responseData = error.response.data;
|
|
107
|
-
let message;
|
|
108
|
-
let backendResponse;
|
|
109
|
-
if (responseData && "error" in responseData && responseData.error) {
|
|
110
|
-
message = responseData.error;
|
|
111
|
-
backendResponse = responseData;
|
|
112
|
-
} else if (responseData && "message" in responseData && responseData.message) {
|
|
113
|
-
message = responseData.message;
|
|
114
|
-
} else {
|
|
115
|
-
message = error.message;
|
|
116
|
-
}
|
|
117
|
-
return new ApiError(message, error.response.status, error.code, backendResponse);
|
|
118
|
-
}
|
|
119
|
-
if (error.request) {
|
|
120
|
-
return new ApiError("No response received from server", void 0, "NETWORK_ERROR");
|
|
108
|
+
const searchParams = new URLSearchParams2();
|
|
109
|
+
for (const [key, value] of Object.entries(params)) {
|
|
110
|
+
if (value === void 0 || value === null) {
|
|
111
|
+
continue;
|
|
121
112
|
}
|
|
122
|
-
|
|
123
|
-
}
|
|
124
|
-
setAuthToken(token) {
|
|
125
|
-
if (token) {
|
|
126
|
-
this.axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
|
127
|
-
this.logger.debug("Auth token set");
|
|
128
|
-
} else {
|
|
129
|
-
delete this.axios.defaults.headers.common["Authorization"];
|
|
130
|
-
this.logger.debug("Auth token cleared");
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
setHeader(key, value) {
|
|
134
|
-
this.axios.defaults.headers.common[key] = value;
|
|
113
|
+
searchParams.set(key, String(value));
|
|
135
114
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
async put(url, data, config) {
|
|
148
|
-
const response = await this.axios.put(url, data, config);
|
|
149
|
-
return this.wrapResponse(response);
|
|
150
|
-
}
|
|
151
|
-
async patch(url, data, config) {
|
|
152
|
-
const response = await this.axios.patch(url, data, config);
|
|
153
|
-
return this.wrapResponse(response);
|
|
154
|
-
}
|
|
155
|
-
async delete(url, config) {
|
|
156
|
-
const response = await this.axios.delete(url, config);
|
|
157
|
-
return this.wrapResponse(response);
|
|
158
|
-
}
|
|
159
|
-
wrapResponse(response) {
|
|
160
|
-
return {
|
|
161
|
-
data: response.data,
|
|
162
|
-
status: response.status,
|
|
163
|
-
headers: response.headers
|
|
164
|
-
};
|
|
115
|
+
return searchParams;
|
|
116
|
+
};
|
|
117
|
+
var wrapResponse = async (response) => {
|
|
118
|
+
const contentType = response.headers.get("content-type") ?? "";
|
|
119
|
+
let data;
|
|
120
|
+
if (response.status === 204) {
|
|
121
|
+
data = void 0;
|
|
122
|
+
} else if (contentType.includes("application/json")) {
|
|
123
|
+
data = await response.json();
|
|
124
|
+
} else {
|
|
125
|
+
data = await response.text();
|
|
165
126
|
}
|
|
166
|
-
|
|
167
|
-
|
|
127
|
+
return {
|
|
128
|
+
data,
|
|
129
|
+
status: response.status,
|
|
130
|
+
headers: Object.fromEntries(response.headers.entries())
|
|
131
|
+
};
|
|
132
|
+
};
|
|
133
|
+
var buildOptions = (defaultHeaders, config) => ({
|
|
134
|
+
headers: {
|
|
135
|
+
...defaultHeaders,
|
|
136
|
+
...config?.headers
|
|
137
|
+
},
|
|
138
|
+
searchParams: parseQueryParams(config?.params),
|
|
139
|
+
timeout: config?.timeout,
|
|
140
|
+
credentials: config?.credentials
|
|
141
|
+
});
|
|
142
|
+
var createApi = (config) => {
|
|
143
|
+
const logger = new Logger("Api");
|
|
144
|
+
if (config.logging === false) {
|
|
145
|
+
logger.disable();
|
|
168
146
|
}
|
|
147
|
+
const defaultHeaders = {
|
|
148
|
+
"Content-Type": "application/json",
|
|
149
|
+
...config.headers
|
|
150
|
+
};
|
|
151
|
+
const client = ky.create({
|
|
152
|
+
timeout: config.timeout ?? 3e4,
|
|
153
|
+
credentials: config.withCredentials ? "include" : "same-origin",
|
|
154
|
+
retry: 0,
|
|
155
|
+
throwHttpErrors: true
|
|
156
|
+
});
|
|
157
|
+
const request = async (method, url, data, requestConfig) => {
|
|
158
|
+
logger.debug(`\u2192 ${method.toUpperCase()} ${url}`);
|
|
159
|
+
try {
|
|
160
|
+
const options = buildOptions(defaultHeaders, requestConfig);
|
|
161
|
+
if (data !== void 0) {
|
|
162
|
+
options.json = data;
|
|
163
|
+
}
|
|
164
|
+
const response = await client(resolveUrl(config.baseURL, url), {
|
|
165
|
+
...options,
|
|
166
|
+
method
|
|
167
|
+
});
|
|
168
|
+
logger.debug(`\u2190 ${response.status} ${url}`);
|
|
169
|
+
return wrapResponse(response);
|
|
170
|
+
} catch (error) {
|
|
171
|
+
const apiError = await parseApiError(error);
|
|
172
|
+
const status = apiError.status ?? apiError.code ?? "ERR";
|
|
173
|
+
logger.error(`\u2190 ${status} ${url}: ${apiError.message}`);
|
|
174
|
+
throw apiError;
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
return {
|
|
178
|
+
setAuthToken: (token) => {
|
|
179
|
+
if (token) {
|
|
180
|
+
defaultHeaders["Authorization"] = `Bearer ${token}`;
|
|
181
|
+
logger.debug("Auth token set");
|
|
182
|
+
} else {
|
|
183
|
+
delete defaultHeaders["Authorization"];
|
|
184
|
+
logger.debug("Auth token cleared");
|
|
185
|
+
}
|
|
186
|
+
},
|
|
187
|
+
setHeader: (key, value) => {
|
|
188
|
+
defaultHeaders[key] = value;
|
|
189
|
+
},
|
|
190
|
+
removeHeader: (key) => {
|
|
191
|
+
delete defaultHeaders[key];
|
|
192
|
+
},
|
|
193
|
+
get: (url, requestConfig) => request("get", url, void 0, requestConfig),
|
|
194
|
+
post: (url, data, requestConfig) => request("post", url, data, requestConfig),
|
|
195
|
+
put: (url, data, requestConfig) => request("put", url, data, requestConfig),
|
|
196
|
+
patch: (url, data, requestConfig) => request("patch", url, data, requestConfig),
|
|
197
|
+
delete: (url, requestConfig) => request("delete", url, void 0, requestConfig)
|
|
198
|
+
};
|
|
169
199
|
};
|
|
170
|
-
var createApi = (config) => new Api(config);
|
|
171
200
|
|
|
172
201
|
// src/resources/auth.ts
|
|
173
202
|
var endpoint = "/auth";
|
|
174
203
|
var createAuthResource = (api) => ({
|
|
175
204
|
login: (credentials) => api.post(`${endpoint}/login`, credentials),
|
|
176
205
|
register: (data) => api.post(`${endpoint}/register`, data),
|
|
177
|
-
refresh: (refreshToken) => api.post(`${endpoint}/refresh`, { refreshToken }),
|
|
206
|
+
refresh: (refreshToken) => api.post(`${endpoint}/refresh`, refreshToken ? { refreshToken } : {}),
|
|
178
207
|
logout: () => api.post(`${endpoint}/logout`),
|
|
179
208
|
me: () => api.get(`${endpoint}/me`),
|
|
180
209
|
forgotPassword: (email) => api.post(`${endpoint}/forgot-password`, { email }),
|
|
@@ -197,6 +226,7 @@ var createUserResource = (api) => ({
|
|
|
197
226
|
var endpoint3 = "/rooms";
|
|
198
227
|
var createRoomResource = (api) => ({
|
|
199
228
|
list: () => api.get(endpoint3),
|
|
229
|
+
mine: (limit = 20) => api.get(`${endpoint3}/mine`, { params: { limit } }),
|
|
200
230
|
featured: () => api.get(`${endpoint3}/featured`),
|
|
201
231
|
getBySlug: (slug) => api.get(`${endpoint3}/${slug}`),
|
|
202
232
|
create: (data) => api.post(endpoint3, data),
|
|
@@ -234,7 +264,8 @@ var createRoomResource = (api) => ({
|
|
|
234
264
|
vote: (slug, type) => api.post(`${endpoint3}/${slug}/booth/vote`, { type }),
|
|
235
265
|
grabTrack: (slug, playlistId) => api.post(`${endpoint3}/${slug}/booth/grab`, playlistId ? { playlistId } : {}),
|
|
236
266
|
getHistory: (slug, page = 1, limit = 20) => api.get(`${endpoint3}/${slug}/history`, { params: { page, limit } }),
|
|
237
|
-
getAuditLog: (slug, limit = 50, before) => api.get(`${endpoint3}/${slug}/audit`, { params: before ? { limit, before } : { limit } })
|
|
267
|
+
getAuditLog: (slug, limit = 50, before) => api.get(`${endpoint3}/${slug}/audit`, { params: before ? { limit, before } : { limit } }),
|
|
268
|
+
activity: (limit = 12) => api.get(`${endpoint3}/activity`, { params: { limit } })
|
|
238
269
|
});
|
|
239
270
|
|
|
240
271
|
// src/resources/chat.ts
|
|
@@ -248,6 +279,7 @@ var createChatResource = (api) => ({
|
|
|
248
279
|
}
|
|
249
280
|
return api.get(`${endpoint4}/${slug}/chat`, { params });
|
|
250
281
|
},
|
|
282
|
+
editMessage: (slug, messageId, data) => api.patch(`${endpoint4}/${slug}/chat/${messageId}`, data),
|
|
251
283
|
deleteMessage: (slug, messageId) => api.delete(`${endpoint4}/${slug}/chat/${messageId}`)
|
|
252
284
|
});
|
|
253
285
|
|
|
@@ -314,7 +346,17 @@ var createSubscriptionResource = (api) => ({
|
|
|
314
346
|
getStatus: () => api.get(`${endpoint8}/status`),
|
|
315
347
|
createIntent: (plan) => api.post(`${endpoint8}/create-intent`, { plan }),
|
|
316
348
|
cancelIntent: (subscriptionId) => api.post(`${endpoint8}/cancel-intent`, { subscriptionId }),
|
|
317
|
-
createPortal: () => api.post(`${endpoint8}/portal`)
|
|
349
|
+
createPortal: () => api.post(`${endpoint8}/portal`),
|
|
350
|
+
createGiftCheckout: (recipientUsername, quantity, isAnonymous = false) => api.post(`${endpoint8}/create-gift-checkout`, {
|
|
351
|
+
recipientUsername,
|
|
352
|
+
quantity,
|
|
353
|
+
isAnonymous
|
|
354
|
+
}),
|
|
355
|
+
getGiftsHistory: () => api.get(`${endpoint8}/gifts-history`),
|
|
356
|
+
retryGiftAssignment: (purchaseId, recipientUsername) => api.post(`${endpoint8}/retry-gift-assignment`, {
|
|
357
|
+
purchaseId,
|
|
358
|
+
recipientUsername
|
|
359
|
+
})
|
|
318
360
|
});
|
|
319
361
|
|
|
320
362
|
// src/resources/friend.ts
|
|
@@ -373,7 +415,6 @@ var createApiClient = (config) => {
|
|
|
373
415
|
};
|
|
374
416
|
};
|
|
375
417
|
export {
|
|
376
|
-
Api,
|
|
377
418
|
ApiError,
|
|
378
419
|
Logger,
|
|
379
420
|
createAdminResource,
|