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