import { mapActions } from 'vuex' import { store } from './utils' import { Types, MapOption, KeyMap } from '../typings/utils.d' import { Actions, Dispatches, MapActions, FromActionsReturn } from '../typings/fromActions.d' const namespaced: KeyMap = {} function fromActions>( actions: T, namespace: string ): FromActionsReturn { if ( namespaced[namespace] !== undefined && process.env.NODE_ENV !== 'development' ) { throw new Error( `vuex-aggregate: conflict fromActions namespace -> ${namespace}` ) } else { namespaced[namespace] = namespace } const actionTypes: KeyMap = {} const dispatches: KeyMap = {} Object.keys(actions).forEach(key => { const type = namespace === '' ? key : `${namespace}/${key}` actionTypes[key] = type dispatches[key] = (payload?: any) => { return store.dispatch(type, payload, { root: true }) } }) function _mapActions>(mapOption: O) { const mapper = mapActions as any return namespace === '' ? mapper(mapOption) : mapper(namespace, mapOption) } return { actionTypes: actionTypes as Types, dispatches: dispatches as Dispatches, mapActions: _mapActions as MapActions } } export { fromActions }