UNPKG

1.81 kBJavaScriptView Raw
1(function () {
2
3/**
4 * Copied from vue-resource
5 */
6
7const { slice } = [];
8
9function isFunction(val) {
10 return typeof val === 'function';
11}
12
13const isArray = Array.isArray;
14
15function isPlainObject(obj) {
16 return isObject(obj) && Object.getPrototypeOf(obj) == Object.prototype;
17}
18
19function isObject(obj) {
20 return obj !== null && typeof obj === 'object';
21}
22
23function _merge(target, source, deep) {
24 for (var key in source) {
25 if (deep && (isPlainObject(source[key]) || isArray(source[key]))) {
26 if (isPlainObject(source[key]) && !isPlainObject(target[key])) {
27 target[key] = {};
28 }
29 if (isArray(source[key]) && !isArray(target[key])) {
30 target[key] = [];
31 }
32 _merge(target[key], source[key], deep);
33 } else if (source[key] !== undefined) {
34 target[key] = source[key];
35 }
36 }
37}
38
39function merge(target) {
40
41 var args = slice.call(arguments, 1);
42
43 args.forEach((source) => {
44 _merge(target, source, true);
45 });
46
47 return target;
48}
49
50function options(fn, obj, opts) {
51
52 opts = opts || {};
53
54 if (isFunction(opts)) {
55 opts = opts.call(obj);
56 }
57
58 return merge(fn.bind({$vm: obj, $options: opts}), fn, {$options: opts});
59}
60
61/**
62 * Install plugin
63 * @param Vue
64 * @param axios
65 */
66
67function plugin(Vue, axios) {
68
69 if (plugin.installed) {
70 return;
71 }
72
73 if (!axios) {
74 console.error('You have to install axios')
75 return
76 }
77
78 Vue.axios = axios
79
80 Object.defineProperties(Vue.prototype, {
81
82 axios: {
83 get() {
84 return options(Vue.axios, this, this.$options.axios);
85 }
86 },
87
88 });
89}
90
91if (typeof exports == "object") {
92 module.exports = plugin;
93} else if (typeof define == "function" && define.amd) {
94 define([], function(){ return plugin });
95} else if (window.Vue && window.axios) {
96 Vue.use(plugin, window.axios);
97}
98
99})();