Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
MR414N-ID
GitHub Repository: MR414N-ID/botku2
Path: blob/master/node_modules/axios/lib/core/dispatchRequest.js
1126 views
1
'use strict';
2
3
var utils = require('./../utils');
4
var transformData = require('./transformData');
5
var isCancel = require('../cancel/isCancel');
6
var defaults = require('../defaults');
7
var Cancel = require('../cancel/Cancel');
8
9
/**
10
* Throws a `Cancel` if cancellation has been requested.
11
*/
12
function throwIfCancellationRequested(config) {
13
if (config.cancelToken) {
14
config.cancelToken.throwIfRequested();
15
}
16
17
if (config.signal && config.signal.aborted) {
18
throw new Cancel('canceled');
19
}
20
}
21
22
/**
23
* Dispatch a request to the server using the configured adapter.
24
*
25
* @param {object} config The config that is to be used for the request
26
* @returns {Promise} The Promise to be fulfilled
27
*/
28
module.exports = function dispatchRequest(config) {
29
throwIfCancellationRequested(config);
30
31
// Ensure headers exist
32
config.headers = config.headers || {};
33
34
// Transform request data
35
config.data = transformData.call(
36
config,
37
config.data,
38
config.headers,
39
config.transformRequest
40
);
41
42
// Flatten headers
43
config.headers = utils.merge(
44
config.headers.common || {},
45
config.headers[config.method] || {},
46
config.headers
47
);
48
49
utils.forEach(
50
['delete', 'get', 'head', 'post', 'put', 'patch', 'common'],
51
function cleanHeaderConfig(method) {
52
delete config.headers[method];
53
}
54
);
55
56
var adapter = config.adapter || defaults.adapter;
57
58
return adapter(config).then(function onAdapterResolution(response) {
59
throwIfCancellationRequested(config);
60
61
// Transform response data
62
response.data = transformData.call(
63
config,
64
response.data,
65
response.headers,
66
config.transformResponse
67
);
68
69
return response;
70
}, function onAdapterRejection(reason) {
71
if (!isCancel(reason)) {
72
throwIfCancellationRequested(config);
73
74
// Transform response data
75
if (reason && reason.response) {
76
reason.response.data = transformData.call(
77
config,
78
reason.response.data,
79
reason.response.headers,
80
config.transformResponse
81
);
82
}
83
}
84
85
return Promise.reject(reason);
86
});
87
};
88
89