@faststore/api 2.1.98 → 2.1.102

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.90";
25
+ var version = "2.1.99";
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.82",
62
- "@faststore/shared": "^2.1.82",
61
+ "@faststore/eslint-config": "^2.1.99",
62
+ "@faststore/shared": "^2.1.99",
63
63
  "@graphql-codegen/cli": "2.2.0",
64
64
  "@graphql-codegen/typescript": "2.2.2",
65
65
  "@types/express": "^4.17.16",
@@ -95,7 +95,7 @@ var packageJson = {
95
95
  };
96
96
 
97
97
  const USER_AGENT = `${packageJson.name}@${packageJson.version}`;
98
- const fetchAPI = async (info, init, getHeaders) => {
98
+ const fetchAPI = async (info, init) => {
99
99
  const response = await fetch(info, {
100
100
  ...init,
101
101
  headers: {
@@ -104,9 +104,6 @@ const fetchAPI = async (info, init, getHeaders) => {
104
104
  }
105
105
  });
106
106
  if (response.ok) {
107
- if (getHeaders) {
108
- getHeaders(response.headers);
109
- }
110
107
  return response.status !== 204 ? response.json() : undefined;
111
108
  }
112
109
  console.error(info, init, response);
@@ -114,23 +111,12 @@ const fetchAPI = async (info, init, getHeaders) => {
114
111
  throw new Error(text);
115
112
  };
116
113
 
117
- function getCookieByName(cookiename, source) {
118
- var cookiestring = RegExp(cookiename + '=[^;]+').exec(source);
119
- return decodeURIComponent(!!cookiestring ? cookiestring.toString().replace(/^[^=]+./, '') : '');
120
- }
121
-
122
114
  const BASE_INIT = {
123
115
  method: 'POST',
124
116
  headers: {
125
117
  'content-type': 'application/json'
126
118
  }
127
119
  };
128
- const setCheckoutOrderFormOwnershipCookie = (headers, ctx) => {
129
- if (headers) {
130
- var _headers$get;
131
- ctx.storage.cookies = `CheckoutOrderFormOwnership=${getCookieByName('CheckoutOrderFormOwnership', (_headers$get = headers.get('set-cookie')) != null ? _headers$get : '')}`;
132
- }
133
- };
134
120
  const VtexCommerce = ({
135
121
  account,
136
122
  environment,
@@ -205,7 +191,7 @@ const VtexCommerce = ({
205
191
  'content-type': 'application/json',
206
192
  cookie: ctx.headers.cookie
207
193
  }
208
- }, headers => setCheckoutOrderFormOwnershipCookie(headers, ctx));
194
+ });
209
195
  },
210
196
  orderForm: ({
211
197
  id,
@@ -219,7 +205,14 @@ const VtexCommerce = ({
219
205
  refreshOutdatedData: refreshOutdatedData.toString(),
220
206
  sc: salesChannel
221
207
  });
222
- return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}?${params.toString()}`, BASE_INIT, headers => setCheckoutOrderFormOwnershipCookie(headers, ctx));
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);
223
216
  },
224
217
  updateOrderFormItems: ({
225
218
  id,
@@ -232,14 +225,25 @@ const VtexCommerce = ({
232
225
  allowOutdatedData,
233
226
  sc: salesChannel
234
227
  });
235
- return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}/items?${params}`, {
236
- ...BASE_INIT,
237
- body: JSON.stringify({
238
- orderItems,
239
- noSplitItem: !shouldSplitItem
240
- }),
228
+ const items = JSON.stringify({
229
+ orderItems,
230
+ noSplitItem: !shouldSplitItem
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,
241
244
  method: 'PATCH'
242
- }, headers => setCheckoutOrderFormOwnershipCookie(headers, ctx));
245
+ };
246
+ return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}/items?${params}`, requestInit);
243
247
  },
244
248
  setCustomData: ({
245
249
  id,
@@ -253,7 +257,7 @@ const VtexCommerce = ({
253
257
  value
254
258
  }),
255
259
  method: 'PUT'
256
- }, headers => setCheckoutOrderFormOwnershipCookie(headers, ctx));
260
+ });
257
261
  },
258
262
  region: async ({
259
263
  postalCode,
@@ -286,7 +290,7 @@ const VtexCommerce = ({
286
290
  cookie: ctx.headers.cookie
287
291
  },
288
292
  body: '{}'
289
- }, headers => setCheckoutOrderFormOwnershipCookie(headers, ctx));
293
+ });
290
294
  },
291
295
  subscribeToNewsletter: data => {
292
296
  return fetchAPI(`${base}/api/dataentities/NL/documents/`, {
@@ -1016,55 +1020,31 @@ function getPropertyId(item) {
1016
1020
  }
1017
1021
 
1018
1022
  const shouldUpdateShippingData = (orderForm, session) => {
1019
- var _orderForm$shippingDa;
1023
+ var _orderForm$shippingDa, _orderForm$shippingDa2;
1020
1024
  if (!hasSessionPostalCodeOrGeoCoordinates(session)) {
1021
1025
  return {
1022
1026
  updateShipping: false,
1023
1027
  addressChanged: false
1024
1028
  };
1025
1029
  }
1026
- const selectedAddress = (_orderForm$shippingDa = orderForm.shippingData) == null ? void 0 : _orderForm$shippingDa.selectedAddresses[0];
1027
- if (checkPostalCode(selectedAddress, session.postalCode)) {
1028
- return {
1029
- updateShipping: true,
1030
- addressChanged: true
1031
- };
1032
- }
1033
- if (checkGeoCoordinates(selectedAddress, session.geoCoordinates, session.postalCode)) {
1030
+ if (!hasItems(orderForm)) {
1034
1031
  return {
1035
- updateShipping: true,
1036
- addressChanged: true
1032
+ updateShipping: false,
1033
+ addressChanged: false
1037
1034
  };
1038
1035
  }
1039
- 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)) {
1040
1038
  return {
1041
1039
  updateShipping: true,
1042
1040
  addressChanged: true
1043
1041
  };
1044
1042
  }
1045
- if (!hasItems(orderForm)) {
1046
- return {
1047
- updateShipping: false,
1048
- addressChanged: false
1049
- };
1050
- }
1051
1043
  // The logisticsInfo will always exist if there´s at least one item inside the cart
1052
1044
  const {
1053
1045
  logisticsInfo
1054
1046
  } = orderForm.shippingData;
1055
- if (shouldUpdateDeliveryChannel(logisticsInfo, session)) {
1056
- return {
1057
- updateShipping: true,
1058
- addressChanged: false
1059
- };
1060
- }
1061
- if (shouldUpdateDeliveryMethod(logisticsInfo, session)) {
1062
- return {
1063
- updateShipping: true,
1064
- addressChanged: false
1065
- };
1066
- }
1067
- if (shouldUpdateDeliveryWindow(logisticsInfo, session)) {
1047
+ if (shouldUpdateDeliveryInfo(logisticsInfo, session)) {
1068
1048
  return {
1069
1049
  updateShipping: true,
1070
1050
  addressChanged: false
@@ -1077,15 +1057,16 @@ const shouldUpdateShippingData = (orderForm, session) => {
1077
1057
  };
1078
1058
  // Validate if theres any postal Code or GeoCoordinates set at the session
1079
1059
  const hasSessionPostalCodeOrGeoCoordinates = session => {
1080
- 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);
1081
1062
  };
1082
1063
  // Validate if theres a difference between the session postal code and orderForm postal code
1083
1064
  const checkPostalCode = (address, postalCode) => {
1084
1065
  return typeof postalCode === 'string' && (address == null ? void 0 : address.postalCode) !== postalCode;
1085
1066
  };
1086
1067
  // Validate if theres a difference between the session geoCoords and orderForm geoCoords
1087
- const checkGeoCoordinates = (address, geoCoordinates, postalCode) => {
1088
- 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));
1089
1070
  };
1090
1071
  const checkAddressType = (address, addressType) => {
1091
1072
  return typeof addressType === 'string' && (address == null ? void 0 : address.addressType) !== addressType;
@@ -1094,67 +1075,29 @@ const checkAddressType = (address, addressType) => {
1094
1075
  const hasItems = orderForm => {
1095
1076
  return orderForm.items.length !== 0;
1096
1077
  };
1097
- // Validate if the deliveryChannel from the session is different from the selected delivery channel
1098
- // and if so needs to validate if the deliveryChannel for the session is available inside the slas for the item
1099
- const shouldUpdateDeliveryChannel = (logisticsInfo, session) => {
1100
- var _session$deliveryMode;
1101
- if (!(session != null && (_session$deliveryMode = session.deliveryMode) != null && _session$deliveryMode.deliveryChannel)) {
1102
- return false;
1103
- }
1104
- const {
1105
- deliveryChannel
1106
- } = session.deliveryMode;
1107
- for (const item of logisticsInfo) {
1108
- if (item.selectedDeliveryChannel !== deliveryChannel) {
1109
- const matchingSla = item.slas.find(sla => sla.deliveryChannel === deliveryChannel);
1110
- if (matchingSla) {
1111
- return true;
1112
- }
1113
- }
1114
- }
1115
- return false;
1116
- };
1117
- // Validate if the deliveryMethod from the session is different from the selectedSLA
1118
- // and if so needs to validate if the deliveryMethod for the session is available inside the slas for the item
1119
- const shouldUpdateDeliveryMethod = (logisticsInfo, session) => {
1120
- var _session$deliveryMode2;
1121
- if (!(session != null && (_session$deliveryMode2 = session.deliveryMode) != null && _session$deliveryMode2.deliveryMethod)) {
1122
- return false;
1123
- }
1124
- const {
1125
- deliveryMethod
1126
- } = session.deliveryMode;
1127
- for (const item of logisticsInfo) {
1128
- if (item.selectedSla !== deliveryMethod) {
1129
- const matchingSla = item.slas.find(sla => sla.id === deliveryMethod);
1130
- if (matchingSla) {
1131
- return true;
1132
- }
1133
- }
1134
- }
1135
- return false;
1136
- };
1137
- // Validate if the deliveryWindow from the session is different from the deliveryWindow of the SLA
1138
- // and if so needs to validate if the deliveryWindow for the session is available inside the availableDeliveryWindows for the item
1139
- const shouldUpdateDeliveryWindow = (logisticsInfo, session) => {
1140
- var _session$deliveryMode3, _session$deliveryMode4, _session$deliveryMode5, _session$deliveryMode6;
1141
- 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)) {
1142
- return false;
1143
- }
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;
1144
1082
  const {
1145
1083
  startDate,
1146
1084
  endDate
1147
- } = session.deliveryMode.deliveryWindow;
1148
- for (const item of logisticsInfo) {
1149
- for (const sla of item.slas) {
1150
- var _sla$availableDeliver;
1151
- const matchingWindow = (_sla$availableDeliver = sla.availableDeliveryWindows) == null ? void 0 : _sla$availableDeliver.some(window => window.startDateUtc === startDate && window.endDateUtc === endDate);
1152
- 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) {
1153
1096
  return true;
1154
1097
  }
1155
- }
1156
- }
1157
- 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
+ });
1158
1101
  };
1159
1102
 
1160
1103
  const getAddressOrderForm = (orderForm, session, addressChanged) => {