-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom-fetch.js
73 lines (71 loc) · 1.91 KB
/
custom-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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import '@ungap/global-this';
export default function customFetch(
endpoint,
{
body,
...customConfig
} = {},
{
fetch = globalThis.fetch,
AbortController = globalThis.AbortController,
responseType = 'text',
timeout
} = {},
) {
let timeoutSignal = undefined;
if (timeout) {
timeoutSignal = createTimeoutSignal(AbortController, timeout);
}
const config = {
method: body ? 'POST' : 'GET',
...customConfig
}
// from here: https://github.com/developit/redaxios/blob/ab73a298ba9849c59d670230a9d24fd7b329fb4d/src/index.js#L171
if (body && typeof body === 'object' && typeof body.append !== 'function') {
config.body = JSON.stringify(body);
config.headers = {
...config.headers,
'content-type': 'application/json'
};
} else if (body) {
config.body = body;
}
if (timeoutSignal) {
config.signal = timeoutSignal.signal;
}
return fetch(endpoint, config).then(async (response) => {
if (timeoutSignal) {
timeoutSignal.clear();
}
if (!response.ok) {
const errorMessage = await response.text();
return Promise.reject(new Error(errorMessage));
}
const result = {};
// see https://github.com/developit/redaxios/blob/ab73a298ba9849c59d670230a9d24fd7b329fb4d/src/index.js#L204-L213
for (const i in response) {
if (typeof response[i] !== 'function') result[i] = response[i];
}
const data = await response[responseType]();
result.data = data;
try {
// just in case we can parse the result
result.data = JSON.parse(data);
} catch(error) {
// ignore
}
return result;
});
}
function createTimeoutSignal(AbortController, timeout) {
const controller = new AbortController();
const timeoutId = globalThis.setTimeout(() => {
controller.abort();
}, timeout);
return {
signal: controller.signal,
clear: () => {
globalThis.clearTimeout(timeoutId);
}
};
}