@rdlabo/ionic-angular-kit 21.5.17 → 21.5.19

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.
@@ -1739,6 +1739,12 @@ function isRemoteAccessActivation(value) {
1739
1739
  * `remote` access requirement is deferred until that exchange succeeds.
1740
1740
  */
1741
1741
  const KIT_AUTH_BOOTSTRAP_REQUEST = new HttpContextToken(() => false);
1742
+ /**
1743
+ * Marks an auth-recovery replay so {@link KitHttpConfig.recoverAuthAccess} is not invoked again.
1744
+ *
1745
+ * @internal
1746
+ */
1747
+ const KIT_AUTH_RECOVERY_REPLAY = new HttpContextToken(() => false);
1742
1748
  /**
1743
1749
  * HTTP methods that are safe to retry automatically.
1744
1750
  *
@@ -1851,6 +1857,45 @@ const provideKitHttp = (configFactory) => makeEnvironmentProviders([{ provide: K
1851
1857
  * @internal
1852
1858
  */
1853
1859
  const shouldRevokeAuthAccess = (config, req, error) => config.isAuthAccessDenial?.(req, error) ?? isExplicitAuthDenial(error);
1860
+ /**
1861
+ * Invoke {@link KitHttpConfig.recoverAuthAccess} without letting hook failures replace the server error.
1862
+ *
1863
+ * @internal
1864
+ */
1865
+ const tryRecoverAuthAccess = async (config, request, error) => {
1866
+ if (!config.recoverAuthAccess) {
1867
+ return false;
1868
+ }
1869
+ try {
1870
+ return (await config.recoverAuthAccess(request, error)) === true;
1871
+ }
1872
+ catch {
1873
+ return false;
1874
+ }
1875
+ };
1876
+ /**
1877
+ * Final error handling after retries and optional auth recovery have been exhausted.
1878
+ *
1879
+ * @internal
1880
+ */
1881
+ const handleTransportError = (config, access, req, error, isAuthBootstrap) => {
1882
+ if (config.enforceAuthAccessMode && isExplicitAuthDenial(error)) {
1883
+ if (shouldRevokeAuthAccess(config, req, error)) {
1884
+ access.clear();
1885
+ config.onAuthAccessDenial?.(req, error);
1886
+ }
1887
+ dispatchError(config, req, error);
1888
+ return throwError(() => error);
1889
+ }
1890
+ if (!isAuthBootstrap) {
1891
+ const fallback = config.offlineFallback?.(req, error);
1892
+ if (fallback) {
1893
+ return fallback;
1894
+ }
1895
+ }
1896
+ dispatchError(config, req, error);
1897
+ return throwError(() => error);
1898
+ };
1854
1899
  /**
1855
1900
  * Classify a final (post-retry) error and invoke the matching {@link KitHttpConfig} hook.
1856
1901
  *
@@ -1903,7 +1948,9 @@ const dispatchError = (config, req, error) => {
1903
1948
  * `Idempotency-Key`), and the status is a {@link RETRYABLE_STATUSES | transient status}. The
1904
1949
  * backoff is `retryCount * 500ms` plus up to 250ms of jitter, or the server's `Retry-After`.
1905
1950
  * When the device is offline it stops retrying immediately.
1906
- * 5. On the final error, enforced 401/403 revokes shared access and is rejected before fallback.
1951
+ * 5. On the first explicit HTTP `401`/`403`, {@link KitHttpConfig.recoverAuthAccess} may replay the
1952
+ * request once with regenerated headers before denial hooks run; recovery replays never recurse.
1953
+ * 6. On the final error, enforced 401/403 revokes shared access and is rejected before fallback.
1907
1954
  * For every other error, `offlineFallback` is consulted first; otherwise the error is classified by
1908
1955
  * status (see {@link dispatchError}): `401`→`onUnauthorized`, `403`→`onForbidden`, `0`→
1909
1956
  * `onNetworkError` (when connected), `429`→`onRateLimited`, maintenance `503`→`onMaintenance`,
@@ -1941,16 +1988,16 @@ const kitAuthInterceptor = (request, next) => {
1941
1988
  }
1942
1989
  return throwError(() => error);
1943
1990
  }
1944
- return from(Promise.resolve(config.getAuthHeaders(request))).pipe(catchError((headerError) => {
1991
+ const sendAuthenticated = (outgoing, allowAuthRecovery) => from(Promise.resolve(config.getAuthHeaders(outgoing))).pipe(catchError((headerError) => {
1945
1992
  // getAuthHeaders failed → the request is never sent; classify it instead of failing silently.
1946
- if (config.enforceAuthAccessMode && isExplicitAuthDenial(headerError) && shouldRevokeAuthAccess(config, request, headerError)) {
1993
+ if (config.enforceAuthAccessMode && isExplicitAuthDenial(headerError) && shouldRevokeAuthAccess(config, outgoing, headerError)) {
1947
1994
  access.clear();
1948
- config.onAuthAccessDenial?.(request, headerError);
1995
+ config.onAuthAccessDenial?.(outgoing, headerError);
1949
1996
  }
1950
- config.onAuthError?.(request, headerError);
1997
+ config.onAuthError?.(outgoing, headerError);
1951
1998
  return throwError(() => headerError);
1952
1999
  }), mergeMap((authHeaders) => {
1953
- const req = request.clone({ setHeaders: { ...authHeaders, ...config.buildExtraHeaders?.(request) } });
2000
+ const req = outgoing.clone({ setHeaders: { ...authHeaders, ...config.buildExtraHeaders?.(outgoing) } });
1954
2001
  const retryable = RETRYABLE_METHODS.includes(req.method) || req.headers.has('Idempotency-Key');
1955
2002
  const base = next(req).pipe(timeout({
1956
2003
  each: DEFAULT_TIMEOUT_MS,
@@ -1984,24 +2031,22 @@ const kitAuthInterceptor = (request, next) => {
1984
2031
  config.onResponse?.(event);
1985
2032
  }
1986
2033
  }), catchError((error) => {
1987
- if (config.enforceAuthAccessMode && isExplicitAuthDenial(error)) {
1988
- if (shouldRevokeAuthAccess(config, req, error)) {
1989
- access.clear();
1990
- config.onAuthAccessDenial?.(req, error);
1991
- }
1992
- dispatchError(config, req, error);
1993
- return throwError(() => error);
1994
- }
1995
- if (!isAuthBootstrap) {
1996
- const fallback = config.offlineFallback?.(req, error);
1997
- if (fallback) {
1998
- return fallback;
1999
- }
2034
+ const mayRecover = allowAuthRecovery && isExplicitAuthDenial(error) && !outgoing.context.get(KIT_AUTH_RECOVERY_REPLAY) && config.recoverAuthAccess;
2035
+ if (mayRecover) {
2036
+ return from(tryRecoverAuthAccess(config, outgoing, error)).pipe(mergeMap((recovered) => {
2037
+ if (recovered) {
2038
+ const replay = outgoing.clone({
2039
+ context: outgoing.context.set(KIT_AUTH_RECOVERY_REPLAY, true),
2040
+ });
2041
+ return sendAuthenticated(replay, false);
2042
+ }
2043
+ return handleTransportError(config, access, req, error, isAuthBootstrap);
2044
+ }));
2000
2045
  }
2001
- dispatchError(config, req, error);
2002
- return throwError(() => error);
2046
+ return handleTransportError(config, access, req, error, isAuthBootstrap);
2003
2047
  }));
2004
2048
  }));
2049
+ return sendAuthenticated(request, true);
2005
2050
  };
2006
2051
 
2007
2052
  /**