-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from zWingz/support
feat: support mapState/mapMutations and parameters checking
- Loading branch information
Showing
7 changed files
with
199 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# CHANGELOG | ||
|
||
## Release 0.0.5 | ||
|
||
### Feature | ||
|
||
- support `mapMutations` | ||
|
||
### Enhance | ||
|
||
- support `mapActions` parameter type checking | ||
|
||
### Bugfix | ||
|
||
- `mapStats` return a function | ||
|
||
## Release 0.0.4 | ||
|
||
- support `mapState` `mapGetters` `mapActions` and `dispatch` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { StoreOptions, ActionHandler, ActionObject } from 'vuex'; | ||
|
||
|
||
type Payload<T> = T extends (arg1: any, arg2: any) => any ? Parameters<T>[1] : never | ||
|
||
|
||
export type PromiseAction<T> = T extends ActionHandler<any, any> | ||
? Payload<T> extends undefined | ||
? () => Promise<ReturnType<T>> | ||
: (payload: Payload<T>) => Promise<ReturnType<T>> | ||
: T extends ActionObject<any, any> | ||
? Payload<T['handler']> extends undefined | ||
? () => Promise<ReturnType<T['handler']>> | ||
: (payload: Payload<T['handler']>) => Promise<ReturnType<T['handler']>> | ||
: never; | ||
|
||
export type Mutation<T> = T extends (...args: any[]) => void | ||
? Payload<T> extends undefined | ||
? () => void | ||
: (payload: Payload<T>) => void | ||
: never; | ||
|
||
export type Module<T extends StoreOptions<any>> = T['modules']; | ||
export type Getters<T extends StoreOptions<any>> = T['getters']; | ||
export type Actions<T extends StoreOptions<any>> = T['actions']; | ||
export type State<T extends StoreOptions<any>> = T['state']; | ||
export type Mutations<T extends StoreOptions<any>> = T['mutations']; | ||
|
||
export type OnlyString<T> = keyof T & string; | ||
|
||
export type GetKeys<T extends StoreOptions<any>, E, K> = E extends OnlyString< | ||
Module<T> | ||
> | ||
? K extends OnlyString<Module<T>[E]> | ||
? OnlyString<Module<T>[E][K]> | ||
: never | ||
: never; | ||
|
||
export type GetObject<T extends StoreOptions<any>, E, K> = E extends OnlyString< | ||
Module<T> | ||
> | ||
? K extends OnlyString<Module<T>[E]> | ||
? Module<T>[E][K] | ||
: never | ||
: never; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { EnhanceStore } from '../src/store'; | ||
const state = { | ||
actions: { | ||
setName({ commit }, payload) { | ||
}, | ||
setName2({ commit }, payload) { | ||
}, | ||
}, | ||
modules: { | ||
user: { | ||
namespaced: true, | ||
actions: { | ||
updateUsername({ commit }, payload: string) {}, | ||
updateUserMeta( | ||
{ commit }, | ||
payload: { age?: number; address?: string } | ||
) {}, | ||
test: { | ||
root: true, | ||
handler({ commit }, payload: string) {}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
const s = new EnhanceStore(state); | ||
export const { mapGetters, store, mapActions, mapMutations, mapState } = s; | ||
|
||
mapActions('user', ['updateUserMeta']).updateUserMeta({ age: 1 }); | ||
mapActions('user', ['updateUsername']).updateUsername('123'); | ||
mapActions('user', ['test']).test('fd'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters