forked from jonbern/fetch-retry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
151 lines (135 loc) · 4.16 KB
/
index.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"use strict";
require("es6-promise").polyfill();
module.exports = function(fetch, defaults = {}) {
if (typeof fetch !== "function") {
throw new ArgumentError("fetch must be a function");
}
if (typeof defaults !== "object") {
throw new ArgumentError("defaults must be an object");
}
if (defaults.retries !== undefined && !isPositiveInteger(defaults.retries)) {
throw new ArgumentError("retries must be a positive integer");
}
if (
defaults.retryDelay !== undefined &&
!isPositiveInteger(defaults.retryDelay) &&
typeof defaults.retryDelay !== "function"
) {
throw new ArgumentError(
"retryDelay must be a positive integer or a function returning a positive integer"
);
}
if (
defaults.retryOn !== undefined &&
!Array.isArray(defaults.retryOn) &&
typeof defaults.retryOn !== "function"
) {
throw new ArgumentError("retryOn property expects an array or function");
}
const baseDefaults = {
retries: 3,
retryDelay: 1000,
retryOn: []
};
defaults = Object.assign(baseDefaults, defaults);
return function fetchRetry(input, init) {
var retries = defaults.retries;
var retryDelay = defaults.retryDelay;
var retryOn = defaults.retryOn;
if (init && init.retries !== undefined) {
if (isPositiveInteger(init.retries)) {
retries = init.retries;
} else {
throw new ArgumentError("retries must be a positive integer");
}
}
if (init && init.retryDelay !== undefined) {
if (
isPositiveInteger(init.retryDelay) ||
typeof init.retryDelay === "function"
) {
retryDelay = init.retryDelay;
} else {
throw new ArgumentError(
"retryDelay must be a positive integer or a function returning a positive integer"
);
}
}
if (init && init.retryOn) {
if (Array.isArray(init.retryOn) || typeof init.retryOn === "function") {
retryOn = init.retryOn;
} else {
throw new ArgumentError(
"retryOn property expects an array or function"
);
}
}
return new Promise(function(resolve, reject) {
const wrappedFetch = function(attempt) {
fetch(input, init)
.then(function(response) {
if (
Array.isArray(retryOn) &&
retryOn.indexOf(response.status) === -1
) {
resolve(response);
} else if (typeof retryOn === "function") {
const retryOnRes = retryOn(attempt, null, response);
if (typeof retryOnRes === "boolean" && retryOnRes) {
retry(attempt, null, response);
} else if (
typeof retryOnRes === "object" &&
typeof retryOnRes.then === "function"
) {
retryOnRes.then(res => {
if (res) {
retry(attempt, null, response);
} else {
resolve(response);
}
});
} else {
resolve(response);
}
} else {
if (attempt < retries) {
retry(attempt, null, response);
} else {
resolve(response);
}
}
})
.catch(function(error) {
if (typeof retryOn === "function") {
if (retryOn(attempt, error, null)) {
retry(attempt, error, null);
} else {
reject(error);
}
} else if (attempt < retries) {
retry(attempt, error, null);
} else {
reject(error);
}
});
};
function retry(attempt, error, response) {
const delay =
typeof retryDelay === "function"
? retryDelay(attempt, error, response)
: retryDelay;
setTimeout(function() {
wrappedFetch(++attempt);
}, delay);
}
wrappedFetch(0);
});
};
};
function isPositiveInteger(value) {
return Number.isInteger(value) && value >= 0;
}
function ArgumentError(message) {
this.name = "ArgumentError";
this.message = message;
}