@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.js CHANGED
@@ -173,6 +173,32 @@ var setCredentials = (_a$2 = authSlice.actions, _a$2.setCredentials), resetCreat
173
173
  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;
174
174
  authSlice.reducer;
175
175
 
176
+ function applyCoupon(coupon, netTotal) {
177
+ var discountedAmount = netTotal * (Number(coupon.discount) / 100);
178
+ var discountedTotal = netTotal - discountedAmount;
179
+ var data = __assign(__assign({}, coupon), { discountedTotal: discountedTotal, discountedAmount: discountedAmount });
180
+ return data;
181
+ }
182
+ function applyTax(taxRate, netTotal) {
183
+ var taxAmount = netTotal * (taxRate.percentage / 100);
184
+ return taxAmount;
185
+ }
186
+ function toCamelCaseObject(obj) {
187
+ var toCamelCase = function (str) {
188
+ return str.replace(/([-_][a-z])/gi, function (match) {
189
+ return match.toUpperCase().replace(/[-_]/, '');
190
+ });
191
+ };
192
+ var camelCaseObject = {};
193
+ for (var key in obj) {
194
+ if (obj.hasOwnProperty(key)) {
195
+ var camelKey = toCamelCase(key);
196
+ camelCaseObject[camelKey] = obj[key];
197
+ }
198
+ }
199
+ return camelCaseObject;
200
+ }
201
+
176
202
  // TODO: typing.
177
203
  var formatUserPayload = function (payload) {
178
204
  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"];
@@ -593,7 +619,7 @@ function formatAuthSession(session) {
593
619
  };
594
620
  }
595
621
  function formatFedaratedSession(session) {
596
- 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;
622
+ 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;
597
623
  return {
598
624
  accessToken: (_a = session === null || session === void 0 ? void 0 : session.accessToken) === null || _a === void 0 ? void 0 : _a.jwtToken,
599
625
  idToken: (_b = session === null || session === void 0 ? void 0 : session.idToken) === null || _b === void 0 ? void 0 : _b.jwtToken,
@@ -612,6 +638,7 @@ function formatFedaratedSession(session) {
612
638
  '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'],
613
639
  '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'],
614
640
  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,
641
+ id: toCamelCaseObject((_5 = session === null || session === void 0 ? void 0 : session.idToken) === null || _5 === void 0 ? void 0 : _5.payload),
615
642
  },
616
643
  };
617
644
  }
@@ -797,7 +824,7 @@ var formatMediaItem = function (data) {
797
824
  });
798
825
  };
799
826
 
800
- var _a$1, _b$1, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t;
827
+ var _a$1, _b$1, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u;
801
828
  // Render the store based on the environment.
802
829
  var isWeb = process.env.NEXT_PUBLIC_WEB_APP;
803
830
  var ENV = isWeb ? process.env : require('react-native-config').default;
@@ -807,23 +834,25 @@ var ENV = isWeb ? process.env : require('react-native-config').default;
807
834
  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;
808
835
  // Payments specific API
809
836
  var API_PAYMENTS_PREFIX = ((_c = process.env) === null || _c === void 0 ? void 0 : _c.NEXT_PUBLIC_API_PAYMENTS_PREFIX) || ENV.API_PAYMENTS_PREFIX;
810
- ((_d = process.env) === null || _d === void 0 ? void 0 : _d.NEXT_PUBLIC_APP_BASE_URL) || ENV.APP_BASE_URL;
811
- var APP_ES_INSTANCE = ((_e = process.env) === null || _e === void 0 ? void 0 : _e.NEXT_PUBLIC_APP_ES_INSTANCE) || ENV.APP_ES_INSTANCE;
812
- var APP_ES_AUTH = ((_f = process.env) === null || _f === void 0 ? void 0 : _f.NEXT_PUBLIC_APP_ES_AUTH) || ENV.APP_ES_AUTH;
813
- ((_g = process.env) === null || _g === void 0 ? void 0 : _g.NEXT_PUBLIC_APP_ES_PROMO_PANEL_INDEX) ||
837
+ // Orders specific API
838
+ var API_ORDERS_PREFIX = ((_d = process.env) === null || _d === void 0 ? void 0 : _d.NEXT_PUBLIC_API_ORDERS_PREFIX) || ENV.API_ORDERS_PREFIX;
839
+ ((_e = process.env) === null || _e === void 0 ? void 0 : _e.NEXT_PUBLIC_APP_BASE_URL) || ENV.APP_BASE_URL;
840
+ var APP_ES_INSTANCE = ((_f = process.env) === null || _f === void 0 ? void 0 : _f.NEXT_PUBLIC_APP_ES_INSTANCE) || ENV.APP_ES_INSTANCE;
841
+ var APP_ES_AUTH = ((_g = process.env) === null || _g === void 0 ? void 0 : _g.NEXT_PUBLIC_APP_ES_AUTH) || ENV.APP_ES_AUTH;
842
+ ((_h = process.env) === null || _h === void 0 ? void 0 : _h.NEXT_PUBLIC_APP_ES_PROMO_PANEL_INDEX) ||
814
843
  ENV.APP_ES_PROMO_PANEL_INDEX;
815
- ((_h = process.env) === null || _h === void 0 ? void 0 : _h.NEXT_PUBLIC_APP_ES_FAQ_INDEX) || ENV.APP_ES_FAQ_INDEX;
816
- ((_j = process.env) === null || _j === void 0 ? void 0 : _j.NEXT_PUBLIC_APP_ES_PAGES_INDEX) || ENV.APP_ES_PAGES_INDEX;
817
- ((_k = process.env) === null || _k === void 0 ? void 0 : _k.NEXT_PUBLIC_APP_ES_SETTINGS_INDEX) || ENV.APP_ES_SETTINGS_INDEX;
818
- ((_l = process.env) === null || _l === void 0 ? void 0 : _l.NEXT_PUBLIC_APP_ES_PRESS_INDEX) || ENV.APP_ES_PRESS_INDEX;
819
- ((_m = process.env) === null || _m === void 0 ? void 0 : _m.NEXT_PUBLIC_APP_ES_GUESTS_INDEX) || ENV.APP_ES_GUESTS_INDEX;
820
- ((_o = process.env) === null || _o === void 0 ? void 0 : _o.NEXT_PUBLIC_APP_ES_VIDEOS_INDEX) || ENV.APP_ES_VIDEOS_INDEX;
821
- ((_p = process.env) === null || _p === void 0 ? void 0 : _p.NEXT_PUBLIC_APP_ES_SECTIONS_INDEX) || ENV.APP_ES_SECTIONS_INDEX;
822
- ((_q = process.env) === null || _q === void 0 ? void 0 : _q.NEXT_PUBLIC_APP_ES_WORKOUTS_INDEX) || ENV.APP_ES_WORKOUTS_INDEX;
823
- ((_r = process.env) === null || _r === void 0 ? void 0 : _r.NEXT_PUBLIC_APP_ES_SCHEDULE_INDEX) || ENV.APP_ES_SCHEDULE_INDEX;
824
- ((_s = process.env) === null || _s === void 0 ? void 0 : _s.NEXT_PUBLIC_APP_ES_CHALLENGES_INDEX) ||
844
+ ((_j = process.env) === null || _j === void 0 ? void 0 : _j.NEXT_PUBLIC_APP_ES_FAQ_INDEX) || ENV.APP_ES_FAQ_INDEX;
845
+ ((_k = process.env) === null || _k === void 0 ? void 0 : _k.NEXT_PUBLIC_APP_ES_PAGES_INDEX) || ENV.APP_ES_PAGES_INDEX;
846
+ ((_l = process.env) === null || _l === void 0 ? void 0 : _l.NEXT_PUBLIC_APP_ES_SETTINGS_INDEX) || ENV.APP_ES_SETTINGS_INDEX;
847
+ ((_m = process.env) === null || _m === void 0 ? void 0 : _m.NEXT_PUBLIC_APP_ES_PRESS_INDEX) || ENV.APP_ES_PRESS_INDEX;
848
+ ((_o = process.env) === null || _o === void 0 ? void 0 : _o.NEXT_PUBLIC_APP_ES_GUESTS_INDEX) || ENV.APP_ES_GUESTS_INDEX;
849
+ ((_p = process.env) === null || _p === void 0 ? void 0 : _p.NEXT_PUBLIC_APP_ES_VIDEOS_INDEX) || ENV.APP_ES_VIDEOS_INDEX;
850
+ ((_q = process.env) === null || _q === void 0 ? void 0 : _q.NEXT_PUBLIC_APP_ES_SECTIONS_INDEX) || ENV.APP_ES_SECTIONS_INDEX;
851
+ ((_r = process.env) === null || _r === void 0 ? void 0 : _r.NEXT_PUBLIC_APP_ES_WORKOUTS_INDEX) || ENV.APP_ES_WORKOUTS_INDEX;
852
+ ((_s = process.env) === null || _s === void 0 ? void 0 : _s.NEXT_PUBLIC_APP_ES_SCHEDULE_INDEX) || ENV.APP_ES_SCHEDULE_INDEX;
853
+ ((_t = process.env) === null || _t === void 0 ? void 0 : _t.NEXT_PUBLIC_APP_ES_CHALLENGES_INDEX) ||
825
854
  ENV.APP_ES_CHALLENGES_INDEX;
826
- ((_t = process.env) === null || _t === void 0 ? void 0 : _t.NEXT_PUBLIC_APP_ES_CHALLENGE_DAYS_INDEX) ||
855
+ ((_u = process.env) === null || _u === void 0 ? void 0 : _u.NEXT_PUBLIC_APP_ES_CHALLENGE_DAYS_INDEX) ||
827
856
  ENV.APP_ES_CHALLENGE_DAYS_INDEX;
828
857
 
829
858
  var authDataBaseQuery = react.fetchBaseQuery({
@@ -1252,16 +1281,92 @@ var paymentApi = react.createApi({
1252
1281
  // Export hooks for usage in functional components.
1253
1282
  paymentApi.useCheckUserSubscriptionQuery; paymentApi.useLazyCheckUserSubscriptionQuery; paymentApi.useGetPaymentPlansQuery; paymentApi.useLazyGetPaymentPlansQuery; paymentApi.useGetTaxRatesQuery; paymentApi.useLazyGetTaxRatesQuery; paymentApi.useCheckPromoCodeQuery; paymentApi.useLazyCheckPromoCodeQuery;
1254
1283
 
1255
- function applyCoupon(coupon, netTotal) {
1256
- var discountedAmount = netTotal * (Number(coupon.discount) / 100);
1257
- var discountedTotal = netTotal - discountedAmount;
1258
- var data = __assign(__assign({}, coupon), { discountedTotal: discountedTotal, discountedAmount: discountedAmount });
1259
- return data;
1260
- }
1261
- function applyTax(taxRate, netTotal) {
1262
- var taxAmount = netTotal * (taxRate.percentage / 100);
1263
- return taxAmount;
1264
- }
1284
+ var dataBaseQuery = react.fetchBaseQuery({
1285
+ baseUrl: API_ORDERS_PREFIX,
1286
+ prepareHeaders: function (headers_1, _a) { return __awaiter(void 0, [headers_1, _a], void 0, function (headers, _b) {
1287
+ var session, idToken, accessToken;
1288
+ return __generator(this, function (_c) {
1289
+ switch (_c.label) {
1290
+ case 0:
1291
+ headers.set('Content-Type', 'application/json');
1292
+ return [4 /*yield*/, awsAmplify.Auth.currentSession()];
1293
+ case 1:
1294
+ session = _c.sent();
1295
+ console.log('session', session);
1296
+ idToken = session.getIdToken().getJwtToken();
1297
+ accessToken = session.getAccessToken().getJwtToken();
1298
+ if (accessToken && idToken) {
1299
+ headers.set('accesstoken', accessToken);
1300
+ headers.set('idtoken', idToken);
1301
+ // headers.set('refreshtoken', tokens.refreshToken);
1302
+ }
1303
+ return [2 /*return*/, headers];
1304
+ }
1305
+ });
1306
+ }); },
1307
+ credentials: 'include',
1308
+ });
1309
+ /**
1310
+ * This function is used to retry a request if we get a 401 error.
1311
+ */
1312
+ var dataBaseQueryWithReauth = function (args, api, extraOptions) { return __awaiter(void 0, void 0, void 0, function () {
1313
+ var result, authSession, user;
1314
+ var _a;
1315
+ return __generator(this, function (_b) {
1316
+ switch (_b.label) {
1317
+ case 0: return [4 /*yield*/, dataBaseQuery(args, api, extraOptions)];
1318
+ case 1:
1319
+ result = _b.sent();
1320
+ console.log('result', result);
1321
+ if (!(((_a = result === null || result === void 0 ? void 0 : result.error) === null || _a === void 0 ? void 0 : _a.status) === 401)) return [3 /*break*/, 6];
1322
+ return [4 /*yield*/, awsAmplify.Auth.currentAuthenticatedUser({
1323
+ bypassCache: true, // Ensures that the user is refreshed from the server
1324
+ })];
1325
+ case 2:
1326
+ authSession = _b.sent();
1327
+ user = formatAuthSession(authSession);
1328
+ api.dispatch(setCredentials(user));
1329
+ if (!(authSession === null || authSession === void 0 ? void 0 : authSession.tokens)) return [3 /*break*/, 4];
1330
+ return [4 /*yield*/, dataBaseQuery(args, api, extraOptions)];
1331
+ case 3:
1332
+ // If we get a new access token, retry the original request.
1333
+ result = _b.sent();
1334
+ return [3 /*break*/, 6];
1335
+ case 4:
1336
+ // TODO; require full data reset?
1337
+ return [4 /*yield*/, awsAmplify.Auth.signOut()];
1338
+ case 5:
1339
+ // TODO; require full data reset?
1340
+ _b.sent();
1341
+ api.dispatch(logout());
1342
+ // If no access token throw error.
1343
+ throw new Error('No access token found');
1344
+ case 6: return [2 /*return*/, result];
1345
+ }
1346
+ });
1347
+ }); };
1348
+ var ordersApi = react.createApi({
1349
+ reducerPath: 'ordersApi',
1350
+ baseQuery: dataBaseQueryWithReauth,
1351
+ tagTypes: ['Orders'],
1352
+ // keepUnusedDataFor: 300,
1353
+ endpoints: function (builder) { return ({
1354
+ /**
1355
+ * USER API.
1356
+ */
1357
+ fetchOrders: builder.query({
1358
+ query: function (ids) { return ({
1359
+ url: '/order/search',
1360
+ method: 'POST',
1361
+ body: {
1362
+ ids: ids,
1363
+ },
1364
+ }); },
1365
+ }),
1366
+ }); },
1367
+ });
1368
+ // Export hooks for usage in functional components.
1369
+ ordersApi.useFetchOrdersQuery; ordersApi.useLazyFetchOrdersQuery;
1265
1370
 
1266
1371
  var _a, _b;
1267
1372
  var initialState = {
@@ -1413,7 +1518,7 @@ function createPersistStorage() {
1413
1518
  }
1414
1519
 
1415
1520
  // Infer the `RootState` type from the root reducer
1416
- var rootReducer = toolkit.combineSlices(cartSlice, authSlice, authApi, contentApi, paymentApi);
1521
+ var rootReducer = toolkit.combineSlices(cartSlice, authSlice, authApi, contentApi, paymentApi, ordersApi);
1417
1522
  // eg. ['auth', 'creating'], or an empty array if you don't want to whitelist any.
1418
1523
  // const storageWhiteList: string[] = [];
1419
1524
  var storageBlackList = [];
@@ -1423,7 +1528,7 @@ var storageBlackList = [];
1423
1528
  // are needed for each request to prevent cross-request state pollution.
1424
1529
  var makeNativeStore = function () {
1425
1530
  var storage = require('@react-native-async-storage/async-storage').default;
1426
- storageBlackList.push(paymentApi.reducerPath, authApi.reducerPath, contentApi.reducerPath);
1531
+ storageBlackList.push(paymentApi.reducerPath, authApi.reducerPath, contentApi.reducerPath, ordersApi.reducerPath);
1427
1532
  var persistConfig = {
1428
1533
  key: 'root',
1429
1534
  storage: storage,
@@ -1434,6 +1539,7 @@ var makeNativeStore = function () {
1434
1539
  authApi.middleware,
1435
1540
  contentApi.middleware,
1436
1541
  paymentApi.middleware,
1542
+ ordersApi.middleware,
1437
1543
  ];
1438
1544
  // `combineSlices` automatically combines the reducers using
1439
1545
  // their `reducerPath`s, therefore we no longer need to call `combineReducers`.
@@ -1471,7 +1577,7 @@ var makeNativeStore = function () {
1471
1577
  };
1472
1578
  var makeWebStore = function () {
1473
1579
  var storage = createPersistStorage();
1474
- storageBlackList.push(paymentApi.reducerPath, authApi.reducerPath, contentApi.reducerPath);
1580
+ storageBlackList.push(paymentApi.reducerPath, authApi.reducerPath, contentApi.reducerPath, ordersApi.reducerPath);
1475
1581
  var persistConfig = {
1476
1582
  key: 'root',
1477
1583
  storage: storage,
@@ -1482,6 +1588,7 @@ var makeWebStore = function () {
1482
1588
  authApi.middleware,
1483
1589
  contentApi.middleware,
1484
1590
  paymentApi.middleware,
1591
+ ordersApi.middleware,
1485
1592
  ];
1486
1593
  // `combineSlices` automatically combines the reducers using
1487
1594
  // their `reducerPath`s, therefore we no longer need to call `combineReducers`.
@@ -1545,6 +1652,7 @@ exports.isAuthenticated = isAuthenticated;
1545
1652
  exports.isVerifying = isVerifying;
1546
1653
  exports.logout = logout;
1547
1654
  exports.nativeStore = nativeStore;
1655
+ exports.ordersApi = ordersApi;
1548
1656
  exports.paymentApi = paymentApi;
1549
1657
  exports.removeFromCart = removeFromCart;
1550
1658
  exports.removePromoCode = removePromoCode;