wallaby-cash 3.6.0 → 3.7.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/dist/index.esm.js CHANGED
@@ -598,7 +598,7 @@ const env = {
598
598
  isNative: !isWeb,
599
599
  isWeb,
600
600
  };
601
- const currentPackageVersion = "3.6.0" ;
601
+ const currentPackageVersion = "3.7.0" ;
602
602
 
603
603
  /**
604
604
  * Storage keys adapter for Storage interface across different platforms.
@@ -7529,32 +7529,41 @@ class Base {
7529
7529
  return await originalRequest(params);
7530
7530
  }
7531
7531
  catch (error) {
7532
- // Reactive 401 handling for the generated client: refresh once and retry
7532
+ // Reactive 401 handling for the generated client: refresh once and retry.
7533
+ // The retry recurses and surfaces its own final error, so the intermediate
7534
+ // 401 must not be routed through toWallabyError (would double-fire onError).
7533
7535
  if (!retried && shouldAttemptTokenRefresh(error)) {
7534
7536
  await this._performTokenRefresh();
7535
7537
  // securityWorker will re-fetch the new access token on retry
7536
7538
  return attemptRequest(true);
7537
7539
  }
7538
- // Transform the generated API error format into WallabyError
7540
+ // Transform the generated API error format into WallabyError.
7541
+ // `error.error` is the parsed response body (no statusCode); the HTTP
7542
+ // status lives on the HttpResponse itself (`error.status`).
7539
7543
  if (error === null || error === void 0 ? void 0 : error.error) {
7540
7544
  const apiError = error.error;
7541
- throw new WallabyError({
7545
+ throw this.toWallabyError({
7542
7546
  message: apiError.message || 'API request failed',
7543
- statusCode: apiError.statusCode || 500,
7547
+ statusCode: apiError.statusCode || error.status || 500,
7544
7548
  errorCode: apiError.errorCode,
7545
7549
  data: apiError,
7546
7550
  });
7547
7551
  }
7548
- // Re-throw if already a WallabyError
7552
+ // Already a WallabyError (e.g. a pre-send guard from securityWorker) — propagate as-is.
7549
7553
  if (error instanceof WallabyError) {
7550
7554
  throw error;
7551
7555
  }
7552
- // Wrap any other errors
7553
- throw new WallabyError({
7554
- message: error.message || 'Unknown error occurred',
7555
- statusCode: error.statusCode || 500,
7556
- errorCode: error.errorCode,
7557
- data: error,
7556
+ // No HTTP response reached us: treat as a network failure, mirroring the
7557
+ // custom request() path so both transports surface the same shape.
7558
+ throw this.toWallabyError({
7559
+ message: error.message || 'Network request failed',
7560
+ statusCode: 0,
7561
+ errorCode: 'NETWORK_ERROR',
7562
+ data: {
7563
+ code: error.code || error.name,
7564
+ originalError: error,
7565
+ },
7566
+ cause: error.cause,
7558
7567
  });
7559
7568
  }
7560
7569
  };
@@ -7647,17 +7656,16 @@ class Base {
7647
7656
  * All concurrent callers share the same in-flight promise via the module-level
7648
7657
  * `refreshDedupeManager` so only one network request is issued per user/environment scope.
7649
7658
  */
7650
- async _performTokenRefresh() {
7659
+ async _performTokenRefresh(opts) {
7651
7660
  const refreshDedupeKey = `refresh:${this.userId}:${this.settings.environment}:${this.settings.network}`;
7652
7661
  await refreshDedupeManager.withDedupe(refreshDedupeKey, async () => {
7662
+ var _a;
7653
7663
  const refreshToken = await this.localStorageService.fetch(LocalStorageItems.RefreshToken);
7654
7664
  if (!refreshToken) {
7655
7665
  return;
7656
7666
  }
7657
7667
  try {
7658
- const response = await this.request(AuthRoutes.refreshAccessToken, {
7659
- payload: { refreshToken },
7660
- });
7668
+ const response = await this.request(AuthRoutes.refreshAccessToken, { payload: { refreshToken } }, false, (_a = opts === null || opts === void 0 ? void 0 : opts.silent) !== null && _a !== void 0 ? _a : false);
7661
7669
  await this.localStorageService.save(LocalStorageItems.AccessToken, response.accessToken);
7662
7670
  await this.localStorageService.save(LocalStorageItems.RefreshToken, response.refreshToken);
7663
7671
  }
@@ -7670,16 +7678,44 @@ class Base {
7670
7678
  }
7671
7679
  });
7672
7680
  }
7681
+ /**
7682
+ * Single chokepoint for surfacing transport failures. Every WallabyError raised
7683
+ * by a transport path is built here so `options.onError` fires exactly once per
7684
+ * logical failure. Retry paths must decide to retry BEFORE calling this so a
7685
+ * retried request does not double-fire.
7686
+ */
7687
+ toWallabyError(payload) {
7688
+ var _a, _b;
7689
+ const error = new WallabyError(payload);
7690
+ try {
7691
+ (_b = (_a = this.options) === null || _a === void 0 ? void 0 : _a.onError) === null || _b === void 0 ? void 0 : _b.call(_a, error);
7692
+ }
7693
+ catch (onErrorException) {
7694
+ // A throwing consumer callback must not replace the WallabyError being surfaced.
7695
+ logger.error('SDK::Base::toWallabyError::onError handler threw', { error: onErrorException });
7696
+ }
7697
+ return error;
7698
+ }
7673
7699
  /**
7674
7700
  * Main request method for making API calls to the Wallaby API.
7675
7701
  * @param route - Route configuration object containing the endpoint path and HTTP method
7676
7702
  * @param options - Optional request options including payload and query parameters
7677
7703
  * @param retried - Internal flag; set to true when retrying after a 401 to prevent loops
7704
+ * @param silent - Internal flag; when true the resulting WallabyError does NOT fire
7705
+ * `options.onError`. Used for best-effort refreshes whose failure is
7706
+ * swallowed by the caller (e.g. proactive refresh in securityWorker),
7707
+ * so an internal request never surfaces a consumer-facing error.
7678
7708
  * @returns Promise resolving to the typed response data
7679
7709
  * @throws Error if the API returns an error response or network failure occurs
7680
7710
  */
7681
- async request(route, options, retried = false) {
7711
+ async request(route, options, retried = false, silent = false) {
7682
7712
  var _a;
7713
+ // `silent` suppresses onError for swallowed internal refreshes. Note: refresh
7714
+ // requests dedupe by user/env/network, so under concurrent mixed-path refreshes
7715
+ // the winning call's `silent` value applies to all sharers — a narrow edge that
7716
+ // does not affect the retry no-double-fire guarantee. A fully race-free fix would
7717
+ // require idempotent (per-error) firing.
7718
+ const emitError = (payload) => silent ? new WallabyError(payload) : this.toWallabyError(payload);
7683
7719
  // Proactive token refresh: if access token is near expiry, refresh before making the request
7684
7720
  if (route !== AuthRoutes.refreshAccessToken && !retried) {
7685
7721
  const accessToken = await this.localStorageService.fetch(LocalStorageItems.AccessToken);
@@ -7730,7 +7766,16 @@ class Base {
7730
7766
  });
7731
7767
  // Handle error responses (status >= 400)
7732
7768
  if (!response.ok) {
7733
- throw new WallabyError({
7769
+ // Reactive token refresh: on a refreshable 401, refresh once and retry the
7770
+ // original request. Decided here (before constructing the WallabyError) so the
7771
+ // retried attempt surfaces the single onError; the intermediate 401 never fires.
7772
+ if (!retried &&
7773
+ route !== AuthRoutes.refreshAccessToken &&
7774
+ shouldAttemptTokenRefresh({ status: response.status, errorCode: data === null || data === void 0 ? void 0 : data.errorCode })) {
7775
+ await this._performTokenRefresh();
7776
+ return this.request(route, options, true);
7777
+ }
7778
+ throw emitError({
7734
7779
  message: (data === null || data === void 0 ? void 0 : data.message) || response.statusText || 'Request failed',
7735
7780
  statusCode: response.status,
7736
7781
  errorCode: data === null || data === void 0 ? void 0 : data.errorCode,
@@ -7740,36 +7785,33 @@ class Base {
7740
7785
  return data;
7741
7786
  }
7742
7787
  catch (error) {
7743
- // Reactive token refresh: on 401, refresh once and retry the original request
7744
- if (!retried && route !== AuthRoutes.refreshAccessToken && shouldAttemptTokenRefresh(error)) {
7745
- await this._performTokenRefresh();
7746
- return this.request(route, options, true);
7788
+ // Already surfaced through toWallabyError (the !response.ok branch above), or a
7789
+ // pre-send guard error from before the fetch — propagate without re-firing onError.
7790
+ if (error instanceof WallabyError) {
7791
+ throw error;
7747
7792
  }
7748
7793
  if (error === null || error === void 0 ? void 0 : error.error) {
7749
7794
  logger.error('SDK::Base::handleApiCall::API Error', { error });
7750
7795
  const apiError = error.error;
7751
- throw new WallabyError({
7796
+ throw emitError({
7752
7797
  message: apiError.message || 'API request failed',
7753
7798
  statusCode: apiError.statusCode || 500,
7754
7799
  errorCode: apiError.errorCode,
7755
7800
  data: apiError,
7756
7801
  });
7757
7802
  }
7758
- if (error instanceof WallabyError) {
7759
- throw error;
7760
- }
7761
7803
  // Handle network errors, fetch failures, etc.
7762
7804
  logger.error(`SDK::Base::${route.method}::NetworkError`, {
7763
7805
  message: error.message,
7764
7806
  name: error.name,
7765
7807
  error,
7766
7808
  });
7767
- throw new WallabyError({
7809
+ throw emitError({
7768
7810
  message: error.message || 'Network request failed',
7769
7811
  statusCode: 0,
7770
7812
  errorCode: 'NETWORK_ERROR',
7771
7813
  data: {
7772
- code: error.name,
7814
+ code: error.code || error.name,
7773
7815
  originalError: error,
7774
7816
  },
7775
7817
  cause: error.cause,
@@ -7797,11 +7839,13 @@ class Base {
7797
7839
  return headers;
7798
7840
  }
7799
7841
  async securityWorker(securityData) {
7800
- // Proactive token refresh before building headers for the generated client
7842
+ // Proactive token refresh before building headers for the generated client.
7843
+ // Best-effort and swallowed below, so it must NOT surface onError — otherwise a
7844
+ // failed internal refresh would fire even when the actual request later succeeds.
7801
7845
  try {
7802
7846
  const accessToken = await this.localStorageService.fetch(LocalStorageItems.AccessToken);
7803
7847
  if (accessToken && isJwtNearExpiry(accessToken, 60, this.clock.now)) {
7804
- await this._performTokenRefresh();
7848
+ await this._performTokenRefresh({ silent: true });
7805
7849
  }
7806
7850
  }
7807
7851
  catch (_a) {