@iblai/web-utils 1.6.2 → 1.6.4

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.js CHANGED
@@ -3093,7 +3093,10 @@ function useAuthProvider({ middleware = new Map(), onAuthSuccess, onAuthFailure,
3093
3093
  if (isProtectedRoute && storageService) {
3094
3094
  const dmTokenInLocalStorage = await storageService.getItem(LOCAL_STORAGE_KEYS.DM_TOKEN_KEY);
3095
3095
  console.log("[auth-redirect] local storage keys ", LOCAL_STORAGE_KEYS);
3096
+ console.log("[auth-redirect] local storage ", JSON.stringify(localStorage));
3097
+ console.log("[auth-redirect] dmTokenInLocalStorage ", dmTokenInLocalStorage);
3096
3098
  const dmTokenExpired = await isDmTokenExpired(storageService);
3099
+ console.log("[auth-redirect] dmTokenExpired ", dmTokenExpired);
3097
3100
  if (!dmTokenInLocalStorage || dmTokenExpired) {
3098
3101
  if (!!dmTokenInLocalStorage) {
3099
3102
  console.log("[auth-redirect] DM token doesn't exists");
@@ -13744,44 +13747,71 @@ const getHeaders = async (service) => {
13744
13747
  }
13745
13748
  return token ? { Authorization: `${prefix} ${token}` } : undefined;
13746
13749
  };
13750
+ /** Status codes that should not be retried for queryFn endpoints. */
13751
+ const QUERYFN_NON_RETRYABLE_STATUSES = [401, 403, 500];
13752
+ /** Default max retries for queryFn endpoints. */
13753
+ const QUERYFN_MAX_RETRIES = 5;
13754
+ /** Base delay in ms for exponential backoff. */
13755
+ const QUERYFN_BASE_DELAY_MS = 1000;
13747
13756
  /**
13748
13757
  * Build a generic RTK Query endpoint from a service function.
13758
+ * Includes built-in retry with exponential backoff (since RTK Query
13759
+ * skips baseQuery retry for queryFn endpoints).
13749
13760
  */
13750
- const buildEndpointFromService = (service, serviceFn) => {
13761
+ const buildEndpointFromService = (service, serviceFn, options) => {
13762
+ var _a;
13763
+ const maxRetries = (_a = void 0 ) !== null && _a !== void 0 ? _a : QUERYFN_MAX_RETRIES;
13751
13764
  return {
13752
13765
  async queryFn(args) {
13753
- try {
13754
- iblaiApi.OpenAPI.BASE = getServiceUrl(service);
13755
- iblaiApi.OpenAPI.HEADERS = await getHeaders(service);
13756
- iblaiApi.OpenAPI.CREDENTIALS = service === SERVICES.LEGACY_LMS ? 'include' : 'omit';
13757
- // API request initiated
13758
- const data = await serviceFn(args);
13759
- // API response received
13760
- return { data };
13761
- }
13762
- catch (err) {
13763
- console.error('[data-layer] API error:', JSON.stringify(err, Object.getOwnPropertyNames(err)));
13764
- if (Object.prototype.hasOwnProperty.call(Config.httpErrorHandlers, err === null || err === void 0 ? void 0 : err.status)) {
13765
- Config.httpErrorHandlers[err === null || err === void 0 ? void 0 : err.status]({ ...((err === null || err === void 0 ? void 0 : err.data) || {}) });
13766
+ let lastError;
13767
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
13768
+ try {
13769
+ iblaiApi.OpenAPI.BASE = getServiceUrl(service);
13770
+ iblaiApi.OpenAPI.HEADERS = await getHeaders(service);
13771
+ iblaiApi.OpenAPI.CREDENTIALS = service === SERVICES.LEGACY_LMS ? 'include' : 'omit';
13772
+ const data = await serviceFn(args);
13773
+ return { data };
13766
13774
  }
13767
- return {
13768
- error: {
13769
- status: (err === null || err === void 0 ? void 0 : err.status) || 500,
13770
- data: (err === null || err === void 0 ? void 0 : err.body) || (err === null || err === void 0 ? void 0 : err.data) || (err === null || err === void 0 ? void 0 : err.message) || 'Unknown error',
13771
- },
13772
- };
13775
+ catch (err) {
13776
+ lastError = err;
13777
+ const status = err === null || err === void 0 ? void 0 : err.status;
13778
+ // Don't retry auth errors
13779
+ if (QUERYFN_NON_RETRYABLE_STATUSES.includes(status)) {
13780
+ break;
13781
+ }
13782
+ // If we have retries left, wait with exponential backoff
13783
+ if (attempt < maxRetries) {
13784
+ const delay = QUERYFN_BASE_DELAY_MS * Math.pow(2, attempt);
13785
+ console.warn(`[data-layer] Retry ${attempt + 1}/${maxRetries}:`, `service=${service}, fn=${serviceFn.name || 'anonymous'}`);
13786
+ await new Promise((resolve) => setTimeout(resolve, delay));
13787
+ continue;
13788
+ }
13789
+ }
13790
+ }
13791
+ // All retries exhausted or non-retryable error
13792
+ console.error('[data-layer] API error:', `service=${service}, fn=${serviceFn.name || 'anonymous'}`, JSON.stringify(lastError, Object.getOwnPropertyNames(lastError)));
13793
+ if (Object.prototype.hasOwnProperty.call(Config.httpErrorHandlers, lastError === null || lastError === void 0 ? void 0 : lastError.status)) {
13794
+ Config.httpErrorHandlers[lastError === null || lastError === void 0 ? void 0 : lastError.status]({
13795
+ ...((lastError === null || lastError === void 0 ? void 0 : lastError.data) || {}),
13796
+ });
13773
13797
  }
13798
+ return {
13799
+ error: {
13800
+ status: (lastError === null || lastError === void 0 ? void 0 : lastError.status) || 500,
13801
+ data: (lastError === null || lastError === void 0 ? void 0 : lastError.body) || (lastError === null || lastError === void 0 ? void 0 : lastError.data) || (lastError === null || lastError === void 0 ? void 0 : lastError.message) || 'Unknown error',
13802
+ },
13803
+ };
13774
13804
  },
13775
13805
  };
13776
13806
  };
13777
13807
  /**
13778
13808
  * Shortcut for building an endpoint using the DM service.
13779
13809
  */
13780
- const buildEndpointFromDmService = (serviceFn) => buildEndpointFromService(SERVICES.DM, serviceFn);
13810
+ const buildEndpointFromDmService = (serviceFn, options) => buildEndpointFromService(SERVICES.DM, serviceFn);
13781
13811
  /**
13782
13812
  * Shortcut for building an endpoint using the AXD service.
13783
13813
  */
13784
- const buildEndpointFromAxdService = (serviceFn) => buildEndpointFromService(SERVICES.AXD, serviceFn);
13814
+ const buildEndpointFromAxdService = (serviceFn, options) => buildEndpointFromService(SERVICES.AXD, serviceFn);
13785
13815
  const isErrorObject = (data) => {
13786
13816
  return (typeof data === 'object' &&
13787
13817
  data !== null &&
@@ -13913,40 +13943,57 @@ const iblFetchBaseQuery = async (args, api, extraOptions) => {
13913
13943
  }
13914
13944
  };
13915
13945
  /**
13916
- * Build a generic RTK Query endpoint from a service function.
13946
+ * Build a generic RTK Query endpoint from a service function (legacy variant).
13947
+ * Includes built-in retry with exponential backoff.
13917
13948
  */
13918
- const buildEndpointFromServiceLegacy = (service, serviceFn) => {
13949
+ const buildEndpointFromServiceLegacy = (service, serviceFn, options) => {
13950
+ var _a;
13951
+ const maxRetries = (_a = void 0 ) !== null && _a !== void 0 ? _a : QUERYFN_MAX_RETRIES;
13919
13952
  return {
13920
13953
  async queryFn(args) {
13921
- try {
13922
- iblaiApi.OpenAPI.BASE = getServiceUrl(service);
13923
- iblaiApi.OpenAPI.HEADERS = await getHeaders(service);
13924
- iblaiApi.OpenAPI.CREDENTIALS = service === SERVICES.LEGACY_LMS ? 'include' : 'omit';
13925
- const data = await serviceFn(...args);
13926
- return { data };
13927
- }
13928
- catch (err) {
13929
- if (Object.prototype.hasOwnProperty.call(Config.httpErrorHandlers, err === null || err === void 0 ? void 0 : err.status)) {
13930
- Config.httpErrorHandlers[err === null || err === void 0 ? void 0 : err.status]({
13931
- status: err === null || err === void 0 ? void 0 : err.status,
13932
- body: err === null || err === void 0 ? void 0 : err.body,
13933
- message: err === null || err === void 0 ? void 0 : err.message,
13934
- });
13954
+ let lastError;
13955
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
13956
+ try {
13957
+ iblaiApi.OpenAPI.BASE = getServiceUrl(service);
13958
+ iblaiApi.OpenAPI.HEADERS = await getHeaders(service);
13959
+ iblaiApi.OpenAPI.CREDENTIALS = service === SERVICES.LEGACY_LMS ? 'include' : 'omit';
13960
+ const data = await serviceFn(...args);
13961
+ return { data };
13962
+ }
13963
+ catch (err) {
13964
+ lastError = err;
13965
+ const status = err === null || err === void 0 ? void 0 : err.status;
13966
+ if (QUERYFN_NON_RETRYABLE_STATUSES.includes(status)) {
13967
+ break;
13968
+ }
13969
+ if (attempt < maxRetries) {
13970
+ const delay = QUERYFN_BASE_DELAY_MS * Math.pow(2, attempt);
13971
+ console.warn(`[data-layer] Retry ${attempt + 1}/${maxRetries}:`, `service=${service}, fn=${serviceFn.name || 'anonymous'}`);
13972
+ await new Promise((resolve) => setTimeout(resolve, delay));
13973
+ continue;
13974
+ }
13935
13975
  }
13936
- return {
13937
- error: {
13938
- status: (err === null || err === void 0 ? void 0 : err.status) || 500,
13939
- data: (err === null || err === void 0 ? void 0 : err.body) || (err === null || err === void 0 ? void 0 : err.message) || 'Unknown error',
13940
- },
13941
- };
13942
13976
  }
13977
+ if (Object.prototype.hasOwnProperty.call(Config.httpErrorHandlers, lastError === null || lastError === void 0 ? void 0 : lastError.status)) {
13978
+ Config.httpErrorHandlers[lastError === null || lastError === void 0 ? void 0 : lastError.status]({
13979
+ status: lastError === null || lastError === void 0 ? void 0 : lastError.status,
13980
+ body: lastError === null || lastError === void 0 ? void 0 : lastError.body,
13981
+ message: lastError === null || lastError === void 0 ? void 0 : lastError.message,
13982
+ });
13983
+ }
13984
+ return {
13985
+ error: {
13986
+ status: (lastError === null || lastError === void 0 ? void 0 : lastError.status) || 500,
13987
+ data: (lastError === null || lastError === void 0 ? void 0 : lastError.body) || (lastError === null || lastError === void 0 ? void 0 : lastError.message) || 'Unknown error',
13988
+ },
13989
+ };
13943
13990
  },
13944
13991
  };
13945
13992
  };
13946
13993
  /**
13947
13994
  * Shortcut for building an endpoint using the DM service.
13948
13995
  */
13949
- const buildEndpointFromDmServiceLegacy = (serviceFn) => buildEndpointFromServiceLegacy(SERVICES.DM, serviceFn);
13996
+ const buildEndpointFromDmServiceLegacy = (serviceFn, options) => buildEndpointFromServiceLegacy(SERVICES.DM, serviceFn);
13950
13997
 
13951
13998
  createApi({
13952
13999
  reducerPath: 'apiKeysApiSlice',