2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-23 20:40:40 +03:00

Merging master

This commit is contained in:
Nick Uraltsev
2016-09-23 15:54:47 -07:00
2 changed files with 38 additions and 38 deletions
+24 -38
View File
@@ -3,6 +3,7 @@
var utils = require('./../utils'); var utils = require('./../utils');
var transformData = require('./transformData'); var transformData = require('./transformData');
var Cancel = require('../cancel/Cancel'); var Cancel = require('../cancel/Cancel');
var defaults = require('../defaults');
/** /**
* Throws a `Cancel` if cancellation has been requested. * Throws a `Cancel` if cancellation has been requested.
@@ -14,8 +15,7 @@ function throwIfCancellationRequested(config) {
} }
/** /**
* Dispatch a request to the server using whichever adapter * Dispatch a request to the server using the configured adapter.
* is supported by the current environment.
* *
* @param {object} config The config that is to be used for the request * @param {object} config The config that is to be used for the request
* @returns {Promise} The Promise to be fulfilled * @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') { return adapter(config).then(function onAdapterResolution(response) {
// For custom adapter support throwIfCancellationRequested(config);
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 Promise.resolve(config) // Transform response data
// Wrap synchronous adapter errors and pass configuration response.data = transformData(
.then(adapter) response.data,
.then(function onFulfilled(response) { response.headers,
config.transformResponse
);
return response;
}, function onAdapterRejection(reason) {
if (!(reason instanceof Cancel)) {
throwIfCancellationRequested(config); throwIfCancellationRequested(config);
// Transform response data // Transform response data
response.data = transformData( if (reason && reason.response) {
response.data, reason.response.data = transformData(
response.headers, reason.response.data,
config.transformResponse reason.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
);
}
} }
}
return Promise.reject(reason); return Promise.reject(reason);
}); });
}; };
+14
View File
@@ -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 = { module.exports = {
adapter: getDefaultAdapter(),
transformRequest: [function transformRequest(data, headers) { transformRequest: [function transformRequest(data, headers) {
normalizeHeaderName(headers, 'Content-Type'); normalizeHeaderName(headers, 'Content-Type');
if (utils.isFormData(data) || if (utils.isFormData(data) ||