You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.1 KiB

3 years ago
'use strict';
3 years ago
import transformData from './transformData.js';
import isCancel from '../cancel/isCancel.js';
import defaults from '../defaults/index.js';
import CanceledError from '../cancel/CanceledError.js';
import AxiosHeaders from '../core/AxiosHeaders.js';
import adapters from "../adapters/adapters.js";
3 years ago
/**
* Throws a `CanceledError` if cancellation has been requested.
3 years ago
*
* @param {Object} config The config that is to be used for the request
*
* @returns {void}
3 years ago
*/
function throwIfCancellationRequested(config) {
if (config.cancelToken) {
config.cancelToken.throwIfRequested();
}
if (config.signal && config.signal.aborted) {
3 years ago
throw new CanceledError(null, config);
3 years ago
}
}
/**
* Dispatch a request to the server using the configured adapter.
*
* @param {object} config The config that is to be used for the request
3 years ago
*
3 years ago
* @returns {Promise} The Promise to be fulfilled
*/
3 years ago
export default function dispatchRequest(config) {
3 years ago
throwIfCancellationRequested(config);
3 years ago
config.headers = AxiosHeaders.from(config.headers);
3 years ago
// Transform request data
config.data = transformData.call(
config,
config.transformRequest
);
3 years ago
if (['post', 'put', 'patch'].indexOf(config.method) !== -1) {
config.headers.setContentType('application/x-www-form-urlencoded', false);
}
3 years ago
3 years ago
const adapter = adapters.getAdapter(config.adapter || defaults.adapter);
3 years ago
return adapter(config).then(function onAdapterResolution(response) {
throwIfCancellationRequested(config);
// Transform response data
response.data = transformData.call(
config,
3 years ago
config.transformResponse,
response
3 years ago
);
3 years ago
response.headers = AxiosHeaders.from(response.headers);
3 years ago
return response;
}, function onAdapterRejection(reason) {
if (!isCancel(reason)) {
throwIfCancellationRequested(config);
// Transform response data
if (reason && reason.response) {
reason.response.data = transformData.call(
config,
3 years ago
config.transformResponse,
reason.response
3 years ago
);
3 years ago
reason.response.headers = AxiosHeaders.from(reason.response.headers);
3 years ago
}
}
return Promise.reject(reason);
});
3 years ago
}