-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchEvents.js
62 lines (62 loc) · 1.75 KB
/
fetchEvents.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
const proxiedFetch = window.fetch;
window.fetch = function(url, init) {
window.dispatchEvent(
new CustomEvent(
"fetchStart",
{
detail: {
url: url,
init: init
}
}
)
);
const promise=proxiedFetch.apply(this, arguments);
promise.then(response => {
if(response.ok) {
window.dispatchEvent(
new CustomEvent("fetchResponseSuccess", {
detail: {
url: url,
init: init,
response: response
}
}));
} else if(response.status >= 400 && response.status < 600) {
window.dispatchEvent(
new CustomEvent("fetchResponseError", {
detail: {
url: url,
init: init,
response: response
}
}));
}
window.dispatchEvent(
new CustomEvent("fetchComplete", {
detail: {
url: url,
init: init,
response: response
}
}));
}, error => {
window.dispatchEvent(
new CustomEvent("fetchError", {
detail: {
url: url,
init: init,
error: error
}
}));
window.dispatchEvent(
new CustomEvent("fetchComplete", {
detail: {
url: url,
init: init,
error: error
}
}));
});
return promise;
};