1 | (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.superagent = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | var isObject = require('./is-object');
|
10 |
|
11 | function isFunction(fn) {
|
12 | var tag = isObject(fn) ? Object.prototype.toString.call(fn) : '';
|
13 | return tag === '[object Function]';
|
14 | }
|
15 |
|
16 | module.exports = isFunction;
|
17 |
|
18 | },{"./is-object":2}],2:[function(require,module,exports){
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | function isObject(obj) {
|
28 | return null !== obj && 'object' === typeof obj;
|
29 | }
|
30 |
|
31 | module.exports = isObject;
|
32 |
|
33 | },{}],3:[function(require,module,exports){
|
34 |
|
35 |
|
36 |
|
37 | var isObject = require('./is-object');
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | module.exports = RequestBase;
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | function RequestBase(obj) {
|
52 | if (obj) return mixin(obj);
|
53 | }
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | function mixin(obj) {
|
64 | for (var key in RequestBase.prototype) {
|
65 | obj[key] = RequestBase.prototype[key];
|
66 | }
|
67 | return obj;
|
68 | }
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 | RequestBase.prototype.clearTimeout = function _clearTimeout(){
|
78 | clearTimeout(this._timer);
|
79 | clearTimeout(this._responseTimeoutTimer);
|
80 | delete this._timer;
|
81 | delete this._responseTimeoutTimer;
|
82 | return this;
|
83 | };
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | RequestBase.prototype.parse = function parse(fn){
|
95 | this._parser = fn;
|
96 | return this;
|
97 | };
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 | RequestBase.prototype.responseType = function(val){
|
118 | this._responseType = val;
|
119 | return this;
|
120 | };
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 | RequestBase.prototype.serialize = function serialize(fn){
|
132 | this._serializer = fn;
|
133 | return this;
|
134 | };
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 | RequestBase.prototype.timeout = function timeout(options){
|
150 | if (!options || 'object' !== typeof options) {
|
151 | this._timeout = options;
|
152 | this._responseTimeout = 0;
|
153 | return this;
|
154 | }
|
155 |
|
156 | if ('undefined' !== typeof options.deadline) {
|
157 | this._timeout = options.deadline;
|
158 | }
|
159 | if ('undefined' !== typeof options.response) {
|
160 | this._responseTimeout = options.response;
|
161 | }
|
162 | return this;
|
163 | };
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 | RequestBase.prototype.retry = function retry(count){
|
176 |
|
177 | if (arguments.length === 0 || count === true) count = 1;
|
178 | if (count <= 0) count = 0;
|
179 | this._maxRetries = count;
|
180 | this._retries = 0;
|
181 | return this;
|
182 | };
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 | RequestBase.prototype._retry = function() {
|
192 | this.clearTimeout();
|
193 |
|
194 |
|
195 | if (this.req) {
|
196 | this.req = null;
|
197 | this.req = this.request();
|
198 | }
|
199 |
|
200 | this._aborted = false;
|
201 | this.timedout = false;
|
202 |
|
203 | return this._end();
|
204 | };
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 | RequestBase.prototype.then = function then(resolve, reject) {
|
215 | if (!this._fullfilledPromise) {
|
216 | var self = this;
|
217 | if (this._endCalled) {
|
218 | console.warn("Warning: superagent request was sent twice, because both .end() and .then() were called. Never call .end() if you use promises");
|
219 | }
|
220 | this._fullfilledPromise = new Promise(function(innerResolve, innerReject){
|
221 | self.end(function(err, res){
|
222 | if (err) innerReject(err); else innerResolve(res);
|
223 | });
|
224 | });
|
225 | }
|
226 | return this._fullfilledPromise.then(resolve, reject);
|
227 | }
|
228 |
|
229 | RequestBase.prototype.catch = function(cb) {
|
230 | return this.then(undefined, cb);
|
231 | };
|
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 | RequestBase.prototype.use = function use(fn) {
|
238 | fn(this);
|
239 | return this;
|
240 | }
|
241 |
|
242 | RequestBase.prototype.ok = function(cb) {
|
243 | if ('function' !== typeof cb) throw Error("Callback required");
|
244 | this._okCallback = cb;
|
245 | return this;
|
246 | };
|
247 |
|
248 | RequestBase.prototype._isResponseOK = function(res) {
|
249 | if (!res) {
|
250 | return false;
|
251 | }
|
252 |
|
253 | if (this._okCallback) {
|
254 | return this._okCallback(res);
|
255 | }
|
256 |
|
257 | return res.status >= 200 && res.status < 300;
|
258 | };
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 | RequestBase.prototype.get = function(field){
|
271 | return this._header[field.toLowerCase()];
|
272 | };
|
273 |
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 | RequestBase.prototype.getHeader = RequestBase.prototype.get;
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 | RequestBase.prototype.set = function(field, val){
|
310 | if (isObject(field)) {
|
311 | for (var key in field) {
|
312 | this.set(key, field[key]);
|
313 | }
|
314 | return this;
|
315 | }
|
316 | this._header[field.toLowerCase()] = val;
|
317 | this.header[field] = val;
|
318 | return this;
|
319 | };
|
320 |
|
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 | RequestBase.prototype.unset = function(field){
|
334 | delete this._header[field.toLowerCase()];
|
335 | delete this.header[field];
|
336 | return this;
|
337 | };
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 | RequestBase.prototype.field = function(name, val) {
|
359 |
|
360 |
|
361 | if (null === name || undefined === name) {
|
362 | throw new Error('.field(name, val) name can not be empty');
|
363 | }
|
364 |
|
365 | if (this._data) {
|
366 | console.error(".field() can't be used if .send() is used. Please use only .send() or only .field() & .attach()");
|
367 | }
|
368 |
|
369 | if (isObject(name)) {
|
370 | for (var key in name) {
|
371 | this.field(key, name[key]);
|
372 | }
|
373 | return this;
|
374 | }
|
375 |
|
376 | if (Array.isArray(val)) {
|
377 | for (var i in val) {
|
378 | this.field(name, val[i]);
|
379 | }
|
380 | return this;
|
381 | }
|
382 |
|
383 |
|
384 | if (null === val || undefined === val) {
|
385 | throw new Error('.field(name, val) val can not be empty');
|
386 | }
|
387 | if ('boolean' === typeof val) {
|
388 | val = '' + val;
|
389 | }
|
390 | this._getFormData().append(name, val);
|
391 | return this;
|
392 | };
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 | RequestBase.prototype.abort = function(){
|
401 | if (this._aborted) {
|
402 | return this;
|
403 | }
|
404 | this._aborted = true;
|
405 | this.xhr && this.xhr.abort();
|
406 | this.req && this.req.abort();
|
407 | this.clearTimeout();
|
408 | this.emit('abort');
|
409 | return this;
|
410 | };
|
411 |
|
412 |
|
413 |
|
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 | RequestBase.prototype.withCredentials = function(){
|
424 |
|
425 | this._withCredentials = true;
|
426 | return this;
|
427 | };
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 | RequestBase.prototype.redirects = function(n){
|
438 | this._maxRedirects = n;
|
439 | return this;
|
440 | };
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 | RequestBase.prototype.toJSON = function(){
|
452 | return {
|
453 | method: this.method,
|
454 | url: this.url,
|
455 | data: this._data,
|
456 | headers: this._header
|
457 | };
|
458 | };
|
459 |
|
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 |
|
480 |
|
481 |
|
482 |
|
483 |
|
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 |
|
497 |
|
498 |
|
499 |
|
500 |
|
501 | RequestBase.prototype.send = function(data){
|
502 | var isObj = isObject(data);
|
503 | var type = this._header['content-type'];
|
504 |
|
505 | if (this._formData) {
|
506 | console.error(".send() can't be used if .attach() or .field() is used. Please use only .send() or only .field() & .attach()");
|
507 | }
|
508 |
|
509 | if (isObj && !this._data) {
|
510 | if (Array.isArray(data)) {
|
511 | this._data = [];
|
512 | } else if (!this._isHost(data)) {
|
513 | this._data = {};
|
514 | }
|
515 | } else if (data && this._data && this._isHost(this._data)) {
|
516 | throw Error("Can't merge these send calls");
|
517 | }
|
518 |
|
519 |
|
520 | if (isObj && isObject(this._data)) {
|
521 | for (var key in data) {
|
522 | this._data[key] = data[key];
|
523 | }
|
524 | } else if ('string' == typeof data) {
|
525 |
|
526 | if (!type) this.type('form');
|
527 | type = this._header['content-type'];
|
528 | if ('application/x-www-form-urlencoded' == type) {
|
529 | this._data = this._data
|
530 | ? this._data + '&' + data
|
531 | : data;
|
532 | } else {
|
533 | this._data = (this._data || '') + data;
|
534 | }
|
535 | } else {
|
536 | this._data = data;
|
537 | }
|
538 |
|
539 | if (!isObj || this._isHost(data)) {
|
540 | return this;
|
541 | }
|
542 |
|
543 |
|
544 | if (!type) this.type('json');
|
545 | return this;
|
546 | };
|
547 |
|
548 |
|
549 |
|
550 |
|
551 |
|
552 |
|
553 |
|
554 |
|
555 |
|
556 |
|
557 |
|
558 |
|
559 |
|
560 |
|
561 |
|
562 |
|
563 |
|
564 |
|
565 |
|
566 |
|
567 |
|
568 |
|
569 |
|
570 |
|
571 |
|
572 |
|
573 |
|
574 |
|
575 |
|
576 |
|
577 | RequestBase.prototype.sortQuery = function(sort) {
|
578 |
|
579 | this._sort = typeof sort === 'undefined' ? true : sort;
|
580 | return this;
|
581 | };
|
582 |
|
583 |
|
584 |
|
585 |
|
586 |
|
587 |
|
588 |
|
589 | RequestBase.prototype._timeoutError = function(reason, timeout){
|
590 | if (this._aborted) {
|
591 | return;
|
592 | }
|
593 | var err = new Error(reason + timeout + 'ms exceeded');
|
594 | err.timeout = timeout;
|
595 | err.code = 'ECONNABORTED';
|
596 | this.timedout = true;
|
597 | this.abort();
|
598 | this.callback(err);
|
599 | };
|
600 |
|
601 | RequestBase.prototype._setTimeouts = function() {
|
602 | var self = this;
|
603 |
|
604 |
|
605 | if (this._timeout && !this._timer) {
|
606 | this._timer = setTimeout(function(){
|
607 | self._timeoutError('Timeout of ', self._timeout);
|
608 | }, this._timeout);
|
609 | }
|
610 |
|
611 | if (this._responseTimeout && !this._responseTimeoutTimer) {
|
612 | this._responseTimeoutTimer = setTimeout(function(){
|
613 | self._timeoutError('Response timeout of ', self._responseTimeout);
|
614 | }, this._responseTimeout);
|
615 | }
|
616 | }
|
617 |
|
618 | },{"./is-object":2}],4:[function(require,module,exports){
|
619 |
|
620 |
|
621 |
|
622 |
|
623 |
|
624 | var utils = require('./utils');
|
625 |
|
626 |
|
627 |
|
628 |
|
629 |
|
630 | module.exports = ResponseBase;
|
631 |
|
632 |
|
633 |
|
634 |
|
635 |
|
636 |
|
637 |
|
638 | function ResponseBase(obj) {
|
639 | if (obj) return mixin(obj);
|
640 | }
|
641 |
|
642 |
|
643 |
|
644 |
|
645 |
|
646 |
|
647 |
|
648 |
|
649 |
|
650 | function mixin(obj) {
|
651 | for (var key in ResponseBase.prototype) {
|
652 | obj[key] = ResponseBase.prototype[key];
|
653 | }
|
654 | return obj;
|
655 | }
|
656 |
|
657 |
|
658 |
|
659 |
|
660 |
|
661 |
|
662 |
|
663 |
|
664 |
|
665 | ResponseBase.prototype.get = function(field){
|
666 | return this.header[field.toLowerCase()];
|
667 | };
|
668 |
|
669 |
|
670 |
|
671 |
|
672 |
|
673 |
|
674 |
|
675 |
|
676 |
|
677 |
|
678 |
|
679 |
|
680 |
|
681 | ResponseBase.prototype._setHeaderProperties = function(header){
|
682 |
|
683 |
|
684 |
|
685 |
|
686 | var ct = header['content-type'] || '';
|
687 | this.type = utils.type(ct);
|
688 |
|
689 |
|
690 | var params = utils.params(ct);
|
691 | for (var key in params) this[key] = params[key];
|
692 |
|
693 | this.links = {};
|
694 |
|
695 |
|
696 | try {
|
697 | if (header.link) {
|
698 | this.links = utils.parseLinks(header.link);
|
699 | }
|
700 | } catch (err) {
|
701 |
|
702 | }
|
703 | };
|
704 |
|
705 |
|
706 |
|
707 |
|
708 |
|
709 |
|
710 |
|
711 |
|
712 |
|
713 |
|
714 |
|
715 |
|
716 |
|
717 |
|
718 |
|
719 |
|
720 |
|
721 |
|
722 |
|
723 |
|
724 |
|
725 |
|
726 | ResponseBase.prototype._setStatusProperties = function(status){
|
727 | var type = status / 100 | 0;
|
728 |
|
729 |
|
730 | this.status = this.statusCode = status;
|
731 | this.statusType = type;
|
732 |
|
733 |
|
734 | this.info = 1 == type;
|
735 | this.ok = 2 == type;
|
736 | this.redirect = 3 == type;
|
737 | this.clientError = 4 == type;
|
738 | this.serverError = 5 == type;
|
739 | this.error = (4 == type || 5 == type)
|
740 | ? this.toError()
|
741 | : false;
|
742 |
|
743 |
|
744 | this.accepted = 202 == status;
|
745 | this.noContent = 204 == status;
|
746 | this.badRequest = 400 == status;
|
747 | this.unauthorized = 401 == status;
|
748 | this.notAcceptable = 406 == status;
|
749 | this.forbidden = 403 == status;
|
750 | this.notFound = 404 == status;
|
751 | };
|
752 |
|
753 | },{"./utils":6}],5:[function(require,module,exports){
|
754 | var ERROR_CODES = [
|
755 | 'ECONNRESET',
|
756 | 'ETIMEDOUT',
|
757 | 'EADDRINFO',
|
758 | 'ESOCKETTIMEDOUT'
|
759 | ];
|
760 |
|
761 |
|
762 |
|
763 |
|
764 |
|
765 |
|
766 |
|
767 |
|
768 |
|
769 | module.exports = function shouldRetry(err, res) {
|
770 | if (err && err.code && ~ERROR_CODES.indexOf(err.code)) return true;
|
771 | if (res && res.status && res.status >= 500) return true;
|
772 |
|
773 | if (err && 'timeout' in err && err.code == 'ECONNABORTED') return true;
|
774 | return false;
|
775 | };
|
776 | },{}],6:[function(require,module,exports){
|
777 |
|
778 |
|
779 |
|
780 |
|
781 |
|
782 |
|
783 |
|
784 |
|
785 |
|
786 | exports.type = function(str){
|
787 | return str.split(/ *; */).shift();
|
788 | };
|
789 |
|
790 |
|
791 |
|
792 |
|
793 |
|
794 |
|
795 |
|
796 |
|
797 |
|
798 | exports.params = function(str){
|
799 | return str.split(/ *; */).reduce(function(obj, str){
|
800 | var parts = str.split(/ *= */);
|
801 | var key = parts.shift();
|
802 | var val = parts.shift();
|
803 |
|
804 | if (key && val) obj[key] = val;
|
805 | return obj;
|
806 | }, {});
|
807 | };
|
808 |
|
809 |
|
810 |
|
811 |
|
812 |
|
813 |
|
814 |
|
815 |
|
816 |
|
817 | exports.parseLinks = function(str){
|
818 | return str.split(/ *, */).reduce(function(obj, str){
|
819 | var parts = str.split(/ *; */);
|
820 | var url = parts[0].slice(1, -1);
|
821 | var rel = parts[1].split(/ *= */)[1].slice(1, -1);
|
822 | obj[rel] = url;
|
823 | return obj;
|
824 | }, {});
|
825 | };
|
826 |
|
827 |
|
828 |
|
829 |
|
830 |
|
831 |
|
832 |
|
833 |
|
834 |
|
835 | exports.cleanHeader = function(header, shouldStripCookie){
|
836 | delete header['content-type'];
|
837 | delete header['content-length'];
|
838 | delete header['transfer-encoding'];
|
839 | delete header['host'];
|
840 | if (shouldStripCookie) {
|
841 | delete header['cookie'];
|
842 | }
|
843 | return header;
|
844 | };
|
845 | },{}],7:[function(require,module,exports){
|
846 |
|
847 |
|
848 |
|
849 |
|
850 |
|
851 | if (typeof module !== 'undefined') {
|
852 | module.exports = Emitter;
|
853 | }
|
854 |
|
855 |
|
856 |
|
857 |
|
858 |
|
859 |
|
860 |
|
861 | function Emitter(obj) {
|
862 | if (obj) return mixin(obj);
|
863 | };
|
864 |
|
865 |
|
866 |
|
867 |
|
868 |
|
869 |
|
870 |
|
871 |
|
872 |
|
873 | function mixin(obj) {
|
874 | for (var key in Emitter.prototype) {
|
875 | obj[key] = Emitter.prototype[key];
|
876 | }
|
877 | return obj;
|
878 | }
|
879 |
|
880 |
|
881 |
|
882 |
|
883 |
|
884 |
|
885 |
|
886 |
|
887 |
|
888 |
|
889 | Emitter.prototype.on =
|
890 | Emitter.prototype.addEventListener = function(event, fn){
|
891 | this._callbacks = this._callbacks || {};
|
892 | (this._callbacks['$' + event] = this._callbacks['$' + event] || [])
|
893 | .push(fn);
|
894 | return this;
|
895 | };
|
896 |
|
897 |
|
898 |
|
899 |
|
900 |
|
901 |
|
902 |
|
903 |
|
904 |
|
905 |
|
906 |
|
907 | Emitter.prototype.once = function(event, fn){
|
908 | function on() {
|
909 | this.off(event, on);
|
910 | fn.apply(this, arguments);
|
911 | }
|
912 |
|
913 | on.fn = fn;
|
914 | this.on(event, on);
|
915 | return this;
|
916 | };
|
917 |
|
918 |
|
919 |
|
920 |
|
921 |
|
922 |
|
923 |
|
924 |
|
925 |
|
926 |
|
927 |
|
928 | Emitter.prototype.off =
|
929 | Emitter.prototype.removeListener =
|
930 | Emitter.prototype.removeAllListeners =
|
931 | Emitter.prototype.removeEventListener = function(event, fn){
|
932 | this._callbacks = this._callbacks || {};
|
933 |
|
934 |
|
935 | if (0 == arguments.length) {
|
936 | this._callbacks = {};
|
937 | return this;
|
938 | }
|
939 |
|
940 |
|
941 | var callbacks = this._callbacks['$' + event];
|
942 | if (!callbacks) return this;
|
943 |
|
944 |
|
945 | if (1 == arguments.length) {
|
946 | delete this._callbacks['$' + event];
|
947 | return this;
|
948 | }
|
949 |
|
950 |
|
951 | var cb;
|
952 | for (var i = 0; i < callbacks.length; i++) {
|
953 | cb = callbacks[i];
|
954 | if (cb === fn || cb.fn === fn) {
|
955 | callbacks.splice(i, 1);
|
956 | break;
|
957 | }
|
958 | }
|
959 | return this;
|
960 | };
|
961 |
|
962 |
|
963 |
|
964 |
|
965 |
|
966 |
|
967 |
|
968 |
|
969 |
|
970 | Emitter.prototype.emit = function(event){
|
971 | this._callbacks = this._callbacks || {};
|
972 | var args = [].slice.call(arguments, 1)
|
973 | , callbacks = this._callbacks['$' + event];
|
974 |
|
975 | if (callbacks) {
|
976 | callbacks = callbacks.slice(0);
|
977 | for (var i = 0, len = callbacks.length; i < len; ++i) {
|
978 | callbacks[i].apply(this, args);
|
979 | }
|
980 | }
|
981 |
|
982 | return this;
|
983 | };
|
984 |
|
985 |
|
986 |
|
987 |
|
988 |
|
989 |
|
990 |
|
991 |
|
992 |
|
993 | Emitter.prototype.listeners = function(event){
|
994 | this._callbacks = this._callbacks || {};
|
995 | return this._callbacks['$' + event] || [];
|
996 | };
|
997 |
|
998 |
|
999 |
|
1000 |
|
1001 |
|
1002 |
|
1003 |
|
1004 |
|
1005 |
|
1006 | Emitter.prototype.hasListeners = function(event){
|
1007 | return !! this.listeners(event).length;
|
1008 | };
|
1009 |
|
1010 | },{}],8:[function(require,module,exports){
|
1011 |
|
1012 |
|
1013 |
|
1014 |
|
1015 | var root;
|
1016 | if (typeof window !== 'undefined') {
|
1017 | root = window;
|
1018 | } else if (typeof self !== 'undefined') {
|
1019 | root = self;
|
1020 | } else {
|
1021 | console.warn("Using browser-only version of superagent in non-browser environment");
|
1022 | root = this;
|
1023 | }
|
1024 |
|
1025 | var Emitter = require('emitter');
|
1026 | var RequestBase = require('./request-base');
|
1027 | var isObject = require('./is-object');
|
1028 | var isFunction = require('./is-function');
|
1029 | var ResponseBase = require('./response-base');
|
1030 | var shouldRetry = require('./should-retry');
|
1031 |
|
1032 |
|
1033 |
|
1034 |
|
1035 |
|
1036 | function noop(){};
|
1037 |
|
1038 |
|
1039 |
|
1040 |
|
1041 |
|
1042 | var request = exports = module.exports = function(method, url) {
|
1043 |
|
1044 | if ('function' == typeof url) {
|
1045 | return new exports.Request('GET', method).end(url);
|
1046 | }
|
1047 |
|
1048 |
|
1049 | if (1 == arguments.length) {
|
1050 | return new exports.Request('GET', method);
|
1051 | }
|
1052 |
|
1053 | return new exports.Request(method, url);
|
1054 | }
|
1055 |
|
1056 | exports.Request = Request;
|
1057 |
|
1058 |
|
1059 |
|
1060 |
|
1061 |
|
1062 | request.getXHR = function () {
|
1063 | if (root.XMLHttpRequest
|
1064 | && (!root.location || 'file:' != root.location.protocol
|
1065 | || !root.ActiveXObject)) {
|
1066 | return new XMLHttpRequest;
|
1067 | } else {
|
1068 | try { return new ActiveXObject('Microsoft.XMLHTTP'); } catch(e) {}
|
1069 | try { return new ActiveXObject('Msxml2.XMLHTTP.6.0'); } catch(e) {}
|
1070 | try { return new ActiveXObject('Msxml2.XMLHTTP.3.0'); } catch(e) {}
|
1071 | try { return new ActiveXObject('Msxml2.XMLHTTP'); } catch(e) {}
|
1072 | }
|
1073 | throw Error("Browser-only verison of superagent could not find XHR");
|
1074 | };
|
1075 |
|
1076 |
|
1077 |
|
1078 |
|
1079 |
|
1080 |
|
1081 |
|
1082 |
|
1083 |
|
1084 | var trim = ''.trim
|
1085 | ? function(s) { return s.trim(); }
|
1086 | : function(s) { return s.replace(/(^\s*|\s*$)/g, ''); };
|
1087 |
|
1088 |
|
1089 |
|
1090 |
|
1091 |
|
1092 |
|
1093 |
|
1094 |
|
1095 |
|
1096 | function serialize(obj) {
|
1097 | if (!isObject(obj)) return obj;
|
1098 | var pairs = [];
|
1099 | for (var key in obj) {
|
1100 | pushEncodedKeyValuePair(pairs, key, obj[key]);
|
1101 | }
|
1102 | return pairs.join('&');
|
1103 | }
|
1104 |
|
1105 |
|
1106 |
|
1107 |
|
1108 |
|
1109 |
|
1110 |
|
1111 |
|
1112 |
|
1113 |
|
1114 | function pushEncodedKeyValuePair(pairs, key, val) {
|
1115 | if (val != null) {
|
1116 | if (Array.isArray(val)) {
|
1117 | val.forEach(function(v) {
|
1118 | pushEncodedKeyValuePair(pairs, key, v);
|
1119 | });
|
1120 | } else if (isObject(val)) {
|
1121 | for(var subkey in val) {
|
1122 | pushEncodedKeyValuePair(pairs, key + '[' + subkey + ']', val[subkey]);
|
1123 | }
|
1124 | } else {
|
1125 | pairs.push(encodeURIComponent(key)
|
1126 | + '=' + encodeURIComponent(val));
|
1127 | }
|
1128 | } else if (val === null) {
|
1129 | pairs.push(encodeURIComponent(key));
|
1130 | }
|
1131 | }
|
1132 |
|
1133 |
|
1134 |
|
1135 |
|
1136 |
|
1137 | request.serializeObject = serialize;
|
1138 |
|
1139 | |
1140 |
|
1141 |
|
1142 |
|
1143 |
|
1144 |
|
1145 |
|
1146 |
|
1147 | function parseString(str) {
|
1148 | var obj = {};
|
1149 | var pairs = str.split('&');
|
1150 | var pair;
|
1151 | var pos;
|
1152 |
|
1153 | for (var i = 0, len = pairs.length; i < len; ++i) {
|
1154 | pair = pairs[i];
|
1155 | pos = pair.indexOf('=');
|
1156 | if (pos == -1) {
|
1157 | obj[decodeURIComponent(pair)] = '';
|
1158 | } else {
|
1159 | obj[decodeURIComponent(pair.slice(0, pos))] =
|
1160 | decodeURIComponent(pair.slice(pos + 1));
|
1161 | }
|
1162 | }
|
1163 |
|
1164 | return obj;
|
1165 | }
|
1166 |
|
1167 |
|
1168 |
|
1169 |
|
1170 |
|
1171 | request.parseString = parseString;
|
1172 |
|
1173 |
|
1174 |
|
1175 |
|
1176 |
|
1177 |
|
1178 |
|
1179 |
|
1180 | request.types = {
|
1181 | html: 'text/html',
|
1182 | json: 'application/json',
|
1183 | xml: 'application/xml',
|
1184 | urlencoded: 'application/x-www-form-urlencoded',
|
1185 | 'form': 'application/x-www-form-urlencoded',
|
1186 | 'form-data': 'application/x-www-form-urlencoded'
|
1187 | };
|
1188 |
|
1189 |
|
1190 |
|
1191 |
|
1192 |
|
1193 |
|
1194 |
|
1195 |
|
1196 |
|
1197 |
|
1198 | request.serialize = {
|
1199 | 'application/x-www-form-urlencoded': serialize,
|
1200 | 'application/json': JSON.stringify
|
1201 | };
|
1202 |
|
1203 | |
1204 |
|
1205 |
|
1206 |
|
1207 |
|
1208 |
|
1209 |
|
1210 |
|
1211 |
|
1212 | request.parse = {
|
1213 | 'application/x-www-form-urlencoded': parseString,
|
1214 | 'application/json': JSON.parse
|
1215 | };
|
1216 |
|
1217 |
|
1218 |
|
1219 |
|
1220 |
|
1221 |
|
1222 |
|
1223 |
|
1224 |
|
1225 |
|
1226 | function parseHeader(str) {
|
1227 | var lines = str.split(/\r?\n/);
|
1228 | var fields = {};
|
1229 | var index;
|
1230 | var line;
|
1231 | var field;
|
1232 | var val;
|
1233 |
|
1234 | lines.pop();
|
1235 |
|
1236 | for (var i = 0, len = lines.length; i < len; ++i) {
|
1237 | line = lines[i];
|
1238 | index = line.indexOf(':');
|
1239 | field = line.slice(0, index).toLowerCase();
|
1240 | val = trim(line.slice(index + 1));
|
1241 | fields[field] = val;
|
1242 | }
|
1243 |
|
1244 | return fields;
|
1245 | }
|
1246 |
|
1247 |
|
1248 |
|
1249 |
|
1250 |
|
1251 |
|
1252 |
|
1253 |
|
1254 |
|
1255 | function isJSON(mime) {
|
1256 | return /[\/+]json\b/.test(mime);
|
1257 | }
|
1258 |
|
1259 |
|
1260 |
|
1261 |
|
1262 |
|
1263 |
|
1264 |
|
1265 |
|
1266 |
|
1267 |
|
1268 |
|
1269 |
|
1270 |
|
1271 |
|
1272 |
|
1273 |
|
1274 |
|
1275 |
|
1276 |
|
1277 |
|
1278 |
|
1279 |
|
1280 |
|
1281 |
|
1282 |
|
1283 |
|
1284 |
|
1285 |
|
1286 |
|
1287 |
|
1288 |
|
1289 |
|
1290 |
|
1291 |
|
1292 |
|
1293 |
|
1294 |
|
1295 |
|
1296 |
|
1297 |
|
1298 |
|
1299 |
|
1300 |
|
1301 |
|
1302 |
|
1303 |
|
1304 |
|
1305 | function Response(req) {
|
1306 | this.req = req;
|
1307 | this.xhr = this.req.xhr;
|
1308 |
|
1309 | this.text = ((this.req.method !='HEAD' && (this.xhr.responseType === '' || this.xhr.responseType === 'text')) || typeof this.xhr.responseType === 'undefined')
|
1310 | ? this.xhr.responseText
|
1311 | : null;
|
1312 | this.statusText = this.req.xhr.statusText;
|
1313 | var status = this.xhr.status;
|
1314 |
|
1315 | if (status === 1223) {
|
1316 | status = 204;
|
1317 | }
|
1318 | this._setStatusProperties(status);
|
1319 | this.header = this.headers = parseHeader(this.xhr.getAllResponseHeaders());
|
1320 |
|
1321 |
|
1322 |
|
1323 | this.header['content-type'] = this.xhr.getResponseHeader('content-type');
|
1324 | this._setHeaderProperties(this.header);
|
1325 |
|
1326 | if (null === this.text && req._responseType) {
|
1327 | this.body = this.xhr.response;
|
1328 | } else {
|
1329 | this.body = this.req.method != 'HEAD'
|
1330 | ? this._parseBody(this.text ? this.text : this.xhr.response)
|
1331 | : null;
|
1332 | }
|
1333 | }
|
1334 |
|
1335 | ResponseBase(Response.prototype);
|
1336 |
|
1337 |
|
1338 |
|
1339 |
|
1340 |
|
1341 |
|
1342 |
|
1343 |
|
1344 |
|
1345 |
|
1346 |
|
1347 |
|
1348 | Response.prototype._parseBody = function(str){
|
1349 | var parse = request.parse[this.type];
|
1350 | if(this.req._parser) {
|
1351 | return this.req._parser(this, str);
|
1352 | }
|
1353 | if (!parse && isJSON(this.type)) {
|
1354 | parse = request.parse['application/json'];
|
1355 | }
|
1356 | return parse && str && (str.length || str instanceof Object)
|
1357 | ? parse(str)
|
1358 | : null;
|
1359 | };
|
1360 |
|
1361 |
|
1362 |
|
1363 |
|
1364 |
|
1365 |
|
1366 |
|
1367 |
|
1368 | Response.prototype.toError = function(){
|
1369 | var req = this.req;
|
1370 | var method = req.method;
|
1371 | var url = req.url;
|
1372 |
|
1373 | var msg = 'cannot ' + method + ' ' + url + ' (' + this.status + ')';
|
1374 | var err = new Error(msg);
|
1375 | err.status = this.status;
|
1376 | err.method = method;
|
1377 | err.url = url;
|
1378 |
|
1379 | return err;
|
1380 | };
|
1381 |
|
1382 |
|
1383 |
|
1384 |
|
1385 |
|
1386 | request.Response = Response;
|
1387 |
|
1388 |
|
1389 |
|
1390 |
|
1391 |
|
1392 |
|
1393 |
|
1394 |
|
1395 |
|
1396 | function Request(method, url) {
|
1397 | var self = this;
|
1398 | this._query = this._query || [];
|
1399 | this.method = method;
|
1400 | this.url = url;
|
1401 | this.header = {};
|
1402 | this._header = {};
|
1403 | this.on('end', function(){
|
1404 | var err = null;
|
1405 | var res = null;
|
1406 |
|
1407 | try {
|
1408 | res = new Response(self);
|
1409 | } catch(e) {
|
1410 | err = new Error('Parser is unable to parse the response');
|
1411 | err.parse = true;
|
1412 | err.original = e;
|
1413 |
|
1414 | if (self.xhr) {
|
1415 |
|
1416 | err.rawResponse = typeof self.xhr.responseType == 'undefined' ? self.xhr.responseText : self.xhr.response;
|
1417 |
|
1418 | err.status = self.xhr.status ? self.xhr.status : null;
|
1419 | err.statusCode = err.status;
|
1420 | } else {
|
1421 | err.rawResponse = null;
|
1422 | err.status = null;
|
1423 | }
|
1424 |
|
1425 | return self.callback(err);
|
1426 | }
|
1427 |
|
1428 | self.emit('response', res);
|
1429 |
|
1430 | var new_err;
|
1431 | try {
|
1432 | if (!self._isResponseOK(res)) {
|
1433 | new_err = new Error(res.statusText || 'Unsuccessful HTTP response');
|
1434 | new_err.original = err;
|
1435 | new_err.response = res;
|
1436 | new_err.status = res.status;
|
1437 | }
|
1438 | } catch(e) {
|
1439 | new_err = e;
|
1440 | }
|
1441 |
|
1442 |
|
1443 | if (new_err) {
|
1444 | self.callback(new_err, res);
|
1445 | } else {
|
1446 | self.callback(null, res);
|
1447 | }
|
1448 | });
|
1449 | }
|
1450 |
|
1451 |
|
1452 |
|
1453 |
|
1454 |
|
1455 | Emitter(Request.prototype);
|
1456 | RequestBase(Request.prototype);
|
1457 |
|
1458 |
|
1459 |
|
1460 |
|
1461 |
|
1462 |
|
1463 |
|
1464 |
|
1465 |
|
1466 |
|
1467 |
|
1468 |
|
1469 |
|
1470 |
|
1471 |
|
1472 |
|
1473 |
|
1474 |
|
1475 |
|
1476 |
|
1477 |
|
1478 |
|
1479 |
|
1480 | Request.prototype.type = function(type){
|
1481 | this.set('Content-Type', request.types[type] || type);
|
1482 | return this;
|
1483 | };
|
1484 |
|
1485 |
|
1486 |
|
1487 |
|
1488 |
|
1489 |
|
1490 |
|
1491 |
|
1492 |
|
1493 |
|
1494 |
|
1495 |
|
1496 |
|
1497 |
|
1498 |
|
1499 |
|
1500 |
|
1501 |
|
1502 |
|
1503 |
|
1504 |
|
1505 | Request.prototype.accept = function(type){
|
1506 | this.set('Accept', request.types[type] || type);
|
1507 | return this;
|
1508 | };
|
1509 |
|
1510 |
|
1511 |
|
1512 |
|
1513 |
|
1514 |
|
1515 |
|
1516 |
|
1517 |
|
1518 |
|
1519 |
|
1520 | Request.prototype.auth = function(user, pass, options){
|
1521 | if (!options) {
|
1522 | options = {
|
1523 | type: 'function' === typeof btoa ? 'basic' : 'auto',
|
1524 | }
|
1525 | }
|
1526 |
|
1527 | switch (options.type) {
|
1528 | case 'basic':
|
1529 | this.set('Authorization', 'Basic ' + btoa(user + ':' + pass));
|
1530 | break;
|
1531 |
|
1532 | case 'auto':
|
1533 | this.username = user;
|
1534 | this.password = pass;
|
1535 | break;
|
1536 | }
|
1537 | return this;
|
1538 | };
|
1539 |
|
1540 |
|
1541 |
|
1542 |
|
1543 |
|
1544 |
|
1545 |
|
1546 |
|
1547 |
|
1548 |
|
1549 |
|
1550 |
|
1551 |
|
1552 |
|
1553 |
|
1554 | Request.prototype.query = function(val){
|
1555 | if ('string' != typeof val) val = serialize(val);
|
1556 | if (val) this._query.push(val);
|
1557 | return this;
|
1558 | };
|
1559 |
|
1560 |
|
1561 |
|
1562 |
|
1563 |
|
1564 |
|
1565 |
|
1566 |
|
1567 |
|
1568 |
|
1569 |
|
1570 |
|
1571 |
|
1572 |
|
1573 |
|
1574 |
|
1575 |
|
1576 |
|
1577 | Request.prototype.attach = function(field, file, options){
|
1578 | if (file) {
|
1579 | if (this._data) {
|
1580 | throw Error("superagent can't mix .send() and .attach()");
|
1581 | }
|
1582 |
|
1583 | this._getFormData().append(field, file, options || file.name);
|
1584 | }
|
1585 | return this;
|
1586 | };
|
1587 |
|
1588 | Request.prototype._getFormData = function(){
|
1589 | if (!this._formData) {
|
1590 | this._formData = new root.FormData();
|
1591 | }
|
1592 | return this._formData;
|
1593 | };
|
1594 |
|
1595 |
|
1596 |
|
1597 |
|
1598 |
|
1599 |
|
1600 |
|
1601 |
|
1602 |
|
1603 |
|
1604 | Request.prototype.callback = function(err, res){
|
1605 |
|
1606 | if (this._maxRetries && this._retries++ < this._maxRetries && shouldRetry(err, res)) {
|
1607 | return this._retry();
|
1608 | }
|
1609 |
|
1610 | var fn = this._callback;
|
1611 | this.clearTimeout();
|
1612 |
|
1613 | if (err) {
|
1614 | if (this._maxRetries) err.retries = this._retries - 1;
|
1615 | this.emit('error', err);
|
1616 | }
|
1617 |
|
1618 | fn(err, res);
|
1619 | };
|
1620 |
|
1621 | /**
|
1622 | * Invoke callback with x-domain error.
|
1623 | *
|
1624 | * @api private
|
1625 | */
|
1626 |
|
1627 | Request.prototype.crossDomainError = function(){
|
1628 | var err = new Error('Request has been terminated\nPossible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc.');
|
1629 | err.crossDomain = true;
|
1630 |
|
1631 | err.status = this.status;
|
1632 | err.method = this.method;
|
1633 | err.url = this.url;
|
1634 |
|
1635 | this.callback(err);
|
1636 | };
|
1637 |
|
1638 | // This only warns, because the request is still likely to work
|
1639 | Request.prototype.buffer = Request.prototype.ca = Request.prototype.agent = function(){
|
1640 | console.warn("This is not supported in browser version of superagent");
|
1641 | return this;
|
1642 | };
|
1643 |
|
1644 | // This throws, because it can't send/receive data as expected
|
1645 | Request.prototype.pipe = Request.prototype.write = function(){
|
1646 | throw Error("Streaming is not supported in browser version of superagent");
|
1647 | };
|
1648 |
|
1649 | /**
|
1650 | * Compose querystring to append to req.url
|
1651 | *
|
1652 | * @api private
|
1653 | */
|
1654 |
|
1655 | Request.prototype._appendQueryString = function(){
|
1656 | var query = this._query.join('&');
|
1657 | if (query) {
|
1658 | this.url += (this.url.indexOf('?') >= 0 ? '&' : '?') + query;
|
1659 | }
|
1660 |
|
1661 | if (this._sort) {
|
1662 | var index = this.url.indexOf('?');
|
1663 | if (index >= 0) {
|
1664 | var queryArr = this.url.substring(index + 1).split('&');
|
1665 | if (isFunction(this._sort)) {
|
1666 | queryArr.sort(this._sort);
|
1667 | } else {
|
1668 | queryArr.sort();
|
1669 | }
|
1670 | this.url = this.url.substring(0, index) + '?' + queryArr.join('&');
|
1671 | }
|
1672 | }
|
1673 | };
|
1674 |
|
1675 | /**
|
1676 | * Check if `obj` is a host object,
|
1677 | * we don't want to serialize these :)
|
1678 | *
|
1679 | * @param {Object} obj
|
1680 | * @return {Boolean}
|
1681 | * @api private
|
1682 | */
|
1683 | Request.prototype._isHost = function _isHost(obj) {
|
1684 | // Native objects stringify to [object File], [object Blob], [object FormData], etc.
|
1685 | return obj && 'object' === typeof obj && !Array.isArray(obj) && Object.prototype.toString.call(obj) !== '[object Object]';
|
1686 | }
|
1687 |
|
1688 | /**
|
1689 | * Initiate request, invoking callback `fn(res)`
|
1690 | * with an instanceof `Response`.
|
1691 | *
|
1692 | * @param {Function} fn
|
1693 | * @return {Request} for chaining
|
1694 | * @api public
|
1695 | */
|
1696 |
|
1697 | Request.prototype.end = function(fn){
|
1698 | if (this._endCalled) {
|
1699 | console.warn("Warning: .end() was called twice. This is not supported in superagent");
|
1700 | }
|
1701 | this._endCalled = true;
|
1702 |
|
1703 | // store callback
|
1704 | this._callback = fn || noop;
|
1705 |
|
1706 | // querystring
|
1707 | this._appendQueryString();
|
1708 |
|
1709 | return this._end();
|
1710 | };
|
1711 |
|
1712 | Request.prototype._end = function() {
|
1713 | var self = this;
|
1714 | var xhr = this.xhr = request.getXHR();
|
1715 | var data = this._formData || this._data;
|
1716 |
|
1717 | this._setTimeouts();
|
1718 |
|
1719 | // state change
|
1720 | xhr.onreadystatechange = function(){
|
1721 | var readyState = xhr.readyState;
|
1722 | if (readyState >= 2 && self._responseTimeoutTimer) {
|
1723 | clearTimeout(self._responseTimeoutTimer);
|
1724 | }
|
1725 | if (4 != readyState) {
|
1726 | return;
|
1727 | }
|
1728 |
|
1729 | // In IE9, reads to any property (e.g. status) off of an aborted XHR will
|
1730 | // result in the error "Could not complete the operation due to error c00c023f"
|
1731 | var status;
|
1732 | try { status = xhr.status } catch(e) { status = 0; }
|
1733 |
|
1734 | if (!status) {
|
1735 | if (self.timedout || self._aborted) return;
|
1736 | return self.crossDomainError();
|
1737 | }
|
1738 | self.emit('end');
|
1739 | };
|
1740 |
|
1741 | // progress
|
1742 | var handleProgress = function(direction, e) {
|
1743 | if (e.total > 0) {
|
1744 | e.percent = e.loaded / e.total * 100;
|
1745 | }
|
1746 | e.direction = direction;
|
1747 | self.emit('progress', e);
|
1748 | }
|
1749 | if (this.hasListeners('progress')) {
|
1750 | try {
|
1751 | xhr.onprogress = handleProgress.bind(null, 'download');
|
1752 | if (xhr.upload) {
|
1753 | xhr.upload.onprogress = handleProgress.bind(null, 'upload');
|
1754 | }
|
1755 | } catch(e) {
|
1756 | // Accessing xhr.upload fails in IE from a web worker, so just pretend it doesn't exist.
|
1757 | // Reported here:
|
1758 | // https://connect.microsoft.com/IE/feedback/details/837245/xmlhttprequest-upload-throws-invalid-argument-when-used-from-web-worker-context
|
1759 | }
|
1760 | }
|
1761 |
|
1762 | // initiate request
|
1763 | try {
|
1764 | if (this.username && this.password) {
|
1765 | xhr.open(this.method, this.url, true, this.username, this.password);
|
1766 | } else {
|
1767 | xhr.open(this.method, this.url, true);
|
1768 | }
|
1769 | } catch (err) {
|
1770 | // see #1149
|
1771 | return this.callback(err);
|
1772 | }
|
1773 |
|
1774 | // CORS
|
1775 | if (this._withCredentials) xhr.withCredentials = true;
|
1776 |
|
1777 | // body
|
1778 | if (!this._formData && 'GET' != this.method && 'HEAD' != this.method && 'string' != typeof data && !this._isHost(data)) {
|
1779 | // serialize stuff
|
1780 | var contentType = this._header['content-type'];
|
1781 | var serialize = this._serializer || request.serialize[contentType ? contentType.split(';')[0] : ''];
|
1782 | if (!serialize && isJSON(contentType)) {
|
1783 | serialize = request.serialize['application/json'];
|
1784 | }
|
1785 | if (serialize) data = serialize(data);
|
1786 | }
|
1787 |
|
1788 | // set header fields
|
1789 | for (var field in this.header) {
|
1790 | if (null == this.header[field]) continue;
|
1791 | xhr.setRequestHeader(field, this.header[field]);
|
1792 | }
|
1793 |
|
1794 | if (this._responseType) {
|
1795 | xhr.responseType = this._responseType;
|
1796 | }
|
1797 |
|
1798 | // send stuff
|
1799 | this.emit('request', this);
|
1800 |
|
1801 | // IE11 xhr.send(undefined) sends 'undefined' string as POST payload (instead of nothing)
|
1802 | // We need null here if data is undefined
|
1803 | xhr.send(typeof data !== 'undefined' ? data : null);
|
1804 | return this;
|
1805 | };
|
1806 |
|
1807 | /**
|
1808 | * GET `url` with optional callback `fn(res)`.
|
1809 | *
|
1810 | * @param {String} url
|
1811 | * @param {Mixed|Function} [data] or fn
|
1812 | * @param {Function} [fn]
|
1813 | * @return {Request}
|
1814 | * @api public
|
1815 | */
|
1816 |
|
1817 | request.get = function(url, data, fn){
|
1818 | var req = request('GET', url);
|
1819 | if ('function' == typeof data) fn = data, data = null;
|
1820 | if (data) req.query(data);
|
1821 | if (fn) req.end(fn);
|
1822 | return req;
|
1823 | };
|
1824 |
|
1825 | /**
|
1826 | * HEAD `url` with optional callback `fn(res)`.
|
1827 | *
|
1828 | * @param {String} url
|
1829 | * @param {Mixed|Function} [data] or fn
|
1830 | * @param {Function} [fn]
|
1831 | * @return {Request}
|
1832 | * @api public
|
1833 | */
|
1834 |
|
1835 | request.head = function(url, data, fn){
|
1836 | var req = request('HEAD', url);
|
1837 | if ('function' == typeof data) fn = data, data = null;
|
1838 | if (data) req.send(data);
|
1839 | if (fn) req.end(fn);
|
1840 | return req;
|
1841 | };
|
1842 |
|
1843 | /**
|
1844 | * OPTIONS query to `url` with optional callback `fn(res)`.
|
1845 | *
|
1846 | * @param {String} url
|
1847 | * @param {Mixed|Function} [data] or fn
|
1848 | * @param {Function} [fn]
|
1849 | * @return {Request}
|
1850 | * @api public
|
1851 | */
|
1852 |
|
1853 | request.options = function(url, data, fn){
|
1854 | var req = request('OPTIONS', url);
|
1855 | if ('function' == typeof data) fn = data, data = null;
|
1856 | if (data) req.send(data);
|
1857 | if (fn) req.end(fn);
|
1858 | return req;
|
1859 | };
|
1860 |
|
1861 | /**
|
1862 | * DELETE `url` with optional `data` and callback `fn(res)`.
|
1863 | *
|
1864 | * @param {String} url
|
1865 | * @param {Mixed} [data]
|
1866 | * @param {Function} [fn]
|
1867 | * @return {Request}
|
1868 | * @api public
|
1869 | */
|
1870 |
|
1871 | function del(url, data, fn){
|
1872 | var req = request('DELETE', url);
|
1873 | if ('function' == typeof data) fn = data, data = null;
|
1874 | if (data) req.send(data);
|
1875 | if (fn) req.end(fn);
|
1876 | return req;
|
1877 | };
|
1878 |
|
1879 | request['del'] = del;
|
1880 | request['delete'] = del;
|
1881 |
|
1882 | /**
|
1883 | * PATCH `url` with optional `data` and callback `fn(res)`.
|
1884 | *
|
1885 | * @param {String} url
|
1886 | * @param {Mixed} [data]
|
1887 | * @param {Function} [fn]
|
1888 | * @return {Request}
|
1889 | * @api public
|
1890 | */
|
1891 |
|
1892 | request.patch = function(url, data, fn){
|
1893 | var req = request('PATCH', url);
|
1894 | if ('function' == typeof data) fn = data, data = null;
|
1895 | if (data) req.send(data);
|
1896 | if (fn) req.end(fn);
|
1897 | return req;
|
1898 | };
|
1899 |
|
1900 | /**
|
1901 | * POST `url` with optional `data` and callback `fn(res)`.
|
1902 | *
|
1903 | * @param {String} url
|
1904 | * @param {Mixed} [data]
|
1905 | * @param {Function} [fn]
|
1906 | * @return {Request}
|
1907 | * @api public
|
1908 | */
|
1909 |
|
1910 | request.post = function(url, data, fn){
|
1911 | var req = request('POST', url);
|
1912 | if ('function' == typeof data) fn = data, data = null;
|
1913 | if (data) req.send(data);
|
1914 | if (fn) req.end(fn);
|
1915 | return req;
|
1916 | };
|
1917 |
|
1918 | /**
|
1919 | * PUT `url` with optional `data` and callback `fn(res)`.
|
1920 | *
|
1921 | * @param {String} url
|
1922 | * @param {Mixed|Function} [data] or fn
|
1923 | * @param {Function} [fn]
|
1924 | * @return {Request}
|
1925 | * @api public
|
1926 | */
|
1927 |
|
1928 | request.put = function(url, data, fn){
|
1929 | var req = request('PUT', url);
|
1930 | if ('function' == typeof data) fn = data, data = null;
|
1931 | if (data) req.send(data);
|
1932 | if (fn) req.end(fn);
|
1933 | return req;
|
1934 | };
|
1935 |
|
1936 | },{"./is-function":1,"./is-object":2,"./request-base":3,"./response-base":4,"./should-retry":5,"emitter":7}]},{},[8])(8)
|
1937 | }); |
\ | No newline at end of file |