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
34 lines (30 loc) · 977 Bytes
/
Copy pathapi.js
File metadata and controls
34 lines (30 loc) · 977 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
32
33
34
import axios from 'axios';
export function createApiClient(baseURL = '/') {
return axios.create({
baseURL,
headers: { 'Content-Type': 'application/json' },
validateStatus: () => true,
});
}
export function extractResponseInterceptor(apiClient, index = 0) {
const interceptor = apiClient.interceptors.response.handlers[index];
if (!interceptor) {
throw new Error(`No response interceptor found at index ${index}`);
}
return interceptor;
}
export function buildAxiosError(status, data = {}) {
const error = new Error(`Request failed with status ${status}`);
error.response = { status, data };
return error;
}
export function buildFetchRequest(body = {}, init = {}) {
return {
json: async () => body,
text: async () => JSON.stringify(body),
status: init.status || 200,
ok: init.status ? init.status >= 200 && init.status < 300 : true,
headers: init.headers || { 'Content-Type': 'application/json' },
...init,
};
}