Path: blob/master/node_modules/axios/lib/core/dispatchRequest.js
1126 views
'use strict';12var utils = require('./../utils');3var transformData = require('./transformData');4var isCancel = require('../cancel/isCancel');5var defaults = require('../defaults');6var Cancel = require('../cancel/Cancel');78/**9* Throws a `Cancel` if cancellation has been requested.10*/11function throwIfCancellationRequested(config) {12if (config.cancelToken) {13config.cancelToken.throwIfRequested();14}1516if (config.signal && config.signal.aborted) {17throw new Cancel('canceled');18}19}2021/**22* Dispatch a request to the server using the configured adapter.23*24* @param {object} config The config that is to be used for the request25* @returns {Promise} The Promise to be fulfilled26*/27module.exports = function dispatchRequest(config) {28throwIfCancellationRequested(config);2930// Ensure headers exist31config.headers = config.headers || {};3233// Transform request data34config.data = transformData.call(35config,36config.data,37config.headers,38config.transformRequest39);4041// Flatten headers42config.headers = utils.merge(43config.headers.common || {},44config.headers[config.method] || {},45config.headers46);4748utils.forEach(49['delete', 'get', 'head', 'post', 'put', 'patch', 'common'],50function cleanHeaderConfig(method) {51delete config.headers[method];52}53);5455var adapter = config.adapter || defaults.adapter;5657return adapter(config).then(function onAdapterResolution(response) {58throwIfCancellationRequested(config);5960// Transform response data61response.data = transformData.call(62config,63response.data,64response.headers,65config.transformResponse66);6768return response;69}, function onAdapterRejection(reason) {70if (!isCancel(reason)) {71throwIfCancellationRequested(config);7273// Transform response data74if (reason && reason.response) {75reason.response.data = transformData.call(76config,77reason.response.data,78reason.response.headers,79config.transformResponse80);81}82}8384return Promise.reject(reason);85});86};878889