mirror of
https://github.com/axios/axios.git
synced 2026-04-13 02:51:56 +08:00
Instances created from axios.create have same API as default axios
closes #217
This commit is contained in:
parent
46eee269da
commit
6161754326
27
lib/axios.js
27
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
|
||||
|
||||
@ -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();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user