@faststore/api 2.2.0-alpha.0 → 2.2.0-alpha.6

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.
@@ -22,7 +22,7 @@ var api = require('@opentelemetry/api');
22
22
  var apiLogs = require('@opentelemetry/api-logs');
23
23
 
24
24
  var name = "@faststore/api";
25
- var version = "2.1.78";
25
+ var version = "2.2.0-alpha.1";
26
26
  var license = "MIT";
27
27
  var main = "dist/index.js";
28
28
  var typings = "dist/index.d.ts";
@@ -58,8 +58,8 @@ var dependencies = {
58
58
  };
59
59
  var devDependencies = {
60
60
  "@envelop/core": "^2.6.0",
61
- "@faststore/eslint-config": "^2.1.78",
62
- "@faststore/shared": "^2.1.78",
61
+ "@faststore/eslint-config": "^2.2.0-alpha.1",
62
+ "@faststore/shared": "^2.2.0-alpha.1",
63
63
  "@graphql-codegen/cli": "2.2.0",
64
64
  "@graphql-codegen/typescript": "2.2.2",
65
65
  "@types/express": "^4.17.16",
@@ -111,16 +111,6 @@ const fetchAPI = async (info, init) => {
111
111
  throw new Error(text);
112
112
  };
113
113
 
114
- const getCookie = (name, cookie) => {
115
- const value = `; ${cookie}`;
116
- const parts = value.split(`; ${name}=`);
117
- if (parts.length === 2) {
118
- var _parts$pop$split$shif, _parts$pop;
119
- return (_parts$pop$split$shif = parts == null ? void 0 : (_parts$pop = parts.pop()) == null ? void 0 : _parts$pop.split(';').shift()) != null ? _parts$pop$split$shif : '';
120
- }
121
- return '';
122
- };
123
-
124
114
  const BASE_INIT = {
125
115
  method: 'POST',
126
116
  headers: {
@@ -215,7 +205,14 @@ const VtexCommerce = ({
215
205
  refreshOutdatedData: refreshOutdatedData.toString(),
216
206
  sc: salesChannel
217
207
  });
218
- return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}?${params.toString()}`, BASE_INIT);
208
+ const requestInit = ctx.headers ? {
209
+ ...BASE_INIT,
210
+ headers: {
211
+ 'content-type': 'application/json',
212
+ cookie: ctx.headers.cookie
213
+ }
214
+ } : BASE_INIT;
215
+ return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}?${params.toString()}`, requestInit);
219
216
  },
220
217
  updateOrderFormItems: ({
221
218
  id,
@@ -228,14 +225,25 @@ const VtexCommerce = ({
228
225
  allowOutdatedData,
229
226
  sc: salesChannel
230
227
  });
231
- return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}/items?${params}`, {
232
- ...BASE_INIT,
233
- body: JSON.stringify({
234
- orderItems,
235
- noSplitItem: !shouldSplitItem
236
- }),
237
- method: 'PATCH'
228
+ const items = JSON.stringify({
229
+ orderItems,
230
+ noSplitItem: !shouldSplitItem
238
231
  });
232
+ const requestInit = ctx.headers ? {
233
+ headers: {
234
+ 'content-type': 'application/json',
235
+ cookie: ctx.headers.cookie
236
+ },
237
+ body: items,
238
+ method: 'PATCH'
239
+ } : {
240
+ headers: {
241
+ 'content-type': 'application/json'
242
+ },
243
+ body: items,
244
+ method: 'PATCH'
245
+ };
246
+ return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}/items?${params}`, requestInit);
239
247
  },
240
248
  setCustomData: ({
241
249
  id,
@@ -275,34 +283,13 @@ const VtexCommerce = ({
275
283
  session: search => {
276
284
  const params = new URLSearchParams(search);
277
285
  params.set('items', 'profile.id,profile.email,profile.firstName,profile.lastName,store.channel,store.countryCode,store.cultureInfo,store.currencyCode,store.currencySymbol');
278
- if (getCookie('vtex_session', ctx.headers.cookie)) {
279
- // cookie set
280
- return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
281
- method: 'GET',
282
- headers: {
283
- 'content-type': 'application/json',
284
- cookie: ctx.headers.cookie
285
- }
286
- });
287
- } else {
288
- // cookie unset -> create session
289
- return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
290
- method: 'POST',
291
- headers: {
292
- 'content-type': 'application/json',
293
- cookie: ctx.headers.cookie
294
- },
295
- body: '{}'
296
- });
297
- }
298
- },
299
- getSessionOrder: () => {
300
- return fetchAPI(`${base}/api/sessions?items=public.orderFormId`, {
301
- method: 'GET',
286
+ return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
287
+ method: 'POST',
302
288
  headers: {
303
289
  'content-type': 'application/json',
304
290
  cookie: ctx.headers.cookie
305
- }
291
+ },
292
+ body: '{}'
306
293
  });
307
294
  },
308
295
  subscribeToNewsletter: data => {
@@ -1033,55 +1020,31 @@ function getPropertyId(item) {
1033
1020
  }
1034
1021
 
1035
1022
  const shouldUpdateShippingData = (orderForm, session) => {
1036
- var _orderForm$shippingDa;
1023
+ var _orderForm$shippingDa, _orderForm$shippingDa2;
1037
1024
  if (!hasSessionPostalCodeOrGeoCoordinates(session)) {
1038
1025
  return {
1039
1026
  updateShipping: false,
1040
1027
  addressChanged: false
1041
1028
  };
1042
1029
  }
1043
- const selectedAddress = (_orderForm$shippingDa = orderForm.shippingData) == null ? void 0 : _orderForm$shippingDa.selectedAddresses[0];
1044
- if (checkPostalCode(selectedAddress, session.postalCode)) {
1045
- return {
1046
- updateShipping: true,
1047
- addressChanged: true
1048
- };
1049
- }
1050
- if (checkGeoCoordinates(selectedAddress, session.geoCoordinates, session.postalCode)) {
1030
+ if (!hasItems(orderForm)) {
1051
1031
  return {
1052
- updateShipping: true,
1053
- addressChanged: true
1032
+ updateShipping: false,
1033
+ addressChanged: false
1054
1034
  };
1055
1035
  }
1056
- if (checkAddressType(selectedAddress, session.addressType)) {
1036
+ const [selectedAddress] = (_orderForm$shippingDa = orderForm == null ? void 0 : (_orderForm$shippingDa2 = orderForm.shippingData) == null ? void 0 : _orderForm$shippingDa2.selectedAddresses) != null ? _orderForm$shippingDa : [];
1037
+ if (checkPostalCode(selectedAddress, session.postalCode) || checkGeoCoordinates(selectedAddress, session.geoCoordinates) || checkAddressType(selectedAddress, session.addressType)) {
1057
1038
  return {
1058
1039
  updateShipping: true,
1059
1040
  addressChanged: true
1060
1041
  };
1061
1042
  }
1062
- if (!hasItems(orderForm)) {
1063
- return {
1064
- updateShipping: false,
1065
- addressChanged: false
1066
- };
1067
- }
1068
1043
  // The logisticsInfo will always exist if there´s at least one item inside the cart
1069
1044
  const {
1070
1045
  logisticsInfo
1071
1046
  } = orderForm.shippingData;
1072
- if (shouldUpdateDeliveryChannel(logisticsInfo, session)) {
1073
- return {
1074
- updateShipping: true,
1075
- addressChanged: false
1076
- };
1077
- }
1078
- if (shouldUpdateDeliveryMethod(logisticsInfo, session)) {
1079
- return {
1080
- updateShipping: true,
1081
- addressChanged: false
1082
- };
1083
- }
1084
- if (shouldUpdateDeliveryWindow(logisticsInfo, session)) {
1047
+ if (shouldUpdateDeliveryInfo(logisticsInfo, session)) {
1085
1048
  return {
1086
1049
  updateShipping: true,
1087
1050
  addressChanged: false
@@ -1094,15 +1057,16 @@ const shouldUpdateShippingData = (orderForm, session) => {
1094
1057
  };
1095
1058
  // Validate if theres any postal Code or GeoCoordinates set at the session
1096
1059
  const hasSessionPostalCodeOrGeoCoordinates = session => {
1097
- return !!session.postalCode || session.geoCoordinates && session.geoCoordinates.latitude && session.geoCoordinates.longitude;
1060
+ var _session$geoCoordinat, _session$geoCoordinat2;
1061
+ return !!session.postalCode || ((_session$geoCoordinat = session.geoCoordinates) == null ? void 0 : _session$geoCoordinat.latitude) && ((_session$geoCoordinat2 = session.geoCoordinates) == null ? void 0 : _session$geoCoordinat2.longitude);
1098
1062
  };
1099
1063
  // Validate if theres a difference between the session postal code and orderForm postal code
1100
1064
  const checkPostalCode = (address, postalCode) => {
1101
1065
  return typeof postalCode === 'string' && (address == null ? void 0 : address.postalCode) !== postalCode;
1102
1066
  };
1103
1067
  // Validate if theres a difference between the session geoCoords and orderForm geoCoords
1104
- const checkGeoCoordinates = (address, geoCoordinates, postalCode) => {
1105
- return typeof (geoCoordinates == null ? void 0 : geoCoordinates.latitude) === 'number' && typeof (geoCoordinates == null ? void 0 : geoCoordinates.longitude) === 'number' && ((address == null ? void 0 : address.geoCoordinates[0]) !== (geoCoordinates == null ? void 0 : geoCoordinates.longitude) || (address == null ? void 0 : address.geoCoordinates[1]) !== (geoCoordinates == null ? void 0 : geoCoordinates.latitude)) && (address == null ? void 0 : address.postalCode) !== postalCode;
1068
+ const checkGeoCoordinates = (address, geoCoordinates) => {
1069
+ return typeof (geoCoordinates == null ? void 0 : geoCoordinates.latitude) === 'number' && typeof (geoCoordinates == null ? void 0 : geoCoordinates.longitude) === 'number' && ((address == null ? void 0 : address.geoCoordinates[0]) !== (geoCoordinates == null ? void 0 : geoCoordinates.longitude) || (address == null ? void 0 : address.geoCoordinates[1]) !== (geoCoordinates == null ? void 0 : geoCoordinates.latitude));
1106
1070
  };
1107
1071
  const checkAddressType = (address, addressType) => {
1108
1072
  return typeof addressType === 'string' && (address == null ? void 0 : address.addressType) !== addressType;
@@ -1111,67 +1075,29 @@ const checkAddressType = (address, addressType) => {
1111
1075
  const hasItems = orderForm => {
1112
1076
  return orderForm.items.length !== 0;
1113
1077
  };
1114
- // Validate if the deliveryChannel from the session is different from the selected delivery channel
1115
- // and if so needs to validate if the deliveryChannel for the session is available inside the slas for the item
1116
- const shouldUpdateDeliveryChannel = (logisticsInfo, session) => {
1117
- var _session$deliveryMode;
1118
- if (!(session != null && (_session$deliveryMode = session.deliveryMode) != null && _session$deliveryMode.deliveryChannel)) {
1119
- return false;
1120
- }
1121
- const {
1122
- deliveryChannel
1123
- } = session.deliveryMode;
1124
- for (const item of logisticsInfo) {
1125
- if (item.selectedDeliveryChannel !== deliveryChannel) {
1126
- const matchingSla = item.slas.find(sla => sla.deliveryChannel === deliveryChannel);
1127
- if (matchingSla) {
1128
- return true;
1129
- }
1130
- }
1131
- }
1132
- return false;
1133
- };
1134
- // Validate if the deliveryMethod from the session is different from the selectedSLA
1135
- // and if so needs to validate if the deliveryMethod for the session is available inside the slas for the item
1136
- const shouldUpdateDeliveryMethod = (logisticsInfo, session) => {
1137
- var _session$deliveryMode2;
1138
- if (!(session != null && (_session$deliveryMode2 = session.deliveryMode) != null && _session$deliveryMode2.deliveryMethod)) {
1139
- return false;
1140
- }
1141
- const {
1142
- deliveryMethod
1143
- } = session.deliveryMode;
1144
- for (const item of logisticsInfo) {
1145
- if (item.selectedSla !== deliveryMethod) {
1146
- const matchingSla = item.slas.find(sla => sla.id === deliveryMethod);
1147
- if (matchingSla) {
1148
- return true;
1149
- }
1150
- }
1151
- }
1152
- return false;
1153
- };
1154
- // Validate if the deliveryWindow from the session is different from the deliveryWindow of the SLA
1155
- // and if so needs to validate if the deliveryWindow for the session is available inside the availableDeliveryWindows for the item
1156
- const shouldUpdateDeliveryWindow = (logisticsInfo, session) => {
1157
- var _session$deliveryMode3, _session$deliveryMode4, _session$deliveryMode5, _session$deliveryMode6;
1158
- if (!(session != null && (_session$deliveryMode3 = session.deliveryMode) != null && (_session$deliveryMode4 = _session$deliveryMode3.deliveryWindow) != null && _session$deliveryMode4.startDate) || !(session != null && (_session$deliveryMode5 = session.deliveryMode) != null && (_session$deliveryMode6 = _session$deliveryMode5.deliveryWindow) != null && _session$deliveryMode6.endDate)) {
1159
- return false;
1160
- }
1078
+ const shouldUpdateDeliveryInfo = (logisticsInfo, session) => {
1079
+ var _session$deliveryMode, _session$deliveryMode2, _session$deliveryMode3;
1080
+ const deliveryChannel = session == null ? void 0 : (_session$deliveryMode = session.deliveryMode) == null ? void 0 : _session$deliveryMode.deliveryChannel;
1081
+ const deliveryMethod = session == null ? void 0 : (_session$deliveryMode2 = session.deliveryMode) == null ? void 0 : _session$deliveryMode2.deliveryMethod;
1161
1082
  const {
1162
1083
  startDate,
1163
1084
  endDate
1164
- } = session.deliveryMode.deliveryWindow;
1165
- for (const item of logisticsInfo) {
1166
- for (const sla of item.slas) {
1167
- var _sla$availableDeliver;
1168
- const matchingWindow = (_sla$availableDeliver = sla.availableDeliveryWindows) == null ? void 0 : _sla$availableDeliver.some(window => window.startDateUtc === startDate && window.endDateUtc === endDate);
1169
- if (matchingWindow) {
1085
+ } = (session == null ? void 0 : (_session$deliveryMode3 = session.deliveryMode) == null ? void 0 : _session$deliveryMode3.deliveryWindow) || {};
1086
+ return logisticsInfo.some(({
1087
+ selectedDeliveryChannel,
1088
+ selectedSla,
1089
+ slas
1090
+ }) => {
1091
+ const checkDeliveryChannel = deliveryChannel && selectedDeliveryChannel !== deliveryChannel;
1092
+ const checkDeliveryMethod = deliveryMethod && selectedSla !== deliveryMethod;
1093
+ return slas == null ? void 0 : slas.some(sla => {
1094
+ var _sla$deliveryWindow, _sla$deliveryWindow2, _sla$availableDeliver;
1095
+ if (checkDeliveryChannel && sla.deliveryChannel === deliveryChannel || checkDeliveryMethod && sla.id === deliveryMethod) {
1170
1096
  return true;
1171
1097
  }
1172
- }
1173
- }
1174
- return false;
1098
+ return startDate && endDate && sla.deliveryChannel === deliveryChannel && sla.id === deliveryMethod && (!(sla != null && sla.deliveryWindow) || (sla == null ? void 0 : (_sla$deliveryWindow = sla.deliveryWindow) == null ? void 0 : _sla$deliveryWindow.startDateUtc) !== startDate || (sla == null ? void 0 : (_sla$deliveryWindow2 = sla.deliveryWindow) == null ? void 0 : _sla$deliveryWindow2.endDateUtc) !== endDate) && ((_sla$availableDeliver = sla.availableDeliveryWindows) == null ? void 0 : _sla$availableDeliver.some(window => (window == null ? void 0 : window.startDateUtc) === startDate && (window == null ? void 0 : window.endDateUtc) === endDate));
1099
+ });
1100
+ });
1175
1101
  };
1176
1102
 
1177
1103
  const getAddressOrderForm = (orderForm, session, addressChanged) => {
@@ -1305,7 +1231,10 @@ const joinItems = form => {
1305
1231
  items: Object.values(itemsById).map(items => {
1306
1232
  const [item] = items;
1307
1233
  const quantity = items.reduce((acc, i) => acc + i.quantity, 0);
1308
- const totalPrice = items.reduce((acc, i) => acc + i.quantity * i.sellingPrice, 0);
1234
+ const totalPrice = items.reduce((acc, i) => {
1235
+ var _i$priceDefinition$to, _i$priceDefinition, _i$quantity, _i$sellingPrice;
1236
+ return acc + ((_i$priceDefinition$to = i == null ? void 0 : (_i$priceDefinition = i.priceDefinition) == null ? void 0 : _i$priceDefinition.total) != null ? _i$priceDefinition$to : ((_i$quantity = i == null ? void 0 : i.quantity) != null ? _i$quantity : 0) * ((_i$sellingPrice = i == null ? void 0 : i.sellingPrice) != null ? _i$sellingPrice : 0));
1237
+ }, 0);
1309
1238
  return {
1310
1239
  ...item,
1311
1240
  quantity,
@@ -1364,17 +1293,6 @@ const isOrderFormStale = form => {
1364
1293
  const newEtag = getOrderFormEtag(form);
1365
1294
  return newEtag !== oldEtag;
1366
1295
  };
1367
- async function getOrderNumberFromSession(headers = {}, commerce) {
1368
- const cookieSession = getCookie('vtex_session', headers.cookie);
1369
- if (cookieSession) {
1370
- var _namespaces$public$or, _namespaces$public, _namespaces$public$or2;
1371
- const {
1372
- namespaces
1373
- } = await commerce.getSessionOrder();
1374
- return (_namespaces$public$or = (_namespaces$public = namespaces.public) == null ? void 0 : (_namespaces$public$or2 = _namespaces$public.orderFormId) == null ? void 0 : _namespaces$public$or2.value) != null ? _namespaces$public$or : undefined;
1375
- }
1376
- return;
1377
- }
1378
1296
  // Returns the regionalized orderForm
1379
1297
  const getOrderForm = async (id, {
1380
1298
  clients: {
@@ -1446,9 +1364,8 @@ const validateCart = async (_, {
1446
1364
  },
1447
1365
  session
1448
1366
  }, ctx) => {
1449
- var _ref;
1450
1367
  const {
1451
- orderNumber: orderNumberFromCart,
1368
+ orderNumber,
1452
1369
  acceptedOffer,
1453
1370
  shouldSplitItem
1454
1371
  } = order;
@@ -1458,8 +1375,7 @@ const validateCart = async (_, {
1458
1375
  },
1459
1376
  loaders: {
1460
1377
  skuLoader
1461
- },
1462
- headers
1378
+ }
1463
1379
  } = ctx;
1464
1380
  const channel = session == null ? void 0 : session.channel;
1465
1381
  const locale = session == null ? void 0 : session.locale;
@@ -1469,19 +1385,9 @@ const validateCart = async (_, {
1469
1385
  if (locale) {
1470
1386
  mutateLocaleContext(ctx, locale);
1471
1387
  }
1472
- const orderNumberFromSession = await getOrderNumberFromSession(headers, commerce);
1473
- const orderNumber = (_ref = orderNumberFromSession != null ? orderNumberFromSession : orderNumberFromCart) != null ? _ref : '';
1474
1388
  // Step1: Get OrderForm from VTEX Commerce
1475
1389
  const orderForm = await getOrderForm(orderNumber, ctx);
1476
- // Step1.1: Checks if the orderForm id has changed. There are three cases for this:
1477
- // Social Selling: the vtex_session cookie contains a new orderForm id with Social Selling data
1478
- // My Orders: the customer clicks on reordering through generating a new cart and when returning to the faststore, this information needs to be returned by vtex_session cookie.
1479
- // New session: a new user enters the website and has no orderForm attributed to it (has no relation to the vtex_session cookie).
1480
- // In all cases, the origin orderForm should replace the copy that's in the browser
1481
- if (orderForm.orderFormId != orderNumberFromCart) {
1482
- return orderFormToCart(orderForm, skuLoader);
1483
- }
1484
- // Step1.2: Check if another system changed the orderForm with this orderNumber
1390
+ // Step1.5: Check if another system changed the orderForm with this orderNumber
1485
1391
  // If so, this means the user interacted with this cart elsewhere and expects
1486
1392
  // to see this new cart state instead of what's stored on the user's browser.
1487
1393
  const isStale = isOrderFormStale(orderForm);