All files / init createVuexStore.js

0% Statements 0/16
0% Branches 0/10
0% Functions 0/3
0% Lines 0/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                             
import Vue from 'vue'
import Vuex from 'vuex'
 
import { initJsonApiPlugin } from './initJsonApiPlugin'
import { checkConfigProperty, hasOwn } from '../shared/utils'
 
Vue.use(Vuex)
 
/**
 * Converts a module listing object (e.g. `{ myModule: myModule }`)
 * to the expected syntax for module registration.
 *
 * By default, this Vuex usage interpretation expects non-api-bound
 * modules to have a `name`-property which defines their namespaced
 * name. This is necessary to facilitate auto-registration of the modules.
 *
 * N.b.: There is no checking done to avoid overwrites of these modules
 * by later-to-be-initialized api-bound modules.
 *
 * @param {object|array} modules
 */
export function prepareModuleHashMap (modules) {
  const moduleHashMap = {}
 
  for (const idx in modules) {
    if (hasOwn(modules, idx)) {
      const module = modules[idx]
      moduleHashMap[module.name] = module
    }
  }
 
  return moduleHashMap
}
 
/**
 * Create Vuex Store with API Plugin
 *
 * @see initJsonApiPlugin
 * @param {Object|Router} Can either just pass a router or a full config object
 * @returns {Promise<Store>}
 */
export async function createVuexStore (config) {
  const router = checkConfigProperty(config, 'router') ? config.router : null
 
  if (!router) {
    throw new Error('You must provide a router')
  }
 
  return router
    .updateRoutes()
    .then(router => {
      const staticModules = checkConfigProperty(config, 'staticModules', false)
        ? config.staticModules
        : {}
 
      const store = new Vuex.Store({
        strict: true,
        modules: prepareModuleHashMap(staticModules),
        plugins: [
          initJsonApiPlugin(config)
        ]
      })
 
      if (process.env.NODE_ENV === 'development') {
        window.store = store
      }
 
      return store
    })
}