-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch.js
49 lines (45 loc) · 1.26 KB
/
fetch.js
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
async function handledFetch(path, options) {
const res = await fetch(path, options);
const contentType = res.headers.get('content-type') || '';
let content;
if (contentType.startsWith('application/json')) {
content = await res.json();
} else {
content = res.text();
}
if (res.status >= 400) {
const err = new Error('Bad response from server');
err.status = res.status;
err.content = content;
throw err;
}
return content;
}
function apiFetch(apiURL, path, options = {}) {
let qs = '';
const isFormData = options.body instanceof FormData;
if (typeof options.body === 'object' && !isFormData) {
options.body = JSON.stringify(options.body);
}
if (options.query) {
const searchParams = new URLSearchParams();
Object.keys(options.query).forEach((key) => {
if (typeof options.query[key] !== 'undefined') {
searchParams.append(key, options.query[key]);
}
});
qs = `?${searchParams.toString()}`;
}
Object.assign(options, { credentials: 'include' });
if (!isFormData) {
options.headers = {
'Content-Type': 'application/json',
...options.headers,
};
}
return handledFetch(`${options.api_url || apiURL}${path}${qs}`, options);
}
module.exports = {
handledFetch,
apiFetch
};