direct-vuex
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -5,4 +5,2 @@ "use strict"; | ||
function createDirectStore(options) { | ||
if (options.modules) | ||
checkNamespaced(options.modules); | ||
const original = new vuex_1.default.Store(options); | ||
@@ -12,27 +10,24 @@ return { | ||
state: original.state, | ||
commit: commitFromOptions(options, original), | ||
dispatch: dispatchFromOptions(options, original), | ||
getters: gettersFromOptions(options, original) | ||
getters: directGettersFromOptions({}, options, original), | ||
commit: commitFromOptions({}, options, original), | ||
dispatch: dispatchFromOptions({}, options, original) | ||
}; | ||
} | ||
exports.createDirectStore = createDirectStore; | ||
function checkNamespaced(modules) { | ||
for (const [moduleName, moduleOptions] of Object.entries(modules)) { | ||
if (!moduleOptions.namespaced) | ||
throw new Error(`The Vuex module '${moduleName}' is not namespaced. Direct-vuex can only work with namespaced Vuex modules.`); | ||
if (moduleOptions.modules) | ||
checkNamespaced(moduleOptions.modules); | ||
} | ||
} | ||
function gettersFromOptions(options, original, hierarchy = []) { | ||
const result = options.getters ? createDirectGetters(options.getters, original, hierarchy) : {}; | ||
// Getters | ||
function directGettersFromOptions(result, options, original, hierarchy = []) { | ||
if (options.getters) | ||
createDirectGetters(result, options.getters, original, hierarchy); | ||
if (options.modules) { | ||
for (const [moduleName, moduleOptions] of Object.entries(options.modules)) | ||
result[moduleName] = gettersFromOptions(moduleOptions, original, [...hierarchy, moduleName]); | ||
for (const [moduleName, moduleOptions] of Object.entries(options.modules)) { | ||
if (moduleOptions.namespaced) | ||
result[moduleName] = directGettersFromOptions({}, moduleOptions, original, [...hierarchy, moduleName]); | ||
else | ||
directGettersFromOptions(result, moduleOptions, original, hierarchy); | ||
} | ||
} | ||
return result; | ||
} | ||
function createDirectGetters(gettersImpl, original, hierarchy) { | ||
function createDirectGetters(result, gettersImpl, original, hierarchy) { | ||
const prefix = !hierarchy || hierarchy.length === 0 ? "" : `${hierarchy.join("/")}/`; | ||
const result = {}; | ||
for (const name of Object.keys(gettersImpl)) { | ||
@@ -45,38 +40,40 @@ Object.defineProperties(result, { | ||
} | ||
return result; | ||
} | ||
exports.createDirectGetters = createDirectGetters; | ||
function commitFromOptions(options, original, hierarchy = []) { | ||
const result = options.mutations ? createDirectMutations(options.mutations, original, hierarchy) : {}; | ||
// Mutations | ||
function commitFromOptions(result, options, original, hierarchy = []) { | ||
if (options.mutations) | ||
createDirectMutations(result, options.mutations, original, hierarchy); | ||
if (options.modules) { | ||
for (const [moduleName, moduleOptions] of Object.entries(options.modules)) | ||
result[moduleName] = commitFromOptions(moduleOptions, original, [...hierarchy, moduleName]); | ||
for (const [moduleName, moduleOptions] of Object.entries(options.modules)) { | ||
if (moduleOptions.namespaced) | ||
result[moduleName] = commitFromOptions({}, moduleOptions, original, [...hierarchy, moduleName]); | ||
else | ||
commitFromOptions(result, moduleOptions, original, hierarchy); | ||
} | ||
} | ||
return result; | ||
} | ||
function createDirectMutations(mutationsImpl, original, hierarchy) { | ||
function createDirectMutations(result, mutationsImpl, original, hierarchy) { | ||
const prefix = !hierarchy || hierarchy.length === 0 ? "" : `${hierarchy.join("/")}/`; | ||
const result = {}; | ||
for (const name of Object.keys(mutationsImpl)) { | ||
for (const name of Object.keys(mutationsImpl)) | ||
result[name] = (payload) => original.commit(`${prefix}${name}`, payload); | ||
} | ||
return result; | ||
} | ||
exports.createDirectMutations = createDirectMutations; | ||
function dispatchFromOptions(options, original, hierarchy = []) { | ||
const result = options.actions ? createDirectActions(options.actions, original, hierarchy) : {}; | ||
// Actions | ||
function dispatchFromOptions(result, options, original, hierarchy = []) { | ||
if (options.actions) | ||
createDirectActions(result, options.actions, original, hierarchy); | ||
if (options.modules) { | ||
for (const [moduleName, moduleOptions] of Object.entries(options.modules)) | ||
result[moduleName] = dispatchFromOptions(moduleOptions, original, [...hierarchy, moduleName]); | ||
for (const [moduleName, moduleOptions] of Object.entries(options.modules)) { | ||
if (moduleOptions.namespaced) | ||
result[moduleName] = dispatchFromOptions({}, moduleOptions, original, [...hierarchy, moduleName]); | ||
else | ||
dispatchFromOptions(result, moduleOptions, original, hierarchy); | ||
} | ||
} | ||
return result; | ||
} | ||
function createDirectActions(actionsImpl, original, hierarchy) { | ||
function createDirectActions(result, actionsImpl, original, hierarchy) { | ||
const prefix = !hierarchy || hierarchy.length === 0 ? "" : `${hierarchy.join("/")}/`; | ||
const result = {}; | ||
for (const name of Object.keys(actionsImpl)) { | ||
for (const name of Object.keys(actionsImpl)) | ||
result[name] = (payload) => original.dispatch(`${prefix}${name}`, payload); | ||
} | ||
return result; | ||
} | ||
exports.createDirectActions = createDirectActions; |
{ | ||
"name": "direct-vuex", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "Just Vuex with typing. Compatible with the Vue 3 composition API.", | ||
@@ -5,0 +5,0 @@ "author": "Paleo", |
@@ -18,4 +18,3 @@ # direct-vuex | ||
export default { | ||
namespaced: true, | ||
// … | ||
// … module implementation here … | ||
} as const | ||
@@ -32,3 +31,3 @@ | ||
export default createDirectStore({ | ||
// … store options here … | ||
// … store implementation here … | ||
} as const) | ||
@@ -71,3 +70,2 @@ | ||
- Modules must be namespaced; | ||
- Actions can't be declared with the object alternative syntax. | ||
@@ -74,0 +72,0 @@ |
import Vuex, { Store } from "vuex" | ||
import { ActionsImpl, GettersImpl, ModulesImpl, MutationsImpl, StoreOptions, StoreOrModuleOptions } from "../types" | ||
import { DirectActions, DirectGetters, DirectMutations, ToDirectActions, ToDirectGetters, ToDirectMutations, ToDirectStore } from "../types/direct-types" | ||
import { ActionsImpl, GettersImpl, MutationsImpl, StoreOptions, StoreOrModuleOptions } from "../types" | ||
import { ToDirectStore } from "../types/direct-types" | ||
export function createDirectStore<O extends StoreOptions>(options: O): ToDirectStore<O> { | ||
if (options.modules) | ||
checkNamespaced(options.modules) | ||
const original = new Vuex.Store(options) | ||
@@ -14,37 +11,36 @@ | ||
state: original.state, | ||
commit: commitFromOptions(options, original), | ||
dispatch: dispatchFromOptions(options, original), | ||
getters: gettersFromOptions(options, original) | ||
getters: directGettersFromOptions({}, options, original), | ||
commit: commitFromOptions({}, options, original), | ||
dispatch: dispatchFromOptions({}, options, original) | ||
} | ||
} | ||
function checkNamespaced(modules: ModulesImpl) { | ||
for (const [moduleName, moduleOptions] of Object.entries(modules)) { | ||
if (!moduleOptions.namespaced) | ||
throw new Error(`The Vuex module '${moduleName}' is not namespaced. Direct-vuex can only work with namespaced Vuex modules.`) | ||
if (moduleOptions.modules) | ||
checkNamespaced(moduleOptions.modules) | ||
} | ||
} | ||
// Getters | ||
function gettersFromOptions<O extends StoreOrModuleOptions>( | ||
options: O, | ||
function directGettersFromOptions( | ||
result: any, | ||
options: StoreOrModuleOptions, | ||
original: Store<any>, | ||
hierarchy: string[] = [] | ||
): DirectGetters<O> { | ||
const result = options.getters ? createDirectGetters(options.getters, original, hierarchy) : {} | ||
): any { | ||
if (options.getters) | ||
createDirectGetters(result, options.getters, original, hierarchy) | ||
if (options.modules) { | ||
for (const [moduleName, moduleOptions] of Object.entries(options.modules)) | ||
result[moduleName] = gettersFromOptions(moduleOptions, original, [...hierarchy, moduleName]) | ||
for (const [moduleName, moduleOptions] of Object.entries(options.modules)) { | ||
if (moduleOptions.namespaced) | ||
result[moduleName] = directGettersFromOptions({}, moduleOptions, original, [...hierarchy, moduleName]) | ||
else | ||
directGettersFromOptions(result, moduleOptions, original, hierarchy) | ||
} | ||
} | ||
return result as DirectGetters<O> | ||
return result | ||
} | ||
export function createDirectGetters<T extends GettersImpl>( | ||
gettersImpl: T, | ||
function createDirectGetters( | ||
result: any, | ||
gettersImpl: GettersImpl, | ||
original: Store<any>, | ||
hierarchy?: string[] | ||
): ToDirectGetters<T> { | ||
) { | ||
const prefix = !hierarchy || hierarchy.length === 0 ? "" : `${hierarchy.join("/")}/` | ||
const result = {} as ToDirectGetters<any> | ||
for (const name of Object.keys(gettersImpl)) { | ||
@@ -57,14 +53,21 @@ Object.defineProperties(result, { | ||
} | ||
return result | ||
} | ||
function commitFromOptions<O extends StoreOrModuleOptions>( | ||
options: O, | ||
// Mutations | ||
function commitFromOptions( | ||
result: any, | ||
options: StoreOrModuleOptions, | ||
original: Store<any>, | ||
hierarchy: string[] = [] | ||
): DirectMutations<O> { | ||
const result: any = options.mutations ? createDirectMutations(options.mutations, original, hierarchy) : {} | ||
): any { | ||
if (options.mutations) | ||
createDirectMutations(result, options.mutations, original, hierarchy) | ||
if (options.modules) { | ||
for (const [moduleName, moduleOptions] of Object.entries(options.modules)) | ||
result[moduleName] = commitFromOptions(moduleOptions, original, [...hierarchy, moduleName]) | ||
for (const [moduleName, moduleOptions] of Object.entries(options.modules)) { | ||
if (moduleOptions.namespaced) | ||
result[moduleName] = commitFromOptions({}, moduleOptions, original, [...hierarchy, moduleName]) | ||
else | ||
commitFromOptions(result, moduleOptions, original, hierarchy) | ||
} | ||
} | ||
@@ -74,24 +77,30 @@ return result | ||
export function createDirectMutations<T extends MutationsImpl>( | ||
mutationsImpl: T, | ||
function createDirectMutations( | ||
result: any, | ||
mutationsImpl: MutationsImpl, | ||
original: Store<any>, | ||
hierarchy?: string[] | ||
): ToDirectMutations<T> { | ||
) { | ||
const prefix = !hierarchy || hierarchy.length === 0 ? "" : `${hierarchy.join("/")}/` | ||
const result = {} as ToDirectMutations<any> | ||
for (const name of Object.keys(mutationsImpl)) { | ||
for (const name of Object.keys(mutationsImpl)) | ||
result[name] = (payload: any) => original.commit(`${prefix}${name}`, payload) | ||
} | ||
return result | ||
} | ||
function dispatchFromOptions<O extends StoreOrModuleOptions>( | ||
options: O, | ||
// Actions | ||
function dispatchFromOptions( | ||
result: any, | ||
options: StoreOrModuleOptions, | ||
original: Store<any>, | ||
hierarchy: string[] = [] | ||
): DirectActions<O> { | ||
const result: any = options.actions ? createDirectActions(options.actions, original, hierarchy) : {} | ||
): any { | ||
if (options.actions) | ||
createDirectActions(result, options.actions, original, hierarchy) | ||
if (options.modules) { | ||
for (const [moduleName, moduleOptions] of Object.entries(options.modules)) | ||
result[moduleName] = dispatchFromOptions(moduleOptions, original, [...hierarchy, moduleName]) | ||
for (const [moduleName, moduleOptions] of Object.entries(options.modules)) { | ||
if (moduleOptions.namespaced) | ||
result[moduleName] = dispatchFromOptions({}, moduleOptions, original, [...hierarchy, moduleName]) | ||
else | ||
dispatchFromOptions(result, moduleOptions, original, hierarchy) | ||
} | ||
} | ||
@@ -101,13 +110,11 @@ return result | ||
export function createDirectActions<T extends ActionsImpl>( | ||
actionsImpl: T, | ||
function createDirectActions( | ||
result: any, | ||
actionsImpl: ActionsImpl, | ||
original: Store<any>, | ||
hierarchy?: string[] | ||
): ToDirectActions<T> { | ||
) { | ||
const prefix = !hierarchy || hierarchy.length === 0 ? "" : `${hierarchy.join("/")}/` | ||
const result = {} as ToDirectActions<any> | ||
for (const name of Object.keys(actionsImpl)) { | ||
for (const name of Object.keys(actionsImpl)) | ||
result[name] = (payload?: any) => original.dispatch(`${prefix}${name}`, payload) | ||
} | ||
return result | ||
} |
@@ -14,8 +14,7 @@ import { Store } from "vuex" | ||
export type DirectState<O extends StoreOrModuleOptions> = O["state"] & GetStateInModulesOrEmpty<O> | ||
export type DirectState<O extends StoreOrModuleOptions> = | ||
O["state"] | ||
& GetStateInModules<OrEmpty<O["modules"]>> | ||
export type GetStateInModulesOrEmpty<O extends StoreOrModuleOptions> = | ||
O["modules"] extends ModulesImpl ? GetStateInModules<O["modules"]> : {} | ||
export type GetStateInModules<I extends ModulesImpl> = { | ||
type GetStateInModules<I extends ModulesImpl> = { | ||
[M in keyof I]: DirectState<I[M]> | ||
@@ -27,9 +26,7 @@ } | ||
export type DirectGetters<O extends StoreOrModuleOptions> = | ||
(O["getters"] extends GettersImpl ? ToDirectGetters<O["getters"]> : {}) | ||
& GetGettersInModulesOrEmpty<O> | ||
ToDirectGetters<OrEmpty<O["getters"]>> | ||
& GetGettersInModules<FilterNamespaced<OrEmpty<O["modules"]>>> | ||
& FlattenGetters<FilterNotNamespaced<OrEmpty<O["modules"]>>> | ||
export type GetGettersInModulesOrEmpty<O extends StoreOrModuleOptions> = | ||
O["modules"] extends ModulesImpl ? GetGettersInModules<O["modules"]> : {} | ||
export type GetGettersInModules<I extends ModulesImpl> = { | ||
type GetGettersInModules<I extends ModulesImpl> = { | ||
[M in keyof I]: DirectGetters<I[M]> | ||
@@ -42,12 +39,12 @@ } | ||
type FlattenGetters<I extends ModulesImpl> = UnionToIntersection<I[keyof I]["getters"]> | ||
// Mutations | ||
export type DirectMutations<O extends StoreOrModuleOptions> = | ||
(O["mutations"] extends MutationsImpl ? ToDirectMutations<O["mutations"]> : {}) | ||
& GetMutationsInModulesOrEmpty<O> | ||
ToDirectMutations<OrEmpty<O["mutations"]>> | ||
& GetMutationsInModules<FilterNamespaced<OrEmpty<O["modules"]>>> | ||
& FlattenMutations<FilterNotNamespaced<OrEmpty<O["modules"]>>> | ||
export type GetMutationsInModulesOrEmpty<O extends StoreOrModuleOptions> = | ||
O["modules"] extends ModulesImpl ? GetMutationsInModules<O["modules"]> : {} | ||
export type GetMutationsInModules<I extends ModulesImpl> = { | ||
type GetMutationsInModules<I extends ModulesImpl> = { | ||
[M in keyof I]: DirectMutations<I[M]> | ||
@@ -62,12 +59,12 @@ } | ||
type FlattenMutations<I extends ModulesImpl> = UnionToIntersection<I[keyof I]["mutations"]> | ||
// Actions | ||
export type DirectActions<O extends StoreOrModuleOptions> = | ||
(O["actions"] extends ActionsImpl ? ToDirectActions<O["actions"]> : {}) | ||
& GetActionsInModulesOrEmpty<O> | ||
ToDirectActions<OrEmpty<O["actions"]>> | ||
& GetActionsInModules<FilterNamespaced<OrEmpty<O["modules"]>>> | ||
& FlattenActions<FilterNotNamespaced<OrEmpty<O["modules"]>>> | ||
export type GetActionsInModulesOrEmpty<O extends StoreOrModuleOptions> = | ||
O["modules"] extends ModulesImpl ? GetActionsInModules<O["modules"]> : {} | ||
export type GetActionsInModules<I extends ModulesImpl> = { | ||
type GetActionsInModules<I extends ModulesImpl> = { | ||
[M in keyof I]: DirectActions<I[M]> | ||
@@ -81,1 +78,16 @@ } | ||
} | ||
type FlattenActions<I extends ModulesImpl> = UnionToIntersection<I[keyof I]["actions"]> | ||
// Common helpers | ||
type FilterNamespaced<I extends ModulesImpl> = Pick<I, KeyOfType<I, { namespaced: true }>> | ||
type FilterNotNamespaced<I extends ModulesImpl> = Pick<I, KeyOfType<I, { namespaced?: false }>> | ||
type KeyOfType<T, U> = { [P in keyof T]: T[P] extends U ? P : never }[keyof T] | ||
type OrEmpty<T> = T extends {} ? T : {} | ||
type UnionToIntersection<U> = | ||
(U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never |
@@ -1,24 +0,5 @@ | ||
import { Store } from "vuex" | ||
import { ToDirectActions, ToDirectGetters, ToDirectMutations, ToDirectStore } from "./direct-types" | ||
import { ToDirectStore } from "./direct-types" | ||
export function createDirectStore<O extends StoreOptions>(options: O): ToDirectStore<O> | ||
export function createDirectGetters<T extends GettersImpl>( | ||
gettersImpl: T, | ||
original: Store<any>, | ||
hierarchy?: string[] | ||
): ToDirectGetters<T> | ||
export function createDirectMutations<T extends MutationsImpl>( | ||
mutationsImpl: T, | ||
original: Store<any>, | ||
hierarchy?: string[] | ||
): ToDirectMutations<T> | ||
export function createDirectActions<T extends ActionsImpl>( | ||
actionsImpl: T, | ||
original: Store<any>, | ||
hierarchy?: string[] | ||
): ToDirectActions<T> | ||
/* | ||
@@ -42,3 +23,3 @@ * Types for Vuex Store Options | ||
export interface ModuleOptions extends StoreOrModuleOptions { | ||
namespaced: true | ||
namespaced?: boolean | ||
} | ||
@@ -45,0 +26,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22180
393
75