@calimero-network/mero-react 9.1.2 → 9.2.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
@@ -394,14 +394,14 @@ useSetSubgroupVisibility, useSubgroupVisibility, useSetTeeAdmissionPolicy
394
394
  useDetachContextFromGroup
395
395
  useNamespaces, useNamespace, useNamespaceGroups, useNamespaceIdentity
396
396
  useNamespacesForApplication, useCreateNamespace, useDeleteNamespace
397
- useJoinNamespace, useCreateNamespaceInvitation, useCreateGroupInNamespace
397
+ useJoinNamespace, useRedeemInvitation, useCreateNamespaceInvitation, useCreateGroupInNamespace
398
398
  useBlobInfo, useBlobUrl, useUploadBlob
399
399
 
400
400
  // Types (mero-react)
401
401
  MeroContextValue, MeroProviderConfig, MeroProviderProps
402
402
  CustomConnectionConfig, AppContext, ExecutionResult
403
403
  ApplicationContextRecord, ContextDiscoveryOptions, ContextDiscoveryState
404
- BlobHookOptions, UseBlobUrlOptions
404
+ BlobHookOptions, UseBlobUrlOptions, MutationOptions
405
405
 
406
406
  // Storage (mero-react)
407
407
  localStorageTokenStorage
package/dist/index.cjs CHANGED
@@ -498,7 +498,22 @@ function MeroProvider({
498
498
  const onError = (err) => {
499
499
  if (!active) return;
500
500
  setIsOnline(false);
501
- if (!err.message.includes("401")) return;
501
+ if (err instanceof meroJs.AuthRevokedError) {
502
+ logout();
503
+ return;
504
+ }
505
+ if (err instanceof meroJs.HTTPError && err.status === 403) {
506
+ if (err.headers?.get("x-auth-error") === "permission_denied") {
507
+ console.warn(
508
+ "[MeroProvider] This session cannot open an event stream (403 permission_denied): its token predates the context:subscribe grant. Logging out \u2014 a fresh login mints a key that can subscribe."
509
+ );
510
+ logout();
511
+ return;
512
+ }
513
+ return;
514
+ }
515
+ const status = err instanceof meroJs.HTTPError ? err.status : null;
516
+ if (status !== 401 && !err.message.includes("401")) return;
502
517
  if (recoveringRef.current) return;
503
518
  recoveringRef.current = true;
504
519
  void (async () => {
@@ -1493,23 +1508,26 @@ function useMountedRef() {
1493
1508
  }, []);
1494
1509
  return mountedRef;
1495
1510
  }
1496
- function useAsyncMutation() {
1511
+ function useAsyncMutation({ throwOnError = false } = {}) {
1497
1512
  const mountedRef = useMountedRef();
1498
1513
  const [loading, setLoading] = react.useState(false);
1499
1514
  const [error, setError] = react.useState(null);
1515
+ const [failure, setFailure] = react.useState(null);
1500
1516
  const run = react.useCallback(
1501
1517
  async (action) => {
1502
1518
  if (mountedRef.current) {
1503
1519
  setLoading(true);
1504
1520
  setError(null);
1521
+ setFailure(null);
1505
1522
  }
1506
1523
  try {
1507
1524
  return await action();
1508
1525
  } catch (err) {
1509
- const errorValue = toError(err);
1510
1526
  if (mountedRef.current) {
1511
- setError(errorValue);
1527
+ setError(toError(err));
1528
+ setFailure(meroJs.classifyError(err));
1512
1529
  }
1530
+ if (throwOnError) throw err;
1513
1531
  return null;
1514
1532
  } finally {
1515
1533
  if (mountedRef.current) {
@@ -1517,9 +1535,9 @@ function useAsyncMutation() {
1517
1535
  }
1518
1536
  }
1519
1537
  },
1520
- [mountedRef]
1538
+ [mountedRef, throwOnError]
1521
1539
  );
1522
- return { loading, error, run, setError };
1540
+ return { loading, error, failure, run, setError };
1523
1541
  }
1524
1542
  function useAsyncResource(fetcher, initialValue, deps) {
1525
1543
  const mountedRef = useMountedRef();
@@ -1913,9 +1931,9 @@ function useDeleteContext() {
1913
1931
  );
1914
1932
  return { deleteContext, loading, error };
1915
1933
  }
1916
- function useJoinContext() {
1934
+ function useJoinContext(options = {}) {
1917
1935
  const { mero } = useMero();
1918
- const { loading, error, run } = useAsyncMutation();
1936
+ const { loading, error, failure, run } = useAsyncMutation(options);
1919
1937
  const joinContext = react.useCallback(
1920
1938
  async (contextId) => {
1921
1939
  if (!mero) return null;
@@ -1923,7 +1941,7 @@ function useJoinContext() {
1923
1941
  },
1924
1942
  [mero, run]
1925
1943
  );
1926
- return { joinContext, loading, error };
1944
+ return { joinContext, loading, error, failure };
1927
1945
  }
1928
1946
  function useJoinSubgroupInheritance() {
1929
1947
  const { mero } = useMero();
@@ -2084,9 +2102,9 @@ function useCreateNamespaceInvitation() {
2084
2102
  );
2085
2103
  return { createNamespaceInvitation, loading, error };
2086
2104
  }
2087
- function useJoinNamespace() {
2105
+ function useJoinNamespace(options = {}) {
2088
2106
  const { mero } = useMero();
2089
- const { loading, error, run } = useAsyncMutation();
2107
+ const { loading, error, failure, run } = useAsyncMutation(options);
2090
2108
  const joinNamespace = react.useCallback(
2091
2109
  async (namespaceId, request) => {
2092
2110
  if (!mero) return null;
@@ -2094,7 +2112,31 @@ function useJoinNamespace() {
2094
2112
  },
2095
2113
  [mero, run]
2096
2114
  );
2097
- return { joinNamespace, loading, error };
2115
+ return { joinNamespace, loading, error, failure };
2116
+ }
2117
+ function useRedeemInvitation() {
2118
+ const { mero } = useMero();
2119
+ const mountedRef = useMountedRef();
2120
+ const [loading, setLoading] = react.useState(false);
2121
+ const [outcome, setOutcome] = react.useState(null);
2122
+ const redeem = react.useCallback(
2123
+ async (namespaceId, invitation, options = {}) => {
2124
+ if (!mero) return null;
2125
+ if (mountedRef.current) {
2126
+ setLoading(true);
2127
+ setOutcome(null);
2128
+ }
2129
+ try {
2130
+ const result = await mero.admin.redeemInvitation(namespaceId, invitation, options);
2131
+ if (mountedRef.current) setOutcome(result);
2132
+ return result;
2133
+ } finally {
2134
+ if (mountedRef.current) setLoading(false);
2135
+ }
2136
+ },
2137
+ [mero, mountedRef]
2138
+ );
2139
+ return { redeem, outcome, loading };
2098
2140
  }
2099
2141
  function useCreateGroupInNamespace() {
2100
2142
  const { mero } = useMero();
@@ -3027,6 +3069,7 @@ exports.useNamespaceIdentity = useNamespaceIdentity;
3027
3069
  exports.useNamespaces = useNamespaces;
3028
3070
  exports.useNamespacesForApplication = useNamespacesForApplication;
3029
3071
  exports.useNodeIdentity = useNodeIdentity;
3072
+ exports.useRedeemInvitation = useRedeemInvitation;
3030
3073
  exports.useRemoveGroupMembers = useRemoveGroupMembers;
3031
3074
  exports.useReparentGroup = useReparentGroup;
3032
3075
  exports.useResyncContext = useResyncContext;