@dynamic-labs-sdk/react-hooks 1.13.0 → 1.16.1

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/index.esm.js CHANGED
@@ -7,7 +7,7 @@ import { getCore } from "@dynamic-labs-sdk/client/core";
7
7
 
8
8
  //#region package.json
9
9
  var name = "@dynamic-labs-sdk/react-hooks";
10
- var version = "1.13.0";
10
+ var version = "1.16.1";
11
11
 
12
12
  //#endregion
13
13
  //#region src/modules/DynamicContext/DynamicContext.ts
@@ -57,6 +57,7 @@ const useDynamicClient = () => {
57
57
 
58
58
  //#endregion
59
59
  //#region src/foundation/useBaseMutation/useBaseMutation.ts
60
+ const EMPTY_MIDDLEWARES$1 = [];
60
61
  /**
61
62
  * Internal hook that wraps a react-query `useMutation` with the conventions
62
63
  * used by every async-write hook in this package:
@@ -67,12 +68,15 @@ const useDynamicClient = () => {
67
68
  * - Callers pass `mutateParams` to override any default
68
69
  * `UseMutationOptions`, so consumers can wire `onSuccess` / `onError` /
69
70
  * etc.
71
+ * - Callers pass `middlewares` (named hooks like `invalidatesQueries`) and
72
+ * each contributed `onSuccess` runs, in order, ahead of the consumer's own.
70
73
  *
71
74
  * Returns the full `useMutation` result — `mutate`, `mutateAsync`, `data`,
72
75
  * `error`, `isPending`, `isSuccess`, `reset`, `status`, etc.
73
76
  */
74
- const useBaseMutation = ({ mutationFn, mutateParams }) => {
77
+ const useBaseMutation = ({ middlewares = EMPTY_MIDDLEWARES$1, mutationFn, mutateParams }) => {
75
78
  const client = useDynamicClient();
79
+ const contributions = middlewares.map((useMiddleware) => useMiddleware());
76
80
  return useMutation({
77
81
  mutationFn: async (variables) => {
78
82
  await waitForClientInitialized(client);
@@ -81,7 +85,11 @@ const useBaseMutation = ({ mutationFn, mutateParams }) => {
81
85
  variables
82
86
  });
83
87
  },
84
- ...mutateParams
88
+ ...mutateParams,
89
+ onSuccess: async (...args) => {
90
+ for (const contribution of contributions) await contribution.onSuccess?.(...args);
91
+ return mutateParams?.onSuccess?.(...args);
92
+ }
85
93
  });
86
94
  };
87
95
 
@@ -103,9 +111,32 @@ const useAcknowledgeRecoveryCodes = ({ mutateParams } = {}) => useBaseMutation({
103
111
  mutationFn: ({ client }) => acknowledgeRecoveryCodes(client)
104
112
  });
105
113
 
114
+ //#endregion
115
+ //#region src/foundation/hookKeyNamespace/hookKeyNamespace.ts
116
+ /**
117
+ * Root namespace shared by every react-query key this package creates.
118
+ * Both `QUERY_KEY_PREFIX` (async-read hooks) and the state-hook key
119
+ * prefix derive from this constant so the namespace is defined exactly
120
+ * once.
121
+ */
122
+ const HOOK_KEY_NAMESPACE = "@dynamic-labs-sdk/react-hooks";
123
+
124
+ //#endregion
125
+ //#region src/foundation/useBaseQuery/queryKeyPrefix.ts
126
+ /**
127
+ * Shared prefix for all query keys created by `useBaseQuery`. Mutation hooks
128
+ * reference this when invalidating related queries on success.
129
+ */
130
+ const QUERY_KEY_PREFIX = [HOOK_KEY_NAMESPACE, "query"];
131
+
106
132
  //#endregion
107
133
  //#region src/foundation/useBaseQuery/useBaseQuery.ts
108
134
  /**
135
+ * Stable empty default for the `middlewares` param so the common (ungated)
136
+ * case never allocates a fresh array per render.
137
+ */
138
+ const EMPTY_MIDDLEWARES = [];
139
+ /**
109
140
  * Internal hook that wraps a react-query `useQuery` with the conventions used
110
141
  * by every async-read hook in this package:
111
142
  *
@@ -116,24 +147,23 @@ const useAcknowledgeRecoveryCodes = ({ mutateParams } = {}) => useBaseMutation({
116
147
  * so consumers can opt into retries / custom staleTime / etc.
117
148
  * - Callers pass `enabled: false` (typically derived from whether their
118
149
  * pseudo-required arguments are present) to short-circuit fetches.
150
+ * - Callers pass `middlewares` (named gating hooks like `requiresUser`) and
151
+ * the query only runs when every one returns `true`.
119
152
  *
120
153
  * Returns the full `useQuery` result — `data`, `error`, `isLoading`,
121
154
  * `isFetching`, `isSuccess`, `refetch`, `status`, etc.
122
155
  */
123
- const useBaseQuery = ({ queryFn, queryKey, queryParams, enabled = true }) => {
156
+ const useBaseQuery = ({ enabled = true, middlewares = EMPTY_MIDDLEWARES, queryFn, queryKey, queryParams }) => {
124
157
  const client = useDynamicClient();
158
+ const middlewaresAllow = middlewares.map((useMiddleware) => useMiddleware()).every(Boolean);
125
159
  return useQuery({
126
160
  queryFn: async () => {
127
161
  await waitForClientInitialized(client);
128
162
  return queryFn(client);
129
163
  },
130
- queryKey: useMemo(() => [
131
- "@dynamic-labs-sdk/react-hooks",
132
- "query",
133
- ...queryKey
134
- ], [queryKey]),
164
+ queryKey: useMemo(() => [...QUERY_KEY_PREFIX, ...queryKey], [queryKey]),
135
165
  ...queryParams,
136
- enabled: enabled && queryParams?.enabled !== false
166
+ enabled: enabled && middlewaresAllow && queryParams?.enabled !== false
137
167
  });
138
168
  };
139
169
 
@@ -363,7 +393,7 @@ const useBaseState = ({ event, queryKey, queryParams, selector, defaultValue, sk
363
393
  return Object.keys(map).sort((a, b) => a.localeCompare(b));
364
394
  }, [eventsMapJson]);
365
395
  const fullQueryKey = useMemo(() => [
366
- "@dynamic-labs-sdk/react-hooks",
396
+ HOOK_KEY_NAMESPACE,
367
397
  "state",
368
398
  ...queryKey
369
399
  ], [JSON.stringify(queryKey)]);