-
Notifications
You must be signed in to change notification settings - Fork 7
/
plugin.js
52 lines (44 loc) · 1.62 KB
/
plugin.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
import Vue from 'vue'
import yn from 'yn'
const parseQueryToggles = (queryObject = {}, queryStringPrefix = '') => Object.entries(queryObject)
.filter(([key]) => key.startsWith(`${queryStringPrefix}_`))
.reduce((toggles, [key, value]) => ({
...toggles,
[key.replace(`${queryStringPrefix}_`, '')]: value
}), {})
const parseYn = (t) => Object.entries(t).reduce((acc, [key, value]) => { acc[key] = yn(value) ?? value; return acc }, {})
export default (ctx, inject) => {
const { $config, route, app: { router } } = ctx
const { toggles: _toggles, queryString: _queryString, queryStringPrefix } = JSON.parse('<%= JSON.stringify(options) %>')
const runtimeConfig = ($config && $config.featureToggle) || {}
const queryString = typeof runtimeConfig.queryString !== 'undefined' ? runtimeConfig.queryString : _queryString
let queryToggles = {}
if (queryString) {
if (router && route) {
queryToggles = parseQueryToggles(route.query, queryStringPrefix)
router.afterEach((to) => {
featureToggle.toggles = parseYn({
..._toggles,
...(runtimeConfig.toggles || {}),
...parseQueryToggles(to.query, queryStringPrefix)
})
})
}
}
const toggles = {
..._toggles,
...(runtimeConfig.toggles || {}),
...queryToggles
}
Vue.component('feature-toggle', () => import('./feature-toggle.vue'))
const featureToggle = {
toggles: parseYn(toggles),
queryString,
queryStringPrefix,
isQueryStringAllowed: (fn) => {
featureToggle.isQueryStringAllowedFn = fn
}
}
ctx.$featureToggle = featureToggle
inject('featureToggle', featureToggle)
}