mirror of
https://github.com/tenrok/axios.git
synced 2026-06-11 18:02:32 +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:
+220
-116
@@ -1,16 +1,16 @@
|
||||
'use strict';
|
||||
|
||||
var bind = require('./helpers/bind');
|
||||
import bind from './helpers/bind.js';
|
||||
|
||||
// utils is a library of generic helper functions non-specific to axios
|
||||
|
||||
var toString = Object.prototype.toString;
|
||||
const toString = Object.prototype.toString;
|
||||
|
||||
// eslint-disable-next-line func-names
|
||||
var kindOf = (function(cache) {
|
||||
const kindOf = (cache => {
|
||||
// eslint-disable-next-line func-names
|
||||
return function(thing) {
|
||||
var str = toString.call(thing);
|
||||
return thing => {
|
||||
const str = toString.call(thing);
|
||||
return cache[str] || (cache[str] = str.slice(8, -1).toLowerCase());
|
||||
};
|
||||
})(Object.create(null));
|
||||
@@ -22,6 +22,12 @@ function kindOfTest(type) {
|
||||
};
|
||||
}
|
||||
|
||||
function typeOfTest(type) {
|
||||
return thing => {
|
||||
return typeof thing === type;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a value is an Array
|
||||
*
|
||||
@@ -36,18 +42,16 @@ function isArray(val) {
|
||||
/**
|
||||
* Determine if a value is undefined
|
||||
*
|
||||
* @param {Object} val The value to test
|
||||
* @param {*} val The value to test
|
||||
*
|
||||
* @returns {boolean} True if the value is undefined, otherwise false
|
||||
*/
|
||||
function isUndefined(val) {
|
||||
return typeof val === 'undefined';
|
||||
}
|
||||
const isUndefined = typeOfTest('undefined');
|
||||
|
||||
/**
|
||||
* Determine if a value is a Buffer
|
||||
*
|
||||
* @param {Object} val The value to test
|
||||
* @param {*} val The value to test
|
||||
*
|
||||
* @returns {boolean} True if value is a Buffer, otherwise false
|
||||
*/
|
||||
@@ -59,22 +63,22 @@ function isBuffer(val) {
|
||||
/**
|
||||
* Determine if a value is an ArrayBuffer
|
||||
*
|
||||
* @param {Object} val The value to test
|
||||
* @param {*} val The value to test
|
||||
*
|
||||
* @returns {boolean} True if value is an ArrayBuffer, otherwise false
|
||||
*/
|
||||
var isArrayBuffer = kindOfTest('ArrayBuffer');
|
||||
const isArrayBuffer = kindOfTest('ArrayBuffer');
|
||||
|
||||
|
||||
/**
|
||||
* Determine if a value is a view on an ArrayBuffer
|
||||
*
|
||||
* @param {Object} val The value to test
|
||||
* @param {*} val The value to test
|
||||
*
|
||||
* @returns {boolean} True if value is a view on an ArrayBuffer, otherwise false
|
||||
*/
|
||||
function isArrayBufferView(val) {
|
||||
var result;
|
||||
let result;
|
||||
if ((typeof ArrayBuffer !== 'undefined') && (ArrayBuffer.isView)) {
|
||||
result = ArrayBuffer.isView(val);
|
||||
} else {
|
||||
@@ -86,40 +90,54 @@ function isArrayBufferView(val) {
|
||||
/**
|
||||
* Determine if a value is a String
|
||||
*
|
||||
* @param {Object} val The value to test
|
||||
* @param {*} val The value to test
|
||||
*
|
||||
* @returns {boolean} True if value is a String, otherwise false
|
||||
*/
|
||||
function isString(val) {
|
||||
return typeof val === 'string';
|
||||
}
|
||||
const isString = typeOfTest('string');
|
||||
|
||||
/**
|
||||
* Determine if a value is a Function
|
||||
*
|
||||
* @param {*} val The value to test
|
||||
* @returns {boolean} True if value is a Function, otherwise false
|
||||
*/
|
||||
const isFunction = typeOfTest('function');
|
||||
|
||||
/**
|
||||
* Determine if a value is a Number
|
||||
*
|
||||
* @param {Object} val The value to test
|
||||
* @param {*} val The value to test
|
||||
*
|
||||
* @returns {boolean} True if value is a Number, otherwise false
|
||||
*/
|
||||
function isNumber(val) {
|
||||
return typeof val === 'number';
|
||||
}
|
||||
const isNumber = typeOfTest('number');
|
||||
|
||||
/**
|
||||
* Determine if a value is an Object
|
||||
*
|
||||
* @param {Object} val The value to test
|
||||
* @param {*} thing The value to test
|
||||
*
|
||||
* @returns {boolean} True if value is an Object, otherwise false
|
||||
*/
|
||||
function isObject(val) {
|
||||
return val !== null && typeof val === 'object';
|
||||
function isObject(thing) {
|
||||
return thing !== null && typeof thing === 'object';
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a value is a Boolean
|
||||
*
|
||||
* @param {*} thing The value to test
|
||||
* @returns {boolean} True if value is a Boolean, otherwise false
|
||||
*/
|
||||
const isBoolean = thing => {
|
||||
return thing === true || thing === false;
|
||||
};
|
||||
|
||||
/**
|
||||
* Determine if a value is a plain Object
|
||||
*
|
||||
* @param {Object} val The value to test
|
||||
* @param {*} val The value to test
|
||||
*
|
||||
* @returns {boolean} True if value is a plain Object, otherwise false
|
||||
*/
|
||||
@@ -128,61 +146,50 @@ function isPlainObject(val) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var prototype = Object.getPrototypeOf(val);
|
||||
const prototype = Object.getPrototypeOf(val);
|
||||
return prototype === null || prototype === Object.prototype;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a value is a Date
|
||||
*
|
||||
* @param {Object} val The value to test
|
||||
* @param {*} val The value to test
|
||||
*
|
||||
* @returns {boolean} True if value is a Date, otherwise false
|
||||
*/
|
||||
var isDate = kindOfTest('Date');
|
||||
const isDate = kindOfTest('Date');
|
||||
|
||||
/**
|
||||
* Determine if a value is a File
|
||||
*
|
||||
* @param {Object} val The value to test
|
||||
* @param {*} val The value to test
|
||||
*
|
||||
* @returns {boolean} True if value is a File, otherwise false
|
||||
*/
|
||||
var isFile = kindOfTest('File');
|
||||
const isFile = kindOfTest('File');
|
||||
|
||||
/**
|
||||
* Determine if a value is a Blob
|
||||
*
|
||||
* @param {Object} val The value to test
|
||||
* @param {*} val The value to test
|
||||
*
|
||||
* @returns {boolean} True if value is a Blob, otherwise false
|
||||
*/
|
||||
var isBlob = kindOfTest('Blob');
|
||||
const isBlob = kindOfTest('Blob');
|
||||
|
||||
/**
|
||||
* Determine if a value is a FileList
|
||||
*
|
||||
* @param {Object} val The value to test
|
||||
* @param {*} val The value to test
|
||||
*
|
||||
* @returns {boolean} True if value is a File, otherwise false
|
||||
*/
|
||||
var isFileList = kindOfTest('FileList');
|
||||
|
||||
/**
|
||||
* Determine if a value is a Function
|
||||
*
|
||||
* @param {Object} val The value to test
|
||||
*
|
||||
* @returns {boolean} True if value is a Function, otherwise false
|
||||
*/
|
||||
function isFunction(val) {
|
||||
return toString.call(val) === '[object Function]';
|
||||
}
|
||||
const isFileList = kindOfTest('FileList');
|
||||
|
||||
/**
|
||||
* Determine if a value is a Stream
|
||||
*
|
||||
* @param {Object} val The value to test
|
||||
* @param {*} val The value to test
|
||||
*
|
||||
* @returns {boolean} True if value is a Stream, otherwise false
|
||||
*/
|
||||
@@ -193,12 +200,12 @@ function isStream(val) {
|
||||
/**
|
||||
* Determine if a value is a FormData
|
||||
*
|
||||
* @param {Object} thing The value to test
|
||||
* @param {*} thing The value to test
|
||||
*
|
||||
* @returns {boolean} True if value is an FormData, otherwise false
|
||||
*/
|
||||
function isFormData(thing) {
|
||||
var pattern = '[object FormData]';
|
||||
const pattern = '[object FormData]';
|
||||
return thing && (
|
||||
(typeof FormData === 'function' && thing instanceof FormData) ||
|
||||
toString.call(thing) === pattern ||
|
||||
@@ -209,11 +216,11 @@ function isFormData(thing) {
|
||||
/**
|
||||
* Determine if a value is a URLSearchParams object
|
||||
*
|
||||
* @param {Object} val The value to test
|
||||
* @param {*} val The value to test
|
||||
*
|
||||
* @returns {boolean} True if value is a URLSearchParams object, otherwise false
|
||||
*/
|
||||
var isURLSearchParams = kindOfTest('URLSearchParams');
|
||||
const isURLSearchParams = kindOfTest('URLSearchParams');
|
||||
|
||||
/**
|
||||
* Trim excess whitespace off the beginning and end of a string
|
||||
@@ -244,7 +251,7 @@ function trim(str) {
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function isStandardBrowserEnv() {
|
||||
var product;
|
||||
let product;
|
||||
if (typeof navigator !== 'undefined' && (
|
||||
(product = navigator.product) === 'ReactNative' ||
|
||||
product === 'NativeScript' ||
|
||||
@@ -268,14 +275,18 @@ function isStandardBrowserEnv() {
|
||||
* @param {Object|Array} obj The object to iterate
|
||||
* @param {Function} fn The callback to invoke for each item
|
||||
*
|
||||
* @param {Boolean} [allOwnKeys = false]
|
||||
* @returns {void}
|
||||
*/
|
||||
function forEach(obj, fn) {
|
||||
function forEach(obj, fn, {allOwnKeys = false} = {}) {
|
||||
// Don't bother if no value provided
|
||||
if (obj === null || typeof obj === 'undefined') {
|
||||
return;
|
||||
}
|
||||
|
||||
let i;
|
||||
let l;
|
||||
|
||||
// Force an array if not already something iterable
|
||||
if (typeof obj !== 'object') {
|
||||
/*eslint no-param-reassign:0*/
|
||||
@@ -284,15 +295,18 @@ function forEach(obj, fn) {
|
||||
|
||||
if (isArray(obj)) {
|
||||
// Iterate over array values
|
||||
for (var i = 0, l = obj.length; i < l; i++) {
|
||||
for (i = 0, l = obj.length; i < l; i++) {
|
||||
fn.call(null, obj[i], i, obj);
|
||||
}
|
||||
} else {
|
||||
// Iterate over object keys
|
||||
for (var key in obj) {
|
||||
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
||||
fn.call(null, obj[key], key, obj);
|
||||
}
|
||||
const keys = allOwnKeys ? Object.getOwnPropertyNames(obj) : Object.keys(obj);
|
||||
const len = keys.length;
|
||||
let key;
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
key = keys[i];
|
||||
fn.call(null, obj[key], key, obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -316,7 +330,7 @@ function forEach(obj, fn) {
|
||||
* @returns {Object} Result of all merge properties
|
||||
*/
|
||||
function merge(/* obj1, obj2, obj3, ... */) {
|
||||
var result = {};
|
||||
const result = {};
|
||||
function assignValue(val, key) {
|
||||
if (isPlainObject(result[key]) && isPlainObject(val)) {
|
||||
result[key] = merge(result[key], val);
|
||||
@@ -329,8 +343,8 @@ function merge(/* obj1, obj2, obj3, ... */) {
|
||||
}
|
||||
}
|
||||
|
||||
for (var i = 0, l = arguments.length; i < l; i++) {
|
||||
forEach(arguments[i], assignValue);
|
||||
for (let i = 0, l = arguments.length; i < l; i++) {
|
||||
arguments[i] && forEach(arguments[i], assignValue);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -342,16 +356,17 @@ function merge(/* obj1, obj2, obj3, ... */) {
|
||||
* @param {Object} b The object to copy properties from
|
||||
* @param {Object} thisArg The object to bind function to
|
||||
*
|
||||
* @param {Boolean} [allOwnKeys]
|
||||
* @returns {Object} The resulting value of object a
|
||||
*/
|
||||
function extend(a, b, thisArg) {
|
||||
function extend(a, b, thisArg, {allOwnKeys}= {}) {
|
||||
forEach(b, function assignValue(val, key) {
|
||||
if (thisArg && typeof val === 'function') {
|
||||
a[key] = bind(val, thisArg);
|
||||
} else {
|
||||
a[key] = val;
|
||||
}
|
||||
});
|
||||
}, {allOwnKeys});
|
||||
return a;
|
||||
}
|
||||
|
||||
@@ -381,6 +396,9 @@ function stripBOM(content) {
|
||||
function inherits(constructor, superConstructor, props, descriptors) {
|
||||
constructor.prototype = Object.create(superConstructor.prototype, descriptors);
|
||||
constructor.prototype.constructor = constructor;
|
||||
Object.defineProperty(constructor, 'super', {
|
||||
value: superConstructor.prototype
|
||||
});
|
||||
props && Object.assign(constructor.prototype, props);
|
||||
}
|
||||
|
||||
@@ -394,10 +412,10 @@ function inherits(constructor, superConstructor, props, descriptors) {
|
||||
* @returns {Object}
|
||||
*/
|
||||
function toFlatObject(sourceObj, destObj, filter, propFilter) {
|
||||
var props;
|
||||
var i;
|
||||
var prop;
|
||||
var merged = {};
|
||||
let props;
|
||||
let i;
|
||||
let prop;
|
||||
const merged = {};
|
||||
|
||||
destObj = destObj || {};
|
||||
// eslint-disable-next-line no-eq-null,eqeqeq
|
||||
@@ -434,7 +452,7 @@ function endsWith(str, searchString, position) {
|
||||
position = str.length;
|
||||
}
|
||||
position -= searchString.length;
|
||||
var lastIndex = str.indexOf(searchString, position);
|
||||
const lastIndex = str.indexOf(searchString, position);
|
||||
return lastIndex !== -1 && lastIndex === position;
|
||||
}
|
||||
|
||||
@@ -449,9 +467,9 @@ function endsWith(str, searchString, position) {
|
||||
function toArray(thing) {
|
||||
if (!thing) return null;
|
||||
if (isArray(thing)) return thing;
|
||||
var i = thing.length;
|
||||
let i = thing.length;
|
||||
if (!isNumber(i)) return null;
|
||||
var arr = new Array(i);
|
||||
const arr = new Array(i);
|
||||
while (i-- > 0) {
|
||||
arr[i] = thing[i];
|
||||
}
|
||||
@@ -467,9 +485,9 @@ function toArray(thing) {
|
||||
* @returns {Array}
|
||||
*/
|
||||
// eslint-disable-next-line func-names
|
||||
var isTypedArray = (function(TypedArray) {
|
||||
const isTypedArray = (TypedArray => {
|
||||
// eslint-disable-next-line func-names
|
||||
return function(thing) {
|
||||
return thing => {
|
||||
return TypedArray && thing instanceof TypedArray;
|
||||
};
|
||||
})(typeof Uint8Array !== 'undefined' && Object.getPrototypeOf(Uint8Array));
|
||||
@@ -483,14 +501,14 @@ var isTypedArray = (function(TypedArray) {
|
||||
* @returns {void}
|
||||
*/
|
||||
function forEachEntry(obj, fn) {
|
||||
var generator = obj && obj[Symbol.iterator];
|
||||
const generator = obj && obj[Symbol.iterator];
|
||||
|
||||
var iterator = generator.call(obj);
|
||||
const iterator = generator.call(obj);
|
||||
|
||||
var result;
|
||||
let result;
|
||||
|
||||
while ((result = iterator.next()) && !result.done) {
|
||||
var pair = result.value;
|
||||
const pair = result.value;
|
||||
fn.call(obj, pair[0], pair[1]);
|
||||
}
|
||||
}
|
||||
@@ -504,8 +522,8 @@ function forEachEntry(obj, fn) {
|
||||
* @returns {Array<boolean>}
|
||||
*/
|
||||
function matchAll(regExp, str) {
|
||||
var matches;
|
||||
var arr = [];
|
||||
let matches;
|
||||
const arr = [];
|
||||
|
||||
while ((matches = regExp.exec(str)) !== null) {
|
||||
arr.push(matches);
|
||||
@@ -515,48 +533,134 @@ function matchAll(regExp, str) {
|
||||
}
|
||||
|
||||
/* Checking if the kindOfTest function returns true when passed an HTMLFormElement. */
|
||||
var isHTMLForm = kindOfTest('HTMLFormElement');
|
||||
const isHTMLForm = kindOfTest('HTMLFormElement');
|
||||
|
||||
const toCamelCase = str => {
|
||||
return str.toLowerCase().replace(/[_-\s]([a-z\d])(\w*)/g,
|
||||
function replacer(m, p1, p2) {
|
||||
return p1.toUpperCase() + p2;
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
/* Creating a function that will check if an object has a property. */
|
||||
var hasOwnProperty = (function resolver(_hasOwnProperty) {
|
||||
return function(obj, prop) {
|
||||
const hasOwnProperty = (function resolver(_hasOwnProperty) {
|
||||
return (obj, prop) => {
|
||||
return _hasOwnProperty.call(obj, prop);
|
||||
};
|
||||
})(Object.prototype.hasOwnProperty);
|
||||
|
||||
module.exports = {
|
||||
isArray: isArray,
|
||||
isArrayBuffer: isArrayBuffer,
|
||||
isBuffer: isBuffer,
|
||||
isFormData: isFormData,
|
||||
isArrayBufferView: isArrayBufferView,
|
||||
isString: isString,
|
||||
isNumber: isNumber,
|
||||
isObject: isObject,
|
||||
isPlainObject: isPlainObject,
|
||||
isUndefined: isUndefined,
|
||||
isDate: isDate,
|
||||
isFile: isFile,
|
||||
isBlob: isBlob,
|
||||
isFunction: isFunction,
|
||||
isStream: isStream,
|
||||
isURLSearchParams: isURLSearchParams,
|
||||
isStandardBrowserEnv: isStandardBrowserEnv,
|
||||
forEach: forEach,
|
||||
merge: merge,
|
||||
extend: extend,
|
||||
trim: trim,
|
||||
stripBOM: stripBOM,
|
||||
inherits: inherits,
|
||||
toFlatObject: toFlatObject,
|
||||
kindOf: kindOf,
|
||||
kindOfTest: kindOfTest,
|
||||
endsWith: endsWith,
|
||||
toArray: toArray,
|
||||
isTypedArray: isTypedArray,
|
||||
isFileList: isFileList,
|
||||
forEachEntry: forEachEntry,
|
||||
matchAll: matchAll,
|
||||
isHTMLForm: isHTMLForm,
|
||||
hasOwnProperty: hasOwnProperty
|
||||
/**
|
||||
* Determine if a value is a RegExp object
|
||||
*
|
||||
* @param {*} val The value to test
|
||||
*
|
||||
* @returns {boolean} True if value is a RegExp object, otherwise false
|
||||
*/
|
||||
const isRegExp = kindOfTest('RegExp');
|
||||
|
||||
function reduceDescriptors(obj, reducer) {
|
||||
const descriptors = Object.getOwnPropertyDescriptors(obj);
|
||||
const reducedDescriptors = {};
|
||||
|
||||
forEach(descriptors, (descriptor, name) => {
|
||||
if (reducer(descriptor, name, obj) !== false) {
|
||||
reducedDescriptors[name] = descriptor;
|
||||
}
|
||||
});
|
||||
|
||||
Object.defineProperties(obj, reducedDescriptors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes all methods read-only
|
||||
* @param {Object} obj
|
||||
*/
|
||||
|
||||
function freezeMethods(obj) {
|
||||
reduceDescriptors(obj, (descriptor, name) => {
|
||||
const value = obj[name];
|
||||
|
||||
if (!isFunction(value)) return;
|
||||
|
||||
descriptor.enumerable = false;
|
||||
|
||||
if ('writable' in descriptor) {
|
||||
descriptor.writable = false;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!descriptor.set) {
|
||||
descriptor.set = () => {
|
||||
throw Error('Can not read-only method \'' + name + '\'');
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function toObjectSet(arrayOrString, delimiter) {
|
||||
const obj = {};
|
||||
|
||||
function define(arr) {
|
||||
arr.forEach(value => {
|
||||
obj[value] = true;
|
||||
});
|
||||
}
|
||||
|
||||
isArray(arrayOrString) ? define(arrayOrString) : define(String(arrayOrString).split(delimiter));
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
function noop() {}
|
||||
|
||||
function toFiniteNumber(value, defaultValue) {
|
||||
value = +value;
|
||||
return Number.isFinite(value) ? value : defaultValue;
|
||||
}
|
||||
|
||||
export default {
|
||||
isArray,
|
||||
isArrayBuffer,
|
||||
isBuffer,
|
||||
isFormData,
|
||||
isArrayBufferView,
|
||||
isString,
|
||||
isNumber,
|
||||
isBoolean,
|
||||
isObject,
|
||||
isPlainObject,
|
||||
isUndefined,
|
||||
isDate,
|
||||
isFile,
|
||||
isBlob,
|
||||
isRegExp,
|
||||
isFunction,
|
||||
isStream,
|
||||
isURLSearchParams,
|
||||
isTypedArray,
|
||||
isFileList,
|
||||
isStandardBrowserEnv,
|
||||
forEach,
|
||||
merge,
|
||||
extend,
|
||||
trim,
|
||||
stripBOM,
|
||||
inherits,
|
||||
toFlatObject,
|
||||
kindOf,
|
||||
kindOfTest,
|
||||
endsWith,
|
||||
toArray,
|
||||
forEachEntry,
|
||||
matchAll,
|
||||
isHTMLForm,
|
||||
hasOwnProperty,
|
||||
hasOwnProp: hasOwnProperty, // an alias to avoid ESLint no-prototype-builtins detection
|
||||
reduceDescriptors,
|
||||
freezeMethods,
|
||||
toObjectSet,
|
||||
toCamelCase,
|
||||
noop,
|
||||
toFiniteNumber
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user