未验证 提交 76f09afc 编写于 作者: J Jay 提交者: GitHub

Release/v0.22.0 (#4107)

* fix/Avoid package.json import; (#4041)

* Added auto-generated config module `env/data.js` for importing package environment vars without importing the whole `package.json`;
Refactored `http.js` to use `env/data.js` instead of package.json;

* Added `env/data.js`;
Added `env/README.md`;

* Feat/export package version constant (#4065)

* Added auto-generated config module `env/data.js` for importing package environment vars without importing the whole `package.json`;
Refactored `http.js` to use `env/data.js` instead of package.json;

* Added `env/data.js`;
Added `env/README.md`;

* Export package version constant;

* Fixed cancelToken leakage; Added AbortController support; (#3305)

* Fixed cancelToken leakage;
Added AbortController support;

* Fixed typings;

* Documented `signal` option;

* Added processing of early cancellation using AbortController without sending a request;
Co-authored-by: NJay <jasonsaayman@gmail.com>

* Updating CI to run on release branches

* Fixed default transitional config for custom Axios instance; (#4052)

Refactored `/core/mergeConfig`;
Co-authored-by: NJay <jasonsaayman@gmail.com>

* Prepping v0.22.0 for release

* Updated date
Co-authored-by: NDmitriy Mozgovoy <robotshara@gmail.com>
上级 7d6bddba
// eslint-disable-next-line strict
module.exports = {
"globals": {
"console": true,
"module": true,
"require": true
'globals': {
'console': true,
'module': true,
'require': true
},
"env": {
"browser": true,
"node": true
'env': {
'browser': true,
'node': true
},
"rules": {
/**
'rules': {
/**
* Strict mode
*/
"strict": [2, "global"], // http://eslint.org/docs/rules/strict
'strict': [2, 'global'], // http://eslint.org/docs/rules/strict
/**
/**
* Variables
*/
"no-shadow": 2, // http://eslint.org/docs/rules/no-shadow
"no-shadow-restricted-names": 2, // http://eslint.org/docs/rules/no-shadow-restricted-names
"no-unused-vars": [2, { // http://eslint.org/docs/rules/no-unused-vars
"vars": "local",
"args": "after-used"
'no-shadow': 2, // http://eslint.org/docs/rules/no-shadow
'no-shadow-restricted-names': 2, // http://eslint.org/docs/rules/no-shadow-restricted-names
'no-unused-vars': [2, { // http://eslint.org/docs/rules/no-unused-vars
'vars': 'local',
'args': 'after-used'
}],
"no-use-before-define": 2, // http://eslint.org/docs/rules/no-use-before-define
'no-use-before-define': 2, // http://eslint.org/docs/rules/no-use-before-define
/**
/**
* Possible errors
*/
"comma-dangle": [2, "never"], // http://eslint.org/docs/rules/comma-dangle
"no-cond-assign": [2, "except-parens"], // http://eslint.org/docs/rules/no-cond-assign
"no-console": 1, // http://eslint.org/docs/rules/no-console
"no-debugger": 1, // http://eslint.org/docs/rules/no-debugger
"no-alert": 1, // http://eslint.org/docs/rules/no-alert
"no-constant-condition": 1, // http://eslint.org/docs/rules/no-constant-condition
"no-dupe-keys": 2, // http://eslint.org/docs/rules/no-dupe-keys
"no-duplicate-case": 2, // http://eslint.org/docs/rules/no-duplicate-case
"no-empty": 2, // http://eslint.org/docs/rules/no-empty
"no-ex-assign": 2, // http://eslint.org/docs/rules/no-ex-assign
"no-extra-boolean-cast": 0, // http://eslint.org/docs/rules/no-extra-boolean-cast
"no-extra-semi": 2, // http://eslint.org/docs/rules/no-extra-semi
"no-func-assign": 2, // http://eslint.org/docs/rules/no-func-assign
"no-inner-declarations": 2, // http://eslint.org/docs/rules/no-inner-declarations
"no-invalid-regexp": 2, // http://eslint.org/docs/rules/no-invalid-regexp
"no-irregular-whitespace": 2, // http://eslint.org/docs/rules/no-irregular-whitespace
"no-obj-calls": 2, // http://eslint.org/docs/rules/no-obj-calls
"no-sparse-arrays": 2, // http://eslint.org/docs/rules/no-sparse-arrays
"no-unreachable": 2, // http://eslint.org/docs/rules/no-unreachable
"use-isnan": 2, // http://eslint.org/docs/rules/use-isnan
"block-scoped-var": 2, // http://eslint.org/docs/rules/block-scoped-var
'comma-dangle': [2, 'never'], // http://eslint.org/docs/rules/comma-dangle
'no-cond-assign': [2, 'except-parens'], // http://eslint.org/docs/rules/no-cond-assign
'no-console': 1, // http://eslint.org/docs/rules/no-console
'no-debugger': 1, // http://eslint.org/docs/rules/no-debugger
'no-alert': 1, // http://eslint.org/docs/rules/no-alert
'no-constant-condition': 1, // http://eslint.org/docs/rules/no-constant-condition
'no-dupe-keys': 2, // http://eslint.org/docs/rules/no-dupe-keys
'no-duplicate-case': 2, // http://eslint.org/docs/rules/no-duplicate-case
'no-empty': 2, // http://eslint.org/docs/rules/no-empty
'no-ex-assign': 2, // http://eslint.org/docs/rules/no-ex-assign
'no-extra-boolean-cast': 0, // http://eslint.org/docs/rules/no-extra-boolean-cast
'no-extra-semi': 2, // http://eslint.org/docs/rules/no-extra-semi
'no-func-assign': 2, // http://eslint.org/docs/rules/no-func-assign
'no-inner-declarations': 2, // http://eslint.org/docs/rules/no-inner-declarations
'no-invalid-regexp': 2, // http://eslint.org/docs/rules/no-invalid-regexp
'no-irregular-whitespace': 2, // http://eslint.org/docs/rules/no-irregular-whitespace
'no-obj-calls': 2, // http://eslint.org/docs/rules/no-obj-calls
'no-sparse-arrays': 2, // http://eslint.org/docs/rules/no-sparse-arrays
'no-unreachable': 2, // http://eslint.org/docs/rules/no-unreachable
'use-isnan': 2, // http://eslint.org/docs/rules/use-isnan
'block-scoped-var': 2, // http://eslint.org/docs/rules/block-scoped-var
/**
/**
* Best practices
*/
"consistent-return": 2, // http://eslint.org/docs/rules/consistent-return
"curly": [2, "multi-line"], // http://eslint.org/docs/rules/curly
"default-case": 2, // http://eslint.org/docs/rules/default-case
"dot-notation": [2, { // http://eslint.org/docs/rules/dot-notation
"allowKeywords": true
'consistent-return': 2, // http://eslint.org/docs/rules/consistent-return
'curly': [2, 'multi-line'], // http://eslint.org/docs/rules/curly
'default-case': 2, // http://eslint.org/docs/rules/default-case
'dot-notation': [2, { // http://eslint.org/docs/rules/dot-notation
'allowKeywords': true
}],
"eqeqeq": 2, // http://eslint.org/docs/rules/eqeqeq
"guard-for-in": 2, // http://eslint.org/docs/rules/guard-for-in
"no-caller": 2, // http://eslint.org/docs/rules/no-caller
"no-else-return": 2, // http://eslint.org/docs/rules/no-else-return
"no-eq-null": 2, // http://eslint.org/docs/rules/no-eq-null
"no-eval": 2, // http://eslint.org/docs/rules/no-eval
"no-extend-native": 2, // http://eslint.org/docs/rules/no-extend-native
"no-extra-bind": 2, // http://eslint.org/docs/rules/no-extra-bind
"no-fallthrough": 2, // http://eslint.org/docs/rules/no-fallthrough
"no-floating-decimal": 2, // http://eslint.org/docs/rules/no-floating-decimal
"no-implied-eval": 2, // http://eslint.org/docs/rules/no-implied-eval
"no-lone-blocks": 2, // http://eslint.org/docs/rules/no-lone-blocks
"no-loop-func": 2, // http://eslint.org/docs/rules/no-loop-func
"no-multi-str": 2, // http://eslint.org/docs/rules/no-multi-str
"no-native-reassign": 2, // http://eslint.org/docs/rules/no-native-reassign
"no-new": 2, // http://eslint.org/docs/rules/no-new
"no-new-func": 2, // http://eslint.org/docs/rules/no-new-func
"no-new-wrappers": 2, // http://eslint.org/docs/rules/no-new-wrappers
"no-octal": 2, // http://eslint.org/docs/rules/no-octal
"no-octal-escape": 2, // http://eslint.org/docs/rules/no-octal-escape
"no-param-reassign": 2, // http://eslint.org/docs/rules/no-param-reassign
"no-proto": 2, // http://eslint.org/docs/rules/no-proto
"no-redeclare": 2, // http://eslint.org/docs/rules/no-redeclare
"no-return-assign": 2, // http://eslint.org/docs/rules/no-return-assign
"no-script-url": 2, // http://eslint.org/docs/rules/no-script-url
"no-self-compare": 2, // http://eslint.org/docs/rules/no-self-compare
"no-sequences": 2, // http://eslint.org/docs/rules/no-sequences
"no-throw-literal": 2, // http://eslint.org/docs/rules/no-throw-literal
"no-with": 2, // http://eslint.org/docs/rules/no-with
"radix": 2, // http://eslint.org/docs/rules/radix
"vars-on-top": 0, // http://eslint.org/docs/rules/vars-on-top
"wrap-iife": [2, "any"], // http://eslint.org/docs/rules/wrap-iife
"yoda": 2, // http://eslint.org/docs/rules/yoda
'eqeqeq': 2, // http://eslint.org/docs/rules/eqeqeq
'guard-for-in': 2, // http://eslint.org/docs/rules/guard-for-in
'no-caller': 2, // http://eslint.org/docs/rules/no-caller
'no-else-return': 2, // http://eslint.org/docs/rules/no-else-return
'no-eq-null': 2, // http://eslint.org/docs/rules/no-eq-null
'no-eval': 2, // http://eslint.org/docs/rules/no-eval
'no-extend-native': 2, // http://eslint.org/docs/rules/no-extend-native
'no-extra-bind': 2, // http://eslint.org/docs/rules/no-extra-bind
'no-fallthrough': 2, // http://eslint.org/docs/rules/no-fallthrough
'no-floating-decimal': 2, // http://eslint.org/docs/rules/no-floating-decimal
'no-implied-eval': 2, // http://eslint.org/docs/rules/no-implied-eval
'no-lone-blocks': 2, // http://eslint.org/docs/rules/no-lone-blocks
'no-loop-func': 2, // http://eslint.org/docs/rules/no-loop-func
'no-multi-str': 2, // http://eslint.org/docs/rules/no-multi-str
'no-native-reassign': 2, // http://eslint.org/docs/rules/no-native-reassign
'no-new': 2, // http://eslint.org/docs/rules/no-new
'no-new-func': 2, // http://eslint.org/docs/rules/no-new-func
'no-new-wrappers': 2, // http://eslint.org/docs/rules/no-new-wrappers
'no-octal': 2, // http://eslint.org/docs/rules/no-octal
'no-octal-escape': 2, // http://eslint.org/docs/rules/no-octal-escape
'no-param-reassign': 2, // http://eslint.org/docs/rules/no-param-reassign
'no-proto': 2, // http://eslint.org/docs/rules/no-proto
'no-redeclare': 2, // http://eslint.org/docs/rules/no-redeclare
'no-return-assign': 2, // http://eslint.org/docs/rules/no-return-assign
'no-script-url': 2, // http://eslint.org/docs/rules/no-script-url
'no-self-compare': 2, // http://eslint.org/docs/rules/no-self-compare
'no-sequences': 2, // http://eslint.org/docs/rules/no-sequences
'no-throw-literal': 2, // http://eslint.org/docs/rules/no-throw-literal
'no-with': 2, // http://eslint.org/docs/rules/no-with
'radix': 2, // http://eslint.org/docs/rules/radix
'vars-on-top': 0, // http://eslint.org/docs/rules/vars-on-top
'wrap-iife': [2, 'any'], // http://eslint.org/docs/rules/wrap-iife
'yoda': 2, // http://eslint.org/docs/rules/yoda
/**
/**
* Style
*/
"indent": [2, 2], // http://eslint.org/docs/rules/indent
"brace-style": [2, // http://eslint.org/docs/rules/brace-style
"1tbs", {
"allowSingleLine": true
}],
"quotes": [
2, "single", "avoid-escape" // http://eslint.org/docs/rules/quotes
'indent': [2, 2], // http://eslint.org/docs/rules/indent
'brace-style': [2, // http://eslint.org/docs/rules/brace-style
'1tbs', {
'allowSingleLine': true
}],
'quotes': [
2, 'single', 'avoid-escape' // http://eslint.org/docs/rules/quotes
],
"camelcase": [2, { // http://eslint.org/docs/rules/camelcase
"properties": "never"
'camelcase': [2, { // http://eslint.org/docs/rules/camelcase
'properties': 'never'
}],
"comma-spacing": [2, { // http://eslint.org/docs/rules/comma-spacing
"before": false,
"after": true
'comma-spacing': [2, { // http://eslint.org/docs/rules/comma-spacing
'before': false,
'after': true
}],
"comma-style": [2, "last"], // http://eslint.org/docs/rules/comma-style
"eol-last": 2, // http://eslint.org/docs/rules/eol-last
"func-names": 1, // http://eslint.org/docs/rules/func-names
"key-spacing": [2, { // http://eslint.org/docs/rules/key-spacing
"beforeColon": false,
"afterColon": true
'comma-style': [2, 'last'], // http://eslint.org/docs/rules/comma-style
'eol-last': 2, // http://eslint.org/docs/rules/eol-last
'func-names': 1, // http://eslint.org/docs/rules/func-names
'key-spacing': [2, { // http://eslint.org/docs/rules/key-spacing
'beforeColon': false,
'afterColon': true
}],
"new-cap": [2, { // http://eslint.org/docs/rules/new-cap
"newIsCap": true
'new-cap': [2, { // http://eslint.org/docs/rules/new-cap
'newIsCap': true
}],
"no-multiple-empty-lines": [2, { // http://eslint.org/docs/rules/no-multiple-empty-lines
"max": 2
'no-multiple-empty-lines': [2, { // http://eslint.org/docs/rules/no-multiple-empty-lines
'max': 2
}],
"no-nested-ternary": 2, // http://eslint.org/docs/rules/no-nested-ternary
"no-new-object": 2, // http://eslint.org/docs/rules/no-new-object
"no-spaced-func": 2, // http://eslint.org/docs/rules/no-spaced-func
"no-trailing-spaces": 2, // http://eslint.org/docs/rules/no-trailing-spaces
"no-extra-parens": [2, "functions"], // http://eslint.org/docs/rules/no-extra-parens
"no-underscore-dangle": 0, // http://eslint.org/docs/rules/no-underscore-dangle
"one-var": [2, "never"], // http://eslint.org/docs/rules/one-var
"padded-blocks": [2, "never"], // http://eslint.org/docs/rules/padded-blocks
"semi": [2, "always"], // http://eslint.org/docs/rules/semi
"semi-spacing": [2, { // http://eslint.org/docs/rules/semi-spacing
"before": false,
"after": true
'no-nested-ternary': 2, // http://eslint.org/docs/rules/no-nested-ternary
'no-new-object': 2, // http://eslint.org/docs/rules/no-new-object
'no-spaced-func': 2, // http://eslint.org/docs/rules/no-spaced-func
'no-trailing-spaces': 2, // http://eslint.org/docs/rules/no-trailing-spaces
'no-extra-parens': [2, 'functions'], // http://eslint.org/docs/rules/no-extra-parens
'no-underscore-dangle': 0, // http://eslint.org/docs/rules/no-underscore-dangle
'one-var': [2, 'never'], // http://eslint.org/docs/rules/one-var
'padded-blocks': [2, 'never'], // http://eslint.org/docs/rules/padded-blocks
'semi': [2, 'always'], // http://eslint.org/docs/rules/semi
'semi-spacing': [2, { // http://eslint.org/docs/rules/semi-spacing
'before': false,
'after': true
}],
"keyword-spacing": 2, // http://eslint.org/docs/rules/keyword-spacing
"space-before-blocks": 2, // http://eslint.org/docs/rules/space-before-blocks
"space-before-function-paren": [2, "never"], // http://eslint.org/docs/rules/space-before-function-paren
"space-infix-ops": 2, // http://eslint.org/docs/rules/space-infix-ops
"spaced-comment": [2, "always", {// http://eslint.org/docs/rules/spaced-comment
"markers": ["global", "eslint"]
'keyword-spacing': 2, // http://eslint.org/docs/rules/keyword-spacing
'space-before-blocks': 2, // http://eslint.org/docs/rules/space-before-blocks
'space-before-function-paren': [2, 'never'], // http://eslint.org/docs/rules/space-before-function-paren
'space-infix-ops': 2, // http://eslint.org/docs/rules/space-infix-ops
'spaced-comment': [2, 'always', {// http://eslint.org/docs/rules/spaced-comment
'markers': ['global', 'eslint']
}]
}
}
},
'ignorePatterns': ['**/env/data.js']
};
......@@ -2,9 +2,9 @@ name: ci
on:
push:
branches: [master]
branches: [master, 'release/*']
pull_request:
branches: [master]
branches: [master, 'release/*']
jobs:
build:
......
# Changelog
### 0.22.0 (October 01, 2021)
Fixes and Functionality:
- Caseless header comparing in HTTP adapter ([#2880](https://github.com/axios/axios/pull/2880))
- Avoid package.json import fixing issues and warnings related to this ([#4041](https://github.com/axios/axios/pull/4041)), ([#4065](https://github.com/axios/axios/pull/4065))
- Fixed cancelToken leakage and added AbortController support ([#3305](https://github.com/axios/axios/pull/3305))
- Updating CI to run on release branches
- Bump follow redirects version
- Fixed default transitional config for custom Axios instance; ([#4052](https://github.com/axios/axios/pull/4052))
Huge thanks to everyone who contributed to this release via code (authors listed below) or via reviews and triaging on GitHub:
- [Jay](mailto:jasonsaayman@gmail.com)
- [Matt R. Wilson](https://github.com/mastermatt)
- [Xianming Zhong](https://github.com/chinesedfan)
- [Dmitriy Mozgovoy](https://github.com/DigitalBrainJS)
### 0.21.4 (September 6, 2021)
Fixes and Functionality:
......
// eslint-disable-next-line strict
module.exports = function(grunt) {
require('load-grunt-tasks')(grunt);
......@@ -37,6 +38,10 @@ module.exports = function(grunt) {
}
},
package2env: {
all: {}
},
usebanner: {
all: {
options: {
......@@ -70,8 +75,8 @@ module.exports = function(grunt) {
src: ['test/unit/**/*.js']
},
options: {
timeout: 30000,
},
timeout: 30000
}
},
watch: {
......@@ -88,12 +93,12 @@ module.exports = function(grunt) {
webpack: require('./webpack.config.js')
});
grunt.registerMultiTask('package2bower', 'Sync package.json to bower.json', function () {
grunt.registerMultiTask('package2bower', 'Sync package.json to bower.json', function() {
var npm = grunt.file.readJSON('package.json');
var bower = grunt.file.readJSON('bower.json');
var fields = this.data.fields || [];
for (var i=0, l=fields.length; i<l; i++) {
for (var i = 0, l = fields.length; i < l; i++) {
var field = fields[i];
bower[field] = npm[field];
}
......@@ -101,7 +106,17 @@ module.exports = function(grunt) {
grunt.file.write('bower.json', JSON.stringify(bower, null, 2));
});
grunt.registerMultiTask('package2env', 'Sync package.json to env.json', function() {
var npm = grunt.file.readJSON('package.json');
grunt.file.write('./lib/env/data.js', [
'module.exports = ',
JSON.stringify({
version: npm.version
}, null, 2),
';'].join(''));
});
grunt.registerTask('test', 'Run the jasmine and mocha tests', ['eslint', 'mochaTest', 'karma:single', 'ts']);
grunt.registerTask('build', 'Run webpack and bundle the source', ['clean', 'webpack']);
grunt.registerTask('version', 'Sync version info for a release', ['usebanner', 'package2bower']);
grunt.registerTask('version', 'Sync version info for a release', ['usebanner', 'package2bower', 'package2env']);
};
......@@ -452,6 +452,9 @@ These are the available config options for making requests. Only the `url` is re
cancelToken: new CancelToken(function (cancel) {
}),
// an alternative way to cancel Axios requests using AbortController
signal: new AbortController().signal,
// `decompress` indicates whether or not the response body should be decompressed
// automatically. If set to `true` will also remove the 'content-encoding' header
// from the responses objects of all decompressed responses
......@@ -734,7 +737,20 @@ axios.get('/user/12345', {
cancel();
```
> Note: you can cancel several requests with the same cancel token.
Axios supports AbortController to abort requests in [`fetch API`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API#aborting_a_fetch) way:
```js
const controller = new AbortController();
axios.get('/foo/bar', {
signal: controller.signal
}).then(function(response) {
//...
});
// cancel the request
controller.abort()
```
> Note: you can cancel several requests with the same cancel token/abort controller.
> If a cancellation token is already cancelled at the moment of starting an Axios request, then the request is cancelled immediately, without any attempts to make real request.
## Using application/x-www-form-urlencoded format
......
{
"name": "axios",
"main": "./dist/axios.js",
"version": "0.21.4",
"version": "0.22.0",
"homepage": "https://axios-http.com",
"authors": [
"Matt Zabriskie"
......
/* axios v0.21.4 | (c) 2021 by Matt Zabriskie */
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
......@@ -126,12 +125,24 @@ var buildFullPath = __webpack_require__(/*! ../core/buildFullPath */ "./lib/core
var parseHeaders = __webpack_require__(/*! ./../helpers/parseHeaders */ "./lib/helpers/parseHeaders.js");
var isURLSameOrigin = __webpack_require__(/*! ./../helpers/isURLSameOrigin */ "./lib/helpers/isURLSameOrigin.js");
var createError = __webpack_require__(/*! ../core/createError */ "./lib/core/createError.js");
var defaults = __webpack_require__(/*! ../defaults */ "./lib/defaults.js");
var Cancel = __webpack_require__(/*! ../cancel/Cancel */ "./lib/cancel/Cancel.js");
module.exports = function xhrAdapter(config) {
return new Promise(function dispatchXhrRequest(resolve, reject) {
var requestData = config.data;
var requestHeaders = config.headers;
var responseType = config.responseType;
var onCanceled;
function done() {
if (config.cancelToken) {
config.cancelToken.unsubscribe(onCanceled);
}
if (config.signal) {
config.signal.removeEventListener('abort', onCanceled);
}
}
if (utils.isFormData(requestData)) {
delete requestHeaders['Content-Type']; // Let the browser set it
......@@ -169,7 +180,13 @@ module.exports = function xhrAdapter(config) {
request: request
};
settle(resolve, reject, response);
settle(function _resolve(value) {
resolve(value);
done();
}, function _reject(err) {
reject(err);
done();
}, response);
// Clean up request
request = null;
......@@ -223,13 +240,14 @@ module.exports = function xhrAdapter(config) {
// Handle timeout
request.ontimeout = function handleTimeout() {
var timeoutErrorMessage = 'timeout of ' + config.timeout + 'ms exceeded';
var transitional = config.transitional || defaults.transitional;
if (config.timeoutErrorMessage) {
timeoutErrorMessage = config.timeoutErrorMessage;
}
reject(createError(
timeoutErrorMessage,
config,
config.transitional && config.transitional.clarifyTimeoutError ? 'ETIMEDOUT' : 'ECONNABORTED',
transitional.clarifyTimeoutError ? 'ETIMEDOUT' : 'ECONNABORTED',
request));
// Clean up request
......@@ -283,18 +301,22 @@ module.exports = function xhrAdapter(config) {
request.upload.addEventListener('progress', config.onUploadProgress);
}
if (config.cancelToken) {
if (config.cancelToken || config.signal) {
// Handle cancellation
config.cancelToken.promise.then(function onCanceled(cancel) {
// eslint-disable-next-line func-names
onCanceled = function(cancel) {
if (!request) {
return;
}
reject(!cancel || (cancel && cancel.type) ? new Cancel('canceled') : cancel);
request.abort();
reject(cancel);
// Clean up request
request = null;
});
};
config.cancelToken && config.cancelToken.subscribe(onCanceled);
if (config.signal) {
config.signal.aborted ? onCanceled() : config.signal.addEventListener('abort', onCanceled);
}
}
if (!requestData) {
......@@ -341,6 +363,11 @@ function createInstance(defaultConfig) {
// Copy context to instance
utils.extend(instance, context);
// Factory for creating new instances
instance.create = function create(instanceConfig) {
return createInstance(mergeConfig(defaultConfig, instanceConfig));
};
return instance;
}
......@@ -350,15 +377,11 @@ var axios = createInstance(defaults);
// Expose Axios class to allow class inheritance
axios.Axios = Axios;
// Factory for creating new instances
axios.create = function create(instanceConfig) {
return createInstance(mergeConfig(axios.defaults, instanceConfig));
};
// Expose Cancel & CancelToken
axios.Cancel = __webpack_require__(/*! ./cancel/Cancel */ "./lib/cancel/Cancel.js");
axios.CancelToken = __webpack_require__(/*! ./cancel/CancelToken */ "./lib/cancel/CancelToken.js");
axios.isCancel = __webpack_require__(/*! ./cancel/isCancel */ "./lib/cancel/isCancel.js");
axios.VERSION = __webpack_require__(/*! ./env/data */ "./lib/env/data.js").version;
// Expose all/spread
axios.all = function all(promises) {
......@@ -432,11 +455,42 @@ function CancelToken(executor) {
}
var resolvePromise;
this.promise = new Promise(function promiseExecutor(resolve) {
resolvePromise = resolve;
});
var token = this;
// eslint-disable-next-line func-names
this.promise.then(function(cancel) {
if (!token._listeners) return;
var i;
var l = token._listeners.length;
for (i = 0; i < l; i++) {
token._listeners[i](cancel);
}
token._listeners = null;
});
// eslint-disable-next-line func-names
this.promise.then = function(onfulfilled) {
var _resolve;
// eslint-disable-next-line func-names
var promise = new Promise(function(resolve) {
token.subscribe(resolve);
_resolve = resolve;
}).then(onfulfilled);
promise.cancel = function reject() {
token.unsubscribe(_resolve);
};
return promise;
};
executor(function cancel(message) {
if (token.reason) {
// Cancellation has already been requested
......@@ -457,6 +511,37 @@ CancelToken.prototype.throwIfRequested = function throwIfRequested() {
}
};
/**
* Subscribe to the cancel signal
*/
CancelToken.prototype.subscribe = function subscribe(listener) {
if (this.reason) {
listener(this.reason);
return;
}
if (this._listeners) {
this._listeners.push(listener);
} else {
this._listeners = [listener];
}
};
/**
* Unsubscribe from the cancel signal
*/
CancelToken.prototype.unsubscribe = function unsubscribe(listener) {
if (!this._listeners) {
return;
}
var index = this._listeners.indexOf(listener);
if (index !== -1) {
this._listeners.splice(index, 1);
}
};
/**
* Returns an object that contains a new `CancelToken` and a function that, when called,
* cancels the `CancelToken`.
......@@ -555,9 +640,9 @@ Axios.prototype.request = function request(config) {
if (transitional !== undefined) {
validator.assertOptions(transitional, {
silentJSONParsing: validators.transitional(validators.boolean, '1.0.0'),
forcedJSONParsing: validators.transitional(validators.boolean, '1.0.0'),
clarifyTimeoutError: validators.transitional(validators.boolean, '1.0.0')
silentJSONParsing: validators.transitional(validators.boolean),
forcedJSONParsing: validators.transitional(validators.boolean),
clarifyTimeoutError: validators.transitional(validators.boolean)
}, false);
}
......@@ -796,6 +881,7 @@ var utils = __webpack_require__(/*! ./../utils */ "./lib/utils.js");
var transformData = __webpack_require__(/*! ./transformData */ "./lib/core/transformData.js");
var isCancel = __webpack_require__(/*! ../cancel/isCancel */ "./lib/cancel/isCancel.js");
var defaults = __webpack_require__(/*! ../defaults */ "./lib/defaults.js");
var Cancel = __webpack_require__(/*! ../cancel/Cancel */ "./lib/cancel/Cancel.js");
/**
* Throws a `Cancel` if cancellation has been requested.
......@@ -804,6 +890,10 @@ function throwIfCancellationRequested(config) {
if (config.cancelToken) {
config.cancelToken.throwIfRequested();
}
if (config.signal && config.signal.aborted) {
throw new Cancel('canceled');
}
}
/**
......@@ -921,7 +1011,8 @@ module.exports = function enhanceError(error, config, code, request, response) {
stack: this.stack,
// Axios
config: this.config,
code: this.code
code: this.code,
status: this.response && this.response.status ? this.response.status : null
};
};
return error;
......@@ -955,17 +1046,6 @@ module.exports = function mergeConfig(config1, config2) {
config2 = config2 || {};
var config = {};
var valueFromConfig2Keys = ['url', 'method', 'data'];
var mergeDeepPropertiesKeys = ['headers', 'auth', 'proxy', 'params'];
var defaultToConfig2Keys = [
'baseURL', 'transformRequest', 'transformResponse', 'paramsSerializer',
'timeout', 'timeoutMessage', 'withCredentials', 'adapter', 'responseType', 'xsrfCookieName',
'xsrfHeaderName', 'onUploadProgress', 'onDownloadProgress', 'decompress',
'maxContentLength', 'maxBodyLength', 'maxRedirects', 'transport', 'httpAgent',
'httpsAgent', 'cancelToken', 'socketPath', 'responseEncoding'
];
var directMergeKeys = ['validateStatus'];
function getMergedValue(target, source) {
if (utils.isPlainObject(target) && utils.isPlainObject(source)) {
return utils.merge(target, source);
......@@ -977,51 +1057,74 @@ module.exports = function mergeConfig(config1, config2) {
return source;
}
// eslint-disable-next-line consistent-return
function mergeDeepProperties(prop) {
if (!utils.isUndefined(config2[prop])) {
config[prop] = getMergedValue(config1[prop], config2[prop]);
return getMergedValue(config1[prop], config2[prop]);
} else if (!utils.isUndefined(config1[prop])) {
config[prop] = getMergedValue(undefined, config1[prop]);
return getMergedValue(undefined, config1[prop]);
}
}
utils.forEach(valueFromConfig2Keys, function valueFromConfig2(prop) {
// eslint-disable-next-line consistent-return
function valueFromConfig2(prop) {
if (!utils.isUndefined(config2[prop])) {
config[prop] = getMergedValue(undefined, config2[prop]);
return getMergedValue(undefined, config2[prop]);
}
});
utils.forEach(mergeDeepPropertiesKeys, mergeDeepProperties);
}
utils.forEach(defaultToConfig2Keys, function defaultToConfig2(prop) {
// eslint-disable-next-line consistent-return
function defaultToConfig2(prop) {
if (!utils.isUndefined(config2[prop])) {
config[prop] = getMergedValue(undefined, config2[prop]);
return getMergedValue(undefined, config2[prop]);
} else if (!utils.isUndefined(config1[prop])) {
config[prop] = getMergedValue(undefined, config1[prop]);
return getMergedValue(undefined, config1[prop]);
}
});
}
utils.forEach(directMergeKeys, function merge(prop) {
// eslint-disable-next-line consistent-return
function mergeDirectKeys(prop) {
if (prop in config2) {
config[prop] = getMergedValue(config1[prop], config2[prop]);
return getMergedValue(config1[prop], config2[prop]);
} else if (prop in config1) {
config[prop] = getMergedValue(undefined, config1[prop]);
return getMergedValue(undefined, config1[prop]);
}
});
var axiosKeys = valueFromConfig2Keys
.concat(mergeDeepPropertiesKeys)
.concat(defaultToConfig2Keys)
.concat(directMergeKeys);
}
var otherKeys = Object
.keys(config1)
.concat(Object.keys(config2))
.filter(function filterAxiosKeys(key) {
return axiosKeys.indexOf(key) === -1;
});
var mergeMap = {
'url': valueFromConfig2,
'method': valueFromConfig2,
'data': valueFromConfig2,
'baseURL': defaultToConfig2,
'transformRequest': defaultToConfig2,
'transformResponse': defaultToConfig2,
'paramsSerializer': defaultToConfig2,
'timeout': defaultToConfig2,
'timeoutMessage': defaultToConfig2,
'withCredentials': defaultToConfig2,
'adapter': defaultToConfig2,
'responseType': defaultToConfig2,
'xsrfCookieName': defaultToConfig2,
'xsrfHeaderName': defaultToConfig2,
'onUploadProgress': defaultToConfig2,
'onDownloadProgress': defaultToConfig2,
'decompress': defaultToConfig2,
'maxContentLength': defaultToConfig2,
'maxBodyLength': defaultToConfig2,
'transport': defaultToConfig2,
'httpAgent': defaultToConfig2,
'httpsAgent': defaultToConfig2,
'cancelToken': defaultToConfig2,
'socketPath': defaultToConfig2,
'responseEncoding': defaultToConfig2,
'validateStatus': mergeDirectKeys
};
utils.forEach(otherKeys, mergeDeepProperties);
utils.forEach(Object.keys(config1).concat(Object.keys(config2)), function computeConfigValue(prop) {
var merge = mergeMap[prop] || mergeDeepProperties;
var configValue = merge(prop);
(utils.isUndefined(configValue) && merge !== mergeDirectKeys) || (config[prop] = configValue);
});
return config;
};
......@@ -1189,7 +1292,7 @@ var defaults = {
}],
transformResponse: [function transformResponse(data) {
var transitional = this.transitional;
var transitional = this.transitional || defaults.transitional;
var silentJSONParsing = transitional && transitional.silentJSONParsing;
var forcedJSONParsing = transitional && transitional.forcedJSONParsing;
var strictJSONParsing = !silentJSONParsing && this.responseType === 'json';
......@@ -1224,12 +1327,12 @@ var defaults = {
validateStatus: function validateStatus(status) {
return status >= 200 && status < 300;
}
};
},
defaults.headers = {
common: {
'Accept': 'application/json, text/plain, */*'
headers: {
common: {
'Accept': 'application/json, text/plain, */*'
}
}
};
......@@ -1244,6 +1347,19 @@ utils.forEach(['post', 'put', 'patch'], function forEachMethodWithData(method) {
module.exports = defaults;
/***/ }),
/***/ "./lib/env/data.js":
/*!*************************!*\
!*** ./lib/env/data.js ***!
\*************************/
/*! no static exports found */
/***/ (function(module, exports) {
module.exports = {
"version": "0.22.0"
};
/***/ }),
/***/ "./lib/helpers/bind.js":
......@@ -1709,7 +1825,7 @@ module.exports = function spread(callback) {
"use strict";
var pkg = __webpack_require__(/*! ./../../package.json */ "./package.json");
var VERSION = __webpack_require__(/*! ../env/data */ "./lib/env/data.js").version;
var validators = {};
......@@ -1721,48 +1837,26 @@ var validators = {};
});
var deprecatedWarnings = {};
var currentVerArr = pkg.version.split('.');
/**
* Compare package versions
* @param {string} version
* @param {string?} thanVersion
* @returns {boolean}
*/
function isOlderVersion(version, thanVersion) {
var pkgVersionArr = thanVersion ? thanVersion.split('.') : currentVerArr;
var destVer = version.split('.');
for (var i = 0; i < 3; i++) {
if (pkgVersionArr[i] > destVer[i]) {
return true;
} else if (pkgVersionArr[i] < destVer[i]) {
return false;
}
}
return false;
}
/**
* Transitional option validator
* @param {function|boolean?} validator
* @param {string?} version
* @param {string} message
* @param {function|boolean?} validator - set to false if the transitional option has been removed
* @param {string?} version - deprecated version / removed since version
* @param {string?} message - some message with additional info
* @returns {function}
*/
validators.transitional = function transitional(validator, version, message) {
var isDeprecated = version && isOlderVersion(version);
function formatMessage(opt, desc) {
return '[Axios v' + pkg.version + '] Transitional option \'' + opt + '\'' + desc + (message ? '. ' + message : '');
return '[Axios v' + VERSION + '] Transitional option \'' + opt + '\'' + desc + (message ? '. ' + message : '');
}
// eslint-disable-next-line func-names
return function(value, opt, opts) {
if (validator === false) {
throw new Error(formatMessage(opt, ' has been removed in ' + version));
throw new Error(formatMessage(opt, ' has been removed' + (version ? ' in ' + version : '')));
}
if (isDeprecated && !deprecatedWarnings[opt]) {
if (version && !deprecatedWarnings[opt]) {
deprecatedWarnings[opt] = true;
// eslint-disable-next-line no-console
console.warn(
......@@ -1808,7 +1902,6 @@ function assertOptions(options, schema, allowUnknown) {
}
module.exports = {
isOlderVersion: isOlderVersion,
assertOptions: assertOptions,
validators: validators
};
......@@ -2175,17 +2268,6 @@ module.exports = {
};
/***/ }),
/***/ "./package.json":
/*!**********************!*\
!*** ./package.json ***!
\**********************/
/*! exports provided: name, version, description, main, scripts, repository, keywords, author, license, bugs, homepage, devDependencies, browser, jsdelivr, unpkg, typings, dependencies, bundlesize, default */
/***/ (function(module) {
module.exports = JSON.parse("{\"name\":\"axios\",\"version\":\"0.21.4\",\"description\":\"Promise based HTTP client for the browser and node.js\",\"main\":\"index.js\",\"scripts\":{\"test\":\"grunt test\",\"start\":\"node ./sandbox/server.js\",\"build\":\"NODE_ENV=production grunt build\",\"preversion\":\"npm test\",\"version\":\"npm run build && grunt version && git add -A dist && git add CHANGELOG.md bower.json package.json\",\"postversion\":\"git push && git push --tags\",\"examples\":\"node ./examples/server.js\",\"coveralls\":\"cat coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js\",\"fix\":\"eslint --fix lib/**/*.js\"},\"repository\":{\"type\":\"git\",\"url\":\"https://github.com/axios/axios.git\"},\"keywords\":[\"xhr\",\"http\",\"ajax\",\"promise\",\"node\"],\"author\":\"Matt Zabriskie\",\"license\":\"MIT\",\"bugs\":{\"url\":\"https://github.com/axios/axios/issues\"},\"homepage\":\"https://axios-http.com\",\"devDependencies\":{\"coveralls\":\"^3.0.0\",\"es6-promise\":\"^4.2.4\",\"grunt\":\"^1.3.0\",\"grunt-banner\":\"^0.6.0\",\"grunt-cli\":\"^1.2.0\",\"grunt-contrib-clean\":\"^1.1.0\",\"grunt-contrib-watch\":\"^1.0.0\",\"grunt-eslint\":\"^23.0.0\",\"grunt-karma\":\"^4.0.0\",\"grunt-mocha-test\":\"^0.13.3\",\"grunt-ts\":\"^6.0.0-beta.19\",\"grunt-webpack\":\"^4.0.2\",\"istanbul-instrumenter-loader\":\"^1.0.0\",\"jasmine-core\":\"^2.4.1\",\"karma\":\"^6.3.2\",\"karma-chrome-launcher\":\"^3.1.0\",\"karma-firefox-launcher\":\"^2.1.0\",\"karma-jasmine\":\"^1.1.1\",\"karma-jasmine-ajax\":\"^0.1.13\",\"karma-safari-launcher\":\"^1.0.0\",\"karma-sauce-launcher\":\"^4.3.6\",\"karma-sinon\":\"^1.0.5\",\"karma-sourcemap-loader\":\"^0.3.8\",\"karma-webpack\":\"^4.0.2\",\"load-grunt-tasks\":\"^3.5.2\",\"minimist\":\"^1.2.0\",\"mocha\":\"^8.2.1\",\"sinon\":\"^4.5.0\",\"terser-webpack-plugin\":\"^4.2.3\",\"typescript\":\"^4.0.5\",\"url-search-params\":\"^0.10.0\",\"webpack\":\"^4.44.2\",\"webpack-dev-server\":\"^3.11.0\"},\"browser\":{\"./lib/adapters/http.js\":\"./lib/adapters/xhr.js\"},\"jsdelivr\":\"dist/axios.min.js\",\"unpkg\":\"dist/axios.min.js\",\"typings\":\"./index.d.ts\",\"dependencies\":{\"follow-redirects\":\"^1.14.0\"},\"bundlesize\":[{\"path\":\"./dist/axios.min.js\",\"threshold\":\"5kB\"}]}");
/***/ })
/******/ });
......
此差异已折叠。
此差异已折叠。
此差异已折叠。
......@@ -78,6 +78,7 @@ export interface AxiosRequestConfig<T = any> {
cancelToken?: CancelToken;
decompress?: boolean;
transitional?: TransitionalOptions
signal?: AbortSignal;
}
export interface AxiosResponse<T = never> {
......@@ -162,6 +163,7 @@ export interface AxiosStatic extends AxiosInstance {
Cancel: CancelStatic;
CancelToken: CancelTokenStatic;
Axios: typeof Axios;
readonly VERSION: string;
isCancel(value: any): boolean;
all<T>(values: (T | Promise<T>)[]): Promise<T[]>;
spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;
......
......@@ -10,9 +10,11 @@ var httpFollow = require('follow-redirects').http;
var httpsFollow = require('follow-redirects').https;
var url = require('url');
var zlib = require('zlib');
var pkg = require('./../../package.json');
var VERSION = require('./../env/data').version;
var createError = require('../core/createError');
var enhanceError = require('../core/enhanceError');
var defaults = require('../defaults');
var Cancel = require('../cancel/Cancel');
var isHttps = /https:?/;
......@@ -44,10 +46,22 @@ function setProxy(options, proxy, location) {
/*eslint consistent-return:0*/
module.exports = function httpAdapter(config) {
return new Promise(function dispatchHttpRequest(resolvePromise, rejectPromise) {
var onCanceled;
function done() {
if (config.cancelToken) {
config.cancelToken.unsubscribe(onCanceled);
}
if (config.signal) {
config.signal.removeEventListener('abort', onCanceled);
}
}
var resolve = function resolve(value) {
done();
resolvePromise(value);
};
var reject = function reject(value) {
done();
rejectPromise(value);
};
var data = config.data;
......@@ -68,7 +82,7 @@ module.exports = function httpAdapter(config) {
// Otherwise, use specified value
} else {
// Only set header if it hasn't been set in config
headers['User-Agent'] = 'axios/' + pkg.version;
headers['User-Agent'] = 'axios/' + VERSION;
}
if (data && !utils.isStream(data)) {
......@@ -310,25 +324,33 @@ module.exports = function httpAdapter(config) {
// ClientRequest.setTimeout will be fired on the specify milliseconds, and can make sure that abort() will be fired after connect.
req.setTimeout(timeout, function handleRequestTimeout() {
req.abort();
var transitional = config.transitional || defaults.transitional;
reject(createError(
'timeout of ' + timeout + 'ms exceeded',
config,
config.transitional && config.transitional.clarifyTimeoutError ? 'ETIMEDOUT' : 'ECONNABORTED',
transitional.clarifyTimeoutError ? 'ETIMEDOUT' : 'ECONNABORTED',
req
));
});
}
if (config.cancelToken) {
if (config.cancelToken || config.signal) {
// Handle cancellation
config.cancelToken.promise.then(function onCanceled(cancel) {
// eslint-disable-next-line func-names
onCanceled = function(cancel) {
if (req.aborted) return;
req.abort();
reject(cancel);
});
reject(!cancel || (cancel && cancel.type) ? new Cancel('canceled') : cancel);
};
config.cancelToken && config.cancelToken.subscribe(onCanceled);
if (config.signal) {
config.signal.aborted ? onCanceled() : config.signal.addEventListener('abort', onCanceled);
}
}
// Send the request
if (utils.isStream(data)) {
data.on('error', function handleStreamError(err) {
......
......@@ -8,12 +8,24 @@ var buildFullPath = require('../core/buildFullPath');
var parseHeaders = require('./../helpers/parseHeaders');
var isURLSameOrigin = require('./../helpers/isURLSameOrigin');
var createError = require('../core/createError');
var defaults = require('../defaults');
var Cancel = require('../cancel/Cancel');
module.exports = function xhrAdapter(config) {
return new Promise(function dispatchXhrRequest(resolve, reject) {
var requestData = config.data;
var requestHeaders = config.headers;
var responseType = config.responseType;
var onCanceled;
function done() {
if (config.cancelToken) {
config.cancelToken.unsubscribe(onCanceled);
}
if (config.signal) {
config.signal.removeEventListener('abort', onCanceled);
}
}
if (utils.isFormData(requestData)) {
delete requestHeaders['Content-Type']; // Let the browser set it
......@@ -51,7 +63,13 @@ module.exports = function xhrAdapter(config) {
request: request
};
settle(resolve, reject, response);
settle(function _resolve(value) {
resolve(value);
done();
}, function _reject(err) {
reject(err);
done();
}, response);
// Clean up request
request = null;
......@@ -105,13 +123,14 @@ module.exports = function xhrAdapter(config) {
// Handle timeout
request.ontimeout = function handleTimeout() {
var timeoutErrorMessage = config.timeout ? 'timeout of ' + config.timeout + 'ms exceeded' : 'timeout exceeded';
var transitional = config.transitional || defaults.transitional;
if (config.timeoutErrorMessage) {
timeoutErrorMessage = config.timeoutErrorMessage;
}
reject(createError(
timeoutErrorMessage,
config,
config.transitional && config.transitional.clarifyTimeoutError ? 'ETIMEDOUT' : 'ECONNABORTED',
transitional.clarifyTimeoutError ? 'ETIMEDOUT' : 'ECONNABORTED',
request));
// Clean up request
......@@ -165,18 +184,22 @@ module.exports = function xhrAdapter(config) {
request.upload.addEventListener('progress', config.onUploadProgress);
}
if (config.cancelToken) {
if (config.cancelToken || config.signal) {
// Handle cancellation
config.cancelToken.promise.then(function onCanceled(cancel) {
// eslint-disable-next-line func-names
onCanceled = function(cancel) {
if (!request) {
return;
}
reject(!cancel || (cancel && cancel.type) ? new Cancel('canceled') : cancel);
request.abort();
reject(cancel);
// Clean up request
request = null;
});
};
config.cancelToken && config.cancelToken.subscribe(onCanceled);
if (config.signal) {
config.signal.aborted ? onCanceled() : config.signal.addEventListener('abort', onCanceled);
}
}
if (!requestData) {
......
......@@ -40,6 +40,7 @@ axios.Axios = Axios;
axios.Cancel = require('./cancel/Cancel');
axios.CancelToken = require('./cancel/CancelToken');
axios.isCancel = require('./cancel/isCancel');
axios.VERSION = require('./env/data').version;
// Expose all/spread
axios.all = function all(promises) {
......
......@@ -14,11 +14,42 @@ function CancelToken(executor) {
}
var resolvePromise;
this.promise = new Promise(function promiseExecutor(resolve) {
resolvePromise = resolve;
});
var token = this;
// eslint-disable-next-line func-names
this.promise.then(function(cancel) {
if (!token._listeners) return;
var i;
var l = token._listeners.length;
for (i = 0; i < l; i++) {
token._listeners[i](cancel);
}
token._listeners = null;
});
// eslint-disable-next-line func-names
this.promise.then = function(onfulfilled) {
var _resolve;
// eslint-disable-next-line func-names
var promise = new Promise(function(resolve) {
token.subscribe(resolve);
_resolve = resolve;
}).then(onfulfilled);
promise.cancel = function reject() {
token.unsubscribe(_resolve);
};
return promise;
};
executor(function cancel(message) {
if (token.reason) {
// Cancellation has already been requested
......@@ -39,6 +70,37 @@ CancelToken.prototype.throwIfRequested = function throwIfRequested() {
}
};
/**
* Subscribe to the cancel signal
*/
CancelToken.prototype.subscribe = function subscribe(listener) {
if (this.reason) {
listener(this.reason);
return;
}
if (this._listeners) {
this._listeners.push(listener);
} else {
this._listeners = [listener];
}
};
/**
* Unsubscribe from the cancel signal
*/
CancelToken.prototype.unsubscribe = function unsubscribe(listener) {
if (!this._listeners) {
return;
}
var index = this._listeners.indexOf(listener);
if (index !== -1) {
this._listeners.splice(index, 1);
}
};
/**
* Returns an object that contains a new `CancelToken` and a function that, when called,
* cancels the `CancelToken`.
......
......@@ -51,9 +51,9 @@ Axios.prototype.request = function request(config) {
if (transitional !== undefined) {
validator.assertOptions(transitional, {
silentJSONParsing: validators.transitional(validators.boolean, '1.0.0'),
forcedJSONParsing: validators.transitional(validators.boolean, '1.0.0'),
clarifyTimeoutError: validators.transitional(validators.boolean, '1.0.0')
silentJSONParsing: validators.transitional(validators.boolean),
forcedJSONParsing: validators.transitional(validators.boolean),
clarifyTimeoutError: validators.transitional(validators.boolean)
}, false);
}
......
......@@ -4,6 +4,7 @@ var utils = require('./../utils');
var transformData = require('./transformData');
var isCancel = require('../cancel/isCancel');
var defaults = require('../defaults');
var Cancel = require('../cancel/Cancel');
/**
* Throws a `Cancel` if cancellation has been requested.
......@@ -12,6 +13,10 @@ function throwIfCancellationRequested(config) {
if (config.cancelToken) {
config.cancelToken.throwIfRequested();
}
if (config.signal && config.signal.aborted) {
throw new Cancel('canceled');
}
}
/**
......
......@@ -15,17 +15,6 @@ module.exports = function mergeConfig(config1, config2) {
config2 = config2 || {};
var config = {};
var valueFromConfig2Keys = ['url', 'method', 'data'];
var mergeDeepPropertiesKeys = ['headers', 'auth', 'proxy', 'params'];
var defaultToConfig2Keys = [
'baseURL', 'transformRequest', 'transformResponse', 'paramsSerializer',
'timeout', 'timeoutMessage', 'withCredentials', 'adapter', 'responseType', 'xsrfCookieName',
'xsrfHeaderName', 'onUploadProgress', 'onDownloadProgress', 'decompress',
'maxContentLength', 'maxBodyLength', 'maxRedirects', 'transport', 'httpAgent',
'httpsAgent', 'cancelToken', 'socketPath', 'responseEncoding'
];
var directMergeKeys = ['validateStatus'];
function getMergedValue(target, source) {
if (utils.isPlainObject(target) && utils.isPlainObject(source)) {
return utils.merge(target, source);
......@@ -37,51 +26,74 @@ module.exports = function mergeConfig(config1, config2) {
return source;
}
// eslint-disable-next-line consistent-return
function mergeDeepProperties(prop) {
if (!utils.isUndefined(config2[prop])) {
config[prop] = getMergedValue(config1[prop], config2[prop]);
return getMergedValue(config1[prop], config2[prop]);
} else if (!utils.isUndefined(config1[prop])) {
config[prop] = getMergedValue(undefined, config1[prop]);
return getMergedValue(undefined, config1[prop]);
}
}
utils.forEach(valueFromConfig2Keys, function valueFromConfig2(prop) {
// eslint-disable-next-line consistent-return
function valueFromConfig2(prop) {
if (!utils.isUndefined(config2[prop])) {
config[prop] = getMergedValue(undefined, config2[prop]);
return getMergedValue(undefined, config2[prop]);
}
});
utils.forEach(mergeDeepPropertiesKeys, mergeDeepProperties);
}
utils.forEach(defaultToConfig2Keys, function defaultToConfig2(prop) {
// eslint-disable-next-line consistent-return
function defaultToConfig2(prop) {
if (!utils.isUndefined(config2[prop])) {
config[prop] = getMergedValue(undefined, config2[prop]);
return getMergedValue(undefined, config2[prop]);
} else if (!utils.isUndefined(config1[prop])) {
config[prop] = getMergedValue(undefined, config1[prop]);
return getMergedValue(undefined, config1[prop]);
}
});
}
utils.forEach(directMergeKeys, function merge(prop) {
// eslint-disable-next-line consistent-return
function mergeDirectKeys(prop) {
if (prop in config2) {
config[prop] = getMergedValue(config1[prop], config2[prop]);
return getMergedValue(config1[prop], config2[prop]);
} else if (prop in config1) {
config[prop] = getMergedValue(undefined, config1[prop]);
return getMergedValue(undefined, config1[prop]);
}
});
var axiosKeys = valueFromConfig2Keys
.concat(mergeDeepPropertiesKeys)
.concat(defaultToConfig2Keys)
.concat(directMergeKeys);
}
var otherKeys = Object
.keys(config1)
.concat(Object.keys(config2))
.filter(function filterAxiosKeys(key) {
return axiosKeys.indexOf(key) === -1;
});
var mergeMap = {
'url': valueFromConfig2,
'method': valueFromConfig2,
'data': valueFromConfig2,
'baseURL': defaultToConfig2,
'transformRequest': defaultToConfig2,
'transformResponse': defaultToConfig2,
'paramsSerializer': defaultToConfig2,
'timeout': defaultToConfig2,
'timeoutMessage': defaultToConfig2,
'withCredentials': defaultToConfig2,
'adapter': defaultToConfig2,
'responseType': defaultToConfig2,
'xsrfCookieName': defaultToConfig2,
'xsrfHeaderName': defaultToConfig2,
'onUploadProgress': defaultToConfig2,
'onDownloadProgress': defaultToConfig2,
'decompress': defaultToConfig2,
'maxContentLength': defaultToConfig2,
'maxBodyLength': defaultToConfig2,
'transport': defaultToConfig2,
'httpAgent': defaultToConfig2,
'httpsAgent': defaultToConfig2,
'cancelToken': defaultToConfig2,
'socketPath': defaultToConfig2,
'responseEncoding': defaultToConfig2,
'validateStatus': mergeDirectKeys
};
utils.forEach(otherKeys, mergeDeepProperties);
utils.forEach(Object.keys(config1).concat(Object.keys(config2)), function computeConfigValue(prop) {
var merge = mergeMap[prop] || mergeDeepProperties;
var configValue = merge(prop);
(utils.isUndefined(configValue) && merge !== mergeDirectKeys) || (config[prop] = configValue);
});
return config;
};
......@@ -79,7 +79,7 @@ var defaults = {
}],
transformResponse: [function transformResponse(data) {
var transitional = this.transitional;
var transitional = this.transitional || defaults.transitional;
var silentJSONParsing = transitional && transitional.silentJSONParsing;
var forcedJSONParsing = transitional && transitional.forcedJSONParsing;
var strictJSONParsing = !silentJSONParsing && this.responseType === 'json';
......@@ -114,12 +114,12 @@ var defaults = {
validateStatus: function validateStatus(status) {
return status >= 200 && status < 300;
}
};
},
defaults.headers = {
common: {
'Accept': 'application/json, text/plain, */*'
headers: {
common: {
'Accept': 'application/json, text/plain, */*'
}
}
};
......
# axios // env
The `data.js` file is updated automatically when the package version is upgrading. Please do not edit it manually.
module.exports = {
"version": "0.22.0"
};
\ No newline at end of file
'use strict';
var pkg = require('./../../package.json');
var VERSION = require('../env/data').version;
var validators = {};
......@@ -12,48 +12,26 @@ var validators = {};
});
var deprecatedWarnings = {};
var currentVerArr = pkg.version.split('.');
/**
* Compare package versions
* @param {string} version
* @param {string?} thanVersion
* @returns {boolean}
*/
function isOlderVersion(version, thanVersion) {
var pkgVersionArr = thanVersion ? thanVersion.split('.') : currentVerArr;
var destVer = version.split('.');
for (var i = 0; i < 3; i++) {
if (pkgVersionArr[i] > destVer[i]) {
return true;
} else if (pkgVersionArr[i] < destVer[i]) {
return false;
}
}
return false;
}
/**
* Transitional option validator
* @param {function|boolean?} validator
* @param {string?} version
* @param {string} message
* @param {function|boolean?} validator - set to false if the transitional option has been removed
* @param {string?} version - deprecated version / removed since version
* @param {string?} message - some message with additional info
* @returns {function}
*/
validators.transitional = function transitional(validator, version, message) {
var isDeprecated = version && isOlderVersion(version);
function formatMessage(opt, desc) {
return '[Axios v' + pkg.version + '] Transitional option \'' + opt + '\'' + desc + (message ? '. ' + message : '');
return '[Axios v' + VERSION + '] Transitional option \'' + opt + '\'' + desc + (message ? '. ' + message : '');
}
// eslint-disable-next-line func-names
return function(value, opt, opts) {
if (validator === false) {
throw new Error(formatMessage(opt, ' has been removed in ' + version));
throw new Error(formatMessage(opt, ' has been removed' + (version ? ' in ' + version : '')));
}
if (isDeprecated && !deprecatedWarnings[opt]) {
if (version && !deprecatedWarnings[opt]) {
deprecatedWarnings[opt] = true;
// eslint-disable-next-line no-console
console.warn(
......@@ -99,7 +77,6 @@ function assertOptions(options, schema, allowUnknown) {
}
module.exports = {
isOlderVersion: isOlderVersion,
assertOptions: assertOptions,
validators: validators
};
{
"name": "axios",
"version": "0.21.4",
"version": "0.22.0",
"description": "Promise based HTTP client for the browser and node.js",
"main": "index.js",
"types": "index.d.ts",
......@@ -8,8 +8,8 @@
"test": "grunt test",
"start": "node ./sandbox/server.js",
"build": "NODE_ENV=production grunt build",
"preversion": "npm test",
"version": "npm run build && grunt version && git add -A dist && git add CHANGELOG.md bower.json package.json",
"preversion": "grunt version && npm test",
"version": "npm run build && git add -A dist && git add CHANGELOG.md bower.json package.json",
"postversion": "git push && git push --tags",
"examples": "node ./examples/server.js",
"coveralls": "cat coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js",
......@@ -33,6 +33,7 @@
},
"homepage": "https://axios-http.com",
"devDependencies": {
"abortcontroller-polyfill": "^1.5.0",
"coveralls": "^3.0.0",
"es6-promise": "^4.2.4",
"grunt": "^1.3.0",
......@@ -74,7 +75,7 @@
"unpkg": "dist/axios.min.js",
"typings": "./index.d.ts",
"dependencies": {
"follow-redirects": "^1.14.0"
"follow-redirects": "^1.14.4"
},
"bundlesize": [
{
......
var Cancel = axios.Cancel;
var CancelToken = axios.CancelToken;
var _AbortController = require('abortcontroller-polyfill/dist/cjs-ponyfill.js').AbortController;
var AbortController = typeof AbortController === 'function' ? AbortController : _AbortController;
describe('cancel', function() {
beforeEach(function() {
......@@ -11,12 +14,12 @@ describe('cancel', function() {
});
describe('when called before sending request', function() {
it('rejects Promise with a Cancel object', function (done) {
it('rejects Promise with a Cancel object', function(done) {
var source = CancelToken.source();
source.cancel('Operation has been canceled.');
axios.get('/foo', {
cancelToken: source.token
}).catch(function (thrown) {
}).catch(function(thrown) {
expect(thrown).toEqual(jasmine.any(Cancel));
expect(thrown.message).toBe('Operation has been canceled.');
done();
......@@ -25,17 +28,17 @@ describe('cancel', function() {
});
describe('when called after request has been sent', function() {
it('rejects Promise with a Cancel object', function (done) {
it('rejects Promise with a Cancel object', function(done) {
var source = CancelToken.source();
axios.get('/foo/bar', {
cancelToken: source.token
}).catch(function (thrown) {
}).catch(function(thrown) {
expect(thrown).toEqual(jasmine.any(Cancel));
expect(thrown.message).toBe('Operation has been canceled.');
done();
});
getAjaxRequest().then(function (request) {
getAjaxRequest().then(function(request) {
// call cancel() when the request has been sent, but a response has not been received
source.cancel('Operation has been canceled.');
request.respondWith({
......@@ -45,7 +48,7 @@ describe('cancel', function() {
});
});
it('calls abort on request object', function (done) {
it('calls abort on request object', function(done) {
var source = CancelToken.source();
var request;
axios.get('/foo/bar', {
......@@ -56,7 +59,7 @@ describe('cancel', function() {
done();
});
getAjaxRequest().then(function (req) {
getAjaxRequest().then(function(req) {
// call cancel() when the request has been sent, but a response has not been received
source.cancel();
request = req;
......@@ -66,19 +69,19 @@ describe('cancel', function() {
describe('when called after response has been received', function() {
// https://github.com/axios/axios/issues/482
it('does not cause unhandled rejection', function (done) {
it('does not cause unhandled rejection', function(done) {
var source = CancelToken.source();
axios.get('/foo', {
cancelToken: source.token
}).then(function () {
window.addEventListener('unhandledrejection', function () {
}).then(function() {
window.addEventListener('unhandledrejection', function() {
done.fail('Unhandled rejection.');
});
source.cancel();
setTimeout(done, 100);
});
getAjaxRequest().then(function (request) {
getAjaxRequest().then(function(request) {
request.respondWith({
status: 200,
responseText: 'OK'
......@@ -86,4 +89,30 @@ describe('cancel', function() {
});
});
});
it('it should support cancellation using AbortController signal', function(done) {
var controller = new AbortController();
axios.get('/foo/bar', {
signal: controller.signal
}).then(function() {
done.fail('Has not been canceled');
},
function(thrown) {
expect(thrown).toEqual(jasmine.any(Cancel));
done();
}
);
getAjaxRequest().then(function (request) {
// call cancel() when the request has been sent, but a response has not been received
controller.abort();
setTimeout(function(){
request.respondWith({
status: 200,
responseText: 'OK'
});
}, 0);
});
});
});
......@@ -2,23 +2,8 @@
var validator = require('../../../lib/helpers/validator');
describe('validator::isOlderVersion', function () {
it('should return true if dest version is older than the package version', function () {
expect(validator.isOlderVersion('0.0.1', '1.0.0')).toEqual(true);
expect(validator.isOlderVersion('0.0.1', '0.1.0')).toEqual(true);
expect(validator.isOlderVersion('0.0.1', '0.0.1')).toEqual(false);
expect(validator.isOlderVersion('100.0.0', '1.0.0')).toEqual(false);
expect(validator.isOlderVersion('100.0.0', '0.1.0')).toEqual(false);
expect(validator.isOlderVersion('100.0.0', '0.0.1')).toEqual(false);
expect(validator.isOlderVersion('0.10000.0', '1000.0.1')).toEqual(true);
});
});
describe('validator::assertOptions', function () {
it('should throw only if unknown an option was passed', function () {
describe('validator::assertOptions', function() {
it('should throw only if unknown an option was passed', function() {
expect(function() {
validator.assertOptions({
x: true
......@@ -37,7 +22,7 @@ describe('validator::assertOptions', function () {
}).not.toThrow(new Error('Unknown option x'));
});
it('should throw TypeError only if option type doesn\'t match', function () {
it('should throw TypeError only if option type doesn\'t match', function() {
expect(function() {
validator.assertOptions({
x: 123
......
......@@ -20,6 +20,7 @@ describe('instance', function () {
'all',
'spread',
'isAxiosError',
'VERSION',
'default'].indexOf(prop) > -1) {
continue;
}
......
......@@ -953,7 +953,7 @@ describe('supports http with nodejs', function () {
axios.get('http://localhost:4444/', {
cancelToken: source.token
}).catch(function (thrown) {
assert.ok(thrown instanceof axios.Cancel, 'Promise must be rejected with a Cancel obejct');
assert.ok(thrown instanceof axios.Cancel, 'Promise must be rejected with a Cancel object');
assert.equal(thrown.message, 'Operation has been canceled.');
done();
});
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册