define-zustand 3.5.0 → 3.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/define.d.ts +11 -0
- package/dist/define.js +39 -1
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/define.d.ts
CHANGED
|
@@ -2,6 +2,17 @@ import { FunctionComponent, ReactNode } from 'react';
|
|
|
2
2
|
import { gettersStateType, optionsType } from './types';
|
|
3
3
|
export declare function defineStateFactory<S extends Record<string, any>, G extends gettersStateType<S>, Actions extends Record<string, any>>(options: optionsType<S, G, Actions>): () => optionsType<S, G, Actions>;
|
|
4
4
|
export declare function defineStore<S extends Record<string, any>, G extends gettersStateType<S>, Actions extends Record<string, any>>(options: optionsType<S, G, Actions>): import("zustand").UseBoundStore<import("zustand").StoreApi<import("./types").modelStateType<S, G, Actions>>>;
|
|
5
|
+
export declare function defineQuery<P, F extends (params: P) => any>(options: {
|
|
6
|
+
params: P;
|
|
7
|
+
queryFn: F;
|
|
8
|
+
}): import("zustand").UseBoundStore<import("zustand").StoreApi<import("./types").modelStateType<{
|
|
9
|
+
params: P;
|
|
10
|
+
loading: boolean;
|
|
11
|
+
data: Awaited<ReturnType<F>> | undefined;
|
|
12
|
+
error: Error | undefined;
|
|
13
|
+
}, {}, {
|
|
14
|
+
query: (params?: Partial<P>) => Promise<void>;
|
|
15
|
+
}>>>;
|
|
5
16
|
export declare function defineContext<S extends Record<string, any>, G extends gettersStateType<S>, Actions extends Record<string, any>>(options: optionsType<S, G, Actions>): {
|
|
6
17
|
Provider: FunctionComponent<{
|
|
7
18
|
children?: ReactNode;
|
package/dist/define.js
CHANGED
|
@@ -9,6 +9,44 @@ function defineStateFactory(options) {
|
|
|
9
9
|
function defineStore(options) {
|
|
10
10
|
return create(storeMiddleware(options));
|
|
11
11
|
}
|
|
12
|
+
function defineQuery(options) {
|
|
13
|
+
return defineStore({
|
|
14
|
+
state: () => ({
|
|
15
|
+
params: options.params,
|
|
16
|
+
loading: false,
|
|
17
|
+
data: void 0,
|
|
18
|
+
error: void 0
|
|
19
|
+
}),
|
|
20
|
+
getters: {},
|
|
21
|
+
actions: (setState, getState) => ({
|
|
22
|
+
query: async params => {
|
|
23
|
+
try {
|
|
24
|
+
const state = getState();
|
|
25
|
+
setState(state => {
|
|
26
|
+
state.loading = true;
|
|
27
|
+
});
|
|
28
|
+
const newParams = {
|
|
29
|
+
...state.params,
|
|
30
|
+
...params
|
|
31
|
+
};
|
|
32
|
+
const data = await options.queryFn(newParams);
|
|
33
|
+
setState(state => {
|
|
34
|
+
state.data = data;
|
|
35
|
+
state.params = newParams;
|
|
36
|
+
});
|
|
37
|
+
} catch (error) {
|
|
38
|
+
setState(state => {
|
|
39
|
+
state.error = new Error(error.message);
|
|
40
|
+
});
|
|
41
|
+
} finally {
|
|
42
|
+
setState(state => {
|
|
43
|
+
state.loading = false;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
})
|
|
48
|
+
});
|
|
49
|
+
}
|
|
12
50
|
function defineContext(options) {
|
|
13
51
|
const creatorResult = storeMiddleware(options);
|
|
14
52
|
const $createStore = () => createStore()(creatorResult);
|
|
@@ -38,4 +76,4 @@ function defineContext(options) {
|
|
|
38
76
|
};
|
|
39
77
|
}
|
|
40
78
|
|
|
41
|
-
export { defineContext, defineStateFactory, defineStore };
|
|
79
|
+
export { defineContext, defineQuery, defineStateFactory, defineStore };
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
export { storeMiddleware } from './store-middleware.js';
|
|
2
|
-
export { defineContext, defineStateFactory, defineStore } from './define.js';
|
|
2
|
+
export { defineContext, defineQuery, defineStateFactory, defineStore } from './define.js';
|