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