@faststore/api 1.12.45 → 1.12.47
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 +32 -69
- 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 +32 -69
- 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 +30 -30
- package/src/platforms/vtex/clients/fetch.ts +9 -1
- package/src/platforms/vtex/index.ts +1 -0
- package/src/platforms/vtex/resolvers/validateCart.ts +10 -33
- 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 = "1.12.
|
|
25
|
+
var version = "1.12.46";
|
|
26
26
|
var license = "MIT";
|
|
27
27
|
var main = "dist/index.js";
|
|
28
28
|
var typings = "dist/index.d.ts";
|
|
@@ -92,7 +92,7 @@ var packageJson = {
|
|
|
92
92
|
};
|
|
93
93
|
|
|
94
94
|
const USER_AGENT = `${packageJson.name}@${packageJson.version}`;
|
|
95
|
-
const fetchAPI = async (info, init) => {
|
|
95
|
+
const fetchAPI = async (info, init, getHeaders) => {
|
|
96
96
|
const response = await fetch(info, { ...init,
|
|
97
97
|
headers: { ...(init == null ? void 0 : init.headers),
|
|
98
98
|
'User-Agent': USER_AGENT
|
|
@@ -100,6 +100,10 @@ const fetchAPI = async (info, init) => {
|
|
|
100
100
|
});
|
|
101
101
|
|
|
102
102
|
if (response.ok) {
|
|
103
|
+
if (getHeaders) {
|
|
104
|
+
getHeaders(response.headers);
|
|
105
|
+
}
|
|
106
|
+
|
|
103
107
|
return response.status !== 204 ? response.json() : undefined;
|
|
104
108
|
}
|
|
105
109
|
|
|
@@ -108,18 +112,10 @@ const fetchAPI = async (info, init) => {
|
|
|
108
112
|
throw new Error(text);
|
|
109
113
|
};
|
|
110
114
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
if (parts.length === 2) {
|
|
116
|
-
var _parts$pop$split$shif, _parts$pop;
|
|
117
|
-
|
|
118
|
-
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 : '';
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
return '';
|
|
122
|
-
};
|
|
115
|
+
function getCookieByName(cookiename, source) {
|
|
116
|
+
var cookiestring = RegExp(cookiename + '=[^;]+').exec(source);
|
|
117
|
+
return decodeURIComponent(!!cookiestring ? cookiestring.toString().replace(/^[^=]+./, '') : '');
|
|
118
|
+
}
|
|
123
119
|
|
|
124
120
|
const BASE_INIT = {
|
|
125
121
|
method: 'POST',
|
|
@@ -127,6 +123,15 @@ const BASE_INIT = {
|
|
|
127
123
|
'content-type': 'application/json'
|
|
128
124
|
}
|
|
129
125
|
};
|
|
126
|
+
|
|
127
|
+
const setCheckoutOrderFormOwnershipCookie = (headers, ctx) => {
|
|
128
|
+
if (headers) {
|
|
129
|
+
var _headers$get;
|
|
130
|
+
|
|
131
|
+
ctx.storage.cookies = `CheckoutOrderFormOwnership=${getCookieByName('CheckoutOrderFormOwnership', (_headers$get = headers.get('set-cookie')) != null ? _headers$get : '')}`;
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
|
|
130
135
|
const VtexCommerce = ({
|
|
131
136
|
account,
|
|
132
137
|
environment
|
|
@@ -183,7 +188,7 @@ const VtexCommerce = ({
|
|
|
183
188
|
|
|
184
189
|
return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}/attachments/shippingData`, { ...BASE_INIT,
|
|
185
190
|
body: JSON.stringify(body)
|
|
186
|
-
});
|
|
191
|
+
}, headers => setCheckoutOrderFormOwnershipCookie(headers, ctx));
|
|
187
192
|
},
|
|
188
193
|
orderForm: ({
|
|
189
194
|
id,
|
|
@@ -197,7 +202,7 @@ const VtexCommerce = ({
|
|
|
197
202
|
refreshOutdatedData: refreshOutdatedData.toString(),
|
|
198
203
|
sc: salesChannel
|
|
199
204
|
});
|
|
200
|
-
return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}?${params.toString()}`, BASE_INIT);
|
|
205
|
+
return fetchAPI(`${base}/api/checkout/pub/orderForm/${id}?${params.toString()}`, BASE_INIT, headers => setCheckoutOrderFormOwnershipCookie(headers, ctx));
|
|
201
206
|
},
|
|
202
207
|
updateOrderFormItems: ({
|
|
203
208
|
id,
|
|
@@ -216,7 +221,7 @@ const VtexCommerce = ({
|
|
|
216
221
|
noSplitItem: !shouldSplitItem
|
|
217
222
|
}),
|
|
218
223
|
method: 'PATCH'
|
|
219
|
-
});
|
|
224
|
+
}, headers => setCheckoutOrderFormOwnershipCookie(headers, ctx));
|
|
220
225
|
},
|
|
221
226
|
setCustomData: ({
|
|
222
227
|
id,
|
|
@@ -229,7 +234,7 @@ const VtexCommerce = ({
|
|
|
229
234
|
value
|
|
230
235
|
}),
|
|
231
236
|
method: 'PUT'
|
|
232
|
-
});
|
|
237
|
+
}, headers => setCheckoutOrderFormOwnershipCookie(headers, ctx));
|
|
233
238
|
},
|
|
234
239
|
region: async ({
|
|
235
240
|
postalCode,
|
|
@@ -248,36 +253,14 @@ const VtexCommerce = ({
|
|
|
248
253
|
session: search => {
|
|
249
254
|
const params = new URLSearchParams(search);
|
|
250
255
|
params.set('items', 'profile.id,profile.email,profile.firstName,profile.lastName,store.channel,store.countryCode,store.cultureInfo,store.currencyCode,store.currencySymbol');
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
// cookie set
|
|
254
|
-
return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
|
|
255
|
-
method: 'GET',
|
|
256
|
-
headers: {
|
|
257
|
-
'content-type': 'application/json',
|
|
258
|
-
cookie: ctx.headers.cookie
|
|
259
|
-
}
|
|
260
|
-
});
|
|
261
|
-
} else {
|
|
262
|
-
// cookie unset -> create session
|
|
263
|
-
return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
|
|
264
|
-
method: 'POST',
|
|
265
|
-
headers: {
|
|
266
|
-
'content-type': 'application/json',
|
|
267
|
-
cookie: ctx.headers.cookie
|
|
268
|
-
},
|
|
269
|
-
body: '{}'
|
|
270
|
-
});
|
|
271
|
-
}
|
|
272
|
-
},
|
|
273
|
-
getSessionOrder: () => {
|
|
274
|
-
return fetchAPI(`${base}/api/sessions?items=checkout.orderFormId`, {
|
|
275
|
-
method: 'GET',
|
|
256
|
+
return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
|
|
257
|
+
method: 'POST',
|
|
276
258
|
headers: {
|
|
277
259
|
'content-type': 'application/json',
|
|
278
260
|
cookie: ctx.headers.cookie
|
|
279
|
-
}
|
|
280
|
-
|
|
261
|
+
},
|
|
262
|
+
body: '{}'
|
|
263
|
+
}, headers => setCheckoutOrderFormOwnershipCookie(headers, ctx));
|
|
281
264
|
},
|
|
282
265
|
subscribeToNewsletter: data => {
|
|
283
266
|
return fetchAPI(`${base}/api/dataentities/NL/documents/`, { ...BASE_INIT,
|
|
@@ -1243,22 +1226,7 @@ const isOrderFormStale = form => {
|
|
|
1243
1226
|
|
|
1244
1227
|
const newEtag = getOrderFormEtag(form);
|
|
1245
1228
|
return newEtag !== oldEtag;
|
|
1246
|
-
};
|
|
1247
|
-
|
|
1248
|
-
async function getOrderNumberFromSession(headers = {}, commerce) {
|
|
1249
|
-
const cookieSession = getCookie('vtex_session', headers.cookie);
|
|
1250
|
-
|
|
1251
|
-
if (cookieSession) {
|
|
1252
|
-
var _namespaces$checkout, _namespaces$checkout$;
|
|
1253
|
-
|
|
1254
|
-
const {
|
|
1255
|
-
namespaces
|
|
1256
|
-
} = await commerce.getSessionOrder();
|
|
1257
|
-
return (_namespaces$checkout = namespaces.checkout) == null ? void 0 : (_namespaces$checkout$ = _namespaces$checkout.orderFormId) == null ? void 0 : _namespaces$checkout$.value;
|
|
1258
|
-
}
|
|
1259
|
-
|
|
1260
|
-
return;
|
|
1261
|
-
} // Returns the regionalized orderForm
|
|
1229
|
+
}; // Returns the regionalized orderForm
|
|
1262
1230
|
|
|
1263
1231
|
|
|
1264
1232
|
const getOrderForm = async (id, session, {
|
|
@@ -1314,10 +1282,8 @@ const validateCart = async (_, {
|
|
|
1314
1282
|
},
|
|
1315
1283
|
session
|
|
1316
1284
|
}, ctx) => {
|
|
1317
|
-
var _ref;
|
|
1318
|
-
|
|
1319
1285
|
const {
|
|
1320
|
-
orderNumber
|
|
1286
|
+
orderNumber,
|
|
1321
1287
|
acceptedOffer,
|
|
1322
1288
|
shouldSplitItem
|
|
1323
1289
|
} = order;
|
|
@@ -1327,8 +1293,7 @@ const validateCart = async (_, {
|
|
|
1327
1293
|
},
|
|
1328
1294
|
loaders: {
|
|
1329
1295
|
skuLoader
|
|
1330
|
-
}
|
|
1331
|
-
headers
|
|
1296
|
+
}
|
|
1332
1297
|
} = ctx;
|
|
1333
1298
|
const channel = session == null ? void 0 : session.channel;
|
|
1334
1299
|
const locale = session == null ? void 0 : session.locale;
|
|
@@ -1339,10 +1304,8 @@ const validateCart = async (_, {
|
|
|
1339
1304
|
|
|
1340
1305
|
if (locale) {
|
|
1341
1306
|
mutateLocaleContext(ctx, locale);
|
|
1342
|
-
}
|
|
1307
|
+
} // Step1: Get OrderForm from VTEX Commerce
|
|
1343
1308
|
|
|
1344
|
-
const orderNumberFromSession = await getOrderNumberFromSession(headers, commerce);
|
|
1345
|
-
const orderNumber = (_ref = orderNumberFromSession != null ? orderNumberFromSession : orderNumberFromCart) != null ? _ref : ''; // Step1: Get OrderForm from VTEX Commerce
|
|
1346
1309
|
|
|
1347
1310
|
const orderForm = await getOrderForm(orderNumber, session, ctx); // Step1.5: Check if another system changed the orderForm with this orderNumber
|
|
1348
1311
|
// If so, this means the user interacted with this cart elsewhere and expects
|