1
- # Request-retry [ ![ Deps] ( https://david-dm.org/FGRibreau/node-request-retry.png )] ( https://david-dm.org/FGRibreau/node-request-retry )
1
+ # Request-retry [ ![ Deps] ( https://david-dm.org/FGRibreau/node-request-retry.png )] ( https://david-dm.org/FGRibreau/node-request-retry ) [ ![ Build Status ] ( https://drone.io/github.com/FGRibreau/node-request-retry/status.png )] ( https://drone.io/github.com/FGRibreau/node-request-retry/latest )
2
2
3
3
[ ![ npm] ( https://nodei.co/npm/requestretry.png )] ( https://npmjs.org/package/requestretry )
4
4
@@ -18,6 +18,7 @@ request({
18
18
// The above parameters are specific to Request-retry:
19
19
maxAttempts: 5 , // (default) try 5 times
20
20
retryDelay: 5000 // (default) wait for 5s before trying again
21
+ retryStrategy: request .RetryStrategies .HTTPOrNetworkError // (default) retry on 5xx or network errors
21
22
}, function (err , response , body ){
22
23
// this callback will only be called when the request succeeded or after maxAttempts or on error
23
24
});
@@ -29,14 +30,42 @@ Install with [npm](https://npmjs.org/package/requestretry).
29
30
30
31
npm install --save requestretry
31
32
33
+ ## Define your own retry strategy
34
+
35
+ ```
36
+ /**
37
+ * @param {Null | Object} err
38
+ * @param {Object} response
39
+ * @return {Boolean} true if the request should be retried
40
+ */
41
+ function myRetryStrategy(err, response){
42
+ // retry the request if we had an error or if the response was a 'Bad Gateway'
43
+ return err || response.statusCode === 502;
44
+ }
45
+
46
+ request({
47
+ url: 'https://api.domain.com/v1/a/b'
48
+ json:true,
49
+ maxAttempts: 5,
50
+ retryDelay: 5000
51
+ retryStrategy: myRetryStrategy
52
+ }, function(err, response, body){
53
+ // this callback will only be called when the request succeeded or after maxAttempts or on error
54
+ });
55
+ ```
56
+
32
57
## Todos
33
58
34
59
- Tests
35
60
- Use an EventEmitter to notify retries
36
- - Allow the end-user to specify its own conditions to trigger a retry
37
61
38
62
## Changelog
39
63
64
+ v1.2.0
65
+
66
+ - support for user-defined retry strategies
67
+ - added ` request.RetryStrategies.HTTPError ` , ` request.RetryStrategies.NetworkError ` and ` request.RetryStrategies.HTTPOrNetworkError `
68
+
40
69
v1.1.0
41
70
42
71
- support for 'end', 'on', 'emit', 'once', 'setMaxListeners', 'start', 'removeListener', 'pipe' request methods by @juliendangers
0 commit comments