summaryrefslogtreecommitdiff
path: root/node_modules/axios/lib/core/dispatchRequest.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/axios/lib/core/dispatchRequest.js')
-rw-r--r--node_modules/axios/lib/core/dispatchRequest.js79
1 files changed, 79 insertions, 0 deletions
diff --git a/node_modules/axios/lib/core/dispatchRequest.js b/node_modules/axios/lib/core/dispatchRequest.js
new file mode 100644
index 000000000..b5111017d
--- /dev/null
+++ b/node_modules/axios/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);
+ });
+};