axios 1.2.0-alpha.1 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of axios might be problematic. Click here for more details.

@@ -1,4 +1,4 @@
1
- // Axios v1.2.0-alpha.1 Copyright (c) 2022 Matt Zabriskie and contributors
1
+ // Axios v1.2.1 Copyright (c) 2022 Matt Zabriskie and contributors
2
2
  'use strict';
3
3
 
4
4
  const FormData$1 = require('form-data');
@@ -618,6 +618,37 @@ const toFiniteNumber = (value, defaultValue) => {
618
618
  return Number.isFinite(value) ? value : defaultValue;
619
619
  };
620
620
 
621
+ const toJSONObject = (obj) => {
622
+ const stack = new Array(10);
623
+
624
+ const visit = (source, i) => {
625
+
626
+ if (isObject(source)) {
627
+ if (stack.indexOf(source) >= 0) {
628
+ return;
629
+ }
630
+
631
+ if(!('toJSON' in source)) {
632
+ stack[i] = source;
633
+ const target = isArray(source) ? [] : {};
634
+
635
+ forEach(source, (value, key) => {
636
+ const reducedValue = visit(value, i + 1);
637
+ !isUndefined(reducedValue) && (target[key] = reducedValue);
638
+ });
639
+
640
+ stack[i] = undefined;
641
+
642
+ return target;
643
+ }
644
+ }
645
+
646
+ return source;
647
+ };
648
+
649
+ return visit(obj, 0);
650
+ };
651
+
621
652
  const utils = {
622
653
  isArray,
623
654
  isArrayBuffer,
@@ -663,7 +694,8 @@ const utils = {
663
694
  toFiniteNumber,
664
695
  findKey,
665
696
  global: _global,
666
- isContextDefined
697
+ isContextDefined,
698
+ toJSONObject
667
699
  };
668
700
 
669
701
  /**
@@ -709,7 +741,7 @@ utils.inherits(AxiosError, Error, {
709
741
  columnNumber: this.columnNumber,
710
742
  stack: this.stack,
711
743
  // Axios
712
- config: this.config,
744
+ config: utils.toJSONObject(this.config),
713
745
  code: this.code,
714
746
  status: this.response && this.response.status ? this.response.status : null
715
747
  };
@@ -1281,148 +1313,162 @@ function formDataToJSON(formData) {
1281
1313
  return null;
1282
1314
  }
1283
1315
 
1316
+ const DEFAULT_CONTENT_TYPE = {
1317
+ 'Content-Type': undefined
1318
+ };
1319
+
1284
1320
  /**
1285
- * Resolve or reject a Promise based on response status.
1321
+ * It takes a string, tries to parse it, and if it fails, it returns the stringified version
1322
+ * of the input
1286
1323
  *
1287
- * @param {Function} resolve A function that resolves the promise.
1288
- * @param {Function} reject A function that rejects the promise.
1289
- * @param {object} response The response.
1324
+ * @param {any} rawValue - The value to be stringified.
1325
+ * @param {Function} parser - A function that parses a string into a JavaScript object.
1326
+ * @param {Function} encoder - A function that takes a value and returns a string.
1290
1327
  *
1291
- * @returns {object} The response.
1328
+ * @returns {string} A stringified version of the rawValue.
1292
1329
  */
1293
- function settle(resolve, reject, response) {
1294
- const validateStatus = response.config.validateStatus;
1295
- if (!response.status || !validateStatus || validateStatus(response.status)) {
1296
- resolve(response);
1297
- } else {
1298
- reject(new AxiosError(
1299
- 'Request failed with status code ' + response.status,
1300
- [AxiosError.ERR_BAD_REQUEST, AxiosError.ERR_BAD_RESPONSE][Math.floor(response.status / 100) - 4],
1301
- response.config,
1302
- response.request,
1303
- response
1304
- ));
1330
+ function stringifySafely(rawValue, parser, encoder) {
1331
+ if (utils.isString(rawValue)) {
1332
+ try {
1333
+ (parser || JSON.parse)(rawValue);
1334
+ return utils.trim(rawValue);
1335
+ } catch (e) {
1336
+ if (e.name !== 'SyntaxError') {
1337
+ throw e;
1338
+ }
1339
+ }
1305
1340
  }
1306
- }
1307
1341
 
1308
- /**
1309
- * Determines whether the specified URL is absolute
1310
- *
1311
- * @param {string} url The URL to test
1312
- *
1313
- * @returns {boolean} True if the specified URL is absolute, otherwise false
1314
- */
1315
- function isAbsoluteURL(url) {
1316
- // A URL is considered absolute if it begins with "<scheme>://" or "//" (protocol-relative URL).
1317
- // RFC 3986 defines scheme name as a sequence of characters beginning with a letter and followed
1318
- // by any combination of letters, digits, plus, period, or hyphen.
1319
- return /^([a-z][a-z\d+\-.]*:)?\/\//i.test(url);
1342
+ return (encoder || JSON.stringify)(rawValue);
1320
1343
  }
1321
1344
 
1322
- /**
1323
- * Creates a new URL by combining the specified URLs
1324
- *
1325
- * @param {string} baseURL The base URL
1326
- * @param {string} relativeURL The relative URL
1327
- *
1328
- * @returns {string} The combined URL
1329
- */
1330
- function combineURLs(baseURL, relativeURL) {
1331
- return relativeURL
1332
- ? baseURL.replace(/\/+$/, '') + '/' + relativeURL.replace(/^\/+/, '')
1333
- : baseURL;
1334
- }
1345
+ const defaults = {
1335
1346
 
1336
- /**
1337
- * Creates a new URL by combining the baseURL with the requestedURL,
1338
- * only when the requestedURL is not already an absolute URL.
1339
- * If the requestURL is absolute, this function returns the requestedURL untouched.
1340
- *
1341
- * @param {string} baseURL The base URL
1342
- * @param {string} requestedURL Absolute or relative URL to combine
1343
- *
1344
- * @returns {string} The combined full path
1345
- */
1346
- function buildFullPath(baseURL, requestedURL) {
1347
- if (baseURL && !isAbsoluteURL(requestedURL)) {
1348
- return combineURLs(baseURL, requestedURL);
1349
- }
1350
- return requestedURL;
1351
- }
1347
+ transitional: transitionalDefaults,
1352
1348
 
1353
- const VERSION = "1.2.0-alpha.1";
1349
+ adapter: ['xhr', 'http'],
1354
1350
 
1355
- /**
1356
- * A `CanceledError` is an object that is thrown when an operation is canceled.
1357
- *
1358
- * @param {string=} message The message.
1359
- * @param {Object=} config The config.
1360
- * @param {Object=} request The request.
1361
- *
1362
- * @returns {CanceledError} The created error.
1363
- */
1364
- function CanceledError(message, config, request) {
1365
- // eslint-disable-next-line no-eq-null,eqeqeq
1366
- AxiosError.call(this, message == null ? 'canceled' : message, AxiosError.ERR_CANCELED, config, request);
1367
- this.name = 'CanceledError';
1368
- }
1351
+ transformRequest: [function transformRequest(data, headers) {
1352
+ const contentType = headers.getContentType() || '';
1353
+ const hasJSONContentType = contentType.indexOf('application/json') > -1;
1354
+ const isObjectPayload = utils.isObject(data);
1369
1355
 
1370
- utils.inherits(CanceledError, AxiosError, {
1371
- __CANCEL__: true
1372
- });
1356
+ if (isObjectPayload && utils.isHTMLForm(data)) {
1357
+ data = new FormData(data);
1358
+ }
1373
1359
 
1374
- function parseProtocol(url) {
1375
- const match = /^([-+\w]{1,25})(:?\/\/|:)/.exec(url);
1376
- return match && match[1] || '';
1377
- }
1360
+ const isFormData = utils.isFormData(data);
1378
1361
 
1379
- const DATA_URL_PATTERN = /^(?:([^;]+);)?(?:[^;]+;)?(base64|),([\s\S]*)$/;
1362
+ if (isFormData) {
1363
+ if (!hasJSONContentType) {
1364
+ return data;
1365
+ }
1366
+ return hasJSONContentType ? JSON.stringify(formDataToJSON(data)) : data;
1367
+ }
1380
1368
 
1381
- /**
1382
- * Parse data uri to a Buffer or Blob
1383
- *
1384
- * @param {String} uri
1385
- * @param {?Boolean} asBlob
1386
- * @param {?Object} options
1387
- * @param {?Function} options.Blob
1388
- *
1389
- * @returns {Buffer|Blob}
1390
- */
1391
- function fromDataURI(uri, asBlob, options) {
1392
- const _Blob = options && options.Blob || platform.classes.Blob;
1393
- const protocol = parseProtocol(uri);
1369
+ if (utils.isArrayBuffer(data) ||
1370
+ utils.isBuffer(data) ||
1371
+ utils.isStream(data) ||
1372
+ utils.isFile(data) ||
1373
+ utils.isBlob(data)
1374
+ ) {
1375
+ return data;
1376
+ }
1377
+ if (utils.isArrayBufferView(data)) {
1378
+ return data.buffer;
1379
+ }
1380
+ if (utils.isURLSearchParams(data)) {
1381
+ headers.setContentType('application/x-www-form-urlencoded;charset=utf-8', false);
1382
+ return data.toString();
1383
+ }
1394
1384
 
1395
- if (asBlob === undefined && _Blob) {
1396
- asBlob = true;
1397
- }
1385
+ let isFileList;
1398
1386
 
1399
- if (protocol === 'data') {
1400
- uri = protocol.length ? uri.slice(protocol.length + 1) : uri;
1387
+ if (isObjectPayload) {
1388
+ if (contentType.indexOf('application/x-www-form-urlencoded') > -1) {
1389
+ return toURLEncodedForm(data, this.formSerializer).toString();
1390
+ }
1401
1391
 
1402
- const match = DATA_URL_PATTERN.exec(uri);
1392
+ if ((isFileList = utils.isFileList(data)) || contentType.indexOf('multipart/form-data') > -1) {
1393
+ const _FormData = this.env && this.env.FormData;
1403
1394
 
1404
- if (!match) {
1405
- throw new AxiosError('Invalid URL', AxiosError.ERR_INVALID_URL);
1395
+ return toFormData(
1396
+ isFileList ? {'files[]': data} : data,
1397
+ _FormData && new _FormData(),
1398
+ this.formSerializer
1399
+ );
1400
+ }
1406
1401
  }
1407
1402
 
1408
- const mime = match[1];
1409
- const isBase64 = match[2];
1410
- const body = match[3];
1411
- const buffer = Buffer.from(decodeURIComponent(body), isBase64 ? 'base64' : 'utf8');
1403
+ if (isObjectPayload || hasJSONContentType ) {
1404
+ headers.setContentType('application/json', false);
1405
+ return stringifySafely(data);
1406
+ }
1412
1407
 
1413
- if (asBlob) {
1414
- if (!_Blob) {
1415
- throw new AxiosError('Blob is not supported', AxiosError.ERR_NOT_SUPPORT);
1416
- }
1408
+ return data;
1409
+ }],
1417
1410
 
1418
- return new _Blob([buffer], {type: mime});
1411
+ transformResponse: [function transformResponse(data) {
1412
+ const transitional = this.transitional || defaults.transitional;
1413
+ const forcedJSONParsing = transitional && transitional.forcedJSONParsing;
1414
+ const JSONRequested = this.responseType === 'json';
1415
+
1416
+ if (data && utils.isString(data) && ((forcedJSONParsing && !this.responseType) || JSONRequested)) {
1417
+ const silentJSONParsing = transitional && transitional.silentJSONParsing;
1418
+ const strictJSONParsing = !silentJSONParsing && JSONRequested;
1419
+
1420
+ try {
1421
+ return JSON.parse(data);
1422
+ } catch (e) {
1423
+ if (strictJSONParsing) {
1424
+ if (e.name === 'SyntaxError') {
1425
+ throw AxiosError.from(e, AxiosError.ERR_BAD_RESPONSE, this, null, this.response);
1426
+ }
1427
+ throw e;
1428
+ }
1429
+ }
1419
1430
  }
1420
1431
 
1421
- return buffer;
1432
+ return data;
1433
+ }],
1434
+
1435
+ /**
1436
+ * A timeout in milliseconds to abort a request. If set to 0 (default) a
1437
+ * timeout is not created.
1438
+ */
1439
+ timeout: 0,
1440
+
1441
+ xsrfCookieName: 'XSRF-TOKEN',
1442
+ xsrfHeaderName: 'X-XSRF-TOKEN',
1443
+
1444
+ maxContentLength: -1,
1445
+ maxBodyLength: -1,
1446
+
1447
+ env: {
1448
+ FormData: platform.classes.FormData,
1449
+ Blob: platform.classes.Blob
1450
+ },
1451
+
1452
+ validateStatus: function validateStatus(status) {
1453
+ return status >= 200 && status < 300;
1454
+ },
1455
+
1456
+ headers: {
1457
+ common: {
1458
+ 'Accept': 'application/json, text/plain, */*'
1459
+ }
1422
1460
  }
1461
+ };
1423
1462
 
1424
- throw new AxiosError('Unsupported protocol ' + protocol, AxiosError.ERR_NOT_SUPPORT);
1425
- }
1463
+ utils.forEach(['delete', 'get', 'head'], function forEachMethodNoData(method) {
1464
+ defaults.headers[method] = {};
1465
+ });
1466
+
1467
+ utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
1468
+ defaults.headers[method] = utils.merge(DEFAULT_CONTENT_TYPE);
1469
+ });
1470
+
1471
+ const defaults$1 = defaults;
1426
1472
 
1427
1473
  // RawAxiosHeaders whose duplicates are ignored by node
1428
1474
  // c.f. https://nodejs.org/api/http.html#http_message_headers
@@ -1733,18 +1779,188 @@ class AxiosHeaders {
1733
1779
  }
1734
1780
  }
1735
1781
 
1736
- utils.isArray(header) ? header.forEach(defineAccessor) : defineAccessor(header);
1782
+ utils.isArray(header) ? header.forEach(defineAccessor) : defineAccessor(header);
1783
+
1784
+ return this;
1785
+ }
1786
+ }
1787
+
1788
+ AxiosHeaders.accessor(['Content-Type', 'Content-Length', 'Accept', 'Accept-Encoding', 'User-Agent']);
1789
+
1790
+ utils.freezeMethods(AxiosHeaders.prototype);
1791
+ utils.freezeMethods(AxiosHeaders);
1792
+
1793
+ const AxiosHeaders$1 = AxiosHeaders;
1794
+
1795
+ /**
1796
+ * Transform the data for a request or a response
1797
+ *
1798
+ * @param {Array|Function} fns A single function or Array of functions
1799
+ * @param {?Object} response The response object
1800
+ *
1801
+ * @returns {*} The resulting transformed data
1802
+ */
1803
+ function transformData(fns, response) {
1804
+ const config = this || defaults$1;
1805
+ const context = response || config;
1806
+ const headers = AxiosHeaders$1.from(context.headers);
1807
+ let data = context.data;
1808
+
1809
+ utils.forEach(fns, function transform(fn) {
1810
+ data = fn.call(config, data, headers.normalize(), response ? response.status : undefined);
1811
+ });
1812
+
1813
+ headers.normalize();
1814
+
1815
+ return data;
1816
+ }
1817
+
1818
+ function isCancel(value) {
1819
+ return !!(value && value.__CANCEL__);
1820
+ }
1821
+
1822
+ /**
1823
+ * A `CanceledError` is an object that is thrown when an operation is canceled.
1824
+ *
1825
+ * @param {string=} message The message.
1826
+ * @param {Object=} config The config.
1827
+ * @param {Object=} request The request.
1828
+ *
1829
+ * @returns {CanceledError} The created error.
1830
+ */
1831
+ function CanceledError(message, config, request) {
1832
+ // eslint-disable-next-line no-eq-null,eqeqeq
1833
+ AxiosError.call(this, message == null ? 'canceled' : message, AxiosError.ERR_CANCELED, config, request);
1834
+ this.name = 'CanceledError';
1835
+ }
1836
+
1837
+ utils.inherits(CanceledError, AxiosError, {
1838
+ __CANCEL__: true
1839
+ });
1840
+
1841
+ /**
1842
+ * Resolve or reject a Promise based on response status.
1843
+ *
1844
+ * @param {Function} resolve A function that resolves the promise.
1845
+ * @param {Function} reject A function that rejects the promise.
1846
+ * @param {object} response The response.
1847
+ *
1848
+ * @returns {object} The response.
1849
+ */
1850
+ function settle(resolve, reject, response) {
1851
+ const validateStatus = response.config.validateStatus;
1852
+ if (!response.status || !validateStatus || validateStatus(response.status)) {
1853
+ resolve(response);
1854
+ } else {
1855
+ reject(new AxiosError(
1856
+ 'Request failed with status code ' + response.status,
1857
+ [AxiosError.ERR_BAD_REQUEST, AxiosError.ERR_BAD_RESPONSE][Math.floor(response.status / 100) - 4],
1858
+ response.config,
1859
+ response.request,
1860
+ response
1861
+ ));
1862
+ }
1863
+ }
1864
+
1865
+ /**
1866
+ * Determines whether the specified URL is absolute
1867
+ *
1868
+ * @param {string} url The URL to test
1869
+ *
1870
+ * @returns {boolean} True if the specified URL is absolute, otherwise false
1871
+ */
1872
+ function isAbsoluteURL(url) {
1873
+ // A URL is considered absolute if it begins with "<scheme>://" or "//" (protocol-relative URL).
1874
+ // RFC 3986 defines scheme name as a sequence of characters beginning with a letter and followed
1875
+ // by any combination of letters, digits, plus, period, or hyphen.
1876
+ return /^([a-z][a-z\d+\-.]*:)?\/\//i.test(url);
1877
+ }
1878
+
1879
+ /**
1880
+ * Creates a new URL by combining the specified URLs
1881
+ *
1882
+ * @param {string} baseURL The base URL
1883
+ * @param {string} relativeURL The relative URL
1884
+ *
1885
+ * @returns {string} The combined URL
1886
+ */
1887
+ function combineURLs(baseURL, relativeURL) {
1888
+ return relativeURL
1889
+ ? baseURL.replace(/\/+$/, '') + '/' + relativeURL.replace(/^\/+/, '')
1890
+ : baseURL;
1891
+ }
1892
+
1893
+ /**
1894
+ * Creates a new URL by combining the baseURL with the requestedURL,
1895
+ * only when the requestedURL is not already an absolute URL.
1896
+ * If the requestURL is absolute, this function returns the requestedURL untouched.
1897
+ *
1898
+ * @param {string} baseURL The base URL
1899
+ * @param {string} requestedURL Absolute or relative URL to combine
1900
+ *
1901
+ * @returns {string} The combined full path
1902
+ */
1903
+ function buildFullPath(baseURL, requestedURL) {
1904
+ if (baseURL && !isAbsoluteURL(requestedURL)) {
1905
+ return combineURLs(baseURL, requestedURL);
1906
+ }
1907
+ return requestedURL;
1908
+ }
1909
+
1910
+ const VERSION = "1.2.1";
1911
+
1912
+ function parseProtocol(url) {
1913
+ const match = /^([-+\w]{1,25})(:?\/\/|:)/.exec(url);
1914
+ return match && match[1] || '';
1915
+ }
1916
+
1917
+ const DATA_URL_PATTERN = /^(?:([^;]+);)?(?:[^;]+;)?(base64|),([\s\S]*)$/;
1918
+
1919
+ /**
1920
+ * Parse data uri to a Buffer or Blob
1921
+ *
1922
+ * @param {String} uri
1923
+ * @param {?Boolean} asBlob
1924
+ * @param {?Object} options
1925
+ * @param {?Function} options.Blob
1926
+ *
1927
+ * @returns {Buffer|Blob}
1928
+ */
1929
+ function fromDataURI(uri, asBlob, options) {
1930
+ const _Blob = options && options.Blob || platform.classes.Blob;
1931
+ const protocol = parseProtocol(uri);
1932
+
1933
+ if (asBlob === undefined && _Blob) {
1934
+ asBlob = true;
1935
+ }
1936
+
1937
+ if (protocol === 'data') {
1938
+ uri = protocol.length ? uri.slice(protocol.length + 1) : uri;
1939
+
1940
+ const match = DATA_URL_PATTERN.exec(uri);
1941
+
1942
+ if (!match) {
1943
+ throw new AxiosError('Invalid URL', AxiosError.ERR_INVALID_URL);
1944
+ }
1945
+
1946
+ const mime = match[1];
1947
+ const isBase64 = match[2];
1948
+ const body = match[3];
1949
+ const buffer = Buffer.from(decodeURIComponent(body), isBase64 ? 'base64' : 'utf8');
1737
1950
 
1738
- return this;
1739
- }
1740
- }
1951
+ if (asBlob) {
1952
+ if (!_Blob) {
1953
+ throw new AxiosError('Blob is not supported', AxiosError.ERR_NOT_SUPPORT);
1954
+ }
1741
1955
 
1742
- AxiosHeaders.accessor(['Content-Type', 'Content-Length', 'Accept', 'Accept-Encoding', 'User-Agent']);
1956
+ return new _Blob([buffer], {type: mime});
1957
+ }
1743
1958
 
1744
- utils.freezeMethods(AxiosHeaders.prototype);
1745
- utils.freezeMethods(AxiosHeaders);
1959
+ return buffer;
1960
+ }
1746
1961
 
1747
- const AxiosHeaders$1 = AxiosHeaders;
1962
+ throw new AxiosError('Unsupported protocol ' + protocol, AxiosError.ERR_NOT_SUPPORT);
1963
+ }
1748
1964
 
1749
1965
  /**
1750
1966
  * Throttle decorator
@@ -1824,7 +2040,7 @@ function speedometer(samplesCount, min) {
1824
2040
 
1825
2041
  const passed = startedAt && now - startedAt;
1826
2042
 
1827
- return passed ? Math.round(bytesCount * 1000 / passed) : undefined;
2043
+ return passed ? Math.round(bytesCount * 1000 / passed) : undefined;
1828
2044
  };
1829
2045
  }
1830
2046
 
@@ -2013,6 +2229,11 @@ class AxiosTransformStream extends stream__default["default"].Transform{
2013
2229
 
2014
2230
  const AxiosTransformStream$1 = AxiosTransformStream;
2015
2231
 
2232
+ const zlibOptions = {
2233
+ flush: zlib__default["default"].constants.Z_SYNC_FLUSH,
2234
+ finishFlush: zlib__default["default"].constants.Z_SYNC_FLUSH
2235
+ };
2236
+
2016
2237
  const isBrotliSupported = utils.isFunction(zlib__default["default"].createBrotliDecompress);
2017
2238
 
2018
2239
  const {http: httpFollow, https: httpsFollow} = followRedirects__default["default"];
@@ -2093,8 +2314,10 @@ function setProxy(options, configProxy, location) {
2093
2314
  };
2094
2315
  }
2095
2316
 
2317
+ const isHttpAdapterSupported = typeof process !== 'undefined' && utils.kindOf(process) === 'process';
2318
+
2096
2319
  /*eslint consistent-return:0*/
2097
- function httpAdapter(config) {
2320
+ const httpAdapter = isHttpAdapterSupported && function httpAdapter(config) {
2098
2321
  return new Promise(function dispatchHttpRequest(resolvePromise, rejectPromise) {
2099
2322
  let data = config.data;
2100
2323
  const responseType = config.responseType;
@@ -2251,7 +2474,7 @@ function httpAdapter(config) {
2251
2474
  }
2252
2475
  }
2253
2476
 
2254
- const contentLength = +headers.getContentLength();
2477
+ const contentLength = utils.toFiniteNumber(headers.getContentLength());
2255
2478
 
2256
2479
  if (utils.isArray(maxRate)) {
2257
2480
  maxUploadRate = maxRate[0];
@@ -2266,7 +2489,7 @@ function httpAdapter(config) {
2266
2489
  }
2267
2490
 
2268
2491
  data = stream__default["default"].pipeline([data, new AxiosTransformStream$1({
2269
- length: utils.toFiniteNumber(contentLength),
2492
+ length: contentLength,
2270
2493
  maxRate: utils.toFiniteNumber(maxUploadRate)
2271
2494
  })], utils.noop);
2272
2495
 
@@ -2309,7 +2532,10 @@ function httpAdapter(config) {
2309
2532
  return reject(customErr);
2310
2533
  }
2311
2534
 
2312
- headers.set('Accept-Encoding', 'gzip, deflate, br', false);
2535
+ headers.set(
2536
+ 'Accept-Encoding',
2537
+ 'gzip, compress, deflate' + (isBrotliSupported ? ', br' : ''), false
2538
+ );
2313
2539
 
2314
2540
  const options = {
2315
2541
  path,
@@ -2364,17 +2590,34 @@ function httpAdapter(config) {
2364
2590
 
2365
2591
  const streams = [res];
2366
2592
 
2367
- // uncompress the response body transparently if required
2593
+ const responseLength = +res.headers['content-length'];
2594
+
2595
+ if (onDownloadProgress) {
2596
+ const transformStream = new AxiosTransformStream$1({
2597
+ length: utils.toFiniteNumber(responseLength),
2598
+ maxRate: utils.toFiniteNumber(maxDownloadRate)
2599
+ });
2600
+
2601
+ onDownloadProgress && transformStream.on('progress', progress => {
2602
+ onDownloadProgress(Object.assign(progress, {
2603
+ download: true
2604
+ }));
2605
+ });
2606
+
2607
+ streams.push(transformStream);
2608
+ }
2609
+
2610
+ // decompress the response body transparently if required
2368
2611
  let responseStream = res;
2369
2612
 
2370
2613
  // return the last request in case of redirects
2371
2614
  const lastRequest = res.req || req;
2372
2615
 
2373
2616
  // if decompress disabled we should not decompress
2374
- if (config.decompress !== false) {
2617
+ if (config.decompress !== false && res.headers['content-encoding']) {
2375
2618
  // if no content, but headers still say that it is encoded,
2376
2619
  // remove the header not confuse downstream operations
2377
- if (data && data.length === 0 && res.headers['content-encoding']) {
2620
+ if (method === 'HEAD' || res.statusCode === 204) {
2378
2621
  delete res.headers['content-encoding'];
2379
2622
  }
2380
2623
 
@@ -2384,36 +2627,19 @@ function httpAdapter(config) {
2384
2627
  case 'compress':
2385
2628
  case 'deflate':
2386
2629
  // add the unzipper to the body stream processing pipeline
2387
- streams.push(zlib__default["default"].createUnzip());
2630
+ streams.push(zlib__default["default"].createUnzip(zlibOptions));
2388
2631
 
2389
2632
  // remove the content-encoding in order to not confuse downstream operations
2390
2633
  delete res.headers['content-encoding'];
2391
2634
  break;
2392
2635
  case 'br':
2393
2636
  if (isBrotliSupported) {
2394
- streams.push(zlib__default["default"].createBrotliDecompress());
2637
+ streams.push(zlib__default["default"].createBrotliDecompress(zlibOptions));
2395
2638
  delete res.headers['content-encoding'];
2396
2639
  }
2397
2640
  }
2398
2641
  }
2399
2642
 
2400
- if (onDownloadProgress) {
2401
- const responseLength = +res.headers['content-length'];
2402
-
2403
- const transformStream = new AxiosTransformStream$1({
2404
- length: utils.toFiniteNumber(responseLength),
2405
- maxRate: utils.toFiniteNumber(maxDownloadRate)
2406
- });
2407
-
2408
- onDownloadProgress && transformStream.on('progress', progress => {
2409
- onDownloadProgress(Object.assign(progress, {
2410
- download: true
2411
- }));
2412
- });
2413
-
2414
- streams.push(transformStream);
2415
- }
2416
-
2417
2643
  responseStream = streams.length > 1 ? stream__default["default"].pipeline(streams, utils.noop) : streams[0];
2418
2644
 
2419
2645
  const offListeners = stream__default["default"].finished(responseStream, () => {
@@ -2577,7 +2803,7 @@ function httpAdapter(config) {
2577
2803
  req.end(data);
2578
2804
  }
2579
2805
  });
2580
- }
2806
+ };
2581
2807
 
2582
2808
  const cookies = platform.isStandardBrowserEnv ?
2583
2809
 
@@ -2719,7 +2945,9 @@ function progressEventReducer(listener, isDownloadStream) {
2719
2945
  };
2720
2946
  }
2721
2947
 
2722
- function xhrAdapter(config) {
2948
+ const isXHRAdapterSupported = typeof XMLHttpRequest !== 'undefined';
2949
+
2950
+ const xhrAdapter = isXHRAdapterSupported && function (config) {
2723
2951
  return new Promise(function dispatchXhrRequest(resolve, reject) {
2724
2952
  let requestData = config.data;
2725
2953
  const requestHeaders = AxiosHeaders$1.from(config.headers).normalize();
@@ -2735,7 +2963,7 @@ function xhrAdapter(config) {
2735
2963
  }
2736
2964
  }
2737
2965
 
2738
- if (utils.isFormData(requestData) && platform.isStandardBrowserEnv) {
2966
+ if (utils.isFormData(requestData) && (platform.isStandardBrowserEnv || platform.isStandardBrowserWebWorkerEnv)) {
2739
2967
  requestHeaders.setContentType(false); // Let the browser set it
2740
2968
  }
2741
2969
 
@@ -2763,7 +2991,7 @@ function xhrAdapter(config) {
2763
2991
  const responseHeaders = AxiosHeaders$1.from(
2764
2992
  'getAllResponseHeaders' in request && request.getAllResponseHeaders()
2765
2993
  );
2766
- const responseData = !responseType || responseType === 'text' || responseType === 'json' ?
2994
+ const responseData = !responseType || responseType === 'text' || responseType === 'json' ?
2767
2995
  request.responseText : request.response;
2768
2996
  const response = {
2769
2997
  data: responseData,
@@ -2920,240 +3148,63 @@ function xhrAdapter(config) {
2920
3148
  // Send the request
2921
3149
  request.send(requestData || null);
2922
3150
  });
2923
- }
3151
+ };
2924
3152
 
2925
- const adapters = {
3153
+ const knownAdapters = {
2926
3154
  http: httpAdapter,
2927
3155
  xhr: xhrAdapter
2928
3156
  };
2929
3157
 
2930
- const adapters$1 = {
2931
- getAdapter: (nameOrAdapter) => {
2932
- if(utils.isString(nameOrAdapter)){
2933
- const adapter = adapters[nameOrAdapter];
2934
-
2935
- if (!nameOrAdapter) {
2936
- throw Error(
2937
- utils.hasOwnProp(nameOrAdapter) ?
2938
- `Adapter '${nameOrAdapter}' is not available in the build` :
2939
- `Can not resolve adapter '${nameOrAdapter}'`
2940
- );
2941
- }
2942
-
2943
- return adapter
2944
- }
2945
-
2946
- if (!utils.isFunction(nameOrAdapter)) {
2947
- throw new TypeError('adapter is not a function');
2948
- }
2949
-
2950
- return nameOrAdapter;
2951
- },
2952
- adapters
2953
- };
2954
-
2955
- const DEFAULT_CONTENT_TYPE = {
2956
- 'Content-Type': undefined
2957
- };
2958
-
2959
- /**
2960
- * If the browser has an XMLHttpRequest object, use the XHR adapter, otherwise use the HTTP
2961
- * adapter
2962
- *
2963
- * @returns {Function}
2964
- */
2965
- function getDefaultAdapter() {
2966
- let adapter;
2967
- if (typeof XMLHttpRequest !== 'undefined') {
2968
- // For browsers use XHR adapter
2969
- adapter = adapters$1.getAdapter('xhr');
2970
- } else if (typeof process !== 'undefined' && utils.kindOf(process) === 'process') {
2971
- // For node use HTTP adapter
2972
- adapter = adapters$1.getAdapter('http');
2973
- }
2974
- return adapter;
2975
- }
2976
-
2977
- /**
2978
- * It takes a string, tries to parse it, and if it fails, it returns the stringified version
2979
- * of the input
2980
- *
2981
- * @param {any} rawValue - The value to be stringified.
2982
- * @param {Function} parser - A function that parses a string into a JavaScript object.
2983
- * @param {Function} encoder - A function that takes a value and returns a string.
2984
- *
2985
- * @returns {string} A stringified version of the rawValue.
2986
- */
2987
- function stringifySafely(rawValue, parser, encoder) {
2988
- if (utils.isString(rawValue)) {
3158
+ utils.forEach(knownAdapters, (fn, value) => {
3159
+ if(fn) {
2989
3160
  try {
2990
- (parser || JSON.parse)(rawValue);
2991
- return utils.trim(rawValue);
3161
+ Object.defineProperty(fn, 'name', {value});
2992
3162
  } catch (e) {
2993
- if (e.name !== 'SyntaxError') {
2994
- throw e;
2995
- }
3163
+ // eslint-disable-next-line no-empty
2996
3164
  }
3165
+ Object.defineProperty(fn, 'adapterName', {value});
2997
3166
  }
3167
+ });
2998
3168
 
2999
- return (encoder || JSON.stringify)(rawValue);
3000
- }
3001
-
3002
- const defaults = {
3003
-
3004
- transitional: transitionalDefaults,
3005
-
3006
- adapter: getDefaultAdapter(),
3007
-
3008
- transformRequest: [function transformRequest(data, headers) {
3009
- const contentType = headers.getContentType() || '';
3010
- const hasJSONContentType = contentType.indexOf('application/json') > -1;
3011
- const isObjectPayload = utils.isObject(data);
3012
-
3013
- if (isObjectPayload && utils.isHTMLForm(data)) {
3014
- data = new FormData(data);
3015
- }
3169
+ const adapters = {
3170
+ getAdapter: (adapters) => {
3171
+ adapters = utils.isArray(adapters) ? adapters : [adapters];
3016
3172
 
3017
- const isFormData = utils.isFormData(data);
3173
+ const {length} = adapters;
3174
+ let nameOrAdapter;
3175
+ let adapter;
3018
3176
 
3019
- if (isFormData) {
3020
- if (!hasJSONContentType) {
3021
- return data;
3177
+ for (let i = 0; i < length; i++) {
3178
+ nameOrAdapter = adapters[i];
3179
+ if((adapter = utils.isString(nameOrAdapter) ? knownAdapters[nameOrAdapter.toLowerCase()] : nameOrAdapter)) {
3180
+ break;
3022
3181
  }
3023
- return hasJSONContentType ? JSON.stringify(formDataToJSON(data)) : data;
3024
- }
3025
-
3026
- if (utils.isArrayBuffer(data) ||
3027
- utils.isBuffer(data) ||
3028
- utils.isStream(data) ||
3029
- utils.isFile(data) ||
3030
- utils.isBlob(data)
3031
- ) {
3032
- return data;
3033
- }
3034
- if (utils.isArrayBufferView(data)) {
3035
- return data.buffer;
3036
3182
  }
3037
- if (utils.isURLSearchParams(data)) {
3038
- headers.setContentType('application/x-www-form-urlencoded;charset=utf-8', false);
3039
- return data.toString();
3040
- }
3041
-
3042
- let isFileList;
3043
-
3044
- if (isObjectPayload) {
3045
- if (contentType.indexOf('application/x-www-form-urlencoded') > -1) {
3046
- return toURLEncodedForm(data, this.formSerializer).toString();
3047
- }
3048
-
3049
- if ((isFileList = utils.isFileList(data)) || contentType.indexOf('multipart/form-data') > -1) {
3050
- const _FormData = this.env && this.env.FormData;
3051
3183
 
3052
- return toFormData(
3053
- isFileList ? {'files[]': data} : data,
3054
- _FormData && new _FormData(),
3055
- this.formSerializer
3184
+ if (!adapter) {
3185
+ if (adapter === false) {
3186
+ throw new AxiosError(
3187
+ `Adapter ${nameOrAdapter} is not supported by the environment`,
3188
+ 'ERR_NOT_SUPPORT'
3056
3189
  );
3057
3190
  }
3058
- }
3059
3191
 
3060
- if (isObjectPayload || hasJSONContentType ) {
3061
- headers.setContentType('application/json', false);
3062
- return stringifySafely(data);
3192
+ throw new Error(
3193
+ utils.hasOwnProp(knownAdapters, nameOrAdapter) ?
3194
+ `Adapter '${nameOrAdapter}' is not available in the build` :
3195
+ `Unknown adapter '${nameOrAdapter}'`
3196
+ );
3063
3197
  }
3064
3198
 
3065
- return data;
3066
- }],
3067
-
3068
- transformResponse: [function transformResponse(data) {
3069
- const transitional = this.transitional || defaults.transitional;
3070
- const forcedJSONParsing = transitional && transitional.forcedJSONParsing;
3071
- const JSONRequested = this.responseType === 'json';
3072
-
3073
- if (data && utils.isString(data) && ((forcedJSONParsing && !this.responseType) || JSONRequested)) {
3074
- const silentJSONParsing = transitional && transitional.silentJSONParsing;
3075
- const strictJSONParsing = !silentJSONParsing && JSONRequested;
3076
-
3077
- try {
3078
- return JSON.parse(data);
3079
- } catch (e) {
3080
- if (strictJSONParsing) {
3081
- if (e.name === 'SyntaxError') {
3082
- throw AxiosError.from(e, AxiosError.ERR_BAD_RESPONSE, this, null, this.response);
3083
- }
3084
- throw e;
3085
- }
3086
- }
3199
+ if (!utils.isFunction(adapter)) {
3200
+ throw new TypeError('adapter is not a function');
3087
3201
  }
3088
3202
 
3089
- return data;
3090
- }],
3091
-
3092
- /**
3093
- * A timeout in milliseconds to abort a request. If set to 0 (default) a
3094
- * timeout is not created.
3095
- */
3096
- timeout: 0,
3097
-
3098
- xsrfCookieName: 'XSRF-TOKEN',
3099
- xsrfHeaderName: 'X-XSRF-TOKEN',
3100
-
3101
- maxContentLength: -1,
3102
- maxBodyLength: -1,
3103
-
3104
- env: {
3105
- FormData: platform.classes.FormData,
3106
- Blob: platform.classes.Blob
3107
- },
3108
-
3109
- validateStatus: function validateStatus(status) {
3110
- return status >= 200 && status < 300;
3203
+ return adapter;
3111
3204
  },
3112
-
3113
- headers: {
3114
- common: {
3115
- 'Accept': 'application/json, text/plain, */*'
3116
- }
3117
- }
3205
+ adapters: knownAdapters
3118
3206
  };
3119
3207
 
3120
- utils.forEach(['delete', 'get', 'head'], function forEachMethodNoData(method) {
3121
- defaults.headers[method] = {};
3122
- });
3123
-
3124
- utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
3125
- defaults.headers[method] = utils.merge(DEFAULT_CONTENT_TYPE);
3126
- });
3127
-
3128
- const defaults$1 = defaults;
3129
-
3130
- /**
3131
- * Transform the data for a request or a response
3132
- *
3133
- * @param {Array|Function} fns A single function or Array of functions
3134
- * @param {?Object} response The response object
3135
- *
3136
- * @returns {*} The resulting transformed data
3137
- */
3138
- function transformData(fns, response) {
3139
- const config = this || defaults$1;
3140
- const context = response || config;
3141
- const headers = AxiosHeaders$1.from(context.headers);
3142
- let data = context.data;
3143
-
3144
- utils.forEach(fns, function transform(fn) {
3145
- data = fn.call(config, data, headers.normalize(), response ? response.status : undefined);
3146
- });
3147
-
3148
- headers.normalize();
3149
-
3150
- return data;
3151
- }
3152
-
3153
- function isCancel(value) {
3154
- return !!(value && value.__CANCEL__);
3155
- }
3156
-
3157
3208
  /**
3158
3209
  * Throws a `CanceledError` if cancellation has been requested.
3159
3210
  *
@@ -3167,7 +3218,7 @@ function throwIfCancellationRequested(config) {
3167
3218
  }
3168
3219
 
3169
3220
  if (config.signal && config.signal.aborted) {
3170
- throw new CanceledError();
3221
+ throw new CanceledError(null, config);
3171
3222
  }
3172
3223
  }
3173
3224
 
@@ -3193,7 +3244,7 @@ function dispatchRequest(config) {
3193
3244
  config.headers.setContentType('application/x-www-form-urlencoded', false);
3194
3245
  }
3195
3246
 
3196
- const adapter = config.adapter || defaults$1.adapter;
3247
+ const adapter = adapters.getAdapter(config.adapter || defaults$1.adapter);
3197
3248
 
3198
3249
  return adapter(config).then(function onAdapterResolution(response) {
3199
3250
  throwIfCancellationRequested(config);
@@ -3812,6 +3863,9 @@ axios.spread = spread;
3812
3863
  // Expose isAxiosError
3813
3864
  axios.isAxiosError = isAxiosError;
3814
3865
 
3866
+ // Expose mergeConfig
3867
+ axios.mergeConfig = mergeConfig;
3868
+
3815
3869
  axios.AxiosHeaders = AxiosHeaders$1;
3816
3870
 
3817
3871
  axios.formToJSON = thing => formDataToJSON(utils.isHTMLForm(thing) ? new FormData(thing) : thing);