axios源码
Posted 杨柳岸残月孤轮
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了axios源码相关的知识,希望对你有一定的参考价值。
今日来看发送请求的dispatchRequest.
dispatchRequest就是一个函数,接受config做参数,核心是使用adapter(config中的属性)方法发送请求,然后将返回的数据进行转换.因为是promise,所以成功返回response,失败返回reject错误
\'use strict\';
var utils = require(\'./../utils\');
var transformData = require(\'./transformData\');
var isCancel = require(\'../cancel/isCancel\');
var defaults = require(\'../defaults\');
/**
* Throws a `Cancel` if cancellation has been requested.
*
* 抛出一个Cancel 如果一个cancellation已经被请求
*/
function throwIfCancellationRequested(config) {
if (config.cancelToken) {
config.cancelToken.throwIfRequested();
}
}
/**
* Dispatch a request to the server using the configured adapter.
*
* 使用配置的适配器给服务器分发一个请求
*
* @param {object} config The config that is to be used for the request 请求的配置
* @returns {Promise} The Promise to be fulfilled 返回完成的Promise
*/
module.exports = function dispatchRequest(config) {
throwIfCancellationRequested(config);
// Ensure headers exist 确保头部存在
config.headers = config.headers || {};
// Transform request data 转化请求数据
config.data = transformData(
config.data,
config.headers,
config.transformRequest
);
// Flatten headers 使头部扁平化
config.headers = utils.merge(
config.headers.common || {},
config.headers[config.method] || {},
config.headers
);
utils.forEach(
[\'delete\', \'get\', \'head\', \'post\', \'put\', \'patch\', \'common\'],
function cleanHeaderConfig(method) {
delete config.headers[method];
}
);
var adapter = config.adapter || defaults.adapter;
return adapter(config).then(function onAdapterResolution(response) {
throwIfCancellationRequested(config);
// Transform response data
response.data = transformData(
response.data,
response.headers,
config.transformResponse
);
return response;
}, function onAdapterRejection(reason) {
if (!isCancel(reason)) {
throwIfCancellationRequested(config);
// Transform response data
if (reason && reason.response) {
reason.response.data = transformData(
reason.response.data,
reason.response.headers,
config.transformResponse
);
}
}
return Promise.reject(reason);
});
};
以上是关于axios源码的主要内容,如果未能解决你的问题,请参考以下文章
VSCode自定义代码片段14——Vue的axios网络请求封装
Android 逆向类加载器 ClassLoader ( 类加载器源码简介 | BaseDexClassLoader | DexClassLoader | PathClassLoader )(代码片段