@faststore/api 1.12.36 → 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.
- package/LICENSE +21 -0
- package/dist/api.cjs.development.js +86 -25
- 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 +86 -25
- package/dist/api.esm.js.map +1 -1
- package/dist/platforms/vtex/clients/commerce/index.d.ts +3 -2
- package/dist/platforms/vtex/clients/commerce/types/Session.d.ts +4 -0
- package/dist/platforms/vtex/clients/index.d.ts +3 -2
- package/dist/platforms/vtex/resolvers/validateCart.d.ts +1 -1
- package/dist/platforms/vtex/utils/getCookies.d.ts +1 -0
- package/package.json +4 -3
- package/src/platforms/vtex/clients/commerce/index.ts +45 -18
- package/src/platforms/vtex/clients/commerce/types/Session.ts +5 -0
- package/src/platforms/vtex/resolvers/validateCart.ts +109 -82
- package/src/platforms/vtex/utils/getCookies.ts +8 -0
package/dist/api.esm.js
CHANGED
|
@@ -20,6 +20,19 @@ const fetchAPI = async (info, init) => {
|
|
|
20
20
|
throw new Error(text);
|
|
21
21
|
};
|
|
22
22
|
|
|
23
|
+
const getCookie = (name, cookie) => {
|
|
24
|
+
const value = `; ${cookie}`;
|
|
25
|
+
const parts = value.split(`; ${name}=`);
|
|
26
|
+
|
|
27
|
+
if (parts.length === 2) {
|
|
28
|
+
var _parts$pop$split$shif, _parts$pop;
|
|
29
|
+
|
|
30
|
+
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 : '';
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return '';
|
|
34
|
+
};
|
|
35
|
+
|
|
23
36
|
const BASE_INIT = {
|
|
24
37
|
method: 'POST',
|
|
25
38
|
headers: {
|
|
@@ -137,13 +150,35 @@ const VtexCommerce = ({
|
|
|
137
150
|
session: search => {
|
|
138
151
|
const params = new URLSearchParams(search);
|
|
139
152
|
params.set('items', 'profile.id,profile.email,profile.firstName,profile.lastName,store.channel,store.countryCode,store.cultureInfo,store.currencyCode,store.currencySymbol');
|
|
140
|
-
|
|
141
|
-
|
|
153
|
+
|
|
154
|
+
if (getCookie('vtex_session', ctx.headers.cookie)) {
|
|
155
|
+
// cookie set
|
|
156
|
+
return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
|
|
157
|
+
method: 'GET',
|
|
158
|
+
headers: {
|
|
159
|
+
'content-type': 'application/json',
|
|
160
|
+
cookie: ctx.headers.cookie
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
} else {
|
|
164
|
+
// cookie unset -> create session
|
|
165
|
+
return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
|
|
166
|
+
method: 'POST',
|
|
167
|
+
headers: {
|
|
168
|
+
'content-type': 'application/json',
|
|
169
|
+
cookie: ctx.headers.cookie
|
|
170
|
+
},
|
|
171
|
+
body: '{}'
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
},
|
|
175
|
+
getSessionOrder: () => {
|
|
176
|
+
return fetchAPI(`${base}/api/sessions?items=checkout.orderFormId`, {
|
|
177
|
+
method: 'GET',
|
|
142
178
|
headers: {
|
|
143
179
|
'content-type': 'application/json',
|
|
144
180
|
cookie: ctx.headers.cookie
|
|
145
|
-
}
|
|
146
|
-
body: '{}'
|
|
181
|
+
}
|
|
147
182
|
});
|
|
148
183
|
},
|
|
149
184
|
subscribeToNewsletter: data => {
|
|
@@ -940,6 +975,13 @@ const subscribeToNewsletter = async (_, {
|
|
|
940
975
|
};
|
|
941
976
|
};
|
|
942
977
|
|
|
978
|
+
const mutateChannelContext = (ctx, channelString) => {
|
|
979
|
+
ctx.storage.channel = ChannelMarshal.parse(channelString);
|
|
980
|
+
};
|
|
981
|
+
const mutateLocaleContext = (ctx, locale) => {
|
|
982
|
+
ctx.storage.locale = locale;
|
|
983
|
+
};
|
|
984
|
+
|
|
943
985
|
const md5 = payload => crypto.createHash('md5').update(payload).digest('hex');
|
|
944
986
|
|
|
945
987
|
const VALUE_REFERENCES = {
|
|
@@ -1103,7 +1145,22 @@ const isOrderFormStale = form => {
|
|
|
1103
1145
|
|
|
1104
1146
|
const newEtag = getOrderFormEtag(form);
|
|
1105
1147
|
return newEtag !== oldEtag;
|
|
1106
|
-
};
|
|
1148
|
+
};
|
|
1149
|
+
|
|
1150
|
+
async function getOrderNumberFromSession(headers = {}, commerce) {
|
|
1151
|
+
const cookieSession = getCookie('vtex_session', headers.cookie);
|
|
1152
|
+
|
|
1153
|
+
if (cookieSession) {
|
|
1154
|
+
var _namespaces$checkout, _namespaces$checkout$;
|
|
1155
|
+
|
|
1156
|
+
const {
|
|
1157
|
+
namespaces
|
|
1158
|
+
} = await commerce.getSessionOrder();
|
|
1159
|
+
return (_namespaces$checkout = namespaces.checkout) == null ? void 0 : (_namespaces$checkout$ = _namespaces$checkout.orderFormId) == null ? void 0 : _namespaces$checkout$.value;
|
|
1160
|
+
}
|
|
1161
|
+
|
|
1162
|
+
return;
|
|
1163
|
+
} // Returns the regionalized orderForm
|
|
1107
1164
|
|
|
1108
1165
|
|
|
1109
1166
|
const getOrderForm = async (id, session, {
|
|
@@ -1159,11 +1216,10 @@ const validateCart = async (_, {
|
|
|
1159
1216
|
},
|
|
1160
1217
|
session
|
|
1161
1218
|
}, ctx) => {
|
|
1219
|
+
var _ref;
|
|
1220
|
+
|
|
1162
1221
|
const {
|
|
1163
|
-
|
|
1164
|
-
} = ctx.storage.flags;
|
|
1165
|
-
const {
|
|
1166
|
-
orderNumber,
|
|
1222
|
+
orderNumber: orderNumberFromCart,
|
|
1167
1223
|
acceptedOffer
|
|
1168
1224
|
} = order;
|
|
1169
1225
|
const {
|
|
@@ -1172,20 +1228,32 @@ const validateCart = async (_, {
|
|
|
1172
1228
|
},
|
|
1173
1229
|
loaders: {
|
|
1174
1230
|
skuLoader
|
|
1175
|
-
}
|
|
1176
|
-
|
|
1231
|
+
},
|
|
1232
|
+
headers
|
|
1233
|
+
} = ctx;
|
|
1234
|
+
const channel = session == null ? void 0 : session.channel;
|
|
1235
|
+
const locale = session == null ? void 0 : session.locale;
|
|
1236
|
+
|
|
1237
|
+
if (channel) {
|
|
1238
|
+
mutateChannelContext(ctx, channel);
|
|
1239
|
+
}
|
|
1240
|
+
|
|
1241
|
+
if (locale) {
|
|
1242
|
+
mutateLocaleContext(ctx, locale);
|
|
1243
|
+
}
|
|
1244
|
+
|
|
1245
|
+
const orderNumberFromSession = await getOrderNumberFromSession(headers, commerce);
|
|
1246
|
+
const orderNumber = (_ref = orderNumberFromSession != null ? orderNumberFromSession : orderNumberFromCart) != null ? _ref : ''; // Step1: Get OrderForm from VTEX Commerce
|
|
1177
1247
|
|
|
1178
1248
|
const orderForm = await getOrderForm(orderNumber, session, ctx); // Step1.5: Check if another system changed the orderForm with this orderNumber
|
|
1179
1249
|
// If so, this means the user interacted with this cart elsewhere and expects
|
|
1180
1250
|
// to see this new cart state instead of what's stored on the user's browser.
|
|
1181
1251
|
|
|
1182
|
-
|
|
1183
|
-
const isStale = isOrderFormStale(orderForm);
|
|
1252
|
+
const isStale = isOrderFormStale(orderForm);
|
|
1184
1253
|
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
}
|
|
1254
|
+
if (isStale && orderNumber) {
|
|
1255
|
+
const newOrderForm = await setOrderFormEtag(orderForm, commerce).then(joinItems);
|
|
1256
|
+
return orderFormToCart(newOrderForm, skuLoader);
|
|
1189
1257
|
} // Step2: Process items from both browser and checkout so they have the same shape
|
|
1190
1258
|
|
|
1191
1259
|
|
|
@@ -1238,7 +1306,7 @@ const validateCart = async (_, {
|
|
|
1238
1306
|
id: orderForm.orderFormId,
|
|
1239
1307
|
orderItems: changes
|
|
1240
1308
|
}) // update orderForm etag so we know last time we touched this orderForm
|
|
1241
|
-
.then(form =>
|
|
1309
|
+
.then(form => setOrderFormEtag(form, commerce)).then(joinItems); // Step5: If no changes detected before/after updating orderForm, the order is validated
|
|
1242
1310
|
|
|
1243
1311
|
if (equals(order, updatedOrderForm)) {
|
|
1244
1312
|
return null;
|
|
@@ -1646,13 +1714,6 @@ const StorePropertyValue = {
|
|
|
1646
1714
|
}) => valueReference
|
|
1647
1715
|
};
|
|
1648
1716
|
|
|
1649
|
-
const mutateChannelContext = (ctx, channelString) => {
|
|
1650
|
-
ctx.storage.channel = ChannelMarshal.parse(channelString);
|
|
1651
|
-
};
|
|
1652
|
-
const mutateLocaleContext = (ctx, locale) => {
|
|
1653
|
-
ctx.storage.locale = locale;
|
|
1654
|
-
};
|
|
1655
|
-
|
|
1656
1717
|
const SORT_MAP = {
|
|
1657
1718
|
price_desc: 'price:desc',
|
|
1658
1719
|
price_asc: 'price:asc',
|