mirror of
https://github.com/tenrok/axios.git
synced 2026-06-17 19:21:29 +03:00
Cleaning up core axios a bit
This commit is contained in:
+4
-31
@@ -1,7 +1,9 @@
|
|||||||
var Promise = require('es6-promise').Promise;
|
var Promise = require('es6-promise').Promise;
|
||||||
var defaults = require('./defaults');
|
var defaults = require('./defaults');
|
||||||
var utils = require('./utils');
|
var utils = require('./utils');
|
||||||
var InterceptorManager = require('./helpers/InterceptorManager');
|
var deprecatedMethod = require('./helpers/deprecatedMethod');
|
||||||
|
var dispatchRequest = require('./core/dispatchRequest');
|
||||||
|
var InterceptorManager = require('./core/InterceptorManager');
|
||||||
|
|
||||||
var axios = module.exports = function axios(config) {
|
var axios = module.exports = function axios(config) {
|
||||||
config = utils.merge({
|
config = utils.merge({
|
||||||
@@ -14,36 +16,6 @@ var axios = module.exports = function axios(config) {
|
|||||||
// Don't allow overriding defaults.withCredentials
|
// Don't allow overriding defaults.withCredentials
|
||||||
config.withCredentials = config.withCredentials || defaults.withCredentials;
|
config.withCredentials = config.withCredentials || defaults.withCredentials;
|
||||||
|
|
||||||
function dispatchRequest(config) {
|
|
||||||
return new Promise(function (resolve, reject) {
|
|
||||||
try {
|
|
||||||
// For browsers use XHR adapter
|
|
||||||
if (typeof window !== 'undefined') {
|
|
||||||
require('./adapters/xhr')(resolve, reject, config);
|
|
||||||
}
|
|
||||||
// For node use HTTP adapter
|
|
||||||
else if (typeof process !== 'undefined') {
|
|
||||||
require('./adapters/http')(resolve, reject, config);
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
reject(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
function deprecatedMethod(method, instead, docs) {
|
|
||||||
try {
|
|
||||||
console.warn(
|
|
||||||
'DEPRECATED method `' + method + '`.' +
|
|
||||||
(instead ? ' Use `' + instead + '` instead.' : '') +
|
|
||||||
' This method will be removed in a future release.');
|
|
||||||
|
|
||||||
if (docs) {
|
|
||||||
console.warn('For more information about usage see ' + docs);
|
|
||||||
}
|
|
||||||
} catch (e) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Hook up interceptors middleware
|
// Hook up interceptors middleware
|
||||||
var chain = [dispatchRequest, undefined];
|
var chain = [dispatchRequest, undefined];
|
||||||
var promise = Promise.resolve(config);
|
var promise = Promise.resolve(config);
|
||||||
@@ -124,3 +96,4 @@ function createShortMethodsWithData() {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
var utils = require('../utils');
|
'use strict';
|
||||||
|
|
||||||
|
var utils = require('./../utils');
|
||||||
|
|
||||||
function InterceptorManager() {
|
function InterceptorManager() {
|
||||||
this.handlers = [];
|
this.handlers = [];
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
var Promise = require('es6-promise').Promise;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dispatch a request to the server using whichever adapter
|
||||||
|
* is supported by the current environment.
|
||||||
|
*
|
||||||
|
* @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) {
|
||||||
|
return new Promise(function (resolve, reject) {
|
||||||
|
try {
|
||||||
|
// For browsers use XHR adapter
|
||||||
|
if (typeof window !== 'undefined') {
|
||||||
|
require('../adapters/xhr')(resolve, reject, config);
|
||||||
|
}
|
||||||
|
// For node use HTTP adapter
|
||||||
|
else if (typeof process !== 'undefined') {
|
||||||
|
require('../adapters/http')(resolve, reject, config);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
reject(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Supply a warning to the developer that a method they are using
|
||||||
|
* has been deprecated.
|
||||||
|
*
|
||||||
|
* @param {string} method The name of the deprecated method
|
||||||
|
* @param {string} [instead] The alternate method to use if applicable
|
||||||
|
* @param {string} [docs] The documentation URL to get further details
|
||||||
|
*/
|
||||||
|
module.exports = function deprecatedMethod(method, instead, docs) {
|
||||||
|
try {
|
||||||
|
console.warn(
|
||||||
|
'DEPRECATED method `' + method + '`.' +
|
||||||
|
(instead ? ' Use `' + instead + '` instead.' : '') +
|
||||||
|
' This method will be removed in a future release.');
|
||||||
|
|
||||||
|
if (docs) {
|
||||||
|
console.warn('For more information about usage see ' + docs);
|
||||||
|
}
|
||||||
|
} catch (e) {}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user