@faststore/api 2.1.83 → 2.1.98

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.82";
21
+ var version = "2.1.90";
22
22
  var license = "MIT";
23
23
  var main = "dist/index.js";
24
24
  var typings = "dist/index.d.ts";
@@ -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) => {
94
+ const fetchAPI = async (info, init, getHeaders) => {
95
95
  const response = await fetch(info, {
96
96
  ...init,
97
97
  headers: {
@@ -100,6 +100,9 @@ const fetchAPI = async (info, init) => {
100
100
  }
101
101
  });
102
102
  if (response.ok) {
103
+ if (getHeaders) {
104
+ getHeaders(response.headers);
105
+ }
103
106
  return response.status !== 204 ? response.json() : undefined;
104
107
  }
105
108
  console.error(info, init, response);
@@ -107,15 +110,10 @@ const fetchAPI = async (info, init) => {
107
110
  throw new Error(text);
108
111
  };
109
112
 
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
- };
113
+ function getCookieByName(cookiename, source) {
114
+ var cookiestring = RegExp(cookiename + '=[^;]+').exec(source);
115
+ return decodeURIComponent(!!cookiestring ? cookiestring.toString().replace(/^[^=]+./, '') : '');
116
+ }
119
117
 
120
118
  const BASE_INIT = {
121
119
  method: 'POST',
@@ -123,6 +121,12 @@ const BASE_INIT = {
123
121
  'content-type': 'application/json'
124
122
  }
125
123
  };
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
+ };
126
130
  const VtexCommerce = ({
127
131
  account,
128
132
  environment,
@@ -197,7 +201,7 @@ const VtexCommerce = ({
197
201
  'content-type': 'application/json',
198
202
  cookie: ctx.headers.cookie
199
203
  }
200
- });
204
+ }, headers => setCheckoutOrderFormOwnershipCookie(headers, ctx));
201
205
  },
202
206
  orderForm: ({
203
207
  id,
@@ -211,7 +215,7 @@ const VtexCommerce = ({
211
215
  refreshOutdatedData: refreshOutdatedData.toString(),
212
216
  sc: salesChannel
213
217
  });
214
- return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}?${params.toString()}`, BASE_INIT);
218
+ return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}?${params.toString()}`, BASE_INIT, headers => setCheckoutOrderFormOwnershipCookie(headers, ctx));
215
219
  },
216
220
  updateOrderFormItems: ({
217
221
  id,
@@ -231,7 +235,7 @@ const VtexCommerce = ({
231
235
  noSplitItem: !shouldSplitItem
232
236
  }),
233
237
  method: 'PATCH'
234
- });
238
+ }, headers => setCheckoutOrderFormOwnershipCookie(headers, ctx));
235
239
  },
236
240
  setCustomData: ({
237
241
  id,
@@ -245,7 +249,7 @@ const VtexCommerce = ({
245
249
  value
246
250
  }),
247
251
  method: 'PUT'
248
- });
252
+ }, headers => setCheckoutOrderFormOwnershipCookie(headers, ctx));
249
253
  },
250
254
  region: async ({
251
255
  postalCode,
@@ -271,35 +275,14 @@ const VtexCommerce = ({
271
275
  session: search => {
272
276
  const params = new URLSearchParams(search);
273
277
  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',
278
+ return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
279
+ method: 'POST',
298
280
  headers: {
299
281
  'content-type': 'application/json',
300
282
  cookie: ctx.headers.cookie
301
- }
302
- });
283
+ },
284
+ body: '{}'
285
+ }, headers => setCheckoutOrderFormOwnershipCookie(headers, ctx));
303
286
  },
304
287
  subscribeToNewsletter: data => {
305
288
  return fetchAPI(`${base}/api/dataentities/NL/documents/`, {
@@ -1363,17 +1346,6 @@ const isOrderFormStale = form => {
1363
1346
  const newEtag = getOrderFormEtag(form);
1364
1347
  return newEtag !== oldEtag;
1365
1348
  };
1366
- async function getOrderNumberFromSession(headers = {}, commerce) {
1367
- const cookieSession = getCookie('vtex_session', headers.cookie);
1368
- if (cookieSession) {
1369
- var _namespaces$public$or, _namespaces$public, _namespaces$public$or2;
1370
- const {
1371
- namespaces
1372
- } = await commerce.getSessionOrder();
1373
- 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;
1374
- }
1375
- return;
1376
- }
1377
1349
  // Returns the regionalized orderForm
1378
1350
  const getOrderForm = async (id, {
1379
1351
  clients: {
@@ -1445,9 +1417,8 @@ const validateCart = async (_, {
1445
1417
  },
1446
1418
  session
1447
1419
  }, ctx) => {
1448
- var _ref;
1449
1420
  const {
1450
- orderNumber: orderNumberFromCart,
1421
+ orderNumber,
1451
1422
  acceptedOffer,
1452
1423
  shouldSplitItem
1453
1424
  } = order;
@@ -1457,8 +1428,7 @@ const validateCart = async (_, {
1457
1428
  },
1458
1429
  loaders: {
1459
1430
  skuLoader
1460
- },
1461
- headers
1431
+ }
1462
1432
  } = ctx;
1463
1433
  const channel = session == null ? void 0 : session.channel;
1464
1434
  const locale = session == null ? void 0 : session.locale;
@@ -1468,19 +1438,9 @@ const validateCart = async (_, {
1468
1438
  if (locale) {
1469
1439
  mutateLocaleContext(ctx, locale);
1470
1440
  }
1471
- const orderNumberFromSession = await getOrderNumberFromSession(headers, commerce);
1472
- const orderNumber = (_ref = orderNumberFromSession != null ? orderNumberFromSession : orderNumberFromCart) != null ? _ref : '';
1473
1441
  // Step1: Get OrderForm from VTEX Commerce
1474
1442
  const orderForm = await getOrderForm(orderNumber, ctx);
1475
- // Step1.1: Checks if the orderForm id has changed. There are three cases for this:
1476
- // Social Selling: the vtex_session cookie contains a new orderForm id with Social Selling data
1477
- // 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.
1478
- // New session: a new user enters the website and has no orderForm attributed to it (has no relation to the vtex_session cookie).
1479
- // In all cases, the origin orderForm should replace the copy that's in the browser
1480
- if (orderForm.orderFormId != orderNumberFromCart) {
1481
- return orderFormToCart(orderForm, skuLoader);
1482
- }
1483
- // Step1.2: Check if another system changed the orderForm with this orderNumber
1443
+ // Step1.5: Check if another system changed the orderForm with this orderNumber
1484
1444
  // If so, this means the user interacted with this cart elsewhere and expects
1485
1445
  // to see this new cart state instead of what's stored on the user's browser.
1486
1446
  const isStale = isOrderFormStale(orderForm);