@techstuff-dev/foundation-api-utils 1.27.0 → 1.27.1

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
@@ -171,6 +171,32 @@ var setCredentials = (_a$2 = authSlice.actions, _a$2.setCredentials), resetCreat
171
171
  var isAuthenticated = (_b$2 = authSlice.selectors, _b$2.isAuthenticated), selectAccessToken = _b$2.selectAccessToken, selectRefreshToken = _b$2.selectRefreshToken, selectCurrentUser = _b$2.selectCurrentUser, isVerifying = _b$2.isVerifying, getUserSession = _b$2.getUserSession, selectCurrentUserSub = _b$2.selectCurrentUserSub, selectCurrentUserRefreshToken = _b$2.selectCurrentUserRefreshToken, selectUserTokens = _b$2.selectUserTokens, selectUserSubscriptionStatus = _b$2.selectUserSubscriptionStatus, selectSubscription = _b$2.selectSubscription, selectSubscriptionPrice = _b$2.selectSubscriptionPrice;
172
172
  authSlice.reducer;
173
173
 
174
+ function applyCoupon(coupon, netTotal) {
175
+ var discountedAmount = netTotal * (Number(coupon.discount) / 100);
176
+ var discountedTotal = netTotal - discountedAmount;
177
+ var data = __assign(__assign({}, coupon), { discountedTotal: discountedTotal, discountedAmount: discountedAmount });
178
+ return data;
179
+ }
180
+ function applyTax(taxRate, netTotal) {
181
+ var taxAmount = netTotal * (taxRate.percentage / 100);
182
+ return taxAmount;
183
+ }
184
+ function toCamelCaseObject(obj) {
185
+ var toCamelCase = function (str) {
186
+ return str.replace(/([-_][a-z])/gi, function (match) {
187
+ return match.toUpperCase().replace(/[-_]/, '');
188
+ });
189
+ };
190
+ var camelCaseObject = {};
191
+ for (var key in obj) {
192
+ if (obj.hasOwnProperty(key)) {
193
+ var camelKey = toCamelCase(key);
194
+ camelCaseObject[camelKey] = obj[key];
195
+ }
196
+ }
197
+ return camelCaseObject;
198
+ }
199
+
174
200
  // TODO: typing.
175
201
  var formatUserPayload = function (payload) {
176
202
  var userEmail = payload.email, firstName = payload.given_name, lastName = payload.family_name, dateOfBirth = payload.birthdate, picture = payload.picture, uuid = payload["cognito:username"], termsAccepted = payload["custom:termsAccepted"], subscriptionId = payload["custom:subscriptionId"], contactPreference = payload["custom:contactPreferences"], customerId = payload["custom:customerId"];
@@ -591,7 +617,7 @@ function formatAuthSession(session) {
591
617
  };
592
618
  }
593
619
  function formatFedaratedSession(session) {
594
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4;
620
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5;
595
621
  return {
596
622
  accessToken: (_a = session === null || session === void 0 ? void 0 : session.accessToken) === null || _a === void 0 ? void 0 : _a.jwtToken,
597
623
  idToken: (_b = session === null || session === void 0 ? void 0 : session.idToken) === null || _b === void 0 ? void 0 : _b.jwtToken,
@@ -610,6 +636,7 @@ function formatFedaratedSession(session) {
610
636
  'custom:contactPreferences': (_0 = (_z = session === null || session === void 0 ? void 0 : session.idToken) === null || _z === void 0 ? void 0 : _z.payload) === null || _0 === void 0 ? void 0 : _0['custom:contactPreferences'],
611
637
  'custom:customerId': (_2 = (_1 = session === null || session === void 0 ? void 0 : session.idToken) === null || _1 === void 0 ? void 0 : _1.payload) === null || _2 === void 0 ? void 0 : _2['custom:customerId'],
612
638
  picture: (_4 = (_3 = session === null || session === void 0 ? void 0 : session.idToken) === null || _3 === void 0 ? void 0 : _3.payload) === null || _4 === void 0 ? void 0 : _4.picture,
639
+ id: toCamelCaseObject((_5 = session === null || session === void 0 ? void 0 : session.idToken) === null || _5 === void 0 ? void 0 : _5.payload),
613
640
  },
614
641
  };
615
642
  }
@@ -795,7 +822,7 @@ var formatMediaItem = function (data) {
795
822
  });
796
823
  };
797
824
 
798
- var _a$1, _b$1, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
825
+ var _a$1, _b$1, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
799
826
  // Render the store based on the environment.
800
827
  var isWeb = process.env.NEXT_PUBLIC_WEB_APP;
801
828
  var ENV = isWeb ? process.env : require('react-native-config').default;
@@ -805,23 +832,25 @@ var ENV = isWeb ? process.env : require('react-native-config').default;
805
832
  var API_AUTH_PREFIX = ((_b$1 = process.env) === null || _b$1 === void 0 ? void 0 : _b$1.NEXT_PUBLIC_API_AUTH_PREFIX) || ENV.API_AUTH_PREFIX;
806
833
  // Payments specific API
807
834
  var API_PAYMENTS_PREFIX = ((_c = process.env) === null || _c === void 0 ? void 0 : _c.NEXT_PUBLIC_API_PAYMENTS_PREFIX) || ENV.API_PAYMENTS_PREFIX;
808
- ((_d = process.env) === null || _d === void 0 ? void 0 : _d.NEXT_PUBLIC_APP_BASE_URL) || ENV.APP_BASE_URL;
809
- var APP_ES_INSTANCE = ((_e = process.env) === null || _e === void 0 ? void 0 : _e.NEXT_PUBLIC_APP_ES_INSTANCE) || ENV.APP_ES_INSTANCE;
810
- var APP_ES_AUTH = ((_f = process.env) === null || _f === void 0 ? void 0 : _f.NEXT_PUBLIC_APP_ES_AUTH) || ENV.APP_ES_AUTH;
811
- ((_g = process.env) === null || _g === void 0 ? void 0 : _g.NEXT_PUBLIC_APP_ES_PROMO_PANEL_INDEX) ||
835
+ // Orders specific API
836
+ var API_ORDERS_PREFIX = ((_d = process.env) === null || _d === void 0 ? void 0 : _d.NEXT_PUBLIC_API_ORDERS_PREFIX) || ENV.API_ORDERS_PREFIX;
837
+ ((_e = process.env) === null || _e === void 0 ? void 0 : _e.NEXT_PUBLIC_APP_BASE_URL) || ENV.APP_BASE_URL;
838
+ var APP_ES_INSTANCE = ((_f = process.env) === null || _f === void 0 ? void 0 : _f.NEXT_PUBLIC_APP_ES_INSTANCE) || ENV.APP_ES_INSTANCE;
839
+ var APP_ES_AUTH = ((_g = process.env) === null || _g === void 0 ? void 0 : _g.NEXT_PUBLIC_APP_ES_AUTH) || ENV.APP_ES_AUTH;
840
+ ((_h = process.env) === null || _h === void 0 ? void 0 : _h.NEXT_PUBLIC_APP_ES_PROMO_PANEL_INDEX) ||
812
841
  ENV.APP_ES_PROMO_PANEL_INDEX;
813
- ((_h = process.env) === null || _h === void 0 ? void 0 : _h.NEXT_PUBLIC_APP_ES_FAQ_INDEX) || ENV.APP_ES_FAQ_INDEX;
814
- ((_j = process.env) === null || _j === void 0 ? void 0 : _j.NEXT_PUBLIC_APP_ES_PAGES_INDEX) || ENV.APP_ES_PAGES_INDEX;
815
- ((_k = process.env) === null || _k === void 0 ? void 0 : _k.NEXT_PUBLIC_APP_ES_SETTINGS_INDEX) || ENV.APP_ES_SETTINGS_INDEX;
816
- ((_l = process.env) === null || _l === void 0 ? void 0 : _l.NEXT_PUBLIC_APP_ES_PRESS_INDEX) || ENV.APP_ES_PRESS_INDEX;
817
- ((_m = process.env) === null || _m === void 0 ? void 0 : _m.NEXT_PUBLIC_APP_ES_GUESTS_INDEX) || ENV.APP_ES_GUESTS_INDEX;
818
- ((_o = process.env) === null || _o === void 0 ? void 0 : _o.NEXT_PUBLIC_APP_ES_VIDEOS_INDEX) || ENV.APP_ES_VIDEOS_INDEX;
819
- ((_p = process.env) === null || _p === void 0 ? void 0 : _p.NEXT_PUBLIC_APP_ES_SECTIONS_INDEX) || ENV.APP_ES_SECTIONS_INDEX;
820
- ((_q = process.env) === null || _q === void 0 ? void 0 : _q.NEXT_PUBLIC_APP_ES_WORKOUTS_INDEX) || ENV.APP_ES_WORKOUTS_INDEX;
821
- ((_r = process.env) === null || _r === void 0 ? void 0 : _r.NEXT_PUBLIC_APP_ES_SCHEDULE_INDEX) || ENV.APP_ES_SCHEDULE_INDEX;
822
- ((_s = process.env) === null || _s === void 0 ? void 0 : _s.NEXT_PUBLIC_APP_ES_CHALLENGES_INDEX) ||
842
+ ((_j = process.env) === null || _j === void 0 ? void 0 : _j.NEXT_PUBLIC_APP_ES_FAQ_INDEX) || ENV.APP_ES_FAQ_INDEX;
843
+ ((_k = process.env) === null || _k === void 0 ? void 0 : _k.NEXT_PUBLIC_APP_ES_PAGES_INDEX) || ENV.APP_ES_PAGES_INDEX;
844
+ ((_l = process.env) === null || _l === void 0 ? void 0 : _l.NEXT_PUBLIC_APP_ES_SETTINGS_INDEX) || ENV.APP_ES_SETTINGS_INDEX;
845
+ ((_m = process.env) === null || _m === void 0 ? void 0 : _m.NEXT_PUBLIC_APP_ES_PRESS_INDEX) || ENV.APP_ES_PRESS_INDEX;
846
+ ((_o = process.env) === null || _o === void 0 ? void 0 : _o.NEXT_PUBLIC_APP_ES_GUESTS_INDEX) || ENV.APP_ES_GUESTS_INDEX;
847
+ ((_p = process.env) === null || _p === void 0 ? void 0 : _p.NEXT_PUBLIC_APP_ES_VIDEOS_INDEX) || ENV.APP_ES_VIDEOS_INDEX;
848
+ ((_q = process.env) === null || _q === void 0 ? void 0 : _q.NEXT_PUBLIC_APP_ES_SECTIONS_INDEX) || ENV.APP_ES_SECTIONS_INDEX;
849
+ ((_r = process.env) === null || _r === void 0 ? void 0 : _r.NEXT_PUBLIC_APP_ES_WORKOUTS_INDEX) || ENV.APP_ES_WORKOUTS_INDEX;
850
+ ((_s = process.env) === null || _s === void 0 ? void 0 : _s.NEXT_PUBLIC_APP_ES_SCHEDULE_INDEX) || ENV.APP_ES_SCHEDULE_INDEX;
851
+ ((_t = process.env) === null || _t === void 0 ? void 0 : _t.NEXT_PUBLIC_APP_ES_CHALLENGES_INDEX) ||
823
852
  ENV.APP_ES_CHALLENGES_INDEX;
824
- ((_t = process.env) === null || _t === void 0 ? void 0 : _t.NEXT_PUBLIC_APP_ES_CHALLENGE_DAYS_INDEX) ||
853
+ ((_u = process.env) === null || _u === void 0 ? void 0 : _u.NEXT_PUBLIC_APP_ES_CHALLENGE_DAYS_INDEX) ||
825
854
  ENV.APP_ES_CHALLENGE_DAYS_INDEX;
826
855
 
827
856
  var authDataBaseQuery = fetchBaseQuery({
@@ -1250,16 +1279,92 @@ var paymentApi = createApi({
1250
1279
  // Export hooks for usage in functional components.
1251
1280
  paymentApi.useCheckUserSubscriptionQuery; paymentApi.useLazyCheckUserSubscriptionQuery; paymentApi.useGetPaymentPlansQuery; paymentApi.useLazyGetPaymentPlansQuery; paymentApi.useGetTaxRatesQuery; paymentApi.useLazyGetTaxRatesQuery; paymentApi.useCheckPromoCodeQuery; paymentApi.useLazyCheckPromoCodeQuery;
1252
1281
 
1253
- function applyCoupon(coupon, netTotal) {
1254
- var discountedAmount = netTotal * (Number(coupon.discount) / 100);
1255
- var discountedTotal = netTotal - discountedAmount;
1256
- var data = __assign(__assign({}, coupon), { discountedTotal: discountedTotal, discountedAmount: discountedAmount });
1257
- return data;
1258
- }
1259
- function applyTax(taxRate, netTotal) {
1260
- var taxAmount = netTotal * (taxRate.percentage / 100);
1261
- return taxAmount;
1262
- }
1282
+ var dataBaseQuery = fetchBaseQuery({
1283
+ baseUrl: API_ORDERS_PREFIX,
1284
+ prepareHeaders: function (headers_1, _a) { return __awaiter(void 0, [headers_1, _a], void 0, function (headers, _b) {
1285
+ var session, idToken, accessToken;
1286
+ return __generator(this, function (_c) {
1287
+ switch (_c.label) {
1288
+ case 0:
1289
+ headers.set('Content-Type', 'application/json');
1290
+ return [4 /*yield*/, Auth.currentSession()];
1291
+ case 1:
1292
+ session = _c.sent();
1293
+ console.log('session', session);
1294
+ idToken = session.getIdToken().getJwtToken();
1295
+ accessToken = session.getAccessToken().getJwtToken();
1296
+ if (accessToken && idToken) {
1297
+ headers.set('accesstoken', accessToken);
1298
+ headers.set('idtoken', idToken);
1299
+ // headers.set('refreshtoken', tokens.refreshToken);
1300
+ }
1301
+ return [2 /*return*/, headers];
1302
+ }
1303
+ });
1304
+ }); },
1305
+ credentials: 'include',
1306
+ });
1307
+ /**
1308
+ * This function is used to retry a request if we get a 401 error.
1309
+ */
1310
+ var dataBaseQueryWithReauth = function (args, api, extraOptions) { return __awaiter(void 0, void 0, void 0, function () {
1311
+ var result, authSession, user;
1312
+ var _a;
1313
+ return __generator(this, function (_b) {
1314
+ switch (_b.label) {
1315
+ case 0: return [4 /*yield*/, dataBaseQuery(args, api, extraOptions)];
1316
+ case 1:
1317
+ result = _b.sent();
1318
+ console.log('result', result);
1319
+ if (!(((_a = result === null || result === void 0 ? void 0 : result.error) === null || _a === void 0 ? void 0 : _a.status) === 401)) return [3 /*break*/, 6];
1320
+ return [4 /*yield*/, Auth.currentAuthenticatedUser({
1321
+ bypassCache: true, // Ensures that the user is refreshed from the server
1322
+ })];
1323
+ case 2:
1324
+ authSession = _b.sent();
1325
+ user = formatAuthSession(authSession);
1326
+ api.dispatch(setCredentials(user));
1327
+ if (!(authSession === null || authSession === void 0 ? void 0 : authSession.tokens)) return [3 /*break*/, 4];
1328
+ return [4 /*yield*/, dataBaseQuery(args, api, extraOptions)];
1329
+ case 3:
1330
+ // If we get a new access token, retry the original request.
1331
+ result = _b.sent();
1332
+ return [3 /*break*/, 6];
1333
+ case 4:
1334
+ // TODO; require full data reset?
1335
+ return [4 /*yield*/, Auth.signOut()];
1336
+ case 5:
1337
+ // TODO; require full data reset?
1338
+ _b.sent();
1339
+ api.dispatch(logout());
1340
+ // If no access token throw error.
1341
+ throw new Error('No access token found');
1342
+ case 6: return [2 /*return*/, result];
1343
+ }
1344
+ });
1345
+ }); };
1346
+ var ordersApi = createApi({
1347
+ reducerPath: 'ordersApi',
1348
+ baseQuery: dataBaseQueryWithReauth,
1349
+ tagTypes: ['Orders'],
1350
+ // keepUnusedDataFor: 300,
1351
+ endpoints: function (builder) { return ({
1352
+ /**
1353
+ * USER API.
1354
+ */
1355
+ fetchOrders: builder.query({
1356
+ query: function (ids) { return ({
1357
+ url: '/order/search',
1358
+ method: 'POST',
1359
+ body: {
1360
+ ids: ids,
1361
+ },
1362
+ }); },
1363
+ }),
1364
+ }); },
1365
+ });
1366
+ // Export hooks for usage in functional components.
1367
+ ordersApi.useFetchOrdersQuery; ordersApi.useLazyFetchOrdersQuery;
1263
1368
 
1264
1369
  var _a, _b;
1265
1370
  var initialState = {
@@ -1411,7 +1516,7 @@ function createPersistStorage() {
1411
1516
  }
1412
1517
 
1413
1518
  // Infer the `RootState` type from the root reducer
1414
- var rootReducer = combineSlices(cartSlice, authSlice, authApi, contentApi, paymentApi);
1519
+ var rootReducer = combineSlices(cartSlice, authSlice, authApi, contentApi, paymentApi, ordersApi);
1415
1520
  // eg. ['auth', 'creating'], or an empty array if you don't want to whitelist any.
1416
1521
  // const storageWhiteList: string[] = [];
1417
1522
  var storageBlackList = [];
@@ -1421,7 +1526,7 @@ var storageBlackList = [];
1421
1526
  // are needed for each request to prevent cross-request state pollution.
1422
1527
  var makeNativeStore = function () {
1423
1528
  var storage = require('@react-native-async-storage/async-storage').default;
1424
- storageBlackList.push(paymentApi.reducerPath, authApi.reducerPath, contentApi.reducerPath);
1529
+ storageBlackList.push(paymentApi.reducerPath, authApi.reducerPath, contentApi.reducerPath, ordersApi.reducerPath);
1425
1530
  var persistConfig = {
1426
1531
  key: 'root',
1427
1532
  storage: storage,
@@ -1432,6 +1537,7 @@ var makeNativeStore = function () {
1432
1537
  authApi.middleware,
1433
1538
  contentApi.middleware,
1434
1539
  paymentApi.middleware,
1540
+ ordersApi.middleware,
1435
1541
  ];
1436
1542
  // `combineSlices` automatically combines the reducers using
1437
1543
  // their `reducerPath`s, therefore we no longer need to call `combineReducers`.
@@ -1469,7 +1575,7 @@ var makeNativeStore = function () {
1469
1575
  };
1470
1576
  var makeWebStore = function () {
1471
1577
  var storage = createPersistStorage();
1472
- storageBlackList.push(paymentApi.reducerPath, authApi.reducerPath, contentApi.reducerPath);
1578
+ storageBlackList.push(paymentApi.reducerPath, authApi.reducerPath, contentApi.reducerPath, ordersApi.reducerPath);
1473
1579
  var persistConfig = {
1474
1580
  key: 'root',
1475
1581
  storage: storage,
@@ -1480,6 +1586,7 @@ var makeWebStore = function () {
1480
1586
  authApi.middleware,
1481
1587
  contentApi.middleware,
1482
1588
  paymentApi.middleware,
1589
+ ordersApi.middleware,
1483
1590
  ];
1484
1591
  // `combineSlices` automatically combines the reducers using
1485
1592
  // their `reducerPath`s, therefore we no longer need to call `combineReducers`.
@@ -1506,5 +1613,5 @@ var makeWebStore = function () {
1506
1613
  var nativeStore = !isWeb && makeNativeStore();
1507
1614
  var webStore = isWeb && makeWebStore();
1508
1615
 
1509
- export { addToCart, authApi, authSlice, cartSlice, contentApi, emptyCart, formatAuthSession, formatChallengeDays, formatChallenges, formatConfig, formatFaqs, formatFedaratedSession, formatGuests, formatLongform, formatMediaItem, formatPages, formatPress, formatPromos, formatSchedule, formatSeasons, formatSecondsToISO8601Duration, formatSectionItems, formatSectionPanels, formatSections, formatSeries, formatSettings, formatShortform, formatSocialAuthSession, formatTaxonomies, formatUserPayload, formatVideos, formatWorkout, getUserSession, isAuthenticated, isVerifying, logout, nativeStore, paymentApi, removeFromCart, removePromoCode, resetCreating, resetTaxRate, selectAccessToken, selectCartCount, selectCartItems, selectCartItemsIds, selectCartNetTotal, selectCartStatus, selectCartTaxAmount, selectCartTaxRate, selectCartTotal, selectCurrentUser, selectCurrentUserRefreshToken, selectCurrentUserSub, selectMediaInCart, selectPromoCode, selectRefreshToken, selectSubscription, selectSubscriptionPrice, selectUserSubscriptionStatus, selectUserTokens, setCredentials, setPromoCode, setTaxRate, toggleCart, updateCart, updateSubscription, useAppDispatch, useAppSelector, useAppStore, useForgottenPasswordMutation, useGetUserInfoQuery, useLazyGetUserInfoQuery, useLazyVerifyUserAttributesQuery, useLazyVerifyUserQuery, useLazyVerifyUserResendQuery, useLoggedIn, useRegisterMutation, useResetPasswordAuthMutation, useResetPasswordMutation, useUpdateUserInfoMutation, useUpdateUserMutation, useVerifyUserAttributesQuery, useVerifyUserQuery, useVerifyUserResendQuery, webStore };
1616
+ export { addToCart, authApi, authSlice, cartSlice, contentApi, emptyCart, formatAuthSession, formatChallengeDays, formatChallenges, formatConfig, formatFaqs, formatFedaratedSession, formatGuests, formatLongform, formatMediaItem, formatPages, formatPress, formatPromos, formatSchedule, formatSeasons, formatSecondsToISO8601Duration, formatSectionItems, formatSectionPanels, formatSections, formatSeries, formatSettings, formatShortform, formatSocialAuthSession, formatTaxonomies, formatUserPayload, formatVideos, formatWorkout, getUserSession, isAuthenticated, isVerifying, logout, nativeStore, ordersApi, paymentApi, removeFromCart, removePromoCode, resetCreating, resetTaxRate, selectAccessToken, selectCartCount, selectCartItems, selectCartItemsIds, selectCartNetTotal, selectCartStatus, selectCartTaxAmount, selectCartTaxRate, selectCartTotal, selectCurrentUser, selectCurrentUserRefreshToken, selectCurrentUserSub, selectMediaInCart, selectPromoCode, selectRefreshToken, selectSubscription, selectSubscriptionPrice, selectUserSubscriptionStatus, selectUserTokens, setCredentials, setPromoCode, setTaxRate, toggleCart, updateCart, updateSubscription, useAppDispatch, useAppSelector, useAppStore, useForgottenPasswordMutation, useGetUserInfoQuery, useLazyGetUserInfoQuery, useLazyVerifyUserAttributesQuery, useLazyVerifyUserQuery, useLazyVerifyUserResendQuery, useLoggedIn, useRegisterMutation, useResetPasswordAuthMutation, useResetPasswordMutation, useUpdateUserInfoMutation, useUpdateUserMutation, useVerifyUserAttributesQuery, useVerifyUserQuery, useVerifyUserResendQuery, webStore };
1510
1617
  //# sourceMappingURL=index.esm.js.map