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

Fixing config weirdness

This commit is contained in:
Matt Zabriskie
2015-12-24 11:44:46 -07:00
parent cdedbf0bb1
commit 82847f737e
6 changed files with 176 additions and 49 deletions
+28 -12
View File
@@ -7,15 +7,10 @@ var InterceptorManager = require('./core/InterceptorManager');
var isAbsoluteURL = require('./helpers/isAbsoluteURL');
var combineURLs = require('./helpers/combineURLs');
var bind = require('./helpers/bind');
var transformData = require('./helpers/transformData');
function Axios(defaultConfig) {
this.defaultConfig = utils.merge({
headers: {},
timeout: defaults.timeout,
transformRequest: defaults.transformRequest,
transformResponse: defaults.transformResponse
}, defaultConfig);
this.defaults = utils.merge({}, defaultConfig);
this.interceptors = {
request: new InterceptorManager(),
response: new InterceptorManager()
@@ -31,14 +26,36 @@ Axios.prototype.request = function request(config) {
}, arguments[1]);
}
config = utils.merge(this.defaultConfig, { method: 'get' }, config);
config = utils.merge(defaults, this.defaults, { method: 'get' }, config);
// Support baseURL config
if (config.baseURL && !isAbsoluteURL(config.url)) {
config.url = combineURLs(config.baseURL, config.url);
}
// Don't allow overriding defaults.withCredentials
config.withCredentials = config.withCredentials || defaults.withCredentials;
config.withCredentials = config.withCredentials || this.defaults.withCredentials;
// 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];
}
);
// Hook up interceptors middleware
var chain = [dispatchRequest, undefined];
@@ -59,8 +76,7 @@ Axios.prototype.request = function request(config) {
return promise;
};
var defaultInstance = new Axios();
var defaultInstance = new Axios(defaults);
var axios = module.exports = bind(Axios.prototype.request, defaultInstance);
axios.create = function create(defaultConfig) {
@@ -68,7 +84,7 @@ axios.create = function create(defaultConfig) {
};
// Expose defaults
axios.defaults = defaults;
axios.defaults = defaultInstance.defaults;
// Expose all/spread
axios.all = function all(promises) {