define-zustand 3.5.0 → 4.0.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/README.md CHANGED
@@ -22,10 +22,10 @@ const useStore = defineStore({
22
22
  a: 1,
23
23
  b: 1
24
24
  }),
25
- action: () => ({}),
26
25
  getter: {
27
26
  count: state => state.a + state.b
28
- }
27
+ },
28
+ action: () => ({}),
29
29
  })
30
30
 
31
31
  // Use defineModelFactory
@@ -34,10 +34,10 @@ const stateFactory = defineStateFactory({
34
34
  a: 1,
35
35
  b: 1
36
36
  }),
37
- action: () => ({}),
38
37
  getter: {
39
38
  count: state => state.a + state.b
40
- }
39
+ },
40
+ action: () => ({}),
41
41
  })
42
42
  const useStore = defineStore(stateFactory())
43
43
  const { Provider, useContext } = defineContext(stateFactory())
package/dist/define.d.ts CHANGED
@@ -1,10 +1,22 @@
1
1
  import { FunctionComponent, ReactNode } from 'react';
2
- import { gettersStateType, optionsType } from './types';
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
- 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 defineContext<S extends Record<string, any>, G extends gettersStateType<S>, Actions extends Record<string, any>>(options: optionsType<S, G, Actions>): {
2
+ import { Options } from './types';
3
+ export declare function defineStateFactory<S extends Record<string, any>, G extends Record<string, any>, Actions extends Record<string, any>>(options: Options<S, G, Actions>): () => Options<S, G, Actions>;
4
+ export declare function defineStore<S extends Record<string, any>, G extends Record<string, any>, Actions extends Record<string, any>>(options: Options<S, G, Actions>): import("zustand").UseBoundStore<import("zustand").StoreApi<import("./types").ModelState<S, G, Actions>>>;
5
+ export declare function defineQuery<F extends (params: any) => any>(options: {
6
+ queryFn: F;
7
+ params: Parameters<F>[0];
8
+ }): import("zustand").UseBoundStore<import("zustand").StoreApi<import("./types").ModelState<{
9
+ params: Parameters<F>[0];
10
+ loading: boolean;
11
+ data: Awaited<ReturnType<F>> | undefined;
12
+ error: Error | undefined;
13
+ }, {}, {
14
+ fetchData: F;
15
+ refetch: (params?: Partial<Parameters<F>[0]> | undefined) => Promise<void>;
16
+ }>>>;
17
+ export declare function defineContext<S extends Record<string, any>, G extends Record<string, any>, Actions extends Record<string, any>>(options: Options<S, G, Actions>): {
6
18
  Provider: FunctionComponent<{
7
19
  children?: ReactNode;
8
20
  } & Partial<S>>;
9
- useContext: <T>(selector: (state: import("./types").modelStateType<S, G, Actions>) => T) => T;
21
+ useContext: <T>(selector: (state: import("./types").ModelState<S, G, Actions>) => T) => T;
10
22
  };
package/dist/define.js CHANGED
@@ -9,6 +9,56 @@ 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
+ async function fetchData(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
+ return data;
38
+ } catch (error) {
39
+ setState(state => {
40
+ state.error = new Error(error.message);
41
+ });
42
+ } finally {
43
+ setState(state => {
44
+ state.loading = false;
45
+ });
46
+ }
47
+ }
48
+ async function refetch(params) {
49
+ const state = getState();
50
+ await options.queryFn({
51
+ ...state.params,
52
+ ...params
53
+ });
54
+ }
55
+ return {
56
+ fetchData: fetchData,
57
+ refetch
58
+ };
59
+ }
60
+ });
61
+ }
12
62
  function defineContext(options) {
13
63
  const creatorResult = storeMiddleware(options);
14
64
  const $createStore = () => createStore()(creatorResult);
@@ -38,4 +88,4 @@ function defineContext(options) {
38
88
  };
39
89
  }
40
90
 
41
- export { defineContext, defineStateFactory, defineStore };
91
+ 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';
@@ -1,3 +1,3 @@
1
1
  import { StateCreator } from 'zustand';
2
- import { gettersStateType, optionsType, modelStateType } from './types';
3
- export declare function storeMiddleware<S extends Record<string, any>, G extends gettersStateType<S>, Actions extends Record<string, any>>(options: optionsType<S, G, Actions>): StateCreator<modelStateType<S, G, Actions>>;
2
+ import { Options, ModelState } from './types';
3
+ export declare function storeMiddleware<S extends Record<string, any>, G extends Record<string, any>, Actions extends Record<string, any>>(options: Options<S, G, Actions>): StateCreator<ModelState<S, G, Actions>>;
@@ -4,8 +4,8 @@ import { forOwn, keys, pick, isEqualWith, some, isEqual } from 'lodash-es';
4
4
  function storeMiddleware(options) {
5
5
  return (set, get, store) => {
6
6
  const state = options.state();
7
- forOwn(options.getters, (getter, k) => {
8
- state[k] = getter(state);
7
+ forOwn(options.getters(state), (getter, k) => {
8
+ state[k] = getter;
9
9
  });
10
10
  // getterListener
11
11
  // ----------------------------------------------------------------------
@@ -16,12 +16,11 @@ function storeMiddleware(options) {
16
16
  const isUpdate = isEqualWith(normalState, pick(prevState, normalStateKeys), (current, prev) => some(current, (v, k) => v !== prev[k]));
17
17
  if (isUpdate) {
18
18
  const newGetterState = pick(state, getterKeys);
19
- forOwn(options.getters, (getter, k) => {
20
- const current = getter(normalState);
21
- if (isEqual(newGetterState[k], current)) {
19
+ forOwn(options.getters(normalState), (getterState, k) => {
20
+ if (isEqual(newGetterState[k], getterState)) {
22
21
  return;
23
22
  }
24
- newGetterState[k] = current;
23
+ newGetterState[k] = getterState;
25
24
  });
26
25
  set(newGetterState);
27
26
  }
@@ -1,4 +1,4 @@
1
- import type { Meta, StoryObj } from '@storybook/react';
1
+ import type { Meta, StoryObj } from '@storybook/react-vite';
2
2
  declare const meta: Meta;
3
3
  export default meta;
4
4
  type Story = StoryObj;
package/dist/types.d.ts CHANGED
@@ -1,22 +1,18 @@
1
1
  import { StoreApi as ZStoreApi, ExtractState as ZExtractState } from 'zustand';
2
- export type gettersStateType<S> = Record<string, (state: S) => any>;
3
- export type ExtraGettersState<G extends gettersStateType<any>> = {
4
- [key in keyof G]: ReturnType<G[key]>;
5
- };
6
- export type stateType<S, G extends gettersStateType<any>> = S & ExtraGettersState<G>;
2
+ export type State<S, G> = S & G;
7
3
  export type StoreApi<S> = Omit<ZStoreApi<S>, 'setState'> & {
8
4
  setState: (updater: ((state: S) => void) | Partial<S>, replace?: boolean) => void;
9
5
  };
10
6
  export type ExtractState<S> = ZExtractState<S>;
11
- export interface insideActionsType<S> {
7
+ export interface InsideActions<S> {
12
8
  reset: () => void;
13
9
  setState: StoreApi<S>['setState'];
14
10
  subscribe: StoreApi<S>['subscribe'];
15
11
  }
16
- export type actionsType<S, G extends gettersStateType<any>, OptionAction> = (setState: insideActionsType<S>['setState'], getState: () => stateType<S, G> & insideActionsType<S>, store: StoreApi<S>) => OptionAction;
17
- export interface optionsType<S, G extends gettersStateType<any>, Actions> {
12
+ export type Actions<S, G, OptionAction> = (setState: InsideActions<S>['setState'], getState: () => State<S, G> & InsideActions<S>, store: StoreApi<S>) => OptionAction;
13
+ export interface Options<S, G, A> {
18
14
  state: () => S;
19
- getters: G;
20
- actions: actionsType<S, G, Actions>;
15
+ getters: (state: S) => G;
16
+ actions: Actions<S, G, A>;
21
17
  }
22
- export type modelStateType<S, G extends gettersStateType<any>, Actions> = stateType<S, G> & Actions & insideActionsType<S>;
18
+ export type ModelState<S, G, A> = State<S, G> & A & InsideActions<S>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "define-zustand",
3
- "version": "3.5.0",
3
+ "version": "4.0.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",