mirror of
https://github.com/tenrok/axios.git
synced 2026-06-14 18:42:33 +03:00
Instances created from axios.create have same API as default axios
closes #217
This commit is contained in:
+20
-7
@@ -4,21 +4,34 @@ var utils = require('./utils');
|
|||||||
var bind = require('./helpers/bind');
|
var bind = require('./helpers/bind');
|
||||||
var Axios = require('./core/Axios');
|
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
|
// Copy axios.prototype to instance
|
||||||
utils.extend(axios, Axios.prototype, defaultInstance);
|
utils.extend(instance, Axios.prototype, context);
|
||||||
|
|
||||||
// Copy defaultInstance to axios instance
|
// Copy context to instance
|
||||||
utils.extend(axios, defaultInstance);
|
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
|
// Expose Axios class to allow class inheritance
|
||||||
axios.Axios = Axios;
|
axios.Axios = Axios;
|
||||||
|
|
||||||
// Factory for creating new instances
|
// Factory for creating new instances
|
||||||
axios.create = function create(defaultConfig) {
|
axios.create = function create(defaultConfig) {
|
||||||
return new Axios(defaultConfig);
|
return createInstance(defaultConfig);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Expose all/spread
|
// Expose all/spread
|
||||||
|
|||||||
@@ -6,6 +6,28 @@ describe('instance', function () {
|
|||||||
afterEach(function () {
|
afterEach(function () {
|
||||||
jasmine.Ajax.uninstall();
|
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) {
|
it('should make an http request', function (done) {
|
||||||
var instance = axios.create();
|
var instance = axios.create();
|
||||||
|
|||||||
Reference in New Issue
Block a user