提交 61617543 编写于 作者: M Matt Zabriskie

Instances created from axios.create have same API as default axios

closes #217
上级 46eee269
......@@ -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
......
......@@ -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();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册