@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.
- package/dist/api.cjs.development.js +75 -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 +75 -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 +2 -2
- 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 +37 -20
- 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 = {
|
|
@@ -957,13 +999,6 @@ function getPropertyId(item) {
|
|
|
957
999
|
return md5(`${item.name}:${JSON.stringify(item.value)}:${item.valueReference}`);
|
|
958
1000
|
}
|
|
959
1001
|
|
|
960
|
-
const mutateChannelContext = (ctx, channelString) => {
|
|
961
|
-
ctx.storage.channel = ChannelMarshal.parse(channelString);
|
|
962
|
-
};
|
|
963
|
-
const mutateLocaleContext = (ctx, locale) => {
|
|
964
|
-
ctx.storage.locale = locale;
|
|
965
|
-
};
|
|
966
|
-
|
|
967
1002
|
const isAttachment = value => value.valueReference === VALUE_REFERENCES.attachment;
|
|
968
1003
|
|
|
969
1004
|
const getId = item => {
|
|
@@ -1110,7 +1145,22 @@ const isOrderFormStale = form => {
|
|
|
1110
1145
|
|
|
1111
1146
|
const newEtag = getOrderFormEtag(form);
|
|
1112
1147
|
return newEtag !== oldEtag;
|
|
1113
|
-
};
|
|
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
|
|
1114
1164
|
|
|
1115
1165
|
|
|
1116
1166
|
const getOrderForm = async (id, session, {
|
|
@@ -1166,11 +1216,10 @@ const validateCart = async (_, {
|
|
|
1166
1216
|
},
|
|
1167
1217
|
session
|
|
1168
1218
|
}, ctx) => {
|
|
1219
|
+
var _ref;
|
|
1220
|
+
|
|
1169
1221
|
const {
|
|
1170
|
-
|
|
1171
|
-
} = ctx.storage.flags;
|
|
1172
|
-
const {
|
|
1173
|
-
orderNumber,
|
|
1222
|
+
orderNumber: orderNumberFromCart,
|
|
1174
1223
|
acceptedOffer
|
|
1175
1224
|
} = order;
|
|
1176
1225
|
const {
|
|
@@ -1179,7 +1228,8 @@ const validateCart = async (_, {
|
|
|
1179
1228
|
},
|
|
1180
1229
|
loaders: {
|
|
1181
1230
|
skuLoader
|
|
1182
|
-
}
|
|
1231
|
+
},
|
|
1232
|
+
headers
|
|
1183
1233
|
} = ctx;
|
|
1184
1234
|
const channel = session == null ? void 0 : session.channel;
|
|
1185
1235
|
const locale = session == null ? void 0 : session.locale;
|
|
@@ -1190,20 +1240,20 @@ const validateCart = async (_, {
|
|
|
1190
1240
|
|
|
1191
1241
|
if (locale) {
|
|
1192
1242
|
mutateLocaleContext(ctx, locale);
|
|
1193
|
-
}
|
|
1243
|
+
}
|
|
1194
1244
|
|
|
1245
|
+
const orderNumberFromSession = await getOrderNumberFromSession(headers, commerce);
|
|
1246
|
+
const orderNumber = (_ref = orderNumberFromSession != null ? orderNumberFromSession : orderNumberFromCart) != null ? _ref : ''; // Step1: Get OrderForm from VTEX Commerce
|
|
1195
1247
|
|
|
1196
1248
|
const orderForm = await getOrderForm(orderNumber, session, ctx); // Step1.5: Check if another system changed the orderForm with this orderNumber
|
|
1197
1249
|
// If so, this means the user interacted with this cart elsewhere and expects
|
|
1198
1250
|
// to see this new cart state instead of what's stored on the user's browser.
|
|
1199
1251
|
|
|
1200
|
-
|
|
1201
|
-
const isStale = isOrderFormStale(orderForm);
|
|
1252
|
+
const isStale = isOrderFormStale(orderForm);
|
|
1202
1253
|
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
}
|
|
1254
|
+
if (isStale && orderNumber) {
|
|
1255
|
+
const newOrderForm = await setOrderFormEtag(orderForm, commerce).then(joinItems);
|
|
1256
|
+
return orderFormToCart(newOrderForm, skuLoader);
|
|
1207
1257
|
} // Step2: Process items from both browser and checkout so they have the same shape
|
|
1208
1258
|
|
|
1209
1259
|
|
|
@@ -1256,7 +1306,7 @@ const validateCart = async (_, {
|
|
|
1256
1306
|
id: orderForm.orderFormId,
|
|
1257
1307
|
orderItems: changes
|
|
1258
1308
|
}) // update orderForm etag so we know last time we touched this orderForm
|
|
1259
|
-
.then(form =>
|
|
1309
|
+
.then(form => setOrderFormEtag(form, commerce)).then(joinItems); // Step5: If no changes detected before/after updating orderForm, the order is validated
|
|
1260
1310
|
|
|
1261
1311
|
if (equals(order, updatedOrderForm)) {
|
|
1262
1312
|
return null;
|