@faststore/api 1.12.37 → 1.12.38

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.
@@ -24,6 +24,19 @@ const fetchAPI = async (info, init) => {
24
24
  throw new Error(text);
25
25
  };
26
26
 
27
+ const getCookie = (name, cookie) => {
28
+ const value = `; ${cookie}`;
29
+ const parts = value.split(`; ${name}=`);
30
+
31
+ if (parts.length === 2) {
32
+ var _parts$pop$split$shif, _parts$pop;
33
+
34
+ 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 : '';
35
+ }
36
+
37
+ return '';
38
+ };
39
+
27
40
  const BASE_INIT = {
28
41
  method: 'POST',
29
42
  headers: {
@@ -141,13 +154,35 @@ const VtexCommerce = ({
141
154
  session: search => {
142
155
  const params = new URLSearchParams(search);
143
156
  params.set('items', 'profile.id,profile.email,profile.firstName,profile.lastName,store.channel,store.countryCode,store.cultureInfo,store.currencyCode,store.currencySymbol');
144
- return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
145
- method: 'POST',
157
+
158
+ if (getCookie('vtex_session', ctx.headers.cookie)) {
159
+ // cookie set
160
+ return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
161
+ method: 'GET',
162
+ headers: {
163
+ 'content-type': 'application/json',
164
+ cookie: ctx.headers.cookie
165
+ }
166
+ });
167
+ } else {
168
+ // cookie unset -> create session
169
+ return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
170
+ method: 'POST',
171
+ headers: {
172
+ 'content-type': 'application/json',
173
+ cookie: ctx.headers.cookie
174
+ },
175
+ body: '{}'
176
+ });
177
+ }
178
+ },
179
+ getSessionOrder: () => {
180
+ return fetchAPI(`${base}/api/sessions?items=checkout.orderFormId`, {
181
+ method: 'GET',
146
182
  headers: {
147
183
  'content-type': 'application/json',
148
184
  cookie: ctx.headers.cookie
149
- },
150
- body: '{}'
185
+ }
151
186
  });
152
187
  },
153
188
  subscribeToNewsletter: data => {
@@ -944,6 +979,13 @@ const subscribeToNewsletter = async (_, {
944
979
  };
945
980
  };
946
981
 
982
+ const mutateChannelContext = (ctx, channelString) => {
983
+ ctx.storage.channel = ChannelMarshal.parse(channelString);
984
+ };
985
+ const mutateLocaleContext = (ctx, locale) => {
986
+ ctx.storage.locale = locale;
987
+ };
988
+
947
989
  const md5 = payload => crypto.createHash('md5').update(payload).digest('hex');
948
990
 
949
991
  const VALUE_REFERENCES = {
@@ -961,13 +1003,6 @@ function getPropertyId(item) {
961
1003
  return md5(`${item.name}:${JSON.stringify(item.value)}:${item.valueReference}`);
962
1004
  }
963
1005
 
964
- const mutateChannelContext = (ctx, channelString) => {
965
- ctx.storage.channel = ChannelMarshal.parse(channelString);
966
- };
967
- const mutateLocaleContext = (ctx, locale) => {
968
- ctx.storage.locale = locale;
969
- };
970
-
971
1006
  const isAttachment = value => value.valueReference === VALUE_REFERENCES.attachment;
972
1007
 
973
1008
  const getId = item => {
@@ -1114,7 +1149,22 @@ const isOrderFormStale = form => {
1114
1149
 
1115
1150
  const newEtag = getOrderFormEtag(form);
1116
1151
  return newEtag !== oldEtag;
1117
- }; // Returns the regionalized orderForm
1152
+ };
1153
+
1154
+ async function getOrderNumberFromSession(headers = {}, commerce) {
1155
+ const cookieSession = getCookie('vtex_session', headers.cookie);
1156
+
1157
+ if (cookieSession) {
1158
+ var _namespaces$checkout, _namespaces$checkout$;
1159
+
1160
+ const {
1161
+ namespaces
1162
+ } = await commerce.getSessionOrder();
1163
+ return (_namespaces$checkout = namespaces.checkout) == null ? void 0 : (_namespaces$checkout$ = _namespaces$checkout.orderFormId) == null ? void 0 : _namespaces$checkout$.value;
1164
+ }
1165
+
1166
+ return;
1167
+ } // Returns the regionalized orderForm
1118
1168
 
1119
1169
 
1120
1170
  const getOrderForm = async (id, session, {
@@ -1170,11 +1220,10 @@ const validateCart = async (_, {
1170
1220
  },
1171
1221
  session
1172
1222
  }, ctx) => {
1223
+ var _ref;
1224
+
1173
1225
  const {
1174
- enableOrderFormSync
1175
- } = ctx.storage.flags;
1176
- const {
1177
- orderNumber,
1226
+ orderNumber: orderNumberFromCart,
1178
1227
  acceptedOffer
1179
1228
  } = order;
1180
1229
  const {
@@ -1183,7 +1232,8 @@ const validateCart = async (_, {
1183
1232
  },
1184
1233
  loaders: {
1185
1234
  skuLoader
1186
- }
1235
+ },
1236
+ headers
1187
1237
  } = ctx;
1188
1238
  const channel = session == null ? void 0 : session.channel;
1189
1239
  const locale = session == null ? void 0 : session.locale;
@@ -1194,20 +1244,20 @@ const validateCart = async (_, {
1194
1244
 
1195
1245
  if (locale) {
1196
1246
  mutateLocaleContext(ctx, locale);
1197
- } // Step1: Get OrderForm from VTEX Commerce
1247
+ }
1198
1248
 
1249
+ const orderNumberFromSession = await getOrderNumberFromSession(headers, commerce);
1250
+ const orderNumber = (_ref = orderNumberFromSession != null ? orderNumberFromSession : orderNumberFromCart) != null ? _ref : ''; // Step1: Get OrderForm from VTEX Commerce
1199
1251
 
1200
1252
  const orderForm = await getOrderForm(orderNumber, session, ctx); // Step1.5: Check if another system changed the orderForm with this orderNumber
1201
1253
  // If so, this means the user interacted with this cart elsewhere and expects
1202
1254
  // to see this new cart state instead of what's stored on the user's browser.
1203
1255
 
1204
- if (enableOrderFormSync === true) {
1205
- const isStale = isOrderFormStale(orderForm);
1256
+ const isStale = isOrderFormStale(orderForm);
1206
1257
 
1207
- if (isStale === true && orderNumber) {
1208
- const newOrderForm = await setOrderFormEtag(orderForm, commerce).then(joinItems);
1209
- return orderFormToCart(newOrderForm, skuLoader);
1210
- }
1258
+ if (isStale && orderNumber) {
1259
+ const newOrderForm = await setOrderFormEtag(orderForm, commerce).then(joinItems);
1260
+ return orderFormToCart(newOrderForm, skuLoader);
1211
1261
  } // Step2: Process items from both browser and checkout so they have the same shape
1212
1262
 
1213
1263
 
@@ -1260,7 +1310,7 @@ const validateCart = async (_, {
1260
1310
  id: orderForm.orderFormId,
1261
1311
  orderItems: changes
1262
1312
  }) // update orderForm etag so we know last time we touched this orderForm
1263
- .then(form => enableOrderFormSync ? setOrderFormEtag(form, commerce) : form).then(joinItems); // Step5: If no changes detected before/after updating orderForm, the order is validated
1313
+ .then(form => setOrderFormEtag(form, commerce)).then(joinItems); // Step5: If no changes detected before/after updating orderForm, the order is validated
1264
1314
 
1265
1315
  if (equals(order, updatedOrderForm)) {
1266
1316
  return null;