diff --git a/lib/axios.js b/lib/axios.js index 67f9d6fd..b0febb03 100644 --- a/lib/axios.js +++ b/lib/axios.js @@ -4,21 +4,34 @@ var utils = require('./utils'); var bind = require('./helpers/bind'); var Axios = require('./core/Axios'); -var defaultInstance = new Axios(); -var axios = module.exports = bind(Axios.prototype.request, defaultInstance); +/** + * Create an instance of Axios + * + * @param {Object} defaultConfig The default config for the instance + * @return {Axios} A new instance of Axios + */ +function createInstance(defaultConfig) { + var context = new Axios(defaultConfig); + var instance = bind(Axios.prototype.request, context); -// Copy Axios.prototype to axios instance -utils.extend(axios, Axios.prototype, defaultInstance); + // Copy axios.prototype to instance + utils.extend(instance, Axios.prototype, context); -// Copy defaultInstance to axios instance -utils.extend(axios, defaultInstance); + // Copy context to instance + utils.extend(instance, context); + + return instance; +} + +// Create the default instance to be exported +var axios = module.exports = createInstance(); // Expose Axios class to allow class inheritance axios.Axios = Axios; // Factory for creating new instances axios.create = function create(defaultConfig) { - return new Axios(defaultConfig); + return createInstance(defaultConfig); }; // Expose all/spread diff --git a/test/specs/instance.spec.js b/test/specs/instance.spec.js index 19f1331f..8fb27680 100644 --- a/test/specs/instance.spec.js +++ b/test/specs/instance.spec.js @@ -6,6 +6,28 @@ describe('instance', function () { afterEach(function () { jasmine.Ajax.uninstall(); }); + + it('should have the same methods as default instance', function () { + var instance = axios.create(); + + for (var prop in axios) { + if (['Axios', 'create', 'all', 'spread'].includes(prop)) { + continue; + } + expect(typeof instance[prop]).toBe(typeof axios[prop]); + } + }); + + it('should make an http request without verb helper', function (done) { + var instance = axios.create(); + + instance('/foo'); + + getAjaxRequest().then(function (request) { + expect(request.url).toBe('/foo'); + done(); + }); + }); it('should make an http request', function (done) { var instance = axios.create();