All files / module ModuleBuilder.js

75.86% Statements 44/58
52.63% Branches 20/38
87.5% Functions 7/8
75.86% Lines 44/58

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 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205                                                                                            3x 3x 3x 3x   3x     3x 3x   3x                             3x   3x   3x     3x   3x   3x 3x 3x     3x 3x           3x             3x       3x 9x           3x             3x   3x                 3x 3x     3x 3x     3x       3x   3x           3x 3x     3x         3x       3x 3x     3x       3x                                       3x         3x 3x     3x      
import { initialState } from './State'
import { createAction } from './actions/createAction'
import { deleteAction } from './actions/deleteAction'
import { getAction } from './actions/getAction'
import { getProperty } from './getters/getProperty'
import { listAction } from './actions/listAction'
import { listRelatedAction } from './actions/listRelatedAction'
import { itemsInRelationshipFormat } from './getters/itemsInRelationshipFormat'
import { resetItemsAction } from './actions/resetItemsAction'
import { removeMutation } from './mutations/removeMutation'
import { resetItemsMutation } from './mutations/resetItemsMutation'
import { restoreFromInitialAction } from './actions/restoreFromInitialAction'
import { saveAction } from './actions/saveAction'
import { setAction } from './actions/setAction'
import { setItemMutation } from './mutations/setItemMutation'
import { setMutation } from './mutations/setMutation'
import { setPaginationMutation } from './mutations/setPaginationMutation'
import { startLoadingMutation, endLoadingMutation } from './mutations/loading'
import { updateMutation } from './mutations/updateMutation'
import { hasChanges } from './getters/hasChanges'
import { ResourceBuilder } from './resource/ResourceBuilder'
import { checkConfigProperty, hasOwn, isAbsoluteUri } from '../shared/utils'
import { Performance } from '../shared/Performance'
 
/**
 * JsonApi-based module builder for Vuex
 *
 * This module builder will create a vuex module based on the assumption of
 * working with valid json api resources.
 *
 * - the proposed json api 1.1 pagination style meta attributes
 *   (-> https://jsonapi.org/format/1.1/#fetching-pagination)
 *
 * @class ModuleBuilder
 */
export class ModuleBuilder {
  /**
   * Module Builder for Json:Api bound Vuex Modules
   *
   * @param {ResourcefulApi} api
   * @param {String} moduleName
   * @param {ResourceProxy} apiMethods
   * @param {Object} options additional objects for the module builder
   * @param {String} presetModuleName
   */
  constructor (api, moduleName, apiMethods, options = {}, presetModuleName = null) {
    this.api = api
    this.moduleName = moduleName
    this.apiMethods = apiMethods || {}
    this.presetModuleName = presetModuleName
 
    this.isCollection = apiMethods.isCollection()
 
    // is this a standalone module with no outside connections?
    this.isStandalone = checkConfigProperty(options, 'standalone', false) && options.standalone ? options.standalone : false
    this.presetOptions = checkConfigProperty(options, 'presetOptions', false) ? options.presetOptions : null
 
    Iif (this.isStandalone) {
      // standalone modules are always collections
      // because they will only be created if a list request
      // ends up having unknown includes
      // and as the law of known unknowns and unknown unknowns goes
      // we know that we may get this unknown other resource
      // but we don't know how many items of that resource there will be
      this.isCollection = true
    }
  }
 
  /**
   * Build the module for this builder instance
   */
  build () {
    Performance.mark('api_build_module_start')
 
    const module = {
      namespaced: true,
      state: () => initialState(this.isCollection)
    }
 
    module.state.options = this.buildOptions()
 
    module.mutations = this.buildMutations()
 
    Eif (!this.isStandalone) {
      module.actions = this.buildActions(module)
      module.getters = this.buildGetters()
    }
 
    Performance.mark('api_build_module_end')
    Performance.measure(
      'api: build module ' + this.moduleName,
      'api_build_module_start',
      'api_build_module_end'
    )
 
    return module
  }
 
  /**
   * Build the options
   */
  buildOptions () {
    const options = {
      absoluteMethods: []
    }
 
    for (const method in this.apiMethods.routes) {
      Iif (hasOwn(this.apiMethods.routes, method) &&
        isAbsoluteUri(this.api.router.routes[this.moduleName][method].url)) {
        options.absoluteMethods.push(method)
      }
    }
 
    return options
  }
 
  /**
   * Build the mutations
   */
  buildMutations () {
    const resourceBuilder = new ResourceBuilder(this.api.store)
 
    const mutations = {
      resetItems: resetItemsMutation(this.isCollection),
      set: setMutation(resourceBuilder, this.isCollection),
      setItem: setItemMutation(resourceBuilder, this.isCollection),
      startLoading: startLoadingMutation,
      endLoading: endLoadingMutation,
      update: updateMutation
    }
 
    Eif (this.apiMethods.allowsDeletion()) {
      mutations.remove = removeMutation(this.isCollection)
    }
 
    Eif (this.isCollection) {
      mutations.setPagination = setPaginationMutation
    }
 
    return mutations
  }
 
  buildActions (module) {
    const defaultQuery = checkConfigProperty(this.presetOptions, 'defaultQuery', false) ? this.presetOptions.defaultQuery : {}
 
    let actions = {
      get: getAction(this.api, this.moduleName, defaultQuery),
      resetItems: resetItemsAction,
      restoreFromInitial: restoreFromInitialAction(this.moduleName, this.presetModuleName, this.isCollection)
    }
 
    Eif (this.isCollection) {
      actions.list = listAction(this.api, this.moduleName, defaultQuery, module)
    }
 
    Iif (this.apiMethods.allowsModification()) {
      actions.set = setAction
      actions.save = saveAction(this.api, this.isCollection, this.moduleName, defaultQuery)
    }
 
    Iif (this.apiMethods.allowsCreation()) {
      actions.create = createAction(this.api, this.moduleName)
    }
 
    Eif (this.apiMethods.allowsDeletion()) {
      actions.delete = deleteAction(this.api, this.moduleName)
    }
 
    Iif (this.apiMethods.hasRelated()) {
      actions = Object.assign(actions, this.buildRelatedActions())
    }
 
    return actions
  }
 
  buildRelatedActions () {
    const relatedActions = {}
 
    for (const relatedObjectType in this.apiMethods.related) {
      const relatedObjectMethods = this.apiMethods.related[relatedObjectType]
      const relatedObjectTypeActionName = relatedObjectType.charAt(0).toUpperCase() + relatedObjectType.slice(1)
 
      if (relatedObjectMethods.isCollection()) {
        const listActionName = `listRelated${relatedObjectTypeActionName}`
        relatedActions[listActionName] = listRelatedAction(this.api, this.moduleName, relatedObjectType)
      }
    }
 
    return relatedActions
  }
 
  buildGetters () {
    const getters = {
      getProperty: getProperty,
      hasChanges: hasChanges(this.isCollection)
    }
 
    Eif (this.isCollection) {
      getters.itemsInRelationshipFormat = itemsInRelationshipFormat
    }
 
    return getters
  }
}