-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMantra.js
executable file
·122 lines (104 loc) · 3.49 KB
/
Mantra.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { MantraPlugin } from './lib/MantraPlugin';
import { error } from './lib/utils';
import { isTruthy, isObject, isEmptyObject } from './lib/utils';
/**
* This function is meant to check if the schemas parameter is an object value.
*
* @param {object} schemas - Contains the schemas configuration.
* @return {boolean} True if the schemas parameter is an object value.
*/
export const isValidSchema = function (schemas) {
const IS_DEFINED = isTruthy(schemas);
return IS_DEFINED && isObject(schemas);
};
/**
* This function is meant to check if the store object is valid.
*
* @param {object} store - Contains the store library and configuration.
* @returns {boolean} - True if the store is valid.
*/
export const isValidStore = function (store) {
const IS_DEFINED = isTruthy(store);
return IS_DEFINED && isObject(store) && !isEmptyObject(store) && isObject(store.lib) && !isEmptyObject(store.lib);
};
/**
* This function is meant to validate the install options parameter.
*
* @param {object} params - Contains the vue install configuration.
* @returns {object} Contains a boolean `valid` prop thats true if options is valid and a `reason` prop in case its not valid
*/
export const isOptionsValid = function (params) {
const {
config: { schemas = null },
plugins: { store = null }
} = params;
if (!isValidSchema(schemas)) {
return { valid: false, reason: 'Schemas property must be an object' };
}
if (!isValidStore(store)) {
return { valid: false, reason: 'Store library must be provided' };
}
return { valid: true };
};
/**
* This function is meant to register Vue components.
*
* @param {Vue} Vue - Contains the Vue Instance.
* @param {object} components - Contains an object of vue components.
* @returns {Error|void} If the components param is falsy or not object then an exception is thrown
*/
export const componentsRegistration = function (Vue, components) {
if (!isTruthy(components) || !isObject(components)) {
throw new Error('Mantra installation expects Vue components in its config parameter');
}
const componentNames = Object.keys(components);
for (let name of componentNames) {
const component = components[name];
Vue.component(name, component);
};
};
/**
* This function is meant to register modules in the store plugin.
*
* @param {object} store - Contains the store plugin instance and config.
* @param {object} options - Contains data that's used by the store modules.
* @returns {object} - Returns the modified store plugin.
*/
export const registerStoreModule = function (store, options) {
const { lib } = store;
const { schemas } = options;
// Registering schema module
lib.registerModule('schemas', {
namespaced: true,
state: {
...schemas
},
getters: {},
mutations: {},
actions: {}
});
return lib;
};
/**
* This function is meant to install Mantra as a Vue Plugin.
*
* @param {Vue} Vue - Contains the Vue Instance.
* @param {object} options - Contains configuration for Mantra installation
* @returns {Error|Console|void}
*/
export const install = function (Vue, options) {
const { valid, reason } = isOptionsValid(options);
if (!valid) return error(reason);
const {
config: { schemas, components },
plugins: { store }
} = options;
try {
componentsRegistration(Vue, components);
} catch (er) {
return error(er.message);
}
const StorePlugin = registerStoreModule(store, { schemas });
MantraPlugin.setConfig({ store: StorePlugin });
};
export default install;