forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
31 lines (27 loc) · 862 Bytes
/
Copy pathapi.js
File metadata and controls
31 lines (27 loc) · 862 Bytes
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
function createApiClient(baseURL = '/') {
return {
baseURL,
headers: { 'Content-Type': 'application/json' },
validateStatus: (status) => status >= 200 && status < 300,
};
}
function extractResponseInterceptor(apiClient, index = 0) {
if (!apiClient || !apiClient.interceptors || !apiClient.interceptors.response) {
throw new Error('API client does not expose interceptors.response');
}
const interceptor = apiClient.interceptors.response.handlers[index];
if (!interceptor) {
throw new Error(`No response interceptor found at index ${index}`);
}
return interceptor;
}
function buildHttpError(status, data = {}) {
const error = new Error(`Request failed with status ${status}`);
error.response = { status, data };
return error;
}
module.exports = {
createApiClient,
extractResponseInterceptor,
buildHttpError,
};