@@ -14,15 +14,15 @@ var RetryStrategies = require('./strategies');
14
14
var _ = require ( 'lodash' ) ;
15
15
16
16
var DEFAULTS = {
17
- maxAttempts : 5 , // try 5 times
18
- retryDelay : 5000 , // wait for 5s before trying again
19
- fullResponse : true , // resolve promise with the full response object
20
- promiseFactory : defaultPromiseFactory // Function to use a different promise implementation library
17
+ maxAttempts : 5 , // try 5 times
18
+ retryDelay : 5000 , // wait for 5s before trying again
19
+ fullResponse : true , // resolve promise with the full response object
20
+ promiseFactory : defaultPromiseFactory // Function to use a different promise implementation library
21
21
} ;
22
22
23
23
// Default promise factory which use bluebird
24
24
function defaultPromiseFactory ( resolver ) {
25
- return when . promise ( resolver ) ;
25
+ return when . promise ( resolver ) ;
26
26
}
27
27
28
28
/**
@@ -34,193 +34,193 @@ function defaultPromiseFactory(resolver) {
34
34
*/
35
35
function makePromise ( requestInstance , promiseFactoryFn ) {
36
36
37
- // Resolver function wich assigns the promise (resolve, reject) functions
38
- // to the requestInstance
39
- function Resolver ( resolve , reject ) {
40
- this . _resolve = resolve ;
41
- this . _reject = reject ;
42
- }
37
+ // Resolver function wich assigns the promise (resolve, reject) functions
38
+ // to the requestInstance
39
+ function Resolver ( resolve , reject ) {
40
+ this . _resolve = resolve ;
41
+ this . _reject = reject ;
42
+ }
43
43
44
- return promiseFactoryFn ( Resolver . bind ( requestInstance ) ) ;
44
+ return promiseFactoryFn ( Resolver . bind ( requestInstance ) ) ;
45
45
}
46
46
47
47
function Request ( url , options , f , retryConfig ) {
48
- // ('url')
49
- if ( _ . isString ( url ) ) {
50
- // ('url', f)
51
- if ( _ . isFunction ( options ) ) {
52
- f = options ;
53
- }
54
-
55
- if ( ! _ . isObject ( options ) ) {
56
- options = { } ;
57
- }
58
-
59
- // ('url', {object})
60
- options . url = url ;
48
+ // ('url')
49
+ if ( _ . isString ( url ) ) {
50
+ // ('url', f)
51
+ if ( _ . isFunction ( options ) ) {
52
+ f = options ;
61
53
}
62
54
63
- if ( _ . isObject ( url ) ) {
64
- if ( _ . isFunction ( options ) ) {
65
- f = options ;
66
- }
67
- options = url ;
55
+ if ( ! _ . isObject ( options ) ) {
56
+ options = { } ;
68
57
}
69
58
70
- this . maxAttempts = retryConfig . maxAttempts ;
71
- this . retryDelay = retryConfig . retryDelay ;
72
- this . fullResponse = retryConfig . fullResponse ;
73
- this . attempts = 0 ;
74
-
75
- /**
76
- * Option object
77
- * @type {Object }
78
- */
79
- this . options = options ;
80
-
81
- /**
82
- * Return true if the request should be retried
83
- * @type {Function } (err, response) -> Boolean
84
- */
85
- this . retryStrategy = _ . isFunction ( options . retryStrategy ) ? options . retryStrategy : RetryStrategies . HTTPOrNetworkError ;
86
-
87
- /**
88
- * Return a number representing how long request-retry should wait before trying again the request
89
- * @type {Boolean } (err, response, body) -> Number
90
- */
91
- this . delayStrategy = _ . isFunction ( options . delayStrategy ) ? options . delayStrategy : function ( ) {
92
- return this . retryDelay ;
93
- } ;
94
-
95
- this . _timeout = null ;
96
- this . _req = null ;
97
-
98
- this . _callback = _ . isFunction ( f ) ? _ . once ( f ) : null ;
99
-
100
- // create the promise only when no callback was provided
101
- if ( ! this . _callback ) {
102
- this . _promise = makePromise ( this , retryConfig . promiseFactory ) ;
103
- }
59
+ // ('url', {object})
60
+ options . url = url ;
61
+ }
104
62
105
- this . reply = function requestRetryReply ( err , response , body ) {
106
- if ( this . _callback ) {
107
- return this . _callback ( err , response , body ) ;
108
- }
63
+ if ( _ . isObject ( url ) ) {
64
+ if ( _ . isFunction ( options ) ) {
65
+ f = options ;
66
+ }
67
+ options = url ;
68
+ }
69
+
70
+ this . maxAttempts = retryConfig . maxAttempts ;
71
+ this . retryDelay = retryConfig . retryDelay ;
72
+ this . fullResponse = retryConfig . fullResponse ;
73
+ this . attempts = 0 ;
74
+
75
+ /**
76
+ * Option object
77
+ * @type {Object }
78
+ */
79
+ this . options = options ;
80
+
81
+ /**
82
+ * Return true if the request should be retried
83
+ * @type {Function } (err, response) -> Boolean
84
+ */
85
+ this . retryStrategy = _ . isFunction ( options . retryStrategy ) ? options . retryStrategy : RetryStrategies . HTTPOrNetworkError ;
86
+
87
+ /**
88
+ * Return a number representing how long request-retry should wait before trying again the request
89
+ * @type {Boolean } (err, response, body) -> Number
90
+ */
91
+ this . delayStrategy = _ . isFunction ( options . delayStrategy ) ? options . delayStrategy : function ( ) { return this . retryDelay ; } ;
92
+
93
+ this . _timeout = null ;
94
+ this . _req = null ;
95
+
96
+ this . _callback = _ . isFunction ( f ) ? _ . once ( f ) : null ;
97
+
98
+ // create the promise only when no callback was provided
99
+ if ( ! this . _callback ) {
100
+ this . _promise = makePromise ( this , retryConfig . promiseFactory ) ;
101
+ }
102
+
103
+ this . reply = function requestRetryReply ( err , response , body ) {
104
+ if ( this . _callback ) {
105
+ return this . _callback ( err , response , body ) ;
106
+ }
109
107
110
- if ( err ) {
111
- return this . _reject ( err ) ;
112
- }
108
+ if ( err ) {
109
+ return this . _reject ( err ) ;
110
+ }
113
111
114
- // resolve with the full response or just the body
115
- response = this . fullResponse ? response : body ;
116
- this . _resolve ( response ) ;
117
- } ;
112
+ // resolve with the full response or just the body
113
+ response = this . fullResponse ? response : body ;
114
+ this . _resolve ( response ) ;
115
+ } ;
118
116
}
119
117
120
118
Request . request = request ;
121
119
122
120
Request . prototype . _tryUntilFail = function ( ) {
123
- this . maxAttempts -- ;
124
- this . attempts ++ ;
125
-
126
- this . _req = Request . request ( this . options , function ( err , response , body ) {
127
- if ( response ) {
128
- response . attempts = this . attempts ;
129
- }
130
- if ( this . retryStrategy ( err , response , body ) && this . maxAttempts > 0 ) {
131
- this . _timeout = setTimeout ( this . _tryUntilFail . bind ( this ) , this . delayStrategy . call ( this , err , response , body ) ) ;
132
- return ;
133
- }
134
-
135
- this . reply ( err , response , body ) ;
136
- } . bind ( this ) ) ;
121
+ this . maxAttempts -- ;
122
+ this . attempts ++ ;
123
+
124
+ this . _req = Request . request ( this . options , function ( err , response , body ) {
125
+ if ( response ) {
126
+ response . attempts = this . attempts ;
127
+ }
128
+ if ( this . retryStrategy ( err , response , body ) && this . maxAttempts > 0 ) {
129
+ this . _timeout = setTimeout ( this . _tryUntilFail . bind ( this ) , this . delayStrategy . call ( this , err , response , body ) ) ;
130
+ return ;
131
+ }
132
+
133
+ this . reply ( err , response , body ) ;
134
+ } . bind ( this ) ) ;
137
135
} ;
138
136
139
137
Request . prototype . abort = function ( ) {
140
- if ( this . _req ) {
141
- this . _req . abort ( ) ;
142
- }
143
- clearTimeout ( this . _timeout ) ;
144
- this . reply ( new Error ( 'Aborted' ) ) ;
138
+ if ( this . _req ) {
139
+ this . _req . abort ( ) ;
140
+ }
141
+ clearTimeout ( this . _timeout ) ;
142
+ this . reply ( new Error ( 'Aborted' ) ) ;
145
143
} ;
146
144
147
145
// expose request methods from RequestRetry
148
- [ 'end' , 'on' , 'emit' , 'once' , 'setMaxListeners' , 'start' , 'removeListener' , 'pipe' , 'write' , 'auth' , ] . forEach ( function ( requestMethod ) {
149
- Request . prototype [ requestMethod ] = function exposedRequestMethod ( ) {
150
- return this . _req [ requestMethod ] . apply ( this . _req , arguments ) ;
151
- } ;
146
+ [ 'end' , 'on' , 'emit' , 'once' , 'setMaxListeners' , 'start' , 'removeListener' , 'pipe' , 'write' , 'auth' ] . forEach ( function ( requestMethod ) {
147
+ Request . prototype [ requestMethod ] = function exposedRequestMethod ( ) {
148
+ return this . _req [ requestMethod ] . apply ( this . _req , arguments ) ;
149
+ } ;
152
150
} ) ;
153
151
154
152
// expose promise methods
155
153
[ 'then' , 'catch' , 'finally' , 'fail' , 'done' ] . forEach ( function ( promiseMethod ) {
156
- Request . prototype [ promiseMethod ] = function exposedPromiseMethod ( ) {
157
- if ( this . _callback ) {
158
- throw new Error ( 'A callback was provided but waiting a promise, use only one pattern' ) ;
159
- }
160
- return this . _promise [ promiseMethod ] . apply ( this . _promise , arguments ) ;
161
- } ;
154
+ Request . prototype [ promiseMethod ] = function exposedPromiseMethod ( ) {
155
+ if ( this . _callback ) {
156
+ throw new Error ( 'A callback was provided but waiting a promise, use only one pattern' ) ;
157
+ }
158
+ return this . _promise [ promiseMethod ] . apply ( this . _promise , arguments ) ;
159
+ } ;
162
160
} ) ;
163
161
164
162
function Factory ( url , options , f ) {
165
- var retryConfig = _ . chain ( _ . isObject ( url ) ? url : options || { } ) . defaults ( DEFAULTS ) . pick ( Object . keys ( DEFAULTS ) ) . value ( ) ;
166
- var req = new Request ( url , options , f , retryConfig ) ;
167
- req . _tryUntilFail ( ) ;
168
- return req ;
163
+ var retryConfig = _ . chain ( _ . isObject ( url ) ? url : options || { } ) . defaults ( DEFAULTS ) . pick ( Object . keys ( DEFAULTS ) ) . value ( ) ;
164
+ var req = new Request ( url , options , f , retryConfig ) ;
165
+ req . _tryUntilFail ( ) ;
166
+ return req ;
169
167
}
170
168
171
169
// adds a helper for HTTP method `verb` to object `obj`
172
170
function makeHelper ( obj , verb ) {
173
- obj [ verb ] = function helper ( url , options , f ) {
174
- // ('url')
175
- if ( _ . isString ( url ) ) {
176
- // ('url', f)
177
- if ( _ . isFunction ( options ) ) {
178
- f = options ;
179
- }
180
-
181
- if ( ! _ . isObject ( options ) ) {
182
- options = { } ;
183
- }
184
-
185
- // ('url', {object})
186
- options . url = url ;
187
- }
188
-
189
- if ( _ . isObject ( url ) ) {
190
- if ( _ . isFunction ( options ) ) {
191
- f = options ;
192
- }
193
- options = url ;
194
- }
195
-
196
- options . method = verb . toUpperCase ( ) ;
197
- return obj ( options , f ) ;
198
- } ;
171
+ obj [ verb ] = function helper ( url , options , f ) {
172
+ // ('url')
173
+ if ( _ . isString ( url ) ) {
174
+ // ('url', f)
175
+ if ( _ . isFunction ( options ) ) {
176
+ f = options ;
177
+ }
178
+
179
+ if ( ! _ . isObject ( options ) ) {
180
+ options = { } ;
181
+ }
182
+
183
+ // ('url', {object})
184
+ options . url = url ;
185
+ }
186
+
187
+ if ( _ . isObject ( url ) ) {
188
+ if ( _ . isFunction ( options ) ) {
189
+ f = options ;
190
+ }
191
+ options = url ;
192
+ }
193
+
194
+ options . method = verb . toUpperCase ( ) ;
195
+ return obj ( options , f ) ;
196
+ } ;
199
197
}
200
198
201
199
function defaults ( defaultOptions , defaultF ) {
202
- var factory = function ( options , f ) {
203
- if ( typeof options === "string" ) {
204
- options = {
205
- uri : options
206
- } ;
207
- }
208
- return Factory . apply ( null , [ extend ( true , { } , defaultOptions , options ) , f || defaultF ] ) ;
209
- } ;
210
-
211
- factory . defaults = function ( newDefaultOptions , newDefaultF ) {
212
- return defaults . apply ( null , [ extend ( true , { } , defaultOptions , newDefaultOptions ) , newDefaultF || defaultF ] ) ;
213
- } ;
214
-
215
- factory . Request = Request ;
216
- factory . RetryStrategies = RetryStrategies ;
200
+ var factory = function ( options , f ) {
201
+ if ( typeof options === "string" ) {
202
+ options = { uri : options } ;
203
+ }
204
+ return Factory . apply ( null , [ extend ( true , { } , defaultOptions , options ) , f || defaultF ] ) ;
205
+ } ;
206
+
207
+ factory . defaults = function ( newDefaultOptions , newDefaultF ) {
208
+ return defaults . apply ( null , [ extend ( true , { } , defaultOptions , newDefaultOptions ) , newDefaultF || defaultF ] ) ;
209
+ } ;
210
+
211
+ factory . Request = Request ;
212
+ factory . RetryStrategies = RetryStrategies ;
217
213
218
214
[ 'get' , 'head' , 'post' , 'put' , 'patch' , 'delete' ] . forEach ( function ( verb ) {
219
- makeHelper ( factory , verb ) ;
220
- } ) ;
221
- factory . del = factory [ 'delete' ] ;
215
+ makeHelper ( factory , verb ) ;
216
+ } ) ;
217
+ factory . del = factory [ 'delete' ] ;
222
218
223
- return factory ;
219
+ [ 'jar' , 'cookie' ] . forEach ( function ( method ) {
220
+ factory [ method ] = Factory . Request . request [ method ] ;
221
+ } ) ;
222
+
223
+ return factory ;
224
224
}
225
225
226
226
module . exports = Factory ;
@@ -231,10 +231,10 @@ Factory.RetryStrategies = RetryStrategies;
231
231
232
232
// define .get/.post/... helpers
233
233
[ 'get' , 'head' , 'post' , 'put' , 'patch' , 'delete' ] . forEach ( function ( verb ) {
234
- makeHelper ( Factory , verb ) ;
234
+ makeHelper ( Factory , verb ) ;
235
235
} ) ;
236
236
Factory . del = Factory [ 'delete' ] ;
237
237
238
- [ 'jar' , 'cookie' ] . forEach ( function ( method ) {
239
- Factory [ method ] = Factory . Request . request [ method ] ;
238
+ [ 'jar' , 'cookie' ] . forEach ( function ( method ) {
239
+ Factory [ method ] = Factory . Request . request [ method ] ;
240
240
} ) ;
0 commit comments