Skip to content

Commit 87bd2a5

Browse files
committed
fix(hooks): improve vuex interface again
1 parent 481bfc6 commit 87bd2a5

File tree

8 files changed

+80
-58
lines changed

8 files changed

+80
-58
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
"module": "esm/index.js",
1313
"files": [
1414
"lib/",
15-
"esm/",
16-
"types/"
15+
"esm/"
1716
],
1817
"scripts": {
1918
"test": "jest",
Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
import { computed, Wrapper } from 'vue-function-api';
2-
import {
3-
useState,
4-
useGetters,
5-
mapState,
6-
mapGetters,
7-
mapMutations,
8-
mapActions,
9-
} from 'vuex';
10-
import { getRuntimeVM } from '../util/runtime';
1+
import { computed } from 'vue-function-api';
2+
import { mapState, mapGetters, mapMutations, mapActions } from 'vuex';
3+
import { useState, useGetters, useMutations, useActions } from './interface';
4+
import { getRuntimeVM } from '../../util/runtime';
115

126
export enum Helper {
137
State,
@@ -16,7 +10,9 @@ export enum Helper {
1610
Actions,
1711
}
1812

19-
function handleComputed(mappedFn: Function): Wrapper<any> {
13+
type Helpers = useState | useGetters | useMutations | useActions;
14+
15+
function handleComputed(mappedFn: Function) {
2016
// TypeError: Cannot read property '_modulesNamespaceMap' of undefined
2117
// You must get `runtimeVM` in real time in the calculation properties.
2218
return computed(() => mappedFn.call(getRuntimeVM()));
@@ -33,12 +29,6 @@ const helpers = {
3329
[Helper.Actions]: { fn: mapActions, handler: handleMethods },
3430
};
3531

36-
export type Helpers =
37-
| typeof useState
38-
| typeof useGetters
39-
| typeof mapMutations
40-
| typeof mapActions;
41-
4232
export default function createVuexHelper<T extends Helpers>(h: Helper) {
4333
const helper = helpers[h];
4434

@@ -53,3 +43,5 @@ export default function createVuexHelper<T extends Helpers>(h: Helper) {
5343
return dictionary;
5444
}) as T;
5545
}
46+
47+
export * from './interface';

src/helpers/vuex/interface.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import Vue from 'vue';
2+
import { Dispatch, Commit } from 'vuex';
3+
import { Wrapper } from 'vue-function-api';
4+
5+
type Dictionary<T> = { [key: string]: T };
6+
type Computed = Wrapper<any>;
7+
type MutationMethod = (...args: any[]) => void;
8+
type ActionMethod = (...args: any[]) => Promise<any>;
9+
type CustomVue = Vue & Dictionary<any>;
10+
11+
interface Mapper<R> {
12+
(map: string[]): Dictionary<R>;
13+
(map: Dictionary<string>): Dictionary<R>;
14+
}
15+
16+
interface MapperWithNamespace<R> {
17+
(namespace: string, map: string[]): Dictionary<R>;
18+
(namespace: string, map: Dictionary<string>): Dictionary<R>;
19+
}
20+
21+
interface FunctionMapper<F, R> {
22+
(
23+
map: Dictionary<(this: CustomVue, fn: F, ...args: any[]) => any>,
24+
): Dictionary<R>;
25+
}
26+
27+
interface FunctionMapperWithNamespace<F, R> {
28+
(
29+
namespace: string,
30+
map: Dictionary<(this: CustomVue, fn: F, ...args: any[]) => any>,
31+
): Dictionary<R>;
32+
}
33+
34+
interface MapperForState {
35+
<S>(
36+
map: Dictionary<(this: CustomVue, state: S, getters: any) => any>,
37+
): Dictionary<Computed>;
38+
}
39+
40+
interface MapperForStateWithNamespace {
41+
<S>(
42+
namespace: string,
43+
map: Dictionary<(this: CustomVue, state: S, getters: any) => any>,
44+
): Dictionary<Computed>;
45+
}
46+
47+
export type useState = Mapper<Computed> &
48+
MapperWithNamespace<Computed> &
49+
MapperForState &
50+
MapperForStateWithNamespace;
51+
52+
export type useGetters = Mapper<Computed> & MapperWithNamespace<Computed>;
53+
54+
export type useMutations = Mapper<MutationMethod> &
55+
MapperWithNamespace<MutationMethod> &
56+
FunctionMapper<Commit, MutationMethod> &
57+
FunctionMapperWithNamespace<Commit, MutationMethod>;
58+
59+
export type useActions = Mapper<ActionMethod> &
60+
MapperWithNamespace<ActionMethod> &
61+
FunctionMapper<Dispatch, ActionMethod> &
62+
FunctionMapperWithNamespace<Dispatch, ActionMethod>;

src/useActions.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { mapActions } from 'vuex';
2-
import createVuexHelper, { Helper } from './helpers/vuex';
1+
import createVuexHelper, { Helper, useActions } from './helpers/vuex';
32

4-
export default createVuexHelper<typeof mapActions>(Helper.Actions);
3+
export default createVuexHelper<useActions>(Helper.Actions);

src/useGetters.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { useGetters } from 'vuex';
2-
import createVuexHelper, { Helper } from './helpers/vuex';
1+
import createVuexHelper, { Helper, useGetters } from './helpers/vuex';
32

4-
export default createVuexHelper<typeof useGetters>(Helper.Getters);
3+
export default createVuexHelper<useGetters>(Helper.Getters);

src/useMutations.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { mapMutations } from 'vuex';
2-
import createVuexHelper, { Helper } from './helpers/vuex';
1+
import createVuexHelper, { Helper, useMutations } from './helpers/vuex';
32

4-
export default createVuexHelper<typeof mapMutations>(Helper.Mutations);
3+
export default createVuexHelper<useMutations>(Helper.Mutations);

src/useState.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { useState } from 'vuex';
2-
import createVuexHelper, { Helper } from './helpers/vuex';
1+
import createVuexHelper, { Helper, useState } from './helpers/vuex';
32

4-
export default createVuexHelper<typeof useState>(Helper.State);
3+
export default createVuexHelper<useState>(Helper.State);

types/vuex/helpers.d.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)