@techstuff-dev/foundation-api-utils 1.50.3 → 1.52.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.
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var toolkit = require('@reduxjs/toolkit');
4
- var slice = require('./slice-Cy4kZKn1.js');
4
+ var slice = require('./slice-Jly4VFS9.js');
5
5
  var slice$1 = require('./slice-CkWobkWw.js');
6
6
 
7
7
  // This file exists just so TypeScript has a module at ./store for IDEs/build.
@@ -11,4 +11,4 @@ var slice$1 = require('./slice-CkWobkWw.js');
11
11
  const rootReducer = toolkit.combineSlices(slice.cartSlice, slice$1.authSlice, slice.authApi, slice.contentApi, slice.paymentApi, slice.ordersApi);
12
12
 
13
13
  exports.rootReducer = rootReducer;
14
- //# sourceMappingURL=shared-9ChxyDgv.js.map
14
+ //# sourceMappingURL=shared-DjCjH7LG.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"shared-9ChxyDgv.js","sources":["../../../lib/store/shared.ts"],"sourcesContent":[null],"names":["combineSlices","cartSlice","authSlice","authApi","contentApi","paymentApi","ordersApi"],"mappings":";;;;;;AAAA;AACA;AASA;AAEA;AACO,MAAM,WAAW,GAAGA,qBAAa,CACtCC,eAAS,EACTC,iBAAS,EACTC,aAAO,EACPC,gBAAU,EACVC,gBAAU,EACVC,eAAS;;;;"}
1
+ {"version":3,"file":"shared-DjCjH7LG.js","sources":["../../../lib/store/shared.ts"],"sourcesContent":[null],"names":["combineSlices","cartSlice","authSlice","authApi","contentApi","paymentApi","ordersApi"],"mappings":";;;;;;AAAA;AACA;AASA;AAEA;AACO,MAAM,WAAW,GAAGA,qBAAa,CACtCC,eAAS,EACTC,iBAAS,EACTC,aAAO,EACPC,gBAAU,EACVC,gBAAU,EACVC,eAAS;;;;"}
@@ -4684,6 +4684,75 @@ Hook ${hookName} was either not provided or not a function.`);
4684
4684
  // src/query/react/index.ts
4685
4685
  var createApi = /* @__PURE__ */ buildCreateApi(coreModule(), reactHooksModule());
4686
4686
 
4687
+ /**
4688
+ * Creates a base query that automatically unwraps standardized API responses.
4689
+ *
4690
+ * If the API returns `{ success: true, data: T }`, this will return just `T`.
4691
+ * If the API returns `{ success: false, error: {...} }`, this will pass through the error correctly.
4692
+ *
4693
+ * This eliminates the need for `transformResponse` on every endpoint when using
4694
+ * a standardized response format across your API routes.
4695
+ *
4696
+ * @param baseQueryOptions - The same options you would pass to fetchBaseQuery
4697
+ * @returns A BaseQueryFn that unwraps standardized responses
4698
+ *
4699
+ * @example
4700
+ * ```typescript
4701
+ * const api = createApi({
4702
+ * baseQuery: createUnwrappingBaseQuery({
4703
+ * baseUrl: '/api',
4704
+ * prepareHeaders: (headers) => {
4705
+ * // Add auth headers
4706
+ * return headers;
4707
+ * },
4708
+ * }),
4709
+ * endpoints: (builder) => ({
4710
+ * getPlans: builder.query<{ plans: Plan[] }, void>({
4711
+ * query: () => '/payment/plans',
4712
+ * // No need for transformResponse!
4713
+ * }),
4714
+ * }),
4715
+ * });
4716
+ * ```
4717
+ */
4718
+ function createUnwrappingBaseQuery(baseQueryOptions) {
4719
+ const baseQuery = fetchBaseQuery(baseQueryOptions);
4720
+ return async (args, api, extraOptions) => {
4721
+ const result = await baseQuery(args, api, extraOptions);
4722
+ // If there's an error, return it as-is
4723
+ if (result.error) {
4724
+ return result;
4725
+ }
4726
+ // If there's data, check if it matches our standard format
4727
+ if (result.data) {
4728
+ const response = result.data;
4729
+ // Check if this is a wrapped response with success flag
4730
+ if (typeof response === 'object' && response !== null && 'success' in response) {
4731
+ // If success is false, treat it as an error
4732
+ if (response.success === false && response.error) {
4733
+ return {
4734
+ error: {
4735
+ status: 'CUSTOM_ERROR',
4736
+ data: response.error,
4737
+ },
4738
+ };
4739
+ }
4740
+ // If success is true and there's data, unwrap it
4741
+ if (response.success === true && 'data' in response) {
4742
+ return {
4743
+ ...result,
4744
+ data: response.data,
4745
+ };
4746
+ }
4747
+ }
4748
+ // If it doesn't match our standard format, return as-is
4749
+ // (e.g., direct responses from external APIs like Elasticsearch)
4750
+ return result;
4751
+ }
4752
+ return result;
4753
+ };
4754
+ }
4755
+
4687
4756
  function onlyUnique(value, index, self) {
4688
4757
  return self.indexOf(value) === index;
4689
4758
  }
@@ -5395,7 +5464,8 @@ const runOnPlatform = (config) => {
5395
5464
  */
5396
5465
  // Helper function to get environment variable with fallback
5397
5466
  const getEnvVar$1 = (key, fallback = '') => {
5398
- return process.env[key] || fallback;
5467
+ const value = process.env[key] || fallback;
5468
+ return value;
5399
5469
  };
5400
5470
  // Core API endpoints
5401
5471
  const API_PREFIX$1 = getEnvVar$1('NEXT_PUBLIC_API_PREFIX');
@@ -5554,30 +5624,38 @@ apiConfig.APP_ES_CHALLENGES_INDEX;
5554
5624
  apiConfig.APP_ES_CHALLENGE_DAYS_INDEX;
5555
5625
  apiConfig.PLATFORM;
5556
5626
 
5557
- const authDataBaseQuery = fetchBaseQuery({
5558
- baseUrl: API_AUTH_PREFIX,
5559
- prepareHeaders: async (headers) => {
5560
- headers.set('Content-Type', 'application/json');
5561
- try {
5562
- // add accessToken to headers from slice using selectAccessToken selector
5563
- const session = await awsAmplify.Auth.currentSession();
5564
- const idToken = session.getIdToken().getJwtToken(); // ID token
5565
- const accessToken = session.getAccessToken().getJwtToken(); // Access token
5566
- if (accessToken && idToken) {
5567
- headers.set('accesstoken', accessToken);
5568
- headers.set('idtoken', idToken);
5569
- // headers.set('refreshtoken', tokens.refreshToken);
5627
+ // Create a dynamic baseQuery that resolves URL at request time and unwraps standard responses
5628
+ const createAuthBaseQuery = () => {
5629
+ const baseUrl = typeof process !== 'undefined' && process.env?.NEXT_PUBLIC_API_AUTH_PREFIX
5630
+ ? process.env.NEXT_PUBLIC_API_AUTH_PREFIX
5631
+ : (API_AUTH_PREFIX || '');
5632
+ console.log('[authApi] Resolved baseUrl:', baseUrl);
5633
+ return createUnwrappingBaseQuery({
5634
+ baseUrl,
5635
+ prepareHeaders: async (headers) => {
5636
+ headers.set('Content-Type', 'application/json');
5637
+ try {
5638
+ // add accessToken to headers from slice using selectAccessToken selector
5639
+ const session = await awsAmplify.Auth.currentSession();
5640
+ const idToken = session.getIdToken().getJwtToken(); // ID token
5641
+ const accessToken = session.getAccessToken().getJwtToken(); // Access token
5642
+ if (accessToken && idToken) {
5643
+ headers.set('accesstoken', accessToken);
5644
+ headers.set('idtoken', idToken);
5645
+ // headers.set('refreshtoken', tokens.refreshToken);
5646
+ }
5647
+ return headers;
5570
5648
  }
5571
- return headers;
5572
- }
5573
- catch (error) {
5574
- // eslint-disable-next-line no-console
5575
- console.error('authDataBaseQuery: ', error);
5576
- return headers;
5577
- }
5578
- },
5579
- credentials: 'include',
5580
- });
5649
+ catch (error) {
5650
+ // eslint-disable-next-line no-console
5651
+ console.error('authDataBaseQuery: ', error);
5652
+ return headers;
5653
+ }
5654
+ },
5655
+ credentials: 'include',
5656
+ });
5657
+ };
5658
+ const authDataBaseQuery = createAuthBaseQuery();
5581
5659
  /**
5582
5660
  * This function is used to retry a request if we get a 401 error.
5583
5661
  */
@@ -5703,9 +5781,10 @@ const authApi = createApi({
5703
5781
  };
5704
5782
  }
5705
5783
  },
5784
+ // No need for transformResponse - createUnwrappingBaseQuery handles it
5706
5785
  transformResponse: (rawResult) => {
5707
- const { data } = rawResult;
5708
- return { route: 'success', message: data?.message };
5786
+ // Additional custom transform after unwrapping
5787
+ return { route: 'success', message: rawResult?.message };
5709
5788
  },
5710
5789
  }),
5711
5790
  verifyUserAttributes: builder.query({
@@ -5758,15 +5837,25 @@ const {
5758
5837
  useResetPasswordMutation, // Use this for mobile app.
5759
5838
  useResetPasswordAuthMutation, useRegisterMutation, useVerifyUserQuery, useLazyVerifyUserQuery, useGetUserInfoQuery, useLazyGetUserInfoQuery, useUpdateUserInfoMutation, useForgottenPasswordMutation, useVerifyUserAttributesQuery, useLazyVerifyUserAttributesQuery, useVerifyUserResendQuery, useLazyVerifyUserResendQuery, useUpdateUserMutation, } = authApi;
5760
5839
 
5761
- const contentApi = createApi({
5762
- reducerPath: 'contentApi',
5763
- baseQuery: fetchBaseQuery({
5764
- baseUrl: APP_ES_INSTANCE,
5840
+ // Create dynamic baseQuery that resolves URL at request time
5841
+ const createContentBaseQuery = () => {
5842
+ const baseUrl = typeof process !== 'undefined' && process.env?.NEXT_PUBLIC_ELASTICSEARCH_HOST
5843
+ ? process.env.NEXT_PUBLIC_ELASTICSEARCH_HOST
5844
+ : (APP_ES_INSTANCE || '');
5845
+ const auth = typeof process !== 'undefined' && process.env?.NEXT_PUBLIC_APP_ES_AUTH
5846
+ ? process.env.NEXT_PUBLIC_APP_ES_AUTH
5847
+ : (APP_ES_AUTH || '');
5848
+ return fetchBaseQuery({
5849
+ baseUrl,
5765
5850
  prepareHeaders: (headers) => {
5766
- headers.set('Authorization', APP_ES_AUTH);
5851
+ headers.set('Authorization', auth);
5767
5852
  return headers;
5768
5853
  },
5769
- }),
5854
+ });
5855
+ };
5856
+ const contentApi = createApi({
5857
+ reducerPath: 'contentApi',
5858
+ baseQuery: createContentBaseQuery(),
5770
5859
  tagTypes: ['Data', 'Workout'],
5771
5860
  // keepUnusedDataFor: 300,
5772
5861
  endpoints: (builder) => ({
@@ -5807,10 +5896,14 @@ const contentApi = createApi({
5807
5896
  });
5808
5897
  const { useGetDataQuery, useLazyGetDataQuery, useGetDataByIdQuery, useLazyGetDataByIdQuery, } = contentApi;
5809
5898
 
5810
- const paymentApi = createApi({
5811
- reducerPath: 'paymentApi',
5812
- baseQuery: fetchBaseQuery({
5813
- baseUrl: API_PAYMENTS_PREFIX,
5899
+ // Dynamic baseQuery that resolves URL at request time and unwraps standard responses
5900
+ const dynamicBaseQuery = async (args, api, extraOptions) => {
5901
+ // Resolve base URL at request time, not module load time
5902
+ const baseUrl = typeof process !== 'undefined' && process.env?.NEXT_PUBLIC_API_PAYMENTS_PREFIX
5903
+ ? process.env.NEXT_PUBLIC_API_PAYMENTS_PREFIX
5904
+ : (API_PAYMENTS_PREFIX || '');
5905
+ const baseQuery = createUnwrappingBaseQuery({
5906
+ baseUrl,
5814
5907
  prepareHeaders: async (headers) => {
5815
5908
  headers.set('Content-Type', 'application/json');
5816
5909
  // add accessToken to headers from slice using selectAccessToken selector
@@ -5825,7 +5918,12 @@ const paymentApi = createApi({
5825
5918
  return headers;
5826
5919
  },
5827
5920
  credentials: 'include',
5828
- }),
5921
+ });
5922
+ return baseQuery(args, api, extraOptions);
5923
+ };
5924
+ const paymentApi = createApi({
5925
+ reducerPath: 'paymentApi',
5926
+ baseQuery: dynamicBaseQuery,
5829
5927
  tagTypes: ['UserSubscription', 'Plans', 'TaxRates', 'PromoCodes'],
5830
5928
  // keepUnusedDataFor: 300,
5831
5929
  endpoints: (builder) => ({
@@ -5867,23 +5965,30 @@ const paymentApi = createApi({
5867
5965
  // Export hooks for usage in functional components.
5868
5966
  const { useCheckUserSubscriptionQuery, useLazyCheckUserSubscriptionQuery, useGetPaymentPlansQuery, useLazyGetPaymentPlansQuery, useGetTaxRatesQuery, useLazyGetTaxRatesQuery, useCheckPromoCodeQuery, useLazyCheckPromoCodeQuery, } = paymentApi;
5869
5967
 
5870
- const dataBaseQuery = fetchBaseQuery({
5871
- baseUrl: API_ORDERS_PREFIX,
5872
- prepareHeaders: async (headers) => {
5873
- headers.set('Content-Type', 'application/json');
5874
- // add accessToken to headers from slice using selectAccessToken selector
5875
- const session = await awsAmplify.Auth.currentSession();
5876
- const idToken = session.getIdToken().getJwtToken(); // ID token
5877
- const accessToken = session.getAccessToken().getJwtToken(); // Access token
5878
- if (accessToken && idToken) {
5879
- headers.set('accesstoken', accessToken);
5880
- headers.set('idtoken', idToken);
5881
- // headers.set('refreshtoken', tokens.refreshToken);
5882
- }
5883
- return headers;
5884
- },
5885
- credentials: 'include',
5886
- });
5968
+ // Create dynamic baseQuery that resolves URL at request time
5969
+ const createOrdersBaseQuery = () => {
5970
+ const baseUrl = typeof process !== 'undefined' && process.env?.NEXT_PUBLIC_API_ORDERS_PREFIX
5971
+ ? process.env.NEXT_PUBLIC_API_ORDERS_PREFIX
5972
+ : (API_ORDERS_PREFIX || '');
5973
+ return fetchBaseQuery({
5974
+ baseUrl,
5975
+ prepareHeaders: async (headers) => {
5976
+ headers.set('Content-Type', 'application/json');
5977
+ // add accessToken to headers from slice using selectAccessToken selector
5978
+ const session = await awsAmplify.Auth.currentSession();
5979
+ const idToken = session.getIdToken().getJwtToken(); // ID token
5980
+ const accessToken = session.getAccessToken().getJwtToken(); // Access token
5981
+ if (accessToken && idToken) {
5982
+ headers.set('accesstoken', accessToken);
5983
+ headers.set('idtoken', idToken);
5984
+ // headers.set('refreshtoken', tokens.refreshToken);
5985
+ }
5986
+ return headers;
5987
+ },
5988
+ credentials: 'include',
5989
+ });
5990
+ };
5991
+ const dataBaseQuery = createOrdersBaseQuery();
5887
5992
  /**
5888
5993
  * This function is used to retry a request if we get a 401 error.
5889
5994
  */
@@ -6091,6 +6196,7 @@ exports.applyTax = applyTax;
6091
6196
  exports.authApi = authApi;
6092
6197
  exports.cartSlice = cartSlice;
6093
6198
  exports.contentApi = contentApi;
6199
+ exports.createUnwrappingBaseQuery = createUnwrappingBaseQuery;
6094
6200
  exports.emptyCart = emptyCart;
6095
6201
  exports.formatAuthSession = formatAuthSession;
6096
6202
  exports.formatChallengeDays = formatChallengeDays;
@@ -6157,4 +6263,4 @@ exports.useUpdateUserMutation = useUpdateUserMutation;
6157
6263
  exports.useVerifyUserAttributesQuery = useVerifyUserAttributesQuery;
6158
6264
  exports.useVerifyUserQuery = useVerifyUserQuery;
6159
6265
  exports.useVerifyUserResendQuery = useVerifyUserResendQuery;
6160
- //# sourceMappingURL=slice-Cy4kZKn1.js.map
6266
+ //# sourceMappingURL=slice-Jly4VFS9.js.map