从v0.22.0
开始,Axios 支持以 fetch API 方式——AbortController
取消请求:
const controller = new AbortController();
axios.get('/foo/bar', {
signal: controller.signal
}).then(function(response) {
//...
});
// 取消请求
controller.abort()
代码不生效,axios版本为v.0.21.0,最新版本为v1.12.1。
旧版本中断请求的方法已废弃,官方文档不推荐使用。
为更新版本整理了一下重要的迭代更新内容。
(迭代内容以时间排序)
if (config.cancelToken || config.signal) {
// Handle cancellation
// eslint-disable-next-line func-names
onCanceled = function(cancel) {
if (req.aborted) return;
req.abort();
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);
}
}
'use strict';
var utils = require('../utils');
/**
* Create an Error with the specified message, config, error code, request and response.
*
* @param {string} message The error message.
* @param {string} [code] The error code (for example, 'ECONNABORTED').
* @param {Object} [config] The config.
* @param {Object} [request] The request.
* @param {Object} [response] The response.
* @returns {Error} The created error.
*/
function AxiosError(message, code, config, request, response) {
Error.call(this);
this.message = message;
this.name = 'AxiosError';
code && (this.code = code);
config && (this.config = config);
request && (this.request = request);
response && (this.response = response);
}
utils.inherits(AxiosError, Error, {
toJSON: function toJSON() {
return {
// Standard
message: this.message,
name: this.name,
// Microsoft
description: this.description,
number: this.number,
// Mozilla
fileName: this.fileName,
lineNumber: this.lineNumber,
columnNumber: this.columnNumber,
stack: this.stack,
// Axios
config: this.config,
code: this.code,
status: this.response && this.response.status ? this.response.status : null
};
}
});
var prototype = AxiosError.prototype;
var descriptors = {};
[
'ERR_BAD_OPTION_VALUE',
'ERR_BAD_OPTION',
'ECONNABORTED',
'ETIMEDOUT',
'ERR_NETWORK',
'ERR_FR_TOO_MANY_REDIRECTS',
'ERR_DEPRECATED',
'ERR_BAD_RESPONSE',
'ERR_BAD_REQUEST',
'ERR_CANCELED'
// eslint-disable-next-line func-names
].forEach(function(code) {
descriptors[code] = {value: code};
});
Object.defineProperties(AxiosError, descriptors);
Object.defineProperty(prototype, 'isAxiosError', {value: true});
// eslint-disable-next-line func-names
AxiosError.from = function(error, code, config, request, response, customProps) {
var axiosError = Object.create(prototype);
utils.toFlatObject(error, axiosError, function filter(obj) {
return obj !== Error.prototype;
});
AxiosError.call(axiosError, error.message, code, config, request, response);
axiosError.name = error.name;
customProps && Object.assign(axiosError, customProps);
return axiosError;
};
module.exports = AxiosError;
'use strict';
function combinedKey(parentKey, elKey) {
return parentKey + '.' + elKey;
}
function buildFormData(formData, data, parentKey) {
if (Array.isArray(data)) {
data.forEach(function buildArray(el, i) {
buildFormData(formData, el, combinedKey(parentKey, i));
});
} else if (
typeof data === 'object' &&
!(data instanceof File || data === null)
) {
Object.keys(data).forEach(function buildObject(key) {
buildFormData(
formData,
data[key],
parentKey ? combinedKey(parentKey, key) : key
);
});
} else {
if (data === undefined) {
return;
}
var value =
typeof data === 'boolean' || typeof data === 'number'
? data.toString()
: data;
formData.append(parentKey, value);
}
}
/**
* convert a data object to FormData
*
* type FormDataPrimitive = string | Blob | number | boolean
* interface FormDataNest {
* [x: string]: FormVal
* }
*
* type FormVal = FormDataNest | FormDataPrimitive
*
* @param {FormVal} data
*/
module.exports = function getFormData(data) {
var formData = new FormData();
buildFormData(formData, data);
return formData;
};
三、其他更新内容
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。