2
0
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:
Dmitriy Mozgovoy
2022-06-18 12:19:27 +03:00
committed by GitHub
parent 1db715dd3b
commit bdf493cf8b
125 changed files with 10462 additions and 7291 deletions
+13 -13
View File
@@ -1,10 +1,10 @@
var AxiosError = require('../../../lib/core/AxiosError');
import AxiosError from '../../../lib/core/AxiosError';
describe('core::AxiosError', function() {
it('should create an Error with message, config, code, request, response, stack and isAxiosError', function() {
var request = { path: '/foo' };
var response = { status: 200, data: { foo: 'bar' } };
var error = new AxiosError('Boom!', 'ESOMETHING', { foo: 'bar' }, request, response);
const request = { path: '/foo' };
const response = { status: 200, data: { foo: 'bar' } };
const error = new AxiosError('Boom!', 'ESOMETHING', { foo: 'bar' }, request, response);
expect(error instanceof Error).toBe(true);
expect(error.message).toBe('Boom!');
expect(error.config).toEqual({ foo: 'bar' });
@@ -17,10 +17,10 @@ describe('core::AxiosError', function() {
it('should create an Error that can be serialized to JSON', function() {
// Attempting to serialize request and response results in
// TypeError: Converting circular structure to JSON
var request = { path: '/foo' };
var response = { status: 200, data: { foo: 'bar' } };
var error = new AxiosError('Boom!', 'ESOMETHING', { foo: 'bar' }, request, response);
var json = error.toJSON();
const request = { path: '/foo' };
const response = { status: 200, data: { foo: 'bar' } };
const error = new AxiosError('Boom!', 'ESOMETHING', { foo: 'bar' }, request, response);
const json = error.toJSON();
expect(json.message).toBe('Boom!');
expect(json.config).toEqual({ foo: 'bar' });
expect(json.code).toBe('ESOMETHING');
@@ -31,11 +31,11 @@ describe('core::AxiosError', function() {
describe('core::createError.from', function() {
it('should add config, config, request and response to error', function() {
var error = new Error('Boom!');
var request = { path: '/foo' };
var response = { status: 200, data: { foo: 'bar' } };
const error = new Error('Boom!');
const request = { path: '/foo' };
const response = { status: 200, data: { foo: 'bar' } };
var axiosError = AxiosError.from(error, 'ESOMETHING', { foo: 'bar' }, request, response);
const axiosError = AxiosError.from(error, 'ESOMETHING', { foo: 'bar' }, request, response);
expect(axiosError.config).toEqual({ foo: 'bar' });
expect(axiosError.code).toBe('ESOMETHING');
expect(axiosError.request).toBe(request);
@@ -44,7 +44,7 @@ describe('core::AxiosError', function() {
});
it('should return error', function() {
var error = new Error('Boom!');
const error = new Error('Boom!');
expect(AxiosError.from(error, 'ESOMETHING', { foo: 'bar' }) instanceof AxiosError).toBeTruthy();
});
});
+1 -1
View File
@@ -1,4 +1,4 @@
var buildFullPath = require('../../../lib/core/buildFullPath');
import buildFullPath from '../../../lib/core/buildFullPath';
describe('helpers::buildFullPath', function () {
it('should combine URLs when the requestedURL is relative', function () {
+68 -68
View File
@@ -1,5 +1,5 @@
var defaults = require('../../../lib/defaults');
var mergeConfig = require('../../../lib/core/mergeConfig');
import defaults from '../../../lib/defaults';
import mergeConfig from '../../../lib/core/mergeConfig';
describe('core::mergeConfig', function() {
it('should accept undefined for second argument', function() {
@@ -11,19 +11,19 @@ describe('core::mergeConfig', function() {
});
it('should not leave references', function() {
var merged = mergeConfig(defaults, {});
const merged = mergeConfig(defaults, {});
expect(merged).not.toBe(defaults);
expect(merged.headers).not.toBe(defaults.headers);
});
it('should allow setting request options', function() {
var config = {
const config = {
url: '__sample url__',
method: '__sample method__',
params: '__sample params__',
data: { foo: true }
};
var merged = mergeConfig(defaults, config);
const merged = mergeConfig(defaults, config);
expect(merged.url).toEqual(config.url);
expect(merged.method).toEqual(config.method);
expect(merged.params).toEqual(config.params);
@@ -31,18 +31,18 @@ describe('core::mergeConfig', function() {
});
it('should not inherit request options', function() {
var localDefaults = {
const localDefaults = {
method: '__sample method__',
data: { foo: true }
};
var merged = mergeConfig(localDefaults, {});
const merged = mergeConfig(localDefaults, {});
expect(merged.method).toEqual(undefined);
expect(merged.data).toEqual(undefined);
});
['auth', 'headers', 'params', 'proxy'].forEach(function(key) {
it('should set new config for' + key + ' without default', function() {
var a = {}, b = {}, c = {}
const a = {}, b = {}, c = {};
a[key] = undefined
b[key] = { user: 'foo', pass: 'test' }
c[key] = { user: 'foo', pass: 'test' }
@@ -51,7 +51,7 @@ describe('core::mergeConfig', function() {
});
it('should merge ' + key + ' with defaults', function() {
var a = {}, b = {}, c = {};
const a = {}, b = {}, c = {};
a[key] = { user: 'foo', pass: 'bar' };
b[key] = { pass: 'test' };
c[key] = { user: 'foo', pass: 'test' };
@@ -61,7 +61,7 @@ describe('core::mergeConfig', function() {
it('should overwrite default ' + key + ' with a non-object value', function() {
[false, null, 123].forEach(function(value) {
var a = {}, b = {}, c = {};
const a = {}, b = {}, c = {};
a[key] = { user: 'foo', pass: 'test' };
b[key] = value;
c[key] = value;
@@ -72,22 +72,22 @@ describe('core::mergeConfig', function() {
});
it('should allow setting other options', function() {
var merged = mergeConfig(defaults, { timeout: 123 });
const merged = mergeConfig(defaults, { timeout: 123 });
expect(merged.timeout).toEqual(123);
});
it('should allow setting custom options', function() {
var merged = mergeConfig(defaults, { foo: 'bar' });
const merged = mergeConfig(defaults, { foo: 'bar' });
expect(merged.foo).toEqual('bar');
});
it('should allow setting custom default options', function() {
var merged = mergeConfig({ foo: 'bar' }, {});
const merged = mergeConfig({ foo: 'bar' }, {});
expect(merged.foo).toEqual('bar');
});
it('should allow merging custom objects in the config', function() {
var merged = mergeConfig({
const merged = mergeConfig({
nestedConfig: {
propertyOnDefaultConfig: true
}
@@ -101,28 +101,28 @@ describe('core::mergeConfig', function() {
});
describe('valueFromConfig2Keys', function() {
var config1 = {url: '/foo', method: 'post', data: {a: 3}};
const config1 = {url: '/foo', method: 'post', data: {a: 3}};
it('should skip if config2 is undefined', function() {
expect(mergeConfig(config1, {})).toEqual({});
});
it('should clone config2 if is plain object', function() {
var data = {a: 1, b: 2};
var merged = mergeConfig(config1, {data: data});
const data = {a: 1, b: 2};
const merged = mergeConfig(config1, {data: data});
expect(merged.data).toEqual(data);
expect(merged.data).not.toBe(data);
});
it('should clone config2 if is array', function() {
var data = [1, 2, 3];
var merged = mergeConfig(config1, {data: data});
const data = [1, 2, 3];
const merged = mergeConfig(config1, {data: data});
expect(merged.data).toEqual(data);
expect(merged.data).not.toBe(data);
});
it('should set as config2 in other cases', function() {
var obj = Object.create({});
const obj = Object.create({});
expect(mergeConfig(config1, {data: 1}).data).toBe(1);
expect(mergeConfig(config1, {data: 'str'}).data).toBe('str');
expect(mergeConfig(config1, {data: obj}).data).toBe(obj);
@@ -141,24 +141,24 @@ describe('core::mergeConfig', function() {
});
it('should clone config2 if is plain object', function() {
var config1 = {headers: [1, 2, 3]};
var config2 = {headers: {a: 1, b: 2}};
var merged = mergeConfig(config1, config2);
const config1 = {headers: [1, 2, 3]};
const config2 = {headers: {a: 1, b: 2}};
const merged = mergeConfig(config1, config2);
expect(merged.headers).toEqual(config2.headers);
expect(merged.headers).not.toBe(config2.headers);
});
it('should clone config2 if is array', function() {
var config1 = {headers: {a: 1, b: 1}};
var config2 = {headers: [1, 2, 3]};
var merged = mergeConfig(config1, config2);
const config1 = {headers: {a: 1, b: 1}};
const config2 = {headers: [1, 2, 3]};
const merged = mergeConfig(config1, config2);
expect(merged.headers).toEqual(config2.headers);
expect(merged.headers).not.toBe(config2.headers);
});
it('should set as config2 in other cases', function() {
var config1 = {headers: {a: 1, b: 1}};
var obj = Object.create({});
const config1 = {headers: {a: 1, b: 1}};
const obj = Object.create({});
expect(mergeConfig(config1, {headers: 1}).headers).toBe(1);
expect(mergeConfig(config1, {headers: 'str'}).headers).toBe('str');
expect(mergeConfig(config1, {headers: obj}).headers).toBe(obj);
@@ -166,24 +166,24 @@ describe('core::mergeConfig', function() {
});
it('should clone config1 if is plain object', function() {
var config1 = {headers: {a: 1, b: 2}};
var config2 = {};
var merged = mergeConfig(config1, config2);
const config1 = {headers: {a: 1, b: 2}};
const config2 = {};
const merged = mergeConfig(config1, config2);
expect(merged.headers).toEqual(config1.headers);
expect(merged.headers).not.toBe(config1.headers);
});
it('should clone config1 if is array', function() {
var config1 = {headers: [1, 2, 3]};
var config2 = {};
var merged = mergeConfig(config1, config2);
const config1 = {headers: [1, 2, 3]};
const config2 = {};
const merged = mergeConfig(config1, config2);
expect(merged.headers).toEqual(config1.headers);
expect(merged.headers).not.toBe(config1.headers);
});
it('should set as config1 in other cases', function() {
var config2 = {};
var obj = Object.create({});
const config2 = {};
const obj = Object.create({});
expect(mergeConfig({headers: 1}, config2).headers).toBe(1);
expect(mergeConfig({headers: 'str'}, config2).headers).toBe('str');
expect(mergeConfig({headers: obj}, config2).headers).toBe(obj);
@@ -197,24 +197,24 @@ describe('core::mergeConfig', function() {
});
it('should clone config2 if both config1 and config2 are plain object', function() {
var config1 = {transformRequest: {a: 1, b: 1}};
var config2 = {transformRequest: {b: 2, c: 2}};
var merged = mergeConfig(config1, config2);
const config1 = {transformRequest: {a: 1, b: 1}};
const config2 = {transformRequest: {b: 2, c: 2}};
const merged = mergeConfig(config1, config2);
expect(merged.transformRequest).toEqual(config2.transformRequest);
expect(merged.transformRequest).not.toBe(config2.transformRequest);
});
it('should clone config2 if is array', function() {
var config1 = {transformRequest: {a: 1, b: 1}};
var config2 = {transformRequest: [1, 2, 3]};
var merged = mergeConfig(config1, config2);
const config1 = {transformRequest: {a: 1, b: 1}};
const config2 = {transformRequest: [1, 2, 3]};
const merged = mergeConfig(config1, config2);
expect(merged.transformRequest).toEqual(config2.transformRequest);
expect(merged.transformRequest).not.toBe(config2.transformRequest);
});
it('should set as config2 in other cases', function() {
var config1 = {transformRequest: {a: 1, b: 1}};
var obj = Object.create({});
const config1 = {transformRequest: {a: 1, b: 1}};
const obj = Object.create({});
expect(mergeConfig(config1, {transformRequest: 1}).transformRequest).toBe(1);
expect(mergeConfig(config1, {transformRequest: 'str'}).transformRequest).toBe('str');
expect(mergeConfig(config1, {transformRequest: obj}).transformRequest).toBe(obj);
@@ -222,24 +222,24 @@ describe('core::mergeConfig', function() {
});
it('should clone config1 if is plain object', function() {
var config1 = {transformRequest: {a: 1, b: 2}};
var config2 = {};
var merged = mergeConfig(config1, config2);
const config1 = {transformRequest: {a: 1, b: 2}};
const config2 = {};
const merged = mergeConfig(config1, config2);
expect(merged.transformRequest).toEqual(config1.transformRequest);
expect(merged.transformRequest).not.toBe(config1.transformRequest);
});
it('should clone config1 if is array', function() {
var config1 = {transformRequest: [1, 2, 3]};
var config2 = {};
var merged = mergeConfig(config1, config2);
const config1 = {transformRequest: [1, 2, 3]};
const config2 = {};
const merged = mergeConfig(config1, config2);
expect(merged.transformRequest).toEqual(config1.transformRequest);
expect(merged.transformRequest).not.toBe(config1.transformRequest);
});
it('should set as config1 in other cases', function() {
var config2 = {};
var obj = Object.create({});
const config2 = {};
const obj = Object.create({});
expect(mergeConfig({transformRequest: 1}, config2).transformRequest).toBe(1);
expect(mergeConfig({transformRequest: 'str'}, config2).transformRequest).toBe('str');
expect(mergeConfig({transformRequest: obj}, config2).transformRequest).toBe(obj);
@@ -258,24 +258,24 @@ describe('core::mergeConfig', function() {
});
it('should clone config2 if is plain object', function() {
var config1 = {validateStatus: [1, 2, 3]};
var config2 = {validateStatus: {a: 1, b: 2}};
var merged = mergeConfig(config1, config2);
const config1 = {validateStatus: [1, 2, 3]};
const config2 = {validateStatus: {a: 1, b: 2}};
const merged = mergeConfig(config1, config2);
expect(merged.validateStatus).toEqual(config2.validateStatus);
expect(merged.validateStatus).not.toBe(config2.validateStatus);
});
it('should clone config2 if is array', function() {
var config1 = {validateStatus: {a: 1, b: 2}};
var config2 = {validateStatus: [1, 2, 3]};
var merged = mergeConfig(config1, config2);
const config1 = {validateStatus: {a: 1, b: 2}};
const config2 = {validateStatus: [1, 2, 3]};
const merged = mergeConfig(config1, config2);
expect(merged.validateStatus).toEqual(config2.validateStatus);
expect(merged.validateStatus).not.toBe(config2.validateStatus);
});
it('should set as config2 in other cases', function() {
var config1 = {validateStatus: {a: 1, b: 2}};
var obj = Object.create({});
const config1 = {validateStatus: {a: 1, b: 2}};
const obj = Object.create({});
expect(mergeConfig(config1, {validateStatus: 1}).validateStatus).toBe(1);
expect(mergeConfig(config1, {validateStatus: 'str'}).validateStatus).toBe('str');
expect(mergeConfig(config1, {validateStatus: obj}).validateStatus).toBe(obj);
@@ -283,24 +283,24 @@ describe('core::mergeConfig', function() {
});
it('should clone config1 if is plain object', function() {
var config1 = {validateStatus: {a: 1, b: 2}};
var config2 = {};
var merged = mergeConfig(config1, config2);
const config1 = {validateStatus: {a: 1, b: 2}};
const config2 = {};
const merged = mergeConfig(config1, config2);
expect(merged.validateStatus).toEqual(config1.validateStatus);
expect(merged.validateStatus).not.toBe(config1.validateStatus);
});
it('should clone config1 if is array', function() {
var config1 = {validateStatus: [1, 2, 3]};
var config2 = {};
var merged = mergeConfig(config1, config2);
const config1 = {validateStatus: [1, 2, 3]};
const config2 = {};
const merged = mergeConfig(config1, config2);
expect(merged.validateStatus).toEqual(config1.validateStatus);
expect(merged.validateStatus).not.toBe(config1.validateStatus);
});
it('should set as config1 in other cases', function() {
var config2 = {};
var obj = Object.create({});
const config2 = {};
const obj = Object.create({});
expect(mergeConfig({validateStatus: 1}, config2).validateStatus).toBe(1);
expect(mergeConfig({validateStatus: 'str'}, config2).validateStatus).toBe('str');
expect(mergeConfig({validateStatus: obj}, config2).validateStatus).toBe(obj);
+11 -11
View File
@@ -1,8 +1,8 @@
var settle = require('../../../lib/core/settle');
import settle from '../../../lib/core/settle';
describe('core::settle', function() {
var resolve;
var reject;
let resolve;
let reject;
beforeEach(function() {
resolve = jasmine.createSpy('resolve');
@@ -10,7 +10,7 @@ describe('core::settle', function() {
});
it('should resolve promise if status is not set', function() {
var response = {
const response = {
config: {
validateStatus: function() {
return true;
@@ -23,7 +23,7 @@ describe('core::settle', function() {
});
it('should resolve promise if validateStatus is not set', function() {
var response = {
const response = {
status: 500,
config: {
}
@@ -34,7 +34,7 @@ describe('core::settle', function() {
});
it('should resolve promise if validateStatus returns true', function() {
var response = {
const response = {
status: 500,
config: {
validateStatus: function() {
@@ -48,10 +48,10 @@ describe('core::settle', function() {
});
it('should reject promise if validateStatus returns false', function() {
var req = {
const req = {
path: '/foo'
};
var response = {
const response = {
status: 500,
config: {
validateStatus: function() {
@@ -63,7 +63,7 @@ describe('core::settle', function() {
settle(resolve, reject, response);
expect(resolve).not.toHaveBeenCalled();
expect(reject).toHaveBeenCalled();
var reason = reject.calls.first().args[0];
const reason = reject.calls.first().args[0];
expect(reason instanceof Error).toBe(true);
expect(reason.message).toBe('Request failed with status code 500');
expect(reason.config).toBe(response.config);
@@ -72,8 +72,8 @@ describe('core::settle', function() {
});
it('should pass status to validateStatus', function() {
var validateStatus = jasmine.createSpy('validateStatus');
var response = {
const validateStatus = jasmine.createSpy('validateStatus');
const response = {
status: 500,
config: {
validateStatus: validateStatus
+15 -12
View File
@@ -1,19 +1,22 @@
var transformData = require('../../../lib/core/transformData');
import transformData from '../../../lib/core/transformData';
describe('core::transformData', function () {
it('should support a single transformer', function () {
var data;
data = transformData(data, null, null, function (data) {
let data;
data = transformData.call({
}, function (data) {
data = 'foo';
return data;
});
})
expect(data).toEqual('foo');
});
it('should support an array of transformers', function () {
var data = '';
data = transformData(data, null, null, [function (data) {
let data = '';
data = transformData.call({data}, [function (data) {
data += 'f';
return data;
}, function (data) {
@@ -28,11 +31,11 @@ describe('core::transformData', function () {
});
it('should support reference headers in transformData', function () {
var headers = {
const headers = {
'content-type': 'foo/bar',
};
var data = '';
data = transformData(data, headers, null, [function (data, headers) {
let data = '';
data = transformData.call({data, headers}, [function (data, headers) {
data += headers['content-type'];
return data;
}]);
@@ -41,11 +44,11 @@ describe('core::transformData', function () {
});
it('should support reference status code in transformData', function () {
var data = '';
data = transformData(data, null, 200, [function (data, headers, status) {
let data = '';
data = transformData.call({}, [function (data, headers, status) {
data += status;
return data;
}]);
}], {data, status: 200});
expect(data).toEqual('200');
});