1 |
|
2 |
|
3 |
|
4 | var isObject = require('./is-object');
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | module.exports = RequestBase;
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | function RequestBase(obj) {
|
19 | if (obj) return mixin(obj);
|
20 | }
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | function mixin(obj) {
|
31 | for (var key in RequestBase.prototype) {
|
32 | obj[key] = RequestBase.prototype[key];
|
33 | }
|
34 | return obj;
|
35 | }
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | RequestBase.prototype.clearTimeout = function _clearTimeout(){
|
45 | clearTimeout(this._timer);
|
46 | clearTimeout(this._responseTimeoutTimer);
|
47 | delete this._timer;
|
48 | delete this._responseTimeoutTimer;
|
49 | return this;
|
50 | };
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | RequestBase.prototype.parse = function parse(fn){
|
62 | this._parser = fn;
|
63 | return this;
|
64 | };
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | RequestBase.prototype.responseType = function(val){
|
85 | this._responseType = val;
|
86 | return this;
|
87 | };
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 | RequestBase.prototype.serialize = function serialize(fn){
|
99 | this._serializer = fn;
|
100 | return this;
|
101 | };
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | RequestBase.prototype.timeout = function timeout(options){
|
117 | if (!options || 'object' !== typeof options) {
|
118 | this._timeout = options;
|
119 | this._responseTimeout = 0;
|
120 | return this;
|
121 | }
|
122 |
|
123 | for(var option in options) {
|
124 | switch(option) {
|
125 | case 'deadline':
|
126 | this._timeout = options.deadline;
|
127 | break;
|
128 | case 'response':
|
129 | this._responseTimeout = options.response;
|
130 | break;
|
131 | default:
|
132 | console.warn("Unknown timeout option", option);
|
133 | }
|
134 | }
|
135 | return this;
|
136 | };
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | RequestBase.prototype.retry = function retry(count){
|
149 |
|
150 | if (arguments.length === 0 || count === true) count = 1;
|
151 | if (count <= 0) count = 0;
|
152 | this._maxRetries = count;
|
153 | this._retries = 0;
|
154 | return this;
|
155 | };
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | RequestBase.prototype._retry = function() {
|
165 | this.clearTimeout();
|
166 |
|
167 |
|
168 | if (this.req) {
|
169 | this.req = null;
|
170 | this.req = this.request();
|
171 | }
|
172 |
|
173 | this._aborted = false;
|
174 | this.timedout = false;
|
175 |
|
176 | return this._end();
|
177 | };
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | RequestBase.prototype.then = function then(resolve, reject) {
|
188 | if (!this._fullfilledPromise) {
|
189 | var self = this;
|
190 | if (this._endCalled) {
|
191 | console.warn("Warning: superagent request was sent twice, because both .end() and .then() were called. Never call .end() if you use promises");
|
192 | }
|
193 | this._fullfilledPromise = new Promise(function(innerResolve, innerReject){
|
194 | self.end(function(err, res){
|
195 | if (err) innerReject(err); else innerResolve(res);
|
196 | });
|
197 | });
|
198 | }
|
199 | return this._fullfilledPromise.then(resolve, reject);
|
200 | }
|
201 |
|
202 | RequestBase.prototype.catch = function(cb) {
|
203 | return this.then(undefined, cb);
|
204 | };
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 | RequestBase.prototype.use = function use(fn) {
|
211 | fn(this);
|
212 | return this;
|
213 | }
|
214 |
|
215 | RequestBase.prototype.ok = function(cb) {
|
216 | if ('function' !== typeof cb) throw Error("Callback required");
|
217 | this._okCallback = cb;
|
218 | return this;
|
219 | };
|
220 |
|
221 | RequestBase.prototype._isResponseOK = function(res) {
|
222 | if (!res) {
|
223 | return false;
|
224 | }
|
225 |
|
226 | if (this._okCallback) {
|
227 | return this._okCallback(res);
|
228 | }
|
229 |
|
230 | return res.status >= 200 && res.status < 300;
|
231 | };
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 | RequestBase.prototype.get = function(field){
|
244 | return this._header[field.toLowerCase()];
|
245 | };
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 | RequestBase.prototype.getHeader = RequestBase.prototype.get;
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 | RequestBase.prototype.set = function(field, val){
|
283 | if (isObject(field)) {
|
284 | for (var key in field) {
|
285 | this.set(key, field[key]);
|
286 | }
|
287 | return this;
|
288 | }
|
289 | this._header[field.toLowerCase()] = val;
|
290 | this.header[field] = val;
|
291 | return this;
|
292 | };
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 | RequestBase.prototype.unset = function(field){
|
307 | delete this._header[field.toLowerCase()];
|
308 | delete this.header[field];
|
309 | return this;
|
310 | };
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 | RequestBase.prototype.field = function(name, val) {
|
332 |
|
333 |
|
334 | if (null === name || undefined === name) {
|
335 | throw new Error('.field(name, val) name can not be empty');
|
336 | }
|
337 |
|
338 | if (this._data) {
|
339 | console.error(".field() can't be used if .send() is used. Please use only .send() or only .field() & .attach()");
|
340 | }
|
341 |
|
342 | if (isObject(name)) {
|
343 | for (var key in name) {
|
344 | this.field(key, name[key]);
|
345 | }
|
346 | return this;
|
347 | }
|
348 |
|
349 | if (Array.isArray(val)) {
|
350 | for (var i in val) {
|
351 | this.field(name, val[i]);
|
352 | }
|
353 | return this;
|
354 | }
|
355 |
|
356 |
|
357 | if (null === val || undefined === val) {
|
358 | throw new Error('.field(name, val) val can not be empty');
|
359 | }
|
360 | if ('boolean' === typeof val) {
|
361 | val = '' + val;
|
362 | }
|
363 | this._getFormData().append(name, val);
|
364 | return this;
|
365 | };
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 | RequestBase.prototype.abort = function(){
|
374 | if (this._aborted) {
|
375 | return this;
|
376 | }
|
377 | this._aborted = true;
|
378 | this.xhr && this.xhr.abort();
|
379 | this.req && this.req.abort();
|
380 | this.clearTimeout();
|
381 | this.emit('abort');
|
382 | return this;
|
383 | };
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 | RequestBase.prototype.withCredentials = function(on){
|
397 |
|
398 | if(on==undefined) on = true;
|
399 | this._withCredentials = on;
|
400 | return this;
|
401 | };
|
402 |
|
403 |
|
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 |
|
410 |
|
411 | RequestBase.prototype.redirects = function(n){
|
412 | this._maxRedirects = n;
|
413 | return this;
|
414 | };
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 |
|
425 | RequestBase.prototype.toJSON = function(){
|
426 | return {
|
427 | method: this.method,
|
428 | url: this.url,
|
429 | data: this._data,
|
430 | headers: this._header
|
431 | };
|
432 | };
|
433 |
|
434 |
|
435 |
|
436 |
|
437 |
|
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 |
|
455 |
|
456 |
|
457 |
|
458 |
|
459 |
|
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 | RequestBase.prototype.send = function(data){
|
476 | var isObj = isObject(data);
|
477 | var type = this._header['content-type'];
|
478 |
|
479 | if (this._formData) {
|
480 | console.error(".send() can't be used if .attach() or .field() is used. Please use only .send() or only .field() & .attach()");
|
481 | }
|
482 |
|
483 | if (isObj && !this._data) {
|
484 | if (Array.isArray(data)) {
|
485 | this._data = [];
|
486 | } else if (!this._isHost(data)) {
|
487 | this._data = {};
|
488 | }
|
489 | } else if (data && this._data && this._isHost(this._data)) {
|
490 | throw Error("Can't merge these send calls");
|
491 | }
|
492 |
|
493 |
|
494 | if (isObj && isObject(this._data)) {
|
495 | for (var key in data) {
|
496 | this._data[key] = data[key];
|
497 | }
|
498 | } else if ('string' == typeof data) {
|
499 |
|
500 | if (!type) this.type('form');
|
501 | type = this._header['content-type'];
|
502 | if ('application/x-www-form-urlencoded' == type) {
|
503 | this._data = this._data
|
504 | ? this._data + '&' + data
|
505 | : data;
|
506 | } else {
|
507 | this._data = (this._data || '') + data;
|
508 | }
|
509 | } else {
|
510 | this._data = data;
|
511 | }
|
512 |
|
513 | if (!isObj || this._isHost(data)) {
|
514 | return this;
|
515 | }
|
516 |
|
517 |
|
518 | if (!type) this.type('json');
|
519 | return this;
|
520 | };
|
521 |
|
522 |
|
523 |
|
524 |
|
525 |
|
526 |
|
527 |
|
528 |
|
529 |
|
530 |
|
531 |
|
532 |
|
533 |
|
534 |
|
535 |
|
536 |
|
537 |
|
538 |
|
539 |
|
540 |
|
541 |
|
542 |
|
543 |
|
544 |
|
545 |
|
546 |
|
547 |
|
548 |
|
549 |
|
550 |
|
551 | RequestBase.prototype.sortQuery = function(sort) {
|
552 |
|
553 | this._sort = typeof sort === 'undefined' ? true : sort;
|
554 | return this;
|
555 | };
|
556 |
|
557 |
|
558 |
|
559 |
|
560 |
|
561 |
|
562 |
|
563 | RequestBase.prototype._timeoutError = function(reason, timeout, errno){
|
564 | if (this._aborted) {
|
565 | return;
|
566 | }
|
567 | var err = new Error(reason + timeout + 'ms exceeded');
|
568 | err.timeout = timeout;
|
569 | err.code = 'ECONNABORTED';
|
570 | err.errno = errno;
|
571 | this.timedout = true;
|
572 | this.abort();
|
573 | this.callback(err);
|
574 | };
|
575 |
|
576 | RequestBase.prototype._setTimeouts = function() {
|
577 | var self = this;
|
578 |
|
579 |
|
580 | if (this._timeout && !this._timer) {
|
581 | this._timer = setTimeout(function(){
|
582 | self._timeoutError('Timeout of ', self._timeout, 'ETIME');
|
583 | }, this._timeout);
|
584 | }
|
585 |
|
586 | if (this._responseTimeout && !this._responseTimeoutTimer) {
|
587 | this._responseTimeoutTimer = setTimeout(function(){
|
588 | self._timeoutError('Response timeout of ', self._responseTimeout, 'ETIMEDOUT');
|
589 | }, this._responseTimeout);
|
590 | }
|
591 | }
|