@dynamic-labs-sdk/react-hooks 1.14.0 → 1.17.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.
Files changed (32) hide show
  1. package/dist/foundation/hookKeyNamespace/hookKeyNamespace.d.ts +8 -0
  2. package/dist/foundation/hookKeyNamespace/hookKeyNamespace.d.ts.map +1 -0
  3. package/dist/foundation/useBaseMutation/index.d.ts +1 -1
  4. package/dist/foundation/useBaseMutation/index.d.ts.map +1 -1
  5. package/dist/foundation/useBaseMutation/useBaseMutation.d.ts +23 -1
  6. package/dist/foundation/useBaseMutation/useBaseMutation.d.ts.map +1 -1
  7. package/dist/foundation/useBaseQuery/index.d.ts +1 -1
  8. package/dist/foundation/useBaseQuery/index.d.ts.map +1 -1
  9. package/dist/foundation/useBaseQuery/queryKeyPrefix.d.ts +6 -0
  10. package/dist/foundation/useBaseQuery/queryKeyPrefix.d.ts.map +1 -0
  11. package/dist/foundation/useBaseQuery/useBaseQuery.d.ts +22 -1
  12. package/dist/foundation/useBaseQuery/useBaseQuery.d.ts.map +1 -1
  13. package/dist/foundation/useBaseState/useBaseState.d.ts +1 -1
  14. package/dist/foundation/useBaseState/useBaseState.d.ts.map +1 -1
  15. package/dist/hooks/wrappers/functions/useAuthenticateTotpMfaDevice/useAuthenticateTotpMfaDevice.d.ts.map +1 -1
  16. package/dist/hooks/wrappers/functions/useCompleteDeviceRegistration/useCompleteDeviceRegistration.d.ts.map +1 -1
  17. package/dist/hooks/wrappers/functions/useDeleteMfaDevice/useDeleteMfaDevice.d.ts.map +1 -1
  18. package/dist/hooks/wrappers/functions/useGetActiveNetworkData/useGetActiveNetworkData.d.ts.map +1 -1
  19. package/dist/hooks/wrappers/functions/useRegisterTotpMfaDevice/useRegisterTotpMfaDevice.d.ts.map +1 -1
  20. package/dist/hooks/wrappers/functions/useRevokeAllRegisteredDevices/useRevokeAllRegisteredDevices.d.ts.map +1 -1
  21. package/dist/hooks/wrappers/functions/useRevokeRegisteredDevice/useRevokeRegisteredDevice.d.ts.map +1 -1
  22. package/dist/hooks/wrappers/functions/useSetDefaultMfaDevice/useSetDefaultMfaDevice.d.ts.map +1 -1
  23. package/dist/index.cjs +115 -27
  24. package/dist/index.cjs.map +1 -1
  25. package/dist/index.esm.js +115 -27
  26. package/dist/index.esm.js.map +1 -1
  27. package/dist/middlewares/invalidatesQueries/index.d.ts +2 -0
  28. package/dist/middlewares/invalidatesQueries/index.d.ts.map +1 -0
  29. package/dist/middlewares/invalidatesQueries/invalidatesQueries.d.ts +19 -0
  30. package/dist/middlewares/invalidatesQueries/invalidatesQueries.d.ts.map +1 -0
  31. package/dist/tsconfig.lib.tsbuildinfo +1 -1
  32. package/package.json +4 -4
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.14.0";
10
+ var version = "1.17.0";
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,24 @@ 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 hooks that gate the query and/or run
151
+ * reactive subscriptions) and the query only runs when every one returns
152
+ * `true`.
119
153
  *
120
154
  * Returns the full `useQuery` result — `data`, `error`, `isLoading`,
121
155
  * `isFetching`, `isSuccess`, `refetch`, `status`, etc.
122
156
  */
123
- const useBaseQuery = ({ queryFn, queryKey, queryParams, enabled = true }) => {
157
+ const useBaseQuery = ({ enabled = true, middlewares = EMPTY_MIDDLEWARES, queryFn, queryKey, queryParams }) => {
124
158
  const client = useDynamicClient();
159
+ const middlewaresAllow = middlewares.map((useMiddleware) => useMiddleware()).every(Boolean);
125
160
  return useQuery({
126
161
  queryFn: async () => {
127
162
  await waitForClientInitialized(client);
128
163
  return queryFn(client);
129
164
  },
130
- queryKey: useMemo(() => [
131
- "@dynamic-labs-sdk/react-hooks",
132
- "query",
133
- ...queryKey
134
- ], [queryKey]),
165
+ queryKey: useMemo(() => [...QUERY_KEY_PREFIX, ...queryKey], [queryKey]),
135
166
  ...queryParams,
136
- enabled: enabled && queryParams?.enabled !== false
167
+ enabled: enabled && middlewaresAllow && queryParams?.enabled !== false
137
168
  });
138
169
  };
139
170
 
@@ -272,6 +303,48 @@ const useAuthenticatePasskeyMFA = ({ mutateParams } = {}) => useBaseMutation({
272
303
  mutationFn: ({ client, variables }) => authenticatePasskeyMFA(variables, client)
273
304
  });
274
305
 
306
+ //#endregion
307
+ //#region src/middlewares/invalidatesQueries/invalidatesQueries.ts
308
+ /**
309
+ * Mutation middleware factory that invalidates one or more query hooks when
310
+ * the mutation succeeds, so any mounted consumer of those queries refetches.
311
+ *
312
+ * Pass the query hooks' names (their `queryKey` leaf, e.g. `'useGetMfaDevices'`);
313
+ * each is namespaced under `QUERY_KEY_PREFIX` to match the keys `useBaseQuery`
314
+ * creates. The invalidation loop lives here, not in `useBaseMutation`.
315
+ *
316
+ * @example
317
+ * ```ts
318
+ * useBaseMutation({
319
+ * middlewares: [invalidatesQueries('useGetMfaDevices')],
320
+ * mutationFn: ({ client, variables }) => deleteMfaDevice(variables, client),
321
+ * });
322
+ * ```
323
+ */
324
+ const invalidatesQueries = (...queryKeys) => () => {
325
+ const queryClient = useQueryClient();
326
+ const client = useDynamicClient();
327
+ /**
328
+ * Swallows an `invalidateQueries` rejection so a failed refetch never
329
+ * surfaces as an unhandled rejection nor rejects the combined invalidation
330
+ * promise, while still logging it at debug level so internal devs can see
331
+ * which query failed to refetch. Curried over the query key and extracted
332
+ * to a named handler so the invalidation map stays within the
333
+ * function-nesting limit.
334
+ */
335
+ const debugInvalidationFailure = (queryKey) => (error) => {
336
+ getCore(client).logger.debug("[invalidatesQueries] failed to invalidate query", {
337
+ error,
338
+ queryKey,
339
+ stack: error instanceof Error ? error.stack : void 0
340
+ });
341
+ };
342
+ return { onSuccess: () => Promise.all(queryKeys.map((key) => {
343
+ const queryKey = [...QUERY_KEY_PREFIX, key];
344
+ return queryClient.invalidateQueries({ queryKey }).catch(debugInvalidationFailure(queryKey));
345
+ })) };
346
+ };
347
+
275
348
  //#endregion
276
349
  //#region src/hooks/wrappers/functions/useAuthenticateTotpMfaDevice/useAuthenticateTotpMfaDevice.ts
277
350
  /**
@@ -289,10 +362,13 @@ const useAuthenticatePasskeyMFA = ({ mutateParams } = {}) => useBaseMutation({
289
362
  * </button>
290
363
  * ```
291
364
  */
292
- const useAuthenticateTotpMfaDevice = ({ mutateParams } = {}) => useBaseMutation({
293
- mutateParams,
294
- mutationFn: ({ client, variables }) => authenticateTotpMfaDevice(variables, client)
295
- });
365
+ const useAuthenticateTotpMfaDevice = ({ mutateParams } = {}) => {
366
+ return useBaseMutation({
367
+ middlewares: [invalidatesQueries("useGetMfaDevices")],
368
+ mutateParams,
369
+ mutationFn: ({ client, variables }) => authenticateTotpMfaDevice(variables, client)
370
+ });
371
+ };
296
372
 
297
373
  //#endregion
298
374
  //#region src/foundation/useBaseState/useBaseState.ts
@@ -363,7 +439,7 @@ const useBaseState = ({ event, queryKey, queryParams, selector, defaultValue, sk
363
439
  return Object.keys(map).sort((a, b) => a.localeCompare(b));
364
440
  }, [eventsMapJson]);
365
441
  const fullQueryKey = useMemo(() => [
366
- "@dynamic-labs-sdk/react-hooks",
442
+ HOOK_KEY_NAMESPACE,
367
443
  "state",
368
444
  ...queryKey
369
445
  ], [JSON.stringify(queryKey)]);
@@ -554,6 +630,7 @@ const useAddCoinbaseOnrampOrderEventListener = ({ listener }) => {
554
630
  * ```
555
631
  */
556
632
  const useCompleteDeviceRegistration = ({ mutateParams } = {}) => useBaseMutation({
633
+ middlewares: [invalidatesQueries("useGetRegisteredDevices")],
557
634
  mutateParams,
558
635
  mutationFn: ({ client, variables }) => completeDeviceRegistration(variables, client)
559
636
  });
@@ -734,10 +811,13 @@ const useCreateNewMfaRecoveryCodes = ({ mutateParams } = {}) => useBaseMutation(
734
811
  * </button>
735
812
  * ```
736
813
  */
737
- const useDeleteMfaDevice = ({ mutateParams } = {}) => useBaseMutation({
738
- mutateParams,
739
- mutationFn: ({ client, variables }) => deleteMfaDevice(variables, client)
740
- });
814
+ const useDeleteMfaDevice = ({ mutateParams } = {}) => {
815
+ return useBaseMutation({
816
+ middlewares: [invalidatesQueries("useGetMfaDevices")],
817
+ mutateParams,
818
+ mutationFn: ({ client, variables }) => deleteMfaDevice(variables, client)
819
+ });
820
+ };
741
821
 
742
822
  //#endregion
743
823
  //#region src/hooks/wrappers/functions/useDeletePasskey/useDeletePasskey.ts
@@ -1252,6 +1332,7 @@ const useRegisterPasskey = ({ mutateParams } = {}) => useBaseMutation({
1252
1332
  * ```
1253
1333
  */
1254
1334
  const useRegisterTotpMfaDevice = ({ mutateParams } = {}) => useBaseMutation({
1335
+ middlewares: [invalidatesQueries("useGetMfaDevices")],
1255
1336
  mutateParams,
1256
1337
  mutationFn: ({ client }) => registerTotpMfaDevice(client)
1257
1338
  });
@@ -1312,10 +1393,13 @@ const useRequestExternalAuthElevatedToken = ({ mutateParams } = {}) => useBaseMu
1312
1393
  * </button>
1313
1394
  * ```
1314
1395
  */
1315
- const useRevokeAllRegisteredDevices = ({ mutateParams } = {}) => useBaseMutation({
1316
- mutateParams,
1317
- mutationFn: ({ client }) => revokeAllRegisteredDevices(client)
1318
- });
1396
+ const useRevokeAllRegisteredDevices = ({ mutateParams } = {}) => {
1397
+ return useBaseMutation({
1398
+ middlewares: [invalidatesQueries("useGetRegisteredDevices")],
1399
+ mutateParams,
1400
+ mutationFn: ({ client }) => revokeAllRegisteredDevices(client)
1401
+ });
1402
+ };
1319
1403
 
1320
1404
  //#endregion
1321
1405
  //#region src/hooks/wrappers/functions/useRevokeRegisteredDevice/useRevokeRegisteredDevice.ts
@@ -1338,10 +1422,13 @@ const useRevokeAllRegisteredDevices = ({ mutateParams } = {}) => useBaseMutation
1338
1422
  * </button>
1339
1423
  * ```
1340
1424
  */
1341
- const useRevokeRegisteredDevice = ({ mutateParams } = {}) => useBaseMutation({
1342
- mutateParams,
1343
- mutationFn: ({ client, variables }) => revokeRegisteredDevice(variables, client)
1344
- });
1425
+ const useRevokeRegisteredDevice = ({ mutateParams } = {}) => {
1426
+ return useBaseMutation({
1427
+ middlewares: [invalidatesQueries("useGetRegisteredDevices")],
1428
+ mutateParams,
1429
+ mutationFn: ({ client, variables }) => revokeRegisteredDevice(variables, client)
1430
+ });
1431
+ };
1345
1432
 
1346
1433
  //#endregion
1347
1434
  //#region src/hooks/wrappers/functions/useSendEmailOTP/useSendEmailOTP.ts
@@ -1416,6 +1503,7 @@ const useSendSmsOTP = ({ mutateParams } = {}) => useBaseMutation({
1416
1503
  * ```
1417
1504
  */
1418
1505
  const useSetDefaultMfaDevice = ({ mutateParams } = {}) => useBaseMutation({
1506
+ middlewares: [invalidatesQueries("useGetMfaDevices")],
1419
1507
  mutateParams,
1420
1508
  mutationFn: ({ client, variables }) => setDefaultMfaDevice(variables, client)
1421
1509
  });