diff options
author | Florian Dold <florian@dold.me> | 2021-08-20 13:31:06 +0200 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2021-08-20 13:31:06 +0200 |
commit | 3fb2c7a5cd0adb550308fe7db9db69453afd0515 (patch) | |
tree | b83ceffb2f45f86585f3899d0407bc1633256002 /lib/core/dispatchRequest.js | |
download | node-vendor-3fb2c7a5cd0adb550308fe7db9db69453afd0515.tar.gz node-vendor-3fb2c7a5cd0adb550308fe7db9db69453afd0515.tar.bz2 node-vendor-3fb2c7a5cd0adb550308fe7db9db69453afd0515.zip |
Squashed 'axios/' content from commit a64050a
git-subtree-dir: axios
git-subtree-split: a64050a6cfbcc708a55a7dc8030d85b1c78cdf38
Diffstat (limited to 'lib/core/dispatchRequest.js')
-rw-r--r-- | lib/core/dispatchRequest.js | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/lib/core/dispatchRequest.js b/lib/core/dispatchRequest.js new file mode 100644 index 0000000..c8267ad --- /dev/null +++ b/lib/core/dispatchRequest.js @@ -0,0 +1,79 @@ +'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. + */ +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 + */ +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); + }); +}; |