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(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
|
2 | "use strict";
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | if (typeof module !== 'undefined') {
|
8 | module.exports = Emitter;
|
9 | }
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | function Emitter(obj) {
|
18 | if (obj) return mixin(obj);
|
19 | }
|
20 |
|
21 | ;
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | function mixin(obj) {
|
31 | for (var key in Emitter.prototype) {
|
32 | obj[key] = Emitter.prototype[key];
|
33 | }
|
34 |
|
35 | return obj;
|
36 | }
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | Emitter.prototype.on = Emitter.prototype.addEventListener = function (event, fn) {
|
48 | this._callbacks = this._callbacks || {};
|
49 | (this._callbacks['$' + event] = this._callbacks['$' + event] || []).push(fn);
|
50 | return this;
|
51 | };
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | Emitter.prototype.once = function (event, fn) {
|
64 | function on() {
|
65 | this.off(event, on);
|
66 | fn.apply(this, arguments);
|
67 | }
|
68 |
|
69 | on.fn = fn;
|
70 | this.on(event, on);
|
71 | return this;
|
72 | };
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 | Emitter.prototype.off = Emitter.prototype.removeListener = Emitter.prototype.removeAllListeners = Emitter.prototype.removeEventListener = function (event, fn) {
|
85 | this._callbacks = this._callbacks || {};
|
86 |
|
87 | if (0 == arguments.length) {
|
88 | this._callbacks = {};
|
89 | return this;
|
90 | }
|
91 |
|
92 |
|
93 | var callbacks = this._callbacks['$' + event];
|
94 | if (!callbacks) return this;
|
95 |
|
96 | if (1 == arguments.length) {
|
97 | delete this._callbacks['$' + event];
|
98 | return this;
|
99 | }
|
100 |
|
101 |
|
102 | var cb;
|
103 |
|
104 | for (var i = 0; i < callbacks.length; i++) {
|
105 | cb = callbacks[i];
|
106 |
|
107 | if (cb === fn || cb.fn === fn) {
|
108 | callbacks.splice(i, 1);
|
109 | break;
|
110 | }
|
111 | }
|
112 |
|
113 |
|
114 |
|
115 | if (callbacks.length === 0) {
|
116 | delete this._callbacks['$' + event];
|
117 | }
|
118 |
|
119 | return this;
|
120 | };
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | Emitter.prototype.emit = function (event) {
|
131 | this._callbacks = this._callbacks || {};
|
132 | var args = new Array(arguments.length - 1),
|
133 | callbacks = this._callbacks['$' + event];
|
134 |
|
135 | for (var i = 1; i < arguments.length; i++) {
|
136 | args[i - 1] = arguments[i];
|
137 | }
|
138 |
|
139 | if (callbacks) {
|
140 | callbacks = callbacks.slice(0);
|
141 |
|
142 | for (var i = 0, len = callbacks.length; i < len; ++i) {
|
143 | callbacks[i].apply(this, args);
|
144 | }
|
145 | }
|
146 |
|
147 | return this;
|
148 | };
|
149 |
|
150 |
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | Emitter.prototype.listeners = function (event) {
|
159 | this._callbacks = this._callbacks || {};
|
160 | return this._callbacks['$' + event] || [];
|
161 | };
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 | Emitter.prototype.hasListeners = function (event) {
|
172 | return !!this.listeners(event).length;
|
173 | };
|
174 |
|
175 | },{}],2:[function(require,module,exports){
|
176 | "use strict";
|
177 |
|
178 | function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
179 |
|
180 | module.exports = stringify;
|
181 | stringify.default = stringify;
|
182 | stringify.stable = deterministicStringify;
|
183 | stringify.stableStringify = deterministicStringify;
|
184 | var arr = [];
|
185 | var replacerStack = [];
|
186 |
|
187 | function stringify(obj, replacer, spacer) {
|
188 | decirc(obj, '', [], undefined);
|
189 | var res;
|
190 |
|
191 | if (replacerStack.length === 0) {
|
192 | res = JSON.stringify(obj, replacer, spacer);
|
193 | } else {
|
194 | res = JSON.stringify(obj, replaceGetterValues(replacer), spacer);
|
195 | }
|
196 |
|
197 | while (arr.length !== 0) {
|
198 | var part = arr.pop();
|
199 |
|
200 | if (part.length === 4) {
|
201 | Object.defineProperty(part[0], part[1], part[3]);
|
202 | } else {
|
203 | part[0][part[1]] = part[2];
|
204 | }
|
205 | }
|
206 |
|
207 | return res;
|
208 | }
|
209 |
|
210 | function decirc(val, k, stack, parent) {
|
211 | var i;
|
212 |
|
213 | if (_typeof(val) === 'object' && val !== null) {
|
214 | for (i = 0; i < stack.length; i++) {
|
215 | if (stack[i] === val) {
|
216 | var propertyDescriptor = Object.getOwnPropertyDescriptor(parent, k);
|
217 |
|
218 | if (propertyDescriptor.get !== undefined) {
|
219 | if (propertyDescriptor.configurable) {
|
220 | Object.defineProperty(parent, k, {
|
221 | value: '[Circular]'
|
222 | });
|
223 | arr.push([parent, k, val, propertyDescriptor]);
|
224 | } else {
|
225 | replacerStack.push([val, k]);
|
226 | }
|
227 | } else {
|
228 | parent[k] = '[Circular]';
|
229 | arr.push([parent, k, val]);
|
230 | }
|
231 |
|
232 | return;
|
233 | }
|
234 | }
|
235 |
|
236 | stack.push(val);
|
237 |
|
238 | if (Array.isArray(val)) {
|
239 | for (i = 0; i < val.length; i++) {
|
240 | decirc(val[i], i, stack, val);
|
241 | }
|
242 | } else {
|
243 | var keys = Object.keys(val);
|
244 |
|
245 | for (i = 0; i < keys.length; i++) {
|
246 | var key = keys[i];
|
247 | decirc(val[key], key, stack, val);
|
248 | }
|
249 | }
|
250 |
|
251 | stack.pop();
|
252 | }
|
253 | }
|
254 |
|
255 |
|
256 | function compareFunction(a, b) {
|
257 | if (a < b) {
|
258 | return -1;
|
259 | }
|
260 |
|
261 | if (a > b) {
|
262 | return 1;
|
263 | }
|
264 |
|
265 | return 0;
|
266 | }
|
267 |
|
268 | function deterministicStringify(obj, replacer, spacer) {
|
269 | var tmp = deterministicDecirc(obj, '', [], undefined) || obj;
|
270 | var res;
|
271 |
|
272 | if (replacerStack.length === 0) {
|
273 | res = JSON.stringify(tmp, replacer, spacer);
|
274 | } else {
|
275 | res = JSON.stringify(tmp, replaceGetterValues(replacer), spacer);
|
276 | }
|
277 |
|
278 | while (arr.length !== 0) {
|
279 | var part = arr.pop();
|
280 |
|
281 | if (part.length === 4) {
|
282 | Object.defineProperty(part[0], part[1], part[3]);
|
283 | } else {
|
284 | part[0][part[1]] = part[2];
|
285 | }
|
286 | }
|
287 |
|
288 | return res;
|
289 | }
|
290 |
|
291 | function deterministicDecirc(val, k, stack, parent) {
|
292 | var i;
|
293 |
|
294 | if (_typeof(val) === 'object' && val !== null) {
|
295 | for (i = 0; i < stack.length; i++) {
|
296 | if (stack[i] === val) {
|
297 | var propertyDescriptor = Object.getOwnPropertyDescriptor(parent, k);
|
298 |
|
299 | if (propertyDescriptor.get !== undefined) {
|
300 | if (propertyDescriptor.configurable) {
|
301 | Object.defineProperty(parent, k, {
|
302 | value: '[Circular]'
|
303 | });
|
304 | arr.push([parent, k, val, propertyDescriptor]);
|
305 | } else {
|
306 | replacerStack.push([val, k]);
|
307 | }
|
308 | } else {
|
309 | parent[k] = '[Circular]';
|
310 | arr.push([parent, k, val]);
|
311 | }
|
312 |
|
313 | return;
|
314 | }
|
315 | }
|
316 |
|
317 | if (typeof val.toJSON === 'function') {
|
318 | return;
|
319 | }
|
320 |
|
321 | stack.push(val);
|
322 |
|
323 | if (Array.isArray(val)) {
|
324 | for (i = 0; i < val.length; i++) {
|
325 | deterministicDecirc(val[i], i, stack, val);
|
326 | }
|
327 | } else {
|
328 |
|
329 | var tmp = {};
|
330 | var keys = Object.keys(val).sort(compareFunction);
|
331 |
|
332 | for (i = 0; i < keys.length; i++) {
|
333 | var key = keys[i];
|
334 | deterministicDecirc(val[key], key, stack, val);
|
335 | tmp[key] = val[key];
|
336 | }
|
337 |
|
338 | if (parent !== undefined) {
|
339 | arr.push([parent, k, val]);
|
340 | parent[k] = tmp;
|
341 | } else {
|
342 | return tmp;
|
343 | }
|
344 | }
|
345 |
|
346 | stack.pop();
|
347 | }
|
348 | }
|
349 |
|
350 |
|
351 |
|
352 | function replaceGetterValues(replacer) {
|
353 | replacer = replacer !== undefined ? replacer : function (k, v) {
|
354 | return v;
|
355 | };
|
356 | return function (key, val) {
|
357 | if (replacerStack.length > 0) {
|
358 | for (var i = 0; i < replacerStack.length; i++) {
|
359 | var part = replacerStack[i];
|
360 |
|
361 | if (part[1] === key && part[0] === val) {
|
362 | val = '[Circular]';
|
363 | replacerStack.splice(i, 1);
|
364 | break;
|
365 | }
|
366 | }
|
367 | }
|
368 |
|
369 | return replacer.call(this, key, val);
|
370 | };
|
371 | }
|
372 |
|
373 | },{}],3:[function(require,module,exports){
|
374 | "use strict";
|
375 |
|
376 | function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }
|
377 |
|
378 | function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }
|
379 |
|
380 | function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }
|
381 |
|
382 | function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }
|
383 |
|
384 | function Agent() {
|
385 | this._defaults = [];
|
386 | }
|
387 |
|
388 | ['use', 'on', 'once', 'set', 'query', 'type', 'accept', 'auth', 'withCredentials', 'sortQuery', 'retry', 'ok', 'redirects', 'timeout', 'buffer', 'serialize', 'parse', 'ca', 'key', 'pfx', 'cert', 'disableTLSCerts'].forEach(function (fn) {
|
389 |
|
390 | Agent.prototype[fn] = function () {
|
391 | for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
392 | args[_key] = arguments[_key];
|
393 | }
|
394 |
|
395 | this._defaults.push({
|
396 | fn: fn,
|
397 | args: args
|
398 | });
|
399 |
|
400 | return this;
|
401 | };
|
402 | });
|
403 |
|
404 | Agent.prototype._setDefaults = function (req) {
|
405 | this._defaults.forEach(function (def) {
|
406 | req[def.fn].apply(req, _toConsumableArray(def.args));
|
407 | });
|
408 | };
|
409 |
|
410 | module.exports = Agent;
|
411 |
|
412 | },{}],4:[function(require,module,exports){
|
413 | "use strict";
|
414 |
|
415 | function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 | function isObject(obj) {
|
425 | return obj !== null && _typeof(obj) === 'object';
|
426 | }
|
427 |
|
428 | module.exports = isObject;
|
429 |
|
430 | },{}],5:[function(require,module,exports){
|
431 | "use strict";
|
432 |
|
433 | function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
434 |
|
435 |
|
436 |
|
437 |
|
438 | var root;
|
439 |
|
440 | if (typeof window !== 'undefined') {
|
441 |
|
442 | root = window;
|
443 | } else if (typeof self === 'undefined') {
|
444 |
|
445 | console.warn('Using browser-only version of superagent in non-browser environment');
|
446 | root = void 0;
|
447 | } else {
|
448 |
|
449 | root = self;
|
450 | }
|
451 |
|
452 | var Emitter = require('component-emitter');
|
453 |
|
454 | var safeStringify = require('fast-safe-stringify');
|
455 |
|
456 | var RequestBase = require('./request-base');
|
457 |
|
458 | var isObject = require('./is-object');
|
459 |
|
460 | var ResponseBase = require('./response-base');
|
461 |
|
462 | var Agent = require('./agent-base');
|
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 | function noop() {}
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 | module.exports = function (method, url) {
|
475 |
|
476 | if (typeof url === 'function') {
|
477 | return new exports.Request('GET', method).end(url);
|
478 | }
|
479 |
|
480 |
|
481 | if (arguments.length === 1) {
|
482 | return new exports.Request('GET', method);
|
483 | }
|
484 |
|
485 | return new exports.Request(method, url);
|
486 | };
|
487 |
|
488 | exports = module.exports;
|
489 | var request = exports;
|
490 | exports.Request = Request;
|
491 |
|
492 |
|
493 |
|
494 |
|
495 | request.getXHR = function () {
|
496 | if (root.XMLHttpRequest && (!root.location || root.location.protocol !== 'file:' || !root.ActiveXObject)) {
|
497 | return new XMLHttpRequest();
|
498 | }
|
499 |
|
500 | try {
|
501 | return new ActiveXObject('Microsoft.XMLHTTP');
|
502 | } catch (_unused) {}
|
503 |
|
504 | try {
|
505 | return new ActiveXObject('Msxml2.XMLHTTP.6.0');
|
506 | } catch (_unused2) {}
|
507 |
|
508 | try {
|
509 | return new ActiveXObject('Msxml2.XMLHTTP.3.0');
|
510 | } catch (_unused3) {}
|
511 |
|
512 | try {
|
513 | return new ActiveXObject('Msxml2.XMLHTTP');
|
514 | } catch (_unused4) {}
|
515 |
|
516 | throw new Error('Browser-only version of superagent could not find XHR');
|
517 | };
|
518 |
|
519 |
|
520 |
|
521 |
|
522 |
|
523 |
|
524 |
|
525 |
|
526 |
|
527 | var trim = ''.trim ? function (s) {
|
528 | return s.trim();
|
529 | } : function (s) {
|
530 | return s.replace(/(^\s*|\s*$)/g, '');
|
531 | };
|
532 |
|
533 |
|
534 |
|
535 |
|
536 |
|
537 |
|
538 |
|
539 |
|
540 | function serialize(obj) {
|
541 | if (!isObject(obj)) return obj;
|
542 | var pairs = [];
|
543 |
|
544 | for (var key in obj) {
|
545 | if (Object.prototype.hasOwnProperty.call(obj, key)) pushEncodedKeyValuePair(pairs, key, obj[key]);
|
546 | }
|
547 |
|
548 | return pairs.join('&');
|
549 | }
|
550 |
|
551 |
|
552 |
|
553 |
|
554 |
|
555 |
|
556 |
|
557 |
|
558 |
|
559 |
|
560 | function pushEncodedKeyValuePair(pairs, key, val) {
|
561 | if (val === undefined) return;
|
562 |
|
563 | if (val === null) {
|
564 | pairs.push(encodeURI(key));
|
565 | return;
|
566 | }
|
567 |
|
568 | if (Array.isArray(val)) {
|
569 | val.forEach(function (v) {
|
570 | pushEncodedKeyValuePair(pairs, key, v);
|
571 | });
|
572 | } else if (isObject(val)) {
|
573 | for (var subkey in val) {
|
574 | if (Object.prototype.hasOwnProperty.call(val, subkey)) pushEncodedKeyValuePair(pairs, "".concat(key, "[").concat(subkey, "]"), val[subkey]);
|
575 | }
|
576 | } else {
|
577 | pairs.push(encodeURI(key) + '=' + encodeURIComponent(val));
|
578 | }
|
579 | }
|
580 |
|
581 |
|
582 |
|
583 |
|
584 |
|
585 | request.serializeObject = serialize;
|
586 |
|
587 |
|
588 |
|
589 |
|
590 |
|
591 |
|
592 |
|
593 |
|
594 | function parseString(str) {
|
595 | var obj = {};
|
596 | var pairs = str.split('&');
|
597 | var pair;
|
598 | var pos;
|
599 |
|
600 | for (var i = 0, len = pairs.length; i < len; ++i) {
|
601 | pair = pairs[i];
|
602 | pos = pair.indexOf('=');
|
603 |
|
604 | if (pos === -1) {
|
605 | obj[decodeURIComponent(pair)] = '';
|
606 | } else {
|
607 | obj[decodeURIComponent(pair.slice(0, pos))] = decodeURIComponent(pair.slice(pos + 1));
|
608 | }
|
609 | }
|
610 |
|
611 | return obj;
|
612 | }
|
613 |
|
614 |
|
615 |
|
616 |
|
617 |
|
618 | request.parseString = parseString;
|
619 |
|
620 |
|
621 |
|
622 |
|
623 |
|
624 |
|
625 |
|
626 | request.types = {
|
627 | html: 'text/html',
|
628 | json: 'application/json',
|
629 | xml: 'text/xml',
|
630 | urlencoded: 'application/x-www-form-urlencoded',
|
631 | form: 'application/x-www-form-urlencoded',
|
632 | 'form-data': 'application/x-www-form-urlencoded'
|
633 | };
|
634 |
|
635 |
|
636 |
|
637 |
|
638 |
|
639 |
|
640 |
|
641 |
|
642 |
|
643 | request.serialize = {
|
644 | 'application/x-www-form-urlencoded': serialize,
|
645 | 'application/json': safeStringify
|
646 | };
|
647 |
|
648 |
|
649 |
|
650 |
|
651 |
|
652 |
|
653 |
|
654 |
|
655 |
|
656 | request.parse = {
|
657 | 'application/x-www-form-urlencoded': parseString,
|
658 | 'application/json': JSON.parse
|
659 | };
|
660 |
|
661 |
|
662 |
|
663 |
|
664 |
|
665 |
|
666 |
|
667 |
|
668 |
|
669 | function parseHeader(str) {
|
670 | var lines = str.split(/\r?\n/);
|
671 | var fields = {};
|
672 | var index;
|
673 | var line;
|
674 | var field;
|
675 | var val;
|
676 |
|
677 | for (var i = 0, len = lines.length; i < len; ++i) {
|
678 | line = lines[i];
|
679 | index = line.indexOf(':');
|
680 |
|
681 | if (index === -1) {
|
682 |
|
683 | continue;
|
684 | }
|
685 |
|
686 | field = line.slice(0, index).toLowerCase();
|
687 | val = trim(line.slice(index + 1));
|
688 | fields[field] = val;
|
689 | }
|
690 |
|
691 | return fields;
|
692 | }
|
693 |
|
694 |
|
695 |
|
696 |
|
697 |
|
698 |
|
699 |
|
700 |
|
701 |
|
702 | function isJSON(mime) {
|
703 |
|
704 |
|
705 | return /[/+]json($|[^-\w])/.test(mime);
|
706 | }
|
707 |
|
708 |
|
709 |
|
710 |
|
711 |
|
712 |
|
713 |
|
714 |
|
715 |
|
716 |
|
717 |
|
718 |
|
719 |
|
720 |
|
721 |
|
722 |
|
723 |
|
724 |
|
725 |
|
726 |
|
727 |
|
728 |
|
729 |
|
730 |
|
731 |
|
732 |
|
733 |
|
734 |
|
735 |
|
736 |
|
737 |
|
738 |
|
739 |
|
740 |
|
741 |
|
742 |
|
743 |
|
744 |
|
745 |
|
746 |
|
747 |
|
748 |
|
749 |
|
750 |
|
751 |
|
752 |
|
753 |
|
754 | function Response(req) {
|
755 | this.req = req;
|
756 | this.xhr = this.req.xhr;
|
757 |
|
758 | this.text = this.req.method !== 'HEAD' && (this.xhr.responseType === '' || this.xhr.responseType === 'text') || typeof this.xhr.responseType === 'undefined' ? this.xhr.responseText : null;
|
759 | this.statusText = this.req.xhr.statusText;
|
760 | var status = this.xhr.status;
|
761 |
|
762 | if (status === 1223) {
|
763 | status = 204;
|
764 | }
|
765 |
|
766 | this._setStatusProperties(status);
|
767 |
|
768 | this.headers = parseHeader(this.xhr.getAllResponseHeaders());
|
769 | this.header = this.headers;
|
770 |
|
771 |
|
772 |
|
773 | this.header['content-type'] = this.xhr.getResponseHeader('content-type');
|
774 |
|
775 | this._setHeaderProperties(this.header);
|
776 |
|
777 | if (this.text === null && req._responseType) {
|
778 | this.body = this.xhr.response;
|
779 | } else {
|
780 | this.body = this.req.method === 'HEAD' ? null : this._parseBody(this.text ? this.text : this.xhr.response);
|
781 | }
|
782 | }
|
783 |
|
784 |
|
785 | ResponseBase(Response.prototype);
|
786 |
|
787 |
|
788 |
|
789 |
|
790 |
|
791 |
|
792 |
|
793 |
|
794 |
|
795 |
|
796 |
|
797 | Response.prototype._parseBody = function (str) {
|
798 | var parse = request.parse[this.type];
|
799 |
|
800 | if (this.req._parser) {
|
801 | return this.req._parser(this, str);
|
802 | }
|
803 |
|
804 | if (!parse && isJSON(this.type)) {
|
805 | parse = request.parse['application/json'];
|
806 | }
|
807 |
|
808 | return parse && str && (str.length > 0 || str instanceof Object) ? parse(str) : null;
|
809 | };
|
810 |
|
811 |
|
812 |
|
813 |
|
814 |
|
815 |
|
816 |
|
817 |
|
818 | Response.prototype.toError = function () {
|
819 | var req = this.req;
|
820 | var method = req.method;
|
821 | var url = req.url;
|
822 | var msg = "cannot ".concat(method, " ").concat(url, " (").concat(this.status, ")");
|
823 | var err = new Error(msg);
|
824 | err.status = this.status;
|
825 | err.method = method;
|
826 | err.url = url;
|
827 | return err;
|
828 | };
|
829 |
|
830 |
|
831 |
|
832 |
|
833 |
|
834 | request.Response = Response;
|
835 |
|
836 |
|
837 |
|
838 |
|
839 |
|
840 |
|
841 |
|
842 |
|
843 | function Request(method, url) {
|
844 | var self = this;
|
845 | this._query = this._query || [];
|
846 | this.method = method;
|
847 | this.url = url;
|
848 | this.header = {};
|
849 |
|
850 | this._header = {};
|
851 |
|
852 | this.on('end', function () {
|
853 | var err = null;
|
854 | var res = null;
|
855 |
|
856 | try {
|
857 | res = new Response(self);
|
858 | } catch (err_) {
|
859 | err = new Error('Parser is unable to parse the response');
|
860 | err.parse = true;
|
861 | err.original = err_;
|
862 |
|
863 | if (self.xhr) {
|
864 |
|
865 | err.rawResponse = typeof self.xhr.responseType === 'undefined' ? self.xhr.responseText : self.xhr.response;
|
866 |
|
867 | err.status = self.xhr.status ? self.xhr.status : null;
|
868 | err.statusCode = err.status;
|
869 | } else {
|
870 | err.rawResponse = null;
|
871 | err.status = null;
|
872 | }
|
873 |
|
874 | return self.callback(err);
|
875 | }
|
876 |
|
877 | self.emit('response', res);
|
878 | var new_err;
|
879 |
|
880 | try {
|
881 | if (!self._isResponseOK(res)) {
|
882 | new_err = new Error(res.statusText || res.text || 'Unsuccessful HTTP response');
|
883 | }
|
884 | } catch (err_) {
|
885 | new_err = err_;
|
886 | }
|
887 |
|
888 |
|
889 | if (new_err) {
|
890 | new_err.original = err;
|
891 | new_err.response = res;
|
892 | new_err.status = res.status;
|
893 | self.callback(new_err, res);
|
894 | } else {
|
895 | self.callback(null, res);
|
896 | }
|
897 | });
|
898 | }
|
899 |
|
900 |
|
901 |
|
902 |
|
903 |
|
904 |
|
905 | Emitter(Request.prototype);
|
906 |
|
907 | RequestBase(Request.prototype);
|
908 |
|
909 |
|
910 |
|
911 |
|
912 |
|
913 |
|
914 |
|
915 |
|
916 |
|
917 |
|
918 |
|
919 |
|
920 |
|
921 |
|
922 |
|
923 |
|
924 |
|
925 |
|
926 |
|
927 |
|
928 |
|
929 |
|
930 | Request.prototype.type = function (type) {
|
931 | this.set('Content-Type', request.types[type] || type);
|
932 | return this;
|
933 | };
|
934 |
|
935 |
|
936 |
|
937 |
|
938 |
|
939 |
|
940 |
|
941 |
|
942 |
|
943 |
|
944 |
|
945 |
|
946 |
|
947 |
|
948 |
|
949 |
|
950 |
|
951 |
|
952 |
|
953 |
|
954 |
|
955 | Request.prototype.accept = function (type) {
|
956 | this.set('Accept', request.types[type] || type);
|
957 | return this;
|
958 | };
|
959 |
|
960 |
|
961 |
|
962 |
|
963 |
|
964 |
|
965 |
|
966 |
|
967 |
|
968 |
|
969 |
|
970 | Request.prototype.auth = function (user, pass, options) {
|
971 | if (arguments.length === 1) pass = '';
|
972 |
|
973 | if (_typeof(pass) === 'object' && pass !== null) {
|
974 |
|
975 | options = pass;
|
976 | pass = '';
|
977 | }
|
978 |
|
979 | if (!options) {
|
980 | options = {
|
981 | type: typeof btoa === 'function' ? 'basic' : 'auto'
|
982 | };
|
983 | }
|
984 |
|
985 | var encoder = function encoder(string) {
|
986 | if (typeof btoa === 'function') {
|
987 | return btoa(string);
|
988 | }
|
989 |
|
990 | throw new Error('Cannot use basic auth, btoa is not a function');
|
991 | };
|
992 |
|
993 | return this._auth(user, pass, options, encoder);
|
994 | };
|
995 |
|
996 |
|
997 |
|
998 |
|
999 |
|
1000 |
|
1001 |
|
1002 |
|
1003 |
|
1004 |
|
1005 |
|
1006 |
|
1007 |
|
1008 |
|
1009 |
|
1010 | Request.prototype.query = function (val) {
|
1011 | if (typeof val !== 'string') val = serialize(val);
|
1012 | if (val) this._query.push(val);
|
1013 | return this;
|
1014 | };
|
1015 |
|
1016 |
|
1017 |
|
1018 |
|
1019 |
|
1020 |
|
1021 |
|
1022 |
|
1023 |
|
1024 |
|
1025 |
|
1026 |
|
1027 |
|
1028 |
|
1029 |
|
1030 |
|
1031 |
|
1032 |
|
1033 | Request.prototype.attach = function (field, file, options) {
|
1034 | if (file) {
|
1035 | if (this._data) {
|
1036 | throw new Error("superagent can't mix .send() and .attach()");
|
1037 | }
|
1038 |
|
1039 | this._getFormData().append(field, file, options || file.name);
|
1040 | }
|
1041 |
|
1042 | return this;
|
1043 | };
|
1044 |
|
1045 | Request.prototype._getFormData = function () {
|
1046 | if (!this._formData) {
|
1047 | this._formData = new root.FormData();
|
1048 | }
|
1049 |
|
1050 | return this._formData;
|
1051 | };
|
1052 |
|
1053 |
|
1054 |
|
1055 |
|
1056 |
|
1057 |
|
1058 |
|
1059 |
|
1060 |
|
1061 |
|
1062 | Request.prototype.callback = function (err, res) {
|
1063 | if (this._shouldRetry(err, res)) {
|
1064 | return this._retry();
|
1065 | }
|
1066 |
|
1067 | var fn = this._callback;
|
1068 | this.clearTimeout();
|
1069 |
|
1070 | if (err) {
|
1071 | if (this._maxRetries) err.retries = this._retries - 1;
|
1072 | this.emit('error', err);
|
1073 | }
|
1074 |
|
1075 | fn(err, res);
|
1076 | };
|
1077 |
|
1078 |
|
1079 |
|
1080 |
|
1081 |
|
1082 |
|
1083 |
|
1084 | Request.prototype.crossDomainError = function () {
|
1085 | 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.');
|
1086 | err.crossDomain = true;
|
1087 | err.status = this.status;
|
1088 | err.method = this.method;
|
1089 | err.url = this.url;
|
1090 | this.callback(err);
|
1091 | };
|
1092 |
|
1093 |
|
1094 | Request.prototype.agent = function () {
|
1095 | console.warn('This is not supported in browser version of superagent');
|
1096 | return this;
|
1097 | };
|
1098 |
|
1099 | Request.prototype.ca = Request.prototype.agent;
|
1100 | Request.prototype.buffer = Request.prototype.ca;
|
1101 |
|
1102 | Request.prototype.write = function () {
|
1103 | throw new Error('Streaming is not supported in browser version of superagent');
|
1104 | };
|
1105 |
|
1106 | Request.prototype.pipe = Request.prototype.write;
|
1107 |
|
1108 |
|
1109 |
|
1110 |
|
1111 |
|
1112 |
|
1113 |
|
1114 |
|
1115 |
|
1116 | Request.prototype._isHost = function (obj) {
|
1117 |
|
1118 | return obj && _typeof(obj) === 'object' && !Array.isArray(obj) && Object.prototype.toString.call(obj) !== '[object Object]';
|
1119 | };
|
1120 |
|
1121 |
|
1122 |
|
1123 |
|
1124 |
|
1125 |
|
1126 |
|
1127 |
|
1128 |
|
1129 |
|
1130 | Request.prototype.end = function (fn) {
|
1131 | if (this._endCalled) {
|
1132 | console.warn('Warning: .end() was called twice. This is not supported in superagent');
|
1133 | }
|
1134 |
|
1135 | this._endCalled = true;
|
1136 |
|
1137 | this._callback = fn || noop;
|
1138 |
|
1139 | this._finalizeQueryString();
|
1140 |
|
1141 | this._end();
|
1142 | };
|
1143 |
|
1144 | Request.prototype._setUploadTimeout = function () {
|
1145 | var self = this;
|
1146 |
|
1147 | if (this._uploadTimeout && !this._uploadTimeoutTimer) {
|
1148 | this._uploadTimeoutTimer = setTimeout(function () {
|
1149 | self._timeoutError('Upload timeout of ', self._uploadTimeout, 'ETIMEDOUT');
|
1150 | }, this._uploadTimeout);
|
1151 | }
|
1152 | };
|
1153 |
|
1154 |
|
1155 | Request.prototype._end = function () {
|
1156 | if (this._aborted) return this.callback(new Error('The request has been aborted even before .end() was called'));
|
1157 | var self = this;
|
1158 | this.xhr = request.getXHR();
|
1159 | var xhr = this.xhr;
|
1160 | var data = this._formData || this._data;
|
1161 |
|
1162 | this._setTimeouts();
|
1163 |
|
1164 |
|
1165 | xhr.onreadystatechange = function () {
|
1166 | var readyState = xhr.readyState;
|
1167 |
|
1168 | if (readyState >= 2 && self._responseTimeoutTimer) {
|
1169 | clearTimeout(self._responseTimeoutTimer);
|
1170 | }
|
1171 |
|
1172 | if (readyState !== 4) {
|
1173 | return;
|
1174 | }
|
1175 |
|
1176 |
|
1177 |
|
1178 | var status;
|
1179 |
|
1180 | try {
|
1181 | status = xhr.status;
|
1182 | } catch (_unused5) {
|
1183 | status = 0;
|
1184 | }
|
1185 |
|
1186 | if (!status) {
|
1187 | if (self.timedout || self._aborted) return;
|
1188 | return self.crossDomainError();
|
1189 | }
|
1190 |
|
1191 | self.emit('end');
|
1192 | };
|
1193 |
|
1194 |
|
1195 | var handleProgress = function handleProgress(direction, e) {
|
1196 | if (e.total > 0) {
|
1197 | e.percent = e.loaded / e.total * 100;
|
1198 |
|
1199 | if (e.percent === 100) {
|
1200 | clearTimeout(self._uploadTimeoutTimer);
|
1201 | }
|
1202 | }
|
1203 |
|
1204 | e.direction = direction;
|
1205 | self.emit('progress', e);
|
1206 | };
|
1207 |
|
1208 | if (this.hasListeners('progress')) {
|
1209 | try {
|
1210 | xhr.addEventListener('progress', handleProgress.bind(null, 'download'));
|
1211 |
|
1212 | if (xhr.upload) {
|
1213 | xhr.upload.addEventListener('progress', handleProgress.bind(null, 'upload'));
|
1214 | }
|
1215 | } catch (_unused6) {
|
1216 |
|
1217 |
|
1218 | }
|
1219 | }
|
1220 |
|
1221 | if (xhr.upload) {
|
1222 | this._setUploadTimeout();
|
1223 | }
|
1224 |
|
1225 |
|
1226 | try {
|
1227 | if (this.username && this.password) {
|
1228 | xhr.open(this.method, this.url, true, this.username, this.password);
|
1229 | } else {
|
1230 | xhr.open(this.method, this.url, true);
|
1231 | }
|
1232 | } catch (err) {
|
1233 |
|
1234 | return this.callback(err);
|
1235 | }
|
1236 |
|
1237 |
|
1238 | if (this._withCredentials) xhr.withCredentials = true;
|
1239 |
|
1240 | if (!this._formData && this.method !== 'GET' && this.method !== 'HEAD' && typeof data !== 'string' && !this._isHost(data)) {
|
1241 |
|
1242 | var contentType = this._header['content-type'];
|
1243 |
|
1244 | var _serialize = this._serializer || request.serialize[contentType ? contentType.split(';')[0] : ''];
|
1245 |
|
1246 | if (!_serialize && isJSON(contentType)) {
|
1247 | _serialize = request.serialize['application/json'];
|
1248 | }
|
1249 |
|
1250 | if (_serialize) data = _serialize(data);
|
1251 | }
|
1252 |
|
1253 |
|
1254 | for (var field in this.header) {
|
1255 | if (this.header[field] === null) continue;
|
1256 | if (Object.prototype.hasOwnProperty.call(this.header, field)) xhr.setRequestHeader(field, this.header[field]);
|
1257 | }
|
1258 |
|
1259 | if (this._responseType) {
|
1260 | xhr.responseType = this._responseType;
|
1261 | }
|
1262 |
|
1263 |
|
1264 | this.emit('request', this);
|
1265 |
|
1266 |
|
1267 | xhr.send(typeof data === 'undefined' ? null : data);
|
1268 | };
|
1269 |
|
1270 | request.agent = function () {
|
1271 | return new Agent();
|
1272 | };
|
1273 |
|
1274 | ['GET', 'POST', 'OPTIONS', 'PATCH', 'PUT', 'DELETE'].forEach(function (method) {
|
1275 | Agent.prototype[method.toLowerCase()] = function (url, fn) {
|
1276 | var req = new request.Request(method, url);
|
1277 |
|
1278 | this._setDefaults(req);
|
1279 |
|
1280 | if (fn) {
|
1281 | req.end(fn);
|
1282 | }
|
1283 |
|
1284 | return req;
|
1285 | };
|
1286 | });
|
1287 | Agent.prototype.del = Agent.prototype.delete;
|
1288 |
|
1289 |
|
1290 |
|
1291 |
|
1292 |
|
1293 |
|
1294 |
|
1295 |
|
1296 |
|
1297 |
|
1298 | request.get = function (url, data, fn) {
|
1299 | var req = request('GET', url);
|
1300 |
|
1301 | if (typeof data === 'function') {
|
1302 | fn = data;
|
1303 | data = null;
|
1304 | }
|
1305 |
|
1306 | if (data) req.query(data);
|
1307 | if (fn) req.end(fn);
|
1308 | return req;
|
1309 | };
|
1310 |
|
1311 |
|
1312 |
|
1313 |
|
1314 |
|
1315 |
|
1316 |
|
1317 |
|
1318 |
|
1319 |
|
1320 |
|
1321 | request.head = function (url, data, fn) {
|
1322 | var req = request('HEAD', url);
|
1323 |
|
1324 | if (typeof data === 'function') {
|
1325 | fn = data;
|
1326 | data = null;
|
1327 | }
|
1328 |
|
1329 | if (data) req.query(data);
|
1330 | if (fn) req.end(fn);
|
1331 | return req;
|
1332 | };
|
1333 |
|
1334 |
|
1335 |
|
1336 |
|
1337 |
|
1338 |
|
1339 |
|
1340 |
|
1341 |
|
1342 |
|
1343 |
|
1344 | request.options = function (url, data, fn) {
|
1345 | var req = request('OPTIONS', url);
|
1346 |
|
1347 | if (typeof data === 'function') {
|
1348 | fn = data;
|
1349 | data = null;
|
1350 | }
|
1351 |
|
1352 | if (data) req.send(data);
|
1353 | if (fn) req.end(fn);
|
1354 | return req;
|
1355 | };
|
1356 |
|
1357 |
|
1358 |
|
1359 |
|
1360 |
|
1361 |
|
1362 |
|
1363 |
|
1364 |
|
1365 |
|
1366 |
|
1367 | function del(url, data, fn) {
|
1368 | var req = request('DELETE', url);
|
1369 |
|
1370 | if (typeof data === 'function') {
|
1371 | fn = data;
|
1372 | data = null;
|
1373 | }
|
1374 |
|
1375 | if (data) req.send(data);
|
1376 | if (fn) req.end(fn);
|
1377 | return req;
|
1378 | }
|
1379 |
|
1380 | request.del = del;
|
1381 | request.delete = del;
|
1382 |
|
1383 |
|
1384 |
|
1385 |
|
1386 |
|
1387 |
|
1388 |
|
1389 |
|
1390 |
|
1391 |
|
1392 | request.patch = function (url, data, fn) {
|
1393 | var req = request('PATCH', url);
|
1394 |
|
1395 | if (typeof data === 'function') {
|
1396 | fn = data;
|
1397 | data = null;
|
1398 | }
|
1399 |
|
1400 | if (data) req.send(data);
|
1401 | if (fn) req.end(fn);
|
1402 | return req;
|
1403 | };
|
1404 |
|
1405 |
|
1406 |
|
1407 |
|
1408 |
|
1409 |
|
1410 |
|
1411 |
|
1412 |
|
1413 |
|
1414 |
|
1415 | request.post = function (url, data, fn) {
|
1416 | var req = request('POST', url);
|
1417 |
|
1418 | if (typeof data === 'function') {
|
1419 | fn = data;
|
1420 | data = null;
|
1421 | }
|
1422 |
|
1423 | if (data) req.send(data);
|
1424 | if (fn) req.end(fn);
|
1425 | return req;
|
1426 | };
|
1427 |
|
1428 |
|
1429 |
|
1430 |
|
1431 |
|
1432 |
|
1433 |
|
1434 |
|
1435 |
|
1436 |
|
1437 |
|
1438 | request.put = function (url, data, fn) {
|
1439 | var req = request('PUT', url);
|
1440 |
|
1441 | if (typeof data === 'function') {
|
1442 | fn = data;
|
1443 | data = null;
|
1444 | }
|
1445 |
|
1446 | if (data) req.send(data);
|
1447 | if (fn) req.end(fn);
|
1448 | return req;
|
1449 | };
|
1450 |
|
1451 | },{"./agent-base":3,"./is-object":4,"./request-base":6,"./response-base":7,"component-emitter":1,"fast-safe-stringify":2}],6:[function(require,module,exports){
|
1452 | "use strict";
|
1453 |
|
1454 | function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
1455 |
|
1456 |
|
1457 |
|
1458 |
|
1459 | var isObject = require('./is-object');
|
1460 |
|
1461 |
|
1462 |
|
1463 |
|
1464 |
|
1465 | module.exports = RequestBase;
|
1466 |
|
1467 |
|
1468 |
|
1469 |
|
1470 |
|
1471 |
|
1472 | function RequestBase(obj) {
|
1473 | if (obj) return mixin(obj);
|
1474 | }
|
1475 |
|
1476 |
|
1477 |
|
1478 |
|
1479 |
|
1480 |
|
1481 |
|
1482 |
|
1483 |
|
1484 | function mixin(obj) {
|
1485 | for (var key in RequestBase.prototype) {
|
1486 | if (Object.prototype.hasOwnProperty.call(RequestBase.prototype, key)) obj[key] = RequestBase.prototype[key];
|
1487 | }
|
1488 |
|
1489 | return obj;
|
1490 | }
|
1491 |
|
1492 |
|
1493 |
|
1494 |
|
1495 |
|
1496 |
|
1497 |
|
1498 |
|
1499 | RequestBase.prototype.clearTimeout = function () {
|
1500 | clearTimeout(this._timer);
|
1501 | clearTimeout(this._responseTimeoutTimer);
|
1502 | clearTimeout(this._uploadTimeoutTimer);
|
1503 | delete this._timer;
|
1504 | delete this._responseTimeoutTimer;
|
1505 | delete this._uploadTimeoutTimer;
|
1506 | return this;
|
1507 | };
|
1508 |
|
1509 |
|
1510 |
|
1511 |
|
1512 |
|
1513 |
|
1514 |
|
1515 |
|
1516 |
|
1517 |
|
1518 | RequestBase.prototype.parse = function (fn) {
|
1519 | this._parser = fn;
|
1520 | return this;
|
1521 | };
|
1522 |
|
1523 |
|
1524 |
|
1525 |
|
1526 |
|
1527 |
|
1528 |
|
1529 |
|
1530 |
|
1531 |
|
1532 |
|
1533 |
|
1534 |
|
1535 |
|
1536 |
|
1537 |
|
1538 |
|
1539 |
|
1540 |
|
1541 | RequestBase.prototype.responseType = function (val) {
|
1542 | this._responseType = val;
|
1543 | return this;
|
1544 | };
|
1545 |
|
1546 |
|
1547 |
|
1548 |
|
1549 |
|
1550 |
|
1551 |
|
1552 |
|
1553 |
|
1554 |
|
1555 | RequestBase.prototype.serialize = function (fn) {
|
1556 | this._serializer = fn;
|
1557 | return this;
|
1558 | };
|
1559 |
|
1560 |
|
1561 |
|
1562 |
|
1563 |
|
1564 |
|
1565 |
|
1566 |
|
1567 |
|
1568 |
|
1569 |
|
1570 |
|
1571 |
|
1572 |
|
1573 |
|
1574 | RequestBase.prototype.timeout = function (options) {
|
1575 | if (!options || _typeof(options) !== 'object') {
|
1576 | this._timeout = options;
|
1577 | this._responseTimeout = 0;
|
1578 | this._uploadTimeout = 0;
|
1579 | return this;
|
1580 | }
|
1581 |
|
1582 | for (var option in options) {
|
1583 | if (Object.prototype.hasOwnProperty.call(options, option)) {
|
1584 | switch (option) {
|
1585 | case 'deadline':
|
1586 | this._timeout = options.deadline;
|
1587 | break;
|
1588 |
|
1589 | case 'response':
|
1590 | this._responseTimeout = options.response;
|
1591 | break;
|
1592 |
|
1593 | case 'upload':
|
1594 | this._uploadTimeout = options.upload;
|
1595 | break;
|
1596 |
|
1597 | default:
|
1598 | console.warn('Unknown timeout option', option);
|
1599 | }
|
1600 | }
|
1601 | }
|
1602 |
|
1603 | return this;
|
1604 | };
|
1605 |
|
1606 |
|
1607 |
|
1608 |
|
1609 |
|
1610 |
|
1611 |
|
1612 |
|
1613 |
|
1614 |
|
1615 |
|
1616 |
|
1617 | RequestBase.prototype.retry = function (count, fn) {
|
1618 |
|
1619 | if (arguments.length === 0 || count === true) count = 1;
|
1620 | if (count <= 0) count = 0;
|
1621 | this._maxRetries = count;
|
1622 | this._retries = 0;
|
1623 | this._retryCallback = fn;
|
1624 | return this;
|
1625 | };
|
1626 |
|
1627 | var ERROR_CODES = ['ECONNRESET', 'ETIMEDOUT', 'EADDRINFO', 'ESOCKETTIMEDOUT'];
|
1628 |
|
1629 |
|
1630 |
|
1631 |
|
1632 |
|
1633 |
|
1634 |
|
1635 |
|
1636 |
|
1637 | RequestBase.prototype._shouldRetry = function (err, res) {
|
1638 | if (!this._maxRetries || this._retries++ >= this._maxRetries) {
|
1639 | return false;
|
1640 | }
|
1641 |
|
1642 | if (this._retryCallback) {
|
1643 | try {
|
1644 | var override = this._retryCallback(err, res);
|
1645 |
|
1646 | if (override === true) return true;
|
1647 | if (override === false) return false;
|
1648 | } catch (err_) {
|
1649 | console.error(err_);
|
1650 | }
|
1651 | }
|
1652 |
|
1653 | if (res && res.status && res.status >= 500 && res.status !== 501) return true;
|
1654 |
|
1655 | if (err) {
|
1656 | if (err.code && ERROR_CODES.includes(err.code)) return true;
|
1657 |
|
1658 | if (err.timeout && err.code === 'ECONNABORTED') return true;
|
1659 | if (err.crossDomain) return true;
|
1660 | }
|
1661 |
|
1662 | return false;
|
1663 | };
|
1664 |
|
1665 |
|
1666 |
|
1667 |
|
1668 |
|
1669 |
|
1670 |
|
1671 |
|
1672 | RequestBase.prototype._retry = function () {
|
1673 | this.clearTimeout();
|
1674 |
|
1675 | if (this.req) {
|
1676 | this.req = null;
|
1677 | this.req = this.request();
|
1678 | }
|
1679 |
|
1680 | this._aborted = false;
|
1681 | this.timedout = false;
|
1682 | this.timedoutError = null;
|
1683 | return this._end();
|
1684 | };
|
1685 |
|
1686 |
|
1687 |
|
1688 |
|
1689 |
|
1690 |
|
1691 |
|
1692 |
|
1693 |
|
1694 | RequestBase.prototype.then = function (resolve, reject) {
|
1695 | var _this = this;
|
1696 |
|
1697 | if (!this._fullfilledPromise) {
|
1698 | var self = this;
|
1699 |
|
1700 | if (this._endCalled) {
|
1701 | console.warn('Warning: superagent request was sent twice, because both .end() and .then() were called. Never call .end() if you use promises');
|
1702 | }
|
1703 |
|
1704 | this._fullfilledPromise = new Promise(function (resolve, reject) {
|
1705 | self.on('abort', function () {
|
1706 | if (_this.timedout && _this.timedoutError) {
|
1707 | reject(_this.timedoutError);
|
1708 | return;
|
1709 | }
|
1710 |
|
1711 | var err = new Error('Aborted');
|
1712 | err.code = 'ABORTED';
|
1713 | err.status = _this.status;
|
1714 | err.method = _this.method;
|
1715 | err.url = _this.url;
|
1716 | reject(err);
|
1717 | });
|
1718 | self.end(function (err, res) {
|
1719 | if (err) reject(err);else resolve(res);
|
1720 | });
|
1721 | });
|
1722 | }
|
1723 |
|
1724 | return this._fullfilledPromise.then(resolve, reject);
|
1725 | };
|
1726 |
|
1727 | RequestBase.prototype.catch = function (cb) {
|
1728 | return this.then(undefined, cb);
|
1729 | };
|
1730 |
|
1731 |
|
1732 |
|
1733 |
|
1734 |
|
1735 | RequestBase.prototype.use = function (fn) {
|
1736 | fn(this);
|
1737 | return this;
|
1738 | };
|
1739 |
|
1740 | RequestBase.prototype.ok = function (cb) {
|
1741 | if (typeof cb !== 'function') throw new Error('Callback required');
|
1742 | this._okCallback = cb;
|
1743 | return this;
|
1744 | };
|
1745 |
|
1746 | RequestBase.prototype._isResponseOK = function (res) {
|
1747 | if (!res) {
|
1748 | return false;
|
1749 | }
|
1750 |
|
1751 | if (this._okCallback) {
|
1752 | return this._okCallback(res);
|
1753 | }
|
1754 |
|
1755 | return res.status >= 200 && res.status < 300;
|
1756 | };
|
1757 |
|
1758 |
|
1759 |
|
1760 |
|
1761 |
|
1762 |
|
1763 |
|
1764 |
|
1765 |
|
1766 |
|
1767 | RequestBase.prototype.get = function (field) {
|
1768 | return this._header[field.toLowerCase()];
|
1769 | };
|
1770 |
|
1771 |
|
1772 |
|
1773 |
|
1774 |
|
1775 |
|
1776 |
|
1777 |
|
1778 |
|
1779 |
|
1780 |
|
1781 |
|
1782 |
|
1783 | RequestBase.prototype.getHeader = RequestBase.prototype.get;
|
1784 |
|
1785 |
|
1786 |
|
1787 |
|
1788 |
|
1789 |
|
1790 |
|
1791 |
|
1792 |
|
1793 |
|
1794 |
|
1795 |
|
1796 |
|
1797 |
|
1798 |
|
1799 |
|
1800 |
|
1801 |
|
1802 |
|
1803 |
|
1804 |
|
1805 | RequestBase.prototype.set = function (field, val) {
|
1806 | if (isObject(field)) {
|
1807 | for (var key in field) {
|
1808 | if (Object.prototype.hasOwnProperty.call(field, key)) this.set(key, field[key]);
|
1809 | }
|
1810 |
|
1811 | return this;
|
1812 | }
|
1813 |
|
1814 | this._header[field.toLowerCase()] = val;
|
1815 | this.header[field] = val;
|
1816 | return this;
|
1817 | };
|
1818 |
|
1819 |
|
1820 |
|
1821 |
|
1822 |
|
1823 |
|
1824 |
|
1825 |
|
1826 |
|
1827 |
|
1828 |
|
1829 |
|
1830 |
|
1831 |
|
1832 | RequestBase.prototype.unset = function (field) {
|
1833 | delete this._header[field.toLowerCase()];
|
1834 | delete this.header[field];
|
1835 | return this;
|
1836 | };
|
1837 |
|
1838 |
|
1839 |
|
1840 |
|
1841 |
|
1842 |
|
1843 |
|
1844 |
|
1845 |
|
1846 |
|
1847 |
|
1848 |
|
1849 |
|
1850 |
|
1851 |
|
1852 |
|
1853 |
|
1854 |
|
1855 |
|
1856 |
|
1857 |
|
1858 | RequestBase.prototype.field = function (name, val) {
|
1859 |
|
1860 | if (name === null || undefined === name) {
|
1861 | throw new Error('.field(name, val) name can not be empty');
|
1862 | }
|
1863 |
|
1864 | if (this._data) {
|
1865 | throw new Error(".field() can't be used if .send() is used. Please use only .send() or only .field() & .attach()");
|
1866 | }
|
1867 |
|
1868 | if (isObject(name)) {
|
1869 | for (var key in name) {
|
1870 | if (Object.prototype.hasOwnProperty.call(name, key)) this.field(key, name[key]);
|
1871 | }
|
1872 |
|
1873 | return this;
|
1874 | }
|
1875 |
|
1876 | if (Array.isArray(val)) {
|
1877 | for (var i in val) {
|
1878 | if (Object.prototype.hasOwnProperty.call(val, i)) this.field(name, val[i]);
|
1879 | }
|
1880 |
|
1881 | return this;
|
1882 | }
|
1883 |
|
1884 |
|
1885 | if (val === null || undefined === val) {
|
1886 | throw new Error('.field(name, val) val can not be empty');
|
1887 | }
|
1888 |
|
1889 | if (typeof val === 'boolean') {
|
1890 | val = String(val);
|
1891 | }
|
1892 |
|
1893 | this._getFormData().append(name, val);
|
1894 |
|
1895 | return this;
|
1896 | };
|
1897 |
|
1898 |
|
1899 |
|
1900 |
|
1901 |
|
1902 |
|
1903 |
|
1904 |
|
1905 | RequestBase.prototype.abort = function () {
|
1906 | if (this._aborted) {
|
1907 | return this;
|
1908 | }
|
1909 |
|
1910 | this._aborted = true;
|
1911 | if (this.xhr) this.xhr.abort();
|
1912 |
|
1913 | if (this.req) this.req.abort();
|
1914 |
|
1915 | this.clearTimeout();
|
1916 | this.emit('abort');
|
1917 | return this;
|
1918 | };
|
1919 |
|
1920 | RequestBase.prototype._auth = function (user, pass, options, base64Encoder) {
|
1921 | switch (options.type) {
|
1922 | case 'basic':
|
1923 | this.set('Authorization', "Basic ".concat(base64Encoder("".concat(user, ":").concat(pass))));
|
1924 | break;
|
1925 |
|
1926 | case 'auto':
|
1927 | this.username = user;
|
1928 | this.password = pass;
|
1929 | break;
|
1930 |
|
1931 | case 'bearer':
|
1932 |
|
1933 | this.set('Authorization', "Bearer ".concat(user));
|
1934 | break;
|
1935 |
|
1936 | default:
|
1937 | break;
|
1938 | }
|
1939 |
|
1940 | return this;
|
1941 | };
|
1942 |
|
1943 |
|
1944 |
|
1945 |
|
1946 |
|
1947 |
|
1948 |
|
1949 |
|
1950 |
|
1951 |
|
1952 |
|
1953 |
|
1954 | RequestBase.prototype.withCredentials = function (on) {
|
1955 |
|
1956 | if (on === undefined) on = true;
|
1957 | this._withCredentials = on;
|
1958 | return this;
|
1959 | };
|
1960 |
|
1961 |
|
1962 |
|
1963 |
|
1964 |
|
1965 |
|
1966 |
|
1967 |
|
1968 |
|
1969 | RequestBase.prototype.redirects = function (n) {
|
1970 | this._maxRedirects = n;
|
1971 | return this;
|
1972 | };
|
1973 |
|
1974 |
|
1975 |
|
1976 |
|
1977 |
|
1978 |
|
1979 |
|
1980 |
|
1981 |
|
1982 | RequestBase.prototype.maxResponseSize = function (n) {
|
1983 | if (typeof n !== 'number') {
|
1984 | throw new TypeError('Invalid argument');
|
1985 | }
|
1986 |
|
1987 | this._maxResponseSize = n;
|
1988 | return this;
|
1989 | };
|
1990 |
|
1991 |
|
1992 |
|
1993 |
|
1994 |
|
1995 |
|
1996 |
|
1997 |
|
1998 |
|
1999 |
|
2000 | RequestBase.prototype.toJSON = function () {
|
2001 | return {
|
2002 | method: this.method,
|
2003 | url: this.url,
|
2004 | data: this._data,
|
2005 | headers: this._header
|
2006 | };
|
2007 | };
|
2008 |
|
2009 |
|
2010 |
|
2011 |
|
2012 |
|
2013 |
|
2014 |
|
2015 |
|
2016 |
|
2017 |
|
2018 |
|
2019 |
|
2020 |
|
2021 |
|
2022 |
|
2023 |
|
2024 |
|
2025 |
|
2026 |
|
2027 |
|
2028 |
|
2029 |
|
2030 |
|
2031 |
|
2032 |
|
2033 |
|
2034 |
|
2035 |
|
2036 |
|
2037 |
|
2038 |
|
2039 |
|
2040 |
|
2041 |
|
2042 |
|
2043 |
|
2044 |
|
2045 |
|
2046 |
|
2047 |
|
2048 |
|
2049 |
|
2050 | RequestBase.prototype.send = function (data) {
|
2051 | var isObj = isObject(data);
|
2052 | var type = this._header['content-type'];
|
2053 |
|
2054 | if (this._formData) {
|
2055 | throw new Error(".send() can't be used if .attach() or .field() is used. Please use only .send() or only .field() & .attach()");
|
2056 | }
|
2057 |
|
2058 | if (isObj && !this._data) {
|
2059 | if (Array.isArray(data)) {
|
2060 | this._data = [];
|
2061 | } else if (!this._isHost(data)) {
|
2062 | this._data = {};
|
2063 | }
|
2064 | } else if (data && this._data && this._isHost(this._data)) {
|
2065 | throw new Error("Can't merge these send calls");
|
2066 | }
|
2067 |
|
2068 |
|
2069 | if (isObj && isObject(this._data)) {
|
2070 | for (var key in data) {
|
2071 | if (Object.prototype.hasOwnProperty.call(data, key)) this._data[key] = data[key];
|
2072 | }
|
2073 | } else if (typeof data === 'string') {
|
2074 |
|
2075 | if (!type) this.type('form');
|
2076 | type = this._header['content-type'];
|
2077 |
|
2078 | if (type === 'application/x-www-form-urlencoded') {
|
2079 | this._data = this._data ? "".concat(this._data, "&").concat(data) : data;
|
2080 | } else {
|
2081 | this._data = (this._data || '') + data;
|
2082 | }
|
2083 | } else {
|
2084 | this._data = data;
|
2085 | }
|
2086 |
|
2087 | if (!isObj || this._isHost(data)) {
|
2088 | return this;
|
2089 | }
|
2090 |
|
2091 |
|
2092 | if (!type) this.type('json');
|
2093 | return this;
|
2094 | };
|
2095 |
|
2096 |
|
2097 |
|
2098 |
|
2099 |
|
2100 |
|
2101 |
|
2102 |
|
2103 |
|
2104 |
|
2105 |
|
2106 |
|
2107 |
|
2108 |
|
2109 |
|
2110 |
|
2111 |
|
2112 |
|
2113 |
|
2114 |
|
2115 |
|
2116 |
|
2117 |
|
2118 |
|
2119 |
|
2120 |
|
2121 |
|
2122 |
|
2123 |
|
2124 | RequestBase.prototype.sortQuery = function (sort) {
|
2125 |
|
2126 | this._sort = typeof sort === 'undefined' ? true : sort;
|
2127 | return this;
|
2128 | };
|
2129 |
|
2130 |
|
2131 |
|
2132 |
|
2133 |
|
2134 |
|
2135 |
|
2136 | RequestBase.prototype._finalizeQueryString = function () {
|
2137 | var query = this._query.join('&');
|
2138 |
|
2139 | if (query) {
|
2140 | this.url += (this.url.includes('?') ? '&' : '?') + query;
|
2141 | }
|
2142 |
|
2143 | this._query.length = 0;
|
2144 |
|
2145 | if (this._sort) {
|
2146 | var index = this.url.indexOf('?');
|
2147 |
|
2148 | if (index >= 0) {
|
2149 | var queryArr = this.url.slice(index + 1).split('&');
|
2150 |
|
2151 | if (typeof this._sort === 'function') {
|
2152 | queryArr.sort(this._sort);
|
2153 | } else {
|
2154 | queryArr.sort();
|
2155 | }
|
2156 |
|
2157 | this.url = this.url.slice(0, index) + '?' + queryArr.join('&');
|
2158 | }
|
2159 | }
|
2160 | };
|
2161 |
|
2162 |
|
2163 | RequestBase.prototype._appendQueryString = function () {
|
2164 | console.warn('Unsupported');
|
2165 | };
|
2166 |
|
2167 |
|
2168 |
|
2169 |
|
2170 |
|
2171 |
|
2172 |
|
2173 | RequestBase.prototype._timeoutError = function (reason, timeout, errno) {
|
2174 | if (this._aborted) {
|
2175 | return;
|
2176 | }
|
2177 |
|
2178 | var err = new Error("".concat(reason + timeout, "ms exceeded"));
|
2179 | err.timeout = timeout;
|
2180 | err.code = 'ECONNABORTED';
|
2181 | err.errno = errno;
|
2182 | this.timedout = true;
|
2183 | this.timedoutError = err;
|
2184 | this.abort();
|
2185 | this.callback(err);
|
2186 | };
|
2187 |
|
2188 | RequestBase.prototype._setTimeouts = function () {
|
2189 | var self = this;
|
2190 |
|
2191 | if (this._timeout && !this._timer) {
|
2192 | this._timer = setTimeout(function () {
|
2193 | self._timeoutError('Timeout of ', self._timeout, 'ETIME');
|
2194 | }, this._timeout);
|
2195 | }
|
2196 |
|
2197 |
|
2198 | if (this._responseTimeout && !this._responseTimeoutTimer) {
|
2199 | this._responseTimeoutTimer = setTimeout(function () {
|
2200 | self._timeoutError('Response timeout of ', self._responseTimeout, 'ETIMEDOUT');
|
2201 | }, this._responseTimeout);
|
2202 | }
|
2203 | };
|
2204 |
|
2205 | },{"./is-object":4}],7:[function(require,module,exports){
|
2206 | "use strict";
|
2207 |
|
2208 |
|
2209 |
|
2210 |
|
2211 | var utils = require('./utils');
|
2212 |
|
2213 |
|
2214 |
|
2215 |
|
2216 |
|
2217 | module.exports = ResponseBase;
|
2218 |
|
2219 |
|
2220 |
|
2221 |
|
2222 |
|
2223 |
|
2224 | function ResponseBase(obj) {
|
2225 | if (obj) return mixin(obj);
|
2226 | }
|
2227 |
|
2228 |
|
2229 |
|
2230 |
|
2231 |
|
2232 |
|
2233 |
|
2234 |
|
2235 |
|
2236 | function mixin(obj) {
|
2237 | for (var key in ResponseBase.prototype) {
|
2238 | if (Object.prototype.hasOwnProperty.call(ResponseBase.prototype, key)) obj[key] = ResponseBase.prototype[key];
|
2239 | }
|
2240 |
|
2241 | return obj;
|
2242 | }
|
2243 |
|
2244 |
|
2245 |
|
2246 |
|
2247 |
|
2248 |
|
2249 |
|
2250 |
|
2251 |
|
2252 | ResponseBase.prototype.get = function (field) {
|
2253 | return this.header[field.toLowerCase()];
|
2254 | };
|
2255 |
|
2256 |
|
2257 |
|
2258 |
|
2259 |
|
2260 |
|
2261 |
|
2262 |
|
2263 |
|
2264 |
|
2265 |
|
2266 |
|
2267 |
|
2268 | ResponseBase.prototype._setHeaderProperties = function (header) {
|
2269 |
|
2270 |
|
2271 |
|
2272 | var ct = header['content-type'] || '';
|
2273 | this.type = utils.type(ct);
|
2274 |
|
2275 | var params = utils.params(ct);
|
2276 |
|
2277 | for (var key in params) {
|
2278 | if (Object.prototype.hasOwnProperty.call(params, key)) this[key] = params[key];
|
2279 | }
|
2280 |
|
2281 | this.links = {};
|
2282 |
|
2283 | try {
|
2284 | if (header.link) {
|
2285 | this.links = utils.parseLinks(header.link);
|
2286 | }
|
2287 | } catch (_unused) {
|
2288 | }
|
2289 | };
|
2290 |
|
2291 |
|
2292 |
|
2293 |
|
2294 |
|
2295 |
|
2296 |
|
2297 |
|
2298 |
|
2299 |
|
2300 |
|
2301 |
|
2302 |
|
2303 |
|
2304 |
|
2305 |
|
2306 |
|
2307 |
|
2308 |
|
2309 |
|
2310 |
|
2311 |
|
2312 | ResponseBase.prototype._setStatusProperties = function (status) {
|
2313 | var type = status / 100 | 0;
|
2314 |
|
2315 | this.statusCode = status;
|
2316 | this.status = this.statusCode;
|
2317 | this.statusType = type;
|
2318 |
|
2319 | this.info = type === 1;
|
2320 | this.ok = type === 2;
|
2321 | this.redirect = type === 3;
|
2322 | this.clientError = type === 4;
|
2323 | this.serverError = type === 5;
|
2324 | this.error = type === 4 || type === 5 ? this.toError() : false;
|
2325 |
|
2326 | this.created = status === 201;
|
2327 | this.accepted = status === 202;
|
2328 | this.noContent = status === 204;
|
2329 | this.badRequest = status === 400;
|
2330 | this.unauthorized = status === 401;
|
2331 | this.notAcceptable = status === 406;
|
2332 | this.forbidden = status === 403;
|
2333 | this.notFound = status === 404;
|
2334 | this.unprocessableEntity = status === 422;
|
2335 | };
|
2336 |
|
2337 | },{"./utils":8}],8:[function(require,module,exports){
|
2338 | "use strict";
|
2339 |
|
2340 |
|
2341 |
|
2342 |
|
2343 |
|
2344 |
|
2345 |
|
2346 |
|
2347 | exports.type = function (str) {
|
2348 | return str.split(/ *; */).shift();
|
2349 | };
|
2350 |
|
2351 |
|
2352 |
|
2353 |
|
2354 |
|
2355 |
|
2356 |
|
2357 |
|
2358 |
|
2359 | exports.params = function (str) {
|
2360 | return str.split(/ *; */).reduce(function (obj, str) {
|
2361 | var parts = str.split(/ *= */);
|
2362 | var key = parts.shift();
|
2363 | var val = parts.shift();
|
2364 | if (key && val) obj[key] = val;
|
2365 | return obj;
|
2366 | }, {});
|
2367 | };
|
2368 |
|
2369 |
|
2370 |
|
2371 |
|
2372 |
|
2373 |
|
2374 |
|
2375 |
|
2376 |
|
2377 | exports.parseLinks = function (str) {
|
2378 | return str.split(/ *, */).reduce(function (obj, str) {
|
2379 | var parts = str.split(/ *; */);
|
2380 | var url = parts[0].slice(1, -1);
|
2381 | var rel = parts[1].split(/ *= */)[1].slice(1, -1);
|
2382 | obj[rel] = url;
|
2383 | return obj;
|
2384 | }, {});
|
2385 | };
|
2386 |
|
2387 |
|
2388 |
|
2389 |
|
2390 |
|
2391 |
|
2392 |
|
2393 |
|
2394 |
|
2395 | exports.cleanHeader = function (header, changesOrigin) {
|
2396 | delete header['content-type'];
|
2397 | delete header['content-length'];
|
2398 | delete header['transfer-encoding'];
|
2399 | delete header.host;
|
2400 |
|
2401 | if (changesOrigin) {
|
2402 | delete header.authorization;
|
2403 | delete header.cookie;
|
2404 | }
|
2405 |
|
2406 | return header;
|
2407 | };
|
2408 |
|
2409 | },{}]},{},[5])(5)
|
2410 | });
|