Skip to content

Commit a4cf976

Browse files
committed
feat(core): implement multiple versions support
1 parent 9c52792 commit a4cf976

File tree

1 file changed

+72
-28
lines changed

1 file changed

+72
-28
lines changed

src/index.js

Lines changed: 72 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,30 @@
11
/**
22
* Install plugin
33
* @param app
4-
* @param axios
4+
* @param {axios|Record<string:axios>}options
55
*/
6-
function plugin(app, axios) {
6+
function plugin(app, options) {
77
if (plugin.installed) {
88
return;
99
}
1010

11-
if (!axios) {
12-
console.error('You have to install axios');
11+
const normalizedConfig = isAxiosLike(options) ? migrateToMultipleInstances(options) : options;
12+
if (!isValidConfig(normalizedConfig)) {
13+
console.error('[vue-axios] configuration is invalid, expected options are either <axios_instance> or { <registration_key>: <axios_instance> }');
1314
return;
1415
}
1516

1617
plugin.installed = true;
1718

18-
if (app.version && app.version.split('.')[0] < 3) {
19-
Object.defineProperties(app.prototype, {
20-
21-
axios: {
22-
get: function get() {
23-
return axios;
24-
}
25-
},
26-
27-
$http: {
28-
get: function get() {
29-
return axios;
30-
}
31-
}
32-
33-
});
34-
} else if (app.version && app.version.split('.')[0] >= 3) {
35-
app.config.globalProperties.axios = axios;
36-
app.config.globalProperties.$http = axios;
37-
} else {
38-
console.error('Unknown Vue version');
19+
const vueVersion = getVueVersion(app);
20+
if (!vueVersion) {
21+
console.error('[vue-axios] unknown Vue version');
3922
return;
4023
}
41-
42-
app.axios = axios;
43-
app.$http = axios;
24+
const handler = vueVersion < 3 ? registerOnVue2 : registerOnVue3;
25+
Object.keys(normalizedConfig).forEach(registrationKey => {
26+
handler(app, registrationKey, normalizedConfig[registrationKey])
27+
})
4428
}
4529

4630
if (typeof exports == "object") {
@@ -52,3 +36,63 @@ if (typeof exports == "object") {
5236
}
5337

5438
export default plugin;
39+
40+
/**
41+
* @param {Vue} app
42+
* @param {string} key
43+
* @param {axios} axiosInstance
44+
* @returns {void}
45+
*/
46+
function registerOnVue2(app, key, axiosInstance) {
47+
Object.defineProperty(app.prototype, key, {
48+
get() {
49+
return axiosInstance
50+
}
51+
})
52+
app[key] = axiosInstance;
53+
}
54+
55+
/**
56+
* @param {Vue} app
57+
* @param {string} key
58+
* @param {axios} axiosInstance
59+
* @returns {void}
60+
*/
61+
function registerOnVue3(app, key, axiosInstance) {
62+
app.config.globalProperties[key] = axiosInstance;
63+
app[key] = axiosInstance;
64+
}
65+
66+
/**
67+
* @param {axios|Record<string|axios>}obj
68+
* @returns {boolean}
69+
*/
70+
function isAxiosLike(obj) {
71+
return obj && typeof obj.get === 'function' && typeof obj.post === 'function'
72+
}
73+
74+
/**
75+
* Migrates previous configuration to support multiple instances
76+
* @param axiosInstance
77+
* @returns {Record<string, axios>}
78+
*/
79+
function migrateToMultipleInstances(axiosInstance) {
80+
return {
81+
axios: axiosInstance,
82+
$http: axiosInstance,
83+
}
84+
}
85+
86+
function isValidConfig(config) {
87+
if (typeof config !== 'object') return false
88+
return Object.keys(config).every(key => isAxiosLike(config[key]))
89+
}
90+
91+
/**
92+
* Return Vue version as a number
93+
* @param {Vue} app
94+
* @returns {?number}
95+
*/
96+
function getVueVersion (app) {
97+
return app && app.version && Number(app.version.split('.')[0])
98+
}

0 commit comments

Comments
 (0)