mirror of
https://github.com/tenrok/axios.git
synced 2026-06-14 18:42:33 +03:00
Axios ES2017 (#4787)
* Added AxiosHeaders class; * Fixed README.md href; * Fixed a potential bug with headers normalization; * Fixed a potential bug with headers normalization; Refactored accessor building routine; Refactored default transforms; Removed `normalizeHeaderName` helper; * Added `Content-Length` accessor; Added missed `has` accessor to TS types; * Added `AxiosTransformStream` class; Added progress capturing ability for node.js environment; Added `maxRate` option to limit the data rate in node.js environment; Refactored event handled by `onUploadProgress` && `onDownloadProgress` listeners in browser environment; Added progress & data rate tests for the http adapter; Added response stream aborting test; Added a manual progress capture test for the browser; Updated TS types; Added TS tests; Refactored request abort logic for the http adapter; Added ability to abort the response stream; * Remove `stream/promises` & `timers/promises` modules usage in tests; * Use `abortcontroller-polyfill`; * Fixed AxiosTransformStream dead-lock in legacy node versions; Fixed CancelError emitting in streams; * Reworked AxiosTransformStream internal logic to optimize memory consumption; Added throwing an error if the request stream was silently destroying (without error) Refers to #3966; * Treat the destruction of the request stream as a cancellation of the request; Fixed tests; * Emit `progress` event in the next tick; * Initial refactoring; * Refactored Mocha tests to use ESM; * Refactored Karma tests to use rollup preprocessor & ESM; Replaced grunt with gulp; Improved dev scripts; Added Babel for rollup build; * Added default commonjs package export for Node build; Added automatic contributors list generator for package.json; Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
+29
-48
@@ -1,34 +1,18 @@
|
||||
'use strict';
|
||||
|
||||
var utils = require('../utils');
|
||||
var normalizeHeaderName = require('../helpers/normalizeHeaderName');
|
||||
var AxiosError = require('../core/AxiosError');
|
||||
var transitionalDefaults = require('./transitional');
|
||||
var toFormData = require('../helpers/toFormData');
|
||||
var toURLEncodedForm = require('../helpers/toURLEncodedForm');
|
||||
var platform = require('../platform');
|
||||
var formDataToJSON = require('../helpers/formDataToJSON');
|
||||
import utils from '../utils.js';
|
||||
import AxiosError from '../core/AxiosError.js';
|
||||
import transitionalDefaults from './transitional.js';
|
||||
import toFormData from '../helpers/toFormData.js';
|
||||
import toURLEncodedForm from '../helpers/toURLEncodedForm.js';
|
||||
import platform from '../platform/index.js';
|
||||
import formDataToJSON from '../helpers/formDataToJSON.js';
|
||||
import adapters from '../adapters/index.js';
|
||||
|
||||
var DEFAULT_CONTENT_TYPE = {
|
||||
const DEFAULT_CONTENT_TYPE = {
|
||||
'Content-Type': 'application/x-www-form-urlencoded'
|
||||
};
|
||||
|
||||
/**
|
||||
* If the headers object is not undefined and the Content-Type property of the headers object
|
||||
* is undefined, then set the Content-Type property of the headers object to the value passed
|
||||
* in
|
||||
*
|
||||
* @param {Object<string, string>} headers - The headers object that will be sent to the server.
|
||||
* @param {any} value - The value of the Content-Type header.
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
function setContentTypeIfUnset(headers, value) {
|
||||
if (!utils.isUndefined(headers) && utils.isUndefined(headers['Content-Type'])) {
|
||||
headers['Content-Type'] = value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If the browser has an XMLHttpRequest object, use the XHR adapter, otherwise use the HTTP
|
||||
* adapter
|
||||
@@ -36,13 +20,13 @@ function setContentTypeIfUnset(headers, value) {
|
||||
* @returns {Function}
|
||||
*/
|
||||
function getDefaultAdapter() {
|
||||
var adapter;
|
||||
let adapter;
|
||||
if (typeof XMLHttpRequest !== 'undefined') {
|
||||
// For browsers use XHR adapter
|
||||
adapter = require('../adapters/xhr');
|
||||
} else if (typeof process !== 'undefined' && Object.prototype.toString.call(process) === '[object process]') {
|
||||
adapter = adapters.getAdapter('xhr');
|
||||
} else if (typeof process !== 'undefined' && utils.kindOf(process) === 'process') {
|
||||
// For node use HTTP adapter
|
||||
adapter = require('../adapters/http');
|
||||
adapter = adapters.getAdapter('http');
|
||||
}
|
||||
return adapter;
|
||||
}
|
||||
@@ -72,25 +56,22 @@ function stringifySafely(rawValue, parser, encoder) {
|
||||
return (encoder || JSON.stringify)(rawValue);
|
||||
}
|
||||
|
||||
var defaults = {
|
||||
const defaults = {
|
||||
|
||||
transitional: transitionalDefaults,
|
||||
|
||||
adapter: getDefaultAdapter(),
|
||||
|
||||
transformRequest: [function transformRequest(data, headers) {
|
||||
normalizeHeaderName(headers, 'Accept');
|
||||
normalizeHeaderName(headers, 'Content-Type');
|
||||
|
||||
var contentType = headers && headers['Content-Type'] || '';
|
||||
var hasJSONContentType = contentType.indexOf('application/json') > -1;
|
||||
var isObjectPayload = utils.isObject(data);
|
||||
const contentType = headers.getContentType() || '';
|
||||
const hasJSONContentType = contentType.indexOf('application/json') > -1;
|
||||
const isObjectPayload = utils.isObject(data);
|
||||
|
||||
if (isObjectPayload && utils.isHTMLForm(data)) {
|
||||
data = new FormData(data);
|
||||
}
|
||||
|
||||
var isFormData = utils.isFormData(data);
|
||||
const isFormData = utils.isFormData(data);
|
||||
|
||||
if (isFormData) {
|
||||
if (!hasJSONContentType) {
|
||||
@@ -111,19 +92,19 @@ var defaults = {
|
||||
return data.buffer;
|
||||
}
|
||||
if (utils.isURLSearchParams(data)) {
|
||||
setContentTypeIfUnset(headers, 'application/x-www-form-urlencoded;charset=utf-8');
|
||||
headers.setContentType('application/x-www-form-urlencoded;charset=utf-8', false);
|
||||
return data.toString();
|
||||
}
|
||||
|
||||
var isFileList;
|
||||
let isFileList;
|
||||
|
||||
if (isObjectPayload) {
|
||||
if (contentType.indexOf('application/x-www-form-urlencoded') !== -1) {
|
||||
if (contentType.indexOf('application/x-www-form-urlencoded') > -1) {
|
||||
return toURLEncodedForm(data, this.formSerializer).toString();
|
||||
}
|
||||
|
||||
if ((isFileList = utils.isFileList(data)) || contentType.indexOf('multipart/form-data') > -1) {
|
||||
var _FormData = this.env && this.env.FormData;
|
||||
const _FormData = this.env && this.env.FormData;
|
||||
|
||||
return toFormData(
|
||||
isFileList ? {'files[]': data} : data,
|
||||
@@ -134,7 +115,7 @@ var defaults = {
|
||||
}
|
||||
|
||||
if (isObjectPayload || hasJSONContentType ) {
|
||||
setContentTypeIfUnset(headers, 'application/json');
|
||||
headers.setContentType('application/json', false);
|
||||
return stringifySafely(data);
|
||||
}
|
||||
|
||||
@@ -142,13 +123,13 @@ var defaults = {
|
||||
}],
|
||||
|
||||
transformResponse: [function transformResponse(data) {
|
||||
var transitional = this.transitional || defaults.transitional;
|
||||
var forcedJSONParsing = transitional && transitional.forcedJSONParsing;
|
||||
var JSONRequested = this.responseType === 'json';
|
||||
const transitional = this.transitional || defaults.transitional;
|
||||
const forcedJSONParsing = transitional && transitional.forcedJSONParsing;
|
||||
const JSONRequested = this.responseType === 'json';
|
||||
|
||||
if (data && utils.isString(data) && ((forcedJSONParsing && !this.responseType) || JSONRequested)) {
|
||||
var silentJSONParsing = transitional && transitional.silentJSONParsing;
|
||||
var strictJSONParsing = !silentJSONParsing && JSONRequested;
|
||||
const silentJSONParsing = transitional && transitional.silentJSONParsing;
|
||||
const strictJSONParsing = !silentJSONParsing && JSONRequested;
|
||||
|
||||
try {
|
||||
return JSON.parse(data);
|
||||
@@ -201,4 +182,4 @@ utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
|
||||
defaults.headers[method] = utils.merge(DEFAULT_CONTENT_TYPE);
|
||||
});
|
||||
|
||||
module.exports = defaults;
|
||||
export default defaults;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = {
|
||||
export default {
|
||||
silentJSONParsing: true,
|
||||
forcedJSONParsing: true,
|
||||
clarifyTimeoutError: false
|
||||
|
||||
Reference in New Issue
Block a user