From bbf437ce4c4115b24b7be575fbb1a74c45e616c8 Mon Sep 17 00:00:00 2001 From: mzabriskie Date: Fri, 23 Jan 2015 02:11:57 -0700 Subject: [PATCH] Cleaning up core axios a bit --- lib/axios.js | 35 +++------------------ lib/{helpers => core}/InterceptorManager.js | 4 ++- lib/core/dispatchRequest.js | 28 +++++++++++++++++ lib/helpers/deprecatedMethod.js | 22 +++++++++++++ 4 files changed, 57 insertions(+), 32 deletions(-) rename lib/{helpers => core}/InterceptorManager.js (95%) create mode 100644 lib/core/dispatchRequest.js create mode 100644 lib/helpers/deprecatedMethod.js diff --git a/lib/axios.js b/lib/axios.js index 9ab8445..10e0c80 100644 --- a/lib/axios.js +++ b/lib/axios.js @@ -1,7 +1,9 @@ var Promise = require('es6-promise').Promise; var defaults = require('./defaults'); 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) { config = utils.merge({ @@ -14,36 +16,6 @@ var axios = module.exports = function axios(config) { // Don't allow overriding 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 var chain = [dispatchRequest, undefined]; var promise = Promise.resolve(config); @@ -124,3 +96,4 @@ function createShortMethodsWithData() { }; }); } + diff --git a/lib/helpers/InterceptorManager.js b/lib/core/InterceptorManager.js similarity index 95% rename from lib/helpers/InterceptorManager.js rename to lib/core/InterceptorManager.js index 15233ca..bfac9bb 100644 --- a/lib/helpers/InterceptorManager.js +++ b/lib/core/InterceptorManager.js @@ -1,4 +1,6 @@ -var utils = require('../utils'); +'use strict'; + +var utils = require('./../utils'); function InterceptorManager() { this.handlers = []; diff --git a/lib/core/dispatchRequest.js b/lib/core/dispatchRequest.js new file mode 100644 index 0000000..a23d350 --- /dev/null +++ b/lib/core/dispatchRequest.js @@ -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); + } + }); +}; + diff --git a/lib/helpers/deprecatedMethod.js b/lib/helpers/deprecatedMethod.js new file mode 100644 index 0000000..dcf93af --- /dev/null +++ b/lib/helpers/deprecatedMethod.js @@ -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) {} +};