@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/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 VTEX
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -24,6 +24,19 @@ const fetchAPI = async (info, init) => {
|
|
|
24
24
|
throw new Error(text);
|
|
25
25
|
};
|
|
26
26
|
|
|
27
|
+
const getCookie = (name, cookie) => {
|
|
28
|
+
const value = `; ${cookie}`;
|
|
29
|
+
const parts = value.split(`; ${name}=`);
|
|
30
|
+
|
|
31
|
+
if (parts.length === 2) {
|
|
32
|
+
var _parts$pop$split$shif, _parts$pop;
|
|
33
|
+
|
|
34
|
+
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 : '';
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return '';
|
|
38
|
+
};
|
|
39
|
+
|
|
27
40
|
const BASE_INIT = {
|
|
28
41
|
method: 'POST',
|
|
29
42
|
headers: {
|
|
@@ -141,13 +154,35 @@ const VtexCommerce = ({
|
|
|
141
154
|
session: search => {
|
|
142
155
|
const params = new URLSearchParams(search);
|
|
143
156
|
params.set('items', 'profile.id,profile.email,profile.firstName,profile.lastName,store.channel,store.countryCode,store.cultureInfo,store.currencyCode,store.currencySymbol');
|
|
144
|
-
|
|
145
|
-
|
|
157
|
+
|
|
158
|
+
if (getCookie('vtex_session', ctx.headers.cookie)) {
|
|
159
|
+
// cookie set
|
|
160
|
+
return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
|
|
161
|
+
method: 'GET',
|
|
162
|
+
headers: {
|
|
163
|
+
'content-type': 'application/json',
|
|
164
|
+
cookie: ctx.headers.cookie
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
} else {
|
|
168
|
+
// cookie unset -> create session
|
|
169
|
+
return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
|
|
170
|
+
method: 'POST',
|
|
171
|
+
headers: {
|
|
172
|
+
'content-type': 'application/json',
|
|
173
|
+
cookie: ctx.headers.cookie
|
|
174
|
+
},
|
|
175
|
+
body: '{}'
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
},
|
|
179
|
+
getSessionOrder: () => {
|
|
180
|
+
return fetchAPI(`${base}/api/sessions?items=checkout.orderFormId`, {
|
|
181
|
+
method: 'GET',
|
|
146
182
|
headers: {
|
|
147
183
|
'content-type': 'application/json',
|
|
148
184
|
cookie: ctx.headers.cookie
|
|
149
|
-
}
|
|
150
|
-
body: '{}'
|
|
185
|
+
}
|
|
151
186
|
});
|
|
152
187
|
},
|
|
153
188
|
subscribeToNewsletter: data => {
|
|
@@ -944,6 +979,13 @@ const subscribeToNewsletter = async (_, {
|
|
|
944
979
|
};
|
|
945
980
|
};
|
|
946
981
|
|
|
982
|
+
const mutateChannelContext = (ctx, channelString) => {
|
|
983
|
+
ctx.storage.channel = ChannelMarshal.parse(channelString);
|
|
984
|
+
};
|
|
985
|
+
const mutateLocaleContext = (ctx, locale) => {
|
|
986
|
+
ctx.storage.locale = locale;
|
|
987
|
+
};
|
|
988
|
+
|
|
947
989
|
const md5 = payload => crypto.createHash('md5').update(payload).digest('hex');
|
|
948
990
|
|
|
949
991
|
const VALUE_REFERENCES = {
|
|
@@ -1107,7 +1149,22 @@ const isOrderFormStale = form => {
|
|
|
1107
1149
|
|
|
1108
1150
|
const newEtag = getOrderFormEtag(form);
|
|
1109
1151
|
return newEtag !== oldEtag;
|
|
1110
|
-
};
|
|
1152
|
+
};
|
|
1153
|
+
|
|
1154
|
+
async function getOrderNumberFromSession(headers = {}, commerce) {
|
|
1155
|
+
const cookieSession = getCookie('vtex_session', headers.cookie);
|
|
1156
|
+
|
|
1157
|
+
if (cookieSession) {
|
|
1158
|
+
var _namespaces$checkout, _namespaces$checkout$;
|
|
1159
|
+
|
|
1160
|
+
const {
|
|
1161
|
+
namespaces
|
|
1162
|
+
} = await commerce.getSessionOrder();
|
|
1163
|
+
return (_namespaces$checkout = namespaces.checkout) == null ? void 0 : (_namespaces$checkout$ = _namespaces$checkout.orderFormId) == null ? void 0 : _namespaces$checkout$.value;
|
|
1164
|
+
}
|
|
1165
|
+
|
|
1166
|
+
return;
|
|
1167
|
+
} // Returns the regionalized orderForm
|
|
1111
1168
|
|
|
1112
1169
|
|
|
1113
1170
|
const getOrderForm = async (id, session, {
|
|
@@ -1163,11 +1220,10 @@ const validateCart = async (_, {
|
|
|
1163
1220
|
},
|
|
1164
1221
|
session
|
|
1165
1222
|
}, ctx) => {
|
|
1223
|
+
var _ref;
|
|
1224
|
+
|
|
1166
1225
|
const {
|
|
1167
|
-
|
|
1168
|
-
} = ctx.storage.flags;
|
|
1169
|
-
const {
|
|
1170
|
-
orderNumber,
|
|
1226
|
+
orderNumber: orderNumberFromCart,
|
|
1171
1227
|
acceptedOffer
|
|
1172
1228
|
} = order;
|
|
1173
1229
|
const {
|
|
@@ -1176,20 +1232,32 @@ const validateCart = async (_, {
|
|
|
1176
1232
|
},
|
|
1177
1233
|
loaders: {
|
|
1178
1234
|
skuLoader
|
|
1179
|
-
}
|
|
1180
|
-
|
|
1235
|
+
},
|
|
1236
|
+
headers
|
|
1237
|
+
} = ctx;
|
|
1238
|
+
const channel = session == null ? void 0 : session.channel;
|
|
1239
|
+
const locale = session == null ? void 0 : session.locale;
|
|
1240
|
+
|
|
1241
|
+
if (channel) {
|
|
1242
|
+
mutateChannelContext(ctx, channel);
|
|
1243
|
+
}
|
|
1244
|
+
|
|
1245
|
+
if (locale) {
|
|
1246
|
+
mutateLocaleContext(ctx, locale);
|
|
1247
|
+
}
|
|
1248
|
+
|
|
1249
|
+
const orderNumberFromSession = await getOrderNumberFromSession(headers, commerce);
|
|
1250
|
+
const orderNumber = (_ref = orderNumberFromSession != null ? orderNumberFromSession : orderNumberFromCart) != null ? _ref : ''; // Step1: Get OrderForm from VTEX Commerce
|
|
1181
1251
|
|
|
1182
1252
|
const orderForm = await getOrderForm(orderNumber, session, ctx); // Step1.5: Check if another system changed the orderForm with this orderNumber
|
|
1183
1253
|
// If so, this means the user interacted with this cart elsewhere and expects
|
|
1184
1254
|
// to see this new cart state instead of what's stored on the user's browser.
|
|
1185
1255
|
|
|
1186
|
-
|
|
1187
|
-
const isStale = isOrderFormStale(orderForm);
|
|
1256
|
+
const isStale = isOrderFormStale(orderForm);
|
|
1188
1257
|
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
}
|
|
1258
|
+
if (isStale && orderNumber) {
|
|
1259
|
+
const newOrderForm = await setOrderFormEtag(orderForm, commerce).then(joinItems);
|
|
1260
|
+
return orderFormToCart(newOrderForm, skuLoader);
|
|
1193
1261
|
} // Step2: Process items from both browser and checkout so they have the same shape
|
|
1194
1262
|
|
|
1195
1263
|
|
|
@@ -1242,7 +1310,7 @@ const validateCart = async (_, {
|
|
|
1242
1310
|
id: orderForm.orderFormId,
|
|
1243
1311
|
orderItems: changes
|
|
1244
1312
|
}) // update orderForm etag so we know last time we touched this orderForm
|
|
1245
|
-
.then(form =>
|
|
1313
|
+
.then(form => setOrderFormEtag(form, commerce)).then(joinItems); // Step5: If no changes detected before/after updating orderForm, the order is validated
|
|
1246
1314
|
|
|
1247
1315
|
if (equals(order, updatedOrderForm)) {
|
|
1248
1316
|
return null;
|
|
@@ -1650,13 +1718,6 @@ const StorePropertyValue = {
|
|
|
1650
1718
|
}) => valueReference
|
|
1651
1719
|
};
|
|
1652
1720
|
|
|
1653
|
-
const mutateChannelContext = (ctx, channelString) => {
|
|
1654
|
-
ctx.storage.channel = ChannelMarshal.parse(channelString);
|
|
1655
|
-
};
|
|
1656
|
-
const mutateLocaleContext = (ctx, locale) => {
|
|
1657
|
-
ctx.storage.locale = locale;
|
|
1658
|
-
};
|
|
1659
|
-
|
|
1660
1721
|
const SORT_MAP = {
|
|
1661
1722
|
price_desc: 'price:desc',
|
|
1662
1723
|
price_asc: 'price:asc',
|