今日来看发送请求的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);
});
};
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。