mirror of
https://github.com/tenrok/axios.git
synced 2026-06-08 17:22:34 +03:00
Merging master
This commit is contained in:
+24
-38
@@ -3,6 +3,7 @@
|
||||
var utils = require('./../utils');
|
||||
var transformData = require('./transformData');
|
||||
var Cancel = require('../cancel/Cancel');
|
||||
var defaults = require('../defaults');
|
||||
|
||||
/**
|
||||
* Throws a `Cancel` if cancellation has been requested.
|
||||
@@ -14,8 +15,7 @@ function throwIfCancellationRequested(config) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatch a request to the server using whichever adapter
|
||||
* is supported by the current environment.
|
||||
* 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
|
||||
@@ -47,47 +47,33 @@ module.exports = function dispatchRequest(config) {
|
||||
}
|
||||
);
|
||||
|
||||
var adapter;
|
||||
var adapter = config.adapter || defaults.adapter;
|
||||
|
||||
if (typeof config.adapter === 'function') {
|
||||
// For custom adapter support
|
||||
adapter = config.adapter;
|
||||
} else if (typeof XMLHttpRequest !== 'undefined') {
|
||||
// For browsers use XHR adapter
|
||||
adapter = require('../adapters/xhr');
|
||||
} else if (typeof process !== 'undefined') {
|
||||
// For node use HTTP adapter
|
||||
adapter = require('../adapters/http');
|
||||
}
|
||||
return adapter(config).then(function onAdapterResolution(response) {
|
||||
throwIfCancellationRequested(config);
|
||||
|
||||
return Promise.resolve(config)
|
||||
// Wrap synchronous adapter errors and pass configuration
|
||||
.then(adapter)
|
||||
.then(function onFulfilled(response) {
|
||||
// Transform response data
|
||||
response.data = transformData(
|
||||
response.data,
|
||||
response.headers,
|
||||
config.transformResponse
|
||||
);
|
||||
|
||||
return response;
|
||||
}, function onAdapterRejection(reason) {
|
||||
if (!(reason instanceof Cancel)) {
|
||||
throwIfCancellationRequested(config);
|
||||
|
||||
// Transform response data
|
||||
response.data = transformData(
|
||||
response.data,
|
||||
response.headers,
|
||||
config.transformResponse
|
||||
);
|
||||
|
||||
return response;
|
||||
}, function onRejected(reason) {
|
||||
if (!(reason instanceof Cancel)) {
|
||||
throwIfCancellationRequested(config);
|
||||
|
||||
// Transform response data
|
||||
if (reason && reason.response) {
|
||||
reason.response.data = transformData(
|
||||
reason.response.data,
|
||||
reason.response.headers,
|
||||
config.transformResponse
|
||||
);
|
||||
}
|
||||
if (reason && reason.response) {
|
||||
reason.response.data = transformData(
|
||||
reason.response.data,
|
||||
reason.response.headers,
|
||||
config.transformResponse
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return Promise.reject(reason);
|
||||
});
|
||||
return Promise.reject(reason);
|
||||
});
|
||||
};
|
||||
|
||||
@@ -14,7 +14,21 @@ function setContentTypeIfUnset(headers, value) {
|
||||
}
|
||||
}
|
||||
|
||||
function getDefaultAdapter() {
|
||||
var adapter;
|
||||
if (typeof XMLHttpRequest !== 'undefined') {
|
||||
// For browsers use XHR adapter
|
||||
adapter = require('./adapters/xhr');
|
||||
} else if (typeof process !== 'undefined') {
|
||||
// For node use HTTP adapter
|
||||
adapter = require('./adapters/http');
|
||||
}
|
||||
return adapter;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
adapter: getDefaultAdapter(),
|
||||
|
||||
transformRequest: [function transformRequest(data, headers) {
|
||||
normalizeHeaderName(headers, 'Content-Type');
|
||||
if (utils.isFormData(data) ||
|
||||
|
||||
Reference in New Issue
Block a user