7
7
* MIT Licensed
8
8
*
9
9
*/
10
+ var Promise = require ( 'bluebird' ) ;
10
11
var request = require ( 'request' ) ;
11
12
var _ = require ( 'fg-lodash' ) ;
12
13
var RetryStrategies = require ( './strategies' ) ;
@@ -15,11 +16,13 @@ var RetryStrategies = require('./strategies');
15
16
var DEFAULTS = {
16
17
maxAttempts : 5 , // try 5 times
17
18
retryDelay : 5000 , // wait for 5s before trying again
19
+ fullResponse : true , // resolve promise with the full response object
18
20
} ;
19
21
20
- function Request ( options , f , maxAttempts , retryDelay ) {
21
- this . maxAttempts = maxAttempts ;
22
- this . retryDelay = retryDelay ;
22
+ function Request ( options , f , retryConfig ) {
23
+ this . maxAttempts = retryConfig . maxAttempts ;
24
+ this . retryDelay = retryConfig . retryDelay ;
25
+ this . fullResponse = retryConfig . fullResponse ;
23
26
this . attempts = 0 ;
24
27
25
28
/**
@@ -34,9 +37,32 @@ function Request(options, f, maxAttempts, retryDelay) {
34
37
*/
35
38
this . retryStrategy = _ . isFunction ( options . retryStrategy ) ? options . retryStrategy : RetryStrategies . HTTPOrNetworkError ;
36
39
37
- this . f = _ . once ( f ) ;
38
40
this . _timeout = null ;
39
41
this . _req = null ;
42
+
43
+ this . _callback = _ . isFunction ( f ) ? _ . once ( f ) : null ;
44
+
45
+ // create the promise only when no callback was provided
46
+ if ( ! this . _callback ) {
47
+ this . _promise = new Promise ( function ( resolve , reject ) {
48
+ this . _resolve = resolve ;
49
+ this . _reject = reject ;
50
+ } . bind ( this ) ) ;
51
+ }
52
+
53
+ this . reply = function requestRetryReply ( err , response , body ) {
54
+ if ( this . _callback ) {
55
+ return this . _callback ( err , response , body ) ;
56
+ }
57
+
58
+ if ( err ) {
59
+ return this . _reject ( err ) ;
60
+ }
61
+
62
+ // resolve with the full response or just the body
63
+ response = this . fullResponse ? response : body ;
64
+ this . _resolve ( response ) ;
65
+ } ;
40
66
}
41
67
42
68
Request . request = request ;
@@ -54,7 +80,7 @@ Request.prototype._tryUntilFail = function () {
54
80
return ;
55
81
}
56
82
57
- return this . f ( err , response , body ) ;
83
+ this . reply ( err , response , body ) ;
58
84
} . bind ( this ) ) ;
59
85
} ;
60
86
@@ -63,24 +89,29 @@ Request.prototype.abort = function () {
63
89
this . _req . abort ( ) ;
64
90
}
65
91
clearTimeout ( this . _timeout ) ;
66
- this . f ( new Error ( 'Aborted' ) ) ;
92
+ this . reply ( new Error ( 'Aborted' ) ) ;
67
93
} ;
68
94
69
95
// expose request methods from RequestRetry
70
- [ 'end' , 'on' , 'emit' , 'once' , 'setMaxListeners' , 'start' , 'removeListener' , 'pipe' , 'write' ] . forEach ( function ( methodName ) {
71
- Request . prototype [ methodName ] = makeGateway ( methodName ) ;
96
+ [ 'end' , 'on' , 'emit' , 'once' , 'setMaxListeners' , 'start' , 'removeListener' , 'pipe' , 'write' ] . forEach ( function ( requestMethod ) {
97
+ Request . prototype [ requestMethod ] = function exposedRequestMethod ( ) {
98
+ return this . _req [ requestMethod ] . apply ( this . _req , arguments ) ;
99
+ } ;
72
100
} ) ;
73
101
74
- function makeGateway ( methodName ) {
75
- return function ( ) {
76
- return this . _req [ methodName ] . apply ( this . _req , Array . prototype . slice . call ( arguments ) ) ;
102
+ // expose promise methods
103
+ [ 'then' , 'catch' , 'finally' ] . forEach ( function ( promiseMethod ) {
104
+ Request . prototype [ promiseMethod ] = function exposedPromiseMethod ( ) {
105
+ if ( this . _callback ) {
106
+ throw new Error ( 'A callback was provided but waiting a promise, use only one pattern' ) ;
107
+ }
108
+ return this . _promise [ promiseMethod ] . apply ( this . _promise , arguments ) ;
77
109
} ;
78
- }
110
+ } ) ;
79
111
80
112
function Factory ( options , f ) {
81
- f = _ . isFunction ( f ) ? f : _ . noop ;
82
- var retry = _ ( options || { } ) . defaults ( DEFAULTS ) . pick ( Object . keys ( DEFAULTS ) ) . value ( ) ;
83
- var req = new Request ( options , f , retry . maxAttempts , retry . retryDelay ) ;
113
+ var retryConfig = _ ( options || { } ) . defaults ( DEFAULTS ) . pick ( Object . keys ( DEFAULTS ) ) . value ( ) ;
114
+ var req = new Request ( options , f , retryConfig ) ;
84
115
req . _tryUntilFail ( ) ;
85
116
return req ;
86
117
}
0 commit comments