diff --git a/lib/axios.js b/lib/axios.js index aa741f0004680516c3e8197c0aae38201fcfa324..ed1f5194131787b20f6e10d3571d7bc616e92c87 100644 --- a/lib/axios.js +++ b/lib/axios.js @@ -3,6 +3,7 @@ var utils = require('./utils'); var bind = require('./helpers/bind'); var Axios = require('./core/Axios'); +var defaults = require('./defaults'); /** * Create an instance of Axios @@ -24,14 +25,14 @@ function createInstance(defaultConfig) { } // Create the default instance to be exported -var axios = createInstance(); +var axios = createInstance(defaults); // Expose Axios class to allow class inheritance axios.Axios = Axios; // Factory for creating new instances -axios.create = function create(defaultConfig) { - return createInstance(defaultConfig); +axios.create = function create(instanceConfig) { + return createInstance(utils.merge(defaults, instanceConfig)); }; // Expose Cancel & CancelToken diff --git a/lib/core/Axios.js b/lib/core/Axios.js index 5ace5cb0d1d63219fc304251cb204b5e3a6a3586..cda4b5d9063b902b58192a626e7e5bc293efa90e 100644 --- a/lib/core/Axios.js +++ b/lib/core/Axios.js @@ -10,10 +10,10 @@ var combineURLs = require('./../helpers/combineURLs'); /** * Create a new instance of Axios * - * @param {Object} defaultConfig The default config for the instance + * @param {Object} instanceConfig The default config for the instance */ -function Axios(defaultConfig) { - this.defaults = utils.merge(defaults, defaultConfig); +function Axios(instanceConfig) { + this.defaults = instanceConfig; this.interceptors = { request: new InterceptorManager(), response: new InterceptorManager() diff --git a/test/specs/defaults.spec.js b/test/specs/defaults.spec.js index f912d89f33c5b76f7599ba35fbd8893cd8f4ea93..de19b6ae4c09d8898332933f01ae68bb6b5f1627 100644 --- a/test/specs/defaults.spec.js +++ b/test/specs/defaults.spec.js @@ -136,4 +136,27 @@ describe('defaults', function () { }); }); + it('should be used by custom instance if set before instance created', function (done) { + axios.defaults.baseURL = 'http://example.org/'; + var instance = axios.create(); + + instance.get('/foo'); + + getAjaxRequest().then(function (request) { + expect(request.url).toBe('http://example.org/foo'); + done(); + }); + }); + + it('should be used by custom instance if set after instance created', function (done) { + var instance = axios.create(); + axios.defaults.baseURL = 'http://example.org/'; + + instance.get('/foo'); + + getAjaxRequest().then(function (request) { + expect(request.url).toBe('http://example.org/foo'); + done(); + }); + }); });