@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.cjs.development.js +27 -67
- package/dist/api.cjs.development.js.map +1 -1
- package/dist/api.cjs.production.min.js +1 -1
- package/dist/api.cjs.production.min.js.map +1 -1
- package/dist/api.esm.js +27 -67
- package/dist/api.esm.js.map +1 -1
- package/dist/platforms/vtex/clients/commerce/index.d.ts +0 -1
- package/dist/platforms/vtex/clients/fetch.d.ts +1 -1
- package/dist/platforms/vtex/clients/index.d.ts +0 -1
- package/dist/platforms/vtex/index.d.ts +1 -0
- package/dist/utils/get-cookie-by-name.d.ts +1 -0
- package/package.json +2 -2
- package/src/platforms/vtex/clients/commerce/index.ts +27 -28
- package/src/platforms/vtex/clients/fetch.ts +9 -1
- package/src/platforms/vtex/index.ts +12 -11
- package/src/platforms/vtex/resolvers/validateCart.ts +5 -37
- package/src/utils/get-cookie-by-name.ts +6 -0
- package/dist/platforms/vtex/utils/getCookies.d.ts +0 -1
- package/src/platforms/vtex/utils/getCookies.ts +0 -8
|
@@ -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.
|
|
25
|
+
var version = "2.1.90";
|
|
26
26
|
var license = "MIT";
|
|
27
27
|
var main = "dist/index.js";
|
|
28
28
|
var typings = "dist/index.d.ts";
|
|
@@ -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) => {
|
|
98
|
+
const fetchAPI = async (info, init, getHeaders) => {
|
|
99
99
|
const response = await fetch(info, {
|
|
100
100
|
...init,
|
|
101
101
|
headers: {
|
|
@@ -104,6 +104,9 @@ const fetchAPI = async (info, init) => {
|
|
|
104
104
|
}
|
|
105
105
|
});
|
|
106
106
|
if (response.ok) {
|
|
107
|
+
if (getHeaders) {
|
|
108
|
+
getHeaders(response.headers);
|
|
109
|
+
}
|
|
107
110
|
return response.status !== 204 ? response.json() : undefined;
|
|
108
111
|
}
|
|
109
112
|
console.error(info, init, response);
|
|
@@ -111,15 +114,10 @@ const fetchAPI = async (info, init) => {
|
|
|
111
114
|
throw new Error(text);
|
|
112
115
|
};
|
|
113
116
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
var _parts$pop$split$shif, _parts$pop;
|
|
119
|
-
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 : '';
|
|
120
|
-
}
|
|
121
|
-
return '';
|
|
122
|
-
};
|
|
117
|
+
function getCookieByName(cookiename, source) {
|
|
118
|
+
var cookiestring = RegExp(cookiename + '=[^;]+').exec(source);
|
|
119
|
+
return decodeURIComponent(!!cookiestring ? cookiestring.toString().replace(/^[^=]+./, '') : '');
|
|
120
|
+
}
|
|
123
121
|
|
|
124
122
|
const BASE_INIT = {
|
|
125
123
|
method: 'POST',
|
|
@@ -127,6 +125,12 @@ const BASE_INIT = {
|
|
|
127
125
|
'content-type': 'application/json'
|
|
128
126
|
}
|
|
129
127
|
};
|
|
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
|
+
};
|
|
130
134
|
const VtexCommerce = ({
|
|
131
135
|
account,
|
|
132
136
|
environment,
|
|
@@ -201,7 +205,7 @@ const VtexCommerce = ({
|
|
|
201
205
|
'content-type': 'application/json',
|
|
202
206
|
cookie: ctx.headers.cookie
|
|
203
207
|
}
|
|
204
|
-
});
|
|
208
|
+
}, headers => setCheckoutOrderFormOwnershipCookie(headers, ctx));
|
|
205
209
|
},
|
|
206
210
|
orderForm: ({
|
|
207
211
|
id,
|
|
@@ -215,7 +219,7 @@ const VtexCommerce = ({
|
|
|
215
219
|
refreshOutdatedData: refreshOutdatedData.toString(),
|
|
216
220
|
sc: salesChannel
|
|
217
221
|
});
|
|
218
|
-
return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}?${params.toString()}`, BASE_INIT);
|
|
222
|
+
return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}?${params.toString()}`, BASE_INIT, headers => setCheckoutOrderFormOwnershipCookie(headers, ctx));
|
|
219
223
|
},
|
|
220
224
|
updateOrderFormItems: ({
|
|
221
225
|
id,
|
|
@@ -235,7 +239,7 @@ const VtexCommerce = ({
|
|
|
235
239
|
noSplitItem: !shouldSplitItem
|
|
236
240
|
}),
|
|
237
241
|
method: 'PATCH'
|
|
238
|
-
});
|
|
242
|
+
}, headers => setCheckoutOrderFormOwnershipCookie(headers, ctx));
|
|
239
243
|
},
|
|
240
244
|
setCustomData: ({
|
|
241
245
|
id,
|
|
@@ -249,7 +253,7 @@ const VtexCommerce = ({
|
|
|
249
253
|
value
|
|
250
254
|
}),
|
|
251
255
|
method: 'PUT'
|
|
252
|
-
});
|
|
256
|
+
}, headers => setCheckoutOrderFormOwnershipCookie(headers, ctx));
|
|
253
257
|
},
|
|
254
258
|
region: async ({
|
|
255
259
|
postalCode,
|
|
@@ -275,35 +279,14 @@ const VtexCommerce = ({
|
|
|
275
279
|
session: search => {
|
|
276
280
|
const params = new URLSearchParams(search);
|
|
277
281
|
params.set('items', 'profile.id,profile.email,profile.firstName,profile.lastName,store.channel,store.countryCode,store.cultureInfo,store.currencyCode,store.currencySymbol');
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
|
|
281
|
-
method: 'GET',
|
|
282
|
-
headers: {
|
|
283
|
-
'content-type': 'application/json',
|
|
284
|
-
cookie: ctx.headers.cookie
|
|
285
|
-
}
|
|
286
|
-
});
|
|
287
|
-
} else {
|
|
288
|
-
// cookie unset -> create session
|
|
289
|
-
return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
|
|
290
|
-
method: 'POST',
|
|
291
|
-
headers: {
|
|
292
|
-
'content-type': 'application/json',
|
|
293
|
-
cookie: ctx.headers.cookie
|
|
294
|
-
},
|
|
295
|
-
body: '{}'
|
|
296
|
-
});
|
|
297
|
-
}
|
|
298
|
-
},
|
|
299
|
-
getSessionOrder: () => {
|
|
300
|
-
return fetchAPI(`${base}/api/sessions?items=public.orderFormId`, {
|
|
301
|
-
method: 'GET',
|
|
282
|
+
return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
|
|
283
|
+
method: 'POST',
|
|
302
284
|
headers: {
|
|
303
285
|
'content-type': 'application/json',
|
|
304
286
|
cookie: ctx.headers.cookie
|
|
305
|
-
}
|
|
306
|
-
|
|
287
|
+
},
|
|
288
|
+
body: '{}'
|
|
289
|
+
}, headers => setCheckoutOrderFormOwnershipCookie(headers, ctx));
|
|
307
290
|
},
|
|
308
291
|
subscribeToNewsletter: data => {
|
|
309
292
|
return fetchAPI(`${base}/api/dataentities/NL/documents/`, {
|
|
@@ -1367,17 +1350,6 @@ const isOrderFormStale = form => {
|
|
|
1367
1350
|
const newEtag = getOrderFormEtag(form);
|
|
1368
1351
|
return newEtag !== oldEtag;
|
|
1369
1352
|
};
|
|
1370
|
-
async function getOrderNumberFromSession(headers = {}, commerce) {
|
|
1371
|
-
const cookieSession = getCookie('vtex_session', headers.cookie);
|
|
1372
|
-
if (cookieSession) {
|
|
1373
|
-
var _namespaces$public$or, _namespaces$public, _namespaces$public$or2;
|
|
1374
|
-
const {
|
|
1375
|
-
namespaces
|
|
1376
|
-
} = await commerce.getSessionOrder();
|
|
1377
|
-
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;
|
|
1378
|
-
}
|
|
1379
|
-
return;
|
|
1380
|
-
}
|
|
1381
1353
|
// Returns the regionalized orderForm
|
|
1382
1354
|
const getOrderForm = async (id, {
|
|
1383
1355
|
clients: {
|
|
@@ -1449,9 +1421,8 @@ const validateCart = async (_, {
|
|
|
1449
1421
|
},
|
|
1450
1422
|
session
|
|
1451
1423
|
}, ctx) => {
|
|
1452
|
-
var _ref;
|
|
1453
1424
|
const {
|
|
1454
|
-
orderNumber
|
|
1425
|
+
orderNumber,
|
|
1455
1426
|
acceptedOffer,
|
|
1456
1427
|
shouldSplitItem
|
|
1457
1428
|
} = order;
|
|
@@ -1461,8 +1432,7 @@ const validateCart = async (_, {
|
|
|
1461
1432
|
},
|
|
1462
1433
|
loaders: {
|
|
1463
1434
|
skuLoader
|
|
1464
|
-
}
|
|
1465
|
-
headers
|
|
1435
|
+
}
|
|
1466
1436
|
} = ctx;
|
|
1467
1437
|
const channel = session == null ? void 0 : session.channel;
|
|
1468
1438
|
const locale = session == null ? void 0 : session.locale;
|
|
@@ -1472,19 +1442,9 @@ const validateCart = async (_, {
|
|
|
1472
1442
|
if (locale) {
|
|
1473
1443
|
mutateLocaleContext(ctx, locale);
|
|
1474
1444
|
}
|
|
1475
|
-
const orderNumberFromSession = await getOrderNumberFromSession(headers, commerce);
|
|
1476
|
-
const orderNumber = (_ref = orderNumberFromSession != null ? orderNumberFromSession : orderNumberFromCart) != null ? _ref : '';
|
|
1477
1445
|
// Step1: Get OrderForm from VTEX Commerce
|
|
1478
1446
|
const orderForm = await getOrderForm(orderNumber, ctx);
|
|
1479
|
-
// Step1.
|
|
1480
|
-
// Social Selling: the vtex_session cookie contains a new orderForm id with Social Selling data
|
|
1481
|
-
// 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.
|
|
1482
|
-
// New session: a new user enters the website and has no orderForm attributed to it (has no relation to the vtex_session cookie).
|
|
1483
|
-
// In all cases, the origin orderForm should replace the copy that's in the browser
|
|
1484
|
-
if (orderForm.orderFormId != orderNumberFromCart) {
|
|
1485
|
-
return orderFormToCart(orderForm, skuLoader);
|
|
1486
|
-
}
|
|
1487
|
-
// Step1.2: Check if another system changed the orderForm with this orderNumber
|
|
1447
|
+
// Step1.5: Check if another system changed the orderForm with this orderNumber
|
|
1488
1448
|
// If so, this means the user interacted with this cart elsewhere and expects
|
|
1489
1449
|
// to see this new cart state instead of what's stored on the user's browser.
|
|
1490
1450
|
const isStale = isOrderFormStale(orderForm);
|