forked from jonbern/fetch-retry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
88 lines (79 loc) · 2.47 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
'use strict';
require('isomorphic-fetch');
require('es6-promise').polyfill();
module.exports = function(url, options) {
var retries = 3;
var retryDelay = 1000;
var retryOn = [];
if (options && options.retries !== undefined) {
if (isPositiveInteger(options.retries)) {
retries = options.retries;
} else {
throw new ArgumentError('retries must be a positive integer');
}
}
if (options && options.retryDelay !== undefined) {
if (isPositiveInteger(options.retryDelay) || (typeof options.retryDelay === 'function')) {
retryDelay = options.retryDelay;
} else {
throw new ArgumentError('retryDelay must be a positive integer or a function returning a positive integer');
}
}
if (options && options.retryOn) {
if (Array.isArray(options.retryOn) || (typeof options.retryOn === 'function')) {
retryOn = options.retryOn;
} else {
throw new ArgumentError('retryOn property expects an array or function');
}
}
return new Promise(function(resolve, reject) {
var wrappedFetch = function(attempt) {
fetch(url, options)
.then(function(response) {
if (Array.isArray(retryOn) && retryOn.indexOf(response.status) === -1) {
resolve(response);
} else if (typeof retryOn === 'function') {
if (retryOn(attempt, null, response)) {
retry(attempt, null, 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) {
var 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;
}