@faststore/api 2.0.91-alpha.0 → 2.0.97-alpha.0
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 +74 -33
- 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 +74 -33
- 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
|
@@ -22,6 +22,16 @@ const fetchAPI = async (info, init) => {
|
|
|
22
22
|
throw new Error(text);
|
|
23
23
|
};
|
|
24
24
|
|
|
25
|
+
const getCookie = (name, cookie) => {
|
|
26
|
+
const value = `; ${cookie}`;
|
|
27
|
+
const parts = value.split(`; ${name}=`);
|
|
28
|
+
if (parts.length === 2) {
|
|
29
|
+
var _parts$pop$split$shif, _parts$pop;
|
|
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
|
+
return '';
|
|
33
|
+
};
|
|
34
|
+
|
|
25
35
|
const BASE_INIT = {
|
|
26
36
|
method: 'POST',
|
|
27
37
|
headers: {
|
|
@@ -145,13 +155,34 @@ const VtexCommerce = ({
|
|
|
145
155
|
session: search => {
|
|
146
156
|
const params = new URLSearchParams(search);
|
|
147
157
|
params.set('items', 'profile.id,profile.email,profile.firstName,profile.lastName,store.channel,store.countryCode,store.cultureInfo,store.currencyCode,store.currencySymbol');
|
|
148
|
-
|
|
149
|
-
|
|
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',
|
|
150
182
|
headers: {
|
|
151
183
|
'content-type': 'application/json',
|
|
152
184
|
cookie: ctx.headers.cookie
|
|
153
|
-
}
|
|
154
|
-
body: '{}'
|
|
185
|
+
}
|
|
155
186
|
});
|
|
156
187
|
},
|
|
157
188
|
subscribeToNewsletter: data => {
|
|
@@ -837,23 +868,6 @@ const subscribeToNewsletter = async (_, {
|
|
|
837
868
|
};
|
|
838
869
|
};
|
|
839
870
|
|
|
840
|
-
const md5 = payload => crypto.createHash('md5').update(payload).digest('hex');
|
|
841
|
-
|
|
842
|
-
const VALUE_REFERENCES = {
|
|
843
|
-
attachment: 'ATTACHMENT',
|
|
844
|
-
specification: 'SPECIFICATION'
|
|
845
|
-
};
|
|
846
|
-
function attachmentToPropertyValue(attachment) {
|
|
847
|
-
return {
|
|
848
|
-
name: attachment.name,
|
|
849
|
-
value: attachment.content,
|
|
850
|
-
valueReference: VALUE_REFERENCES.attachment
|
|
851
|
-
};
|
|
852
|
-
}
|
|
853
|
-
function getPropertyId(item) {
|
|
854
|
-
return md5(`${item.name}:${JSON.stringify(item.value)}:${item.valueReference}`);
|
|
855
|
-
}
|
|
856
|
-
|
|
857
871
|
class ChannelMarshal {
|
|
858
872
|
static parse(channelString) {
|
|
859
873
|
try {
|
|
@@ -881,6 +895,23 @@ const mutateLocaleContext = (ctx, locale) => {
|
|
|
881
895
|
ctx.storage.locale = locale;
|
|
882
896
|
};
|
|
883
897
|
|
|
898
|
+
const md5 = payload => crypto.createHash('md5').update(payload).digest('hex');
|
|
899
|
+
|
|
900
|
+
const VALUE_REFERENCES = {
|
|
901
|
+
attachment: 'ATTACHMENT',
|
|
902
|
+
specification: 'SPECIFICATION'
|
|
903
|
+
};
|
|
904
|
+
function attachmentToPropertyValue(attachment) {
|
|
905
|
+
return {
|
|
906
|
+
name: attachment.name,
|
|
907
|
+
value: attachment.content,
|
|
908
|
+
valueReference: VALUE_REFERENCES.attachment
|
|
909
|
+
};
|
|
910
|
+
}
|
|
911
|
+
function getPropertyId(item) {
|
|
912
|
+
return md5(`${item.name}:${JSON.stringify(item.value)}:${item.valueReference}`);
|
|
913
|
+
}
|
|
914
|
+
|
|
884
915
|
const isAttachment = value => value.valueReference === VALUE_REFERENCES.attachment;
|
|
885
916
|
const getId = item => {
|
|
886
917
|
var _item$itemOffered$add;
|
|
@@ -1010,6 +1041,17 @@ const isOrderFormStale = form => {
|
|
|
1010
1041
|
const newEtag = getOrderFormEtag(form);
|
|
1011
1042
|
return newEtag !== oldEtag;
|
|
1012
1043
|
};
|
|
1044
|
+
async function getOrderNumberFromSession(headers = {}, commerce) {
|
|
1045
|
+
const cookieSession = getCookie('vtex_session', headers.cookie);
|
|
1046
|
+
if (cookieSession) {
|
|
1047
|
+
var _namespaces$checkout, _namespaces$checkout$;
|
|
1048
|
+
const {
|
|
1049
|
+
namespaces
|
|
1050
|
+
} = await commerce.getSessionOrder();
|
|
1051
|
+
return (_namespaces$checkout = namespaces.checkout) == null ? void 0 : (_namespaces$checkout$ = _namespaces$checkout.orderFormId) == null ? void 0 : _namespaces$checkout$.value;
|
|
1052
|
+
}
|
|
1053
|
+
return;
|
|
1054
|
+
}
|
|
1013
1055
|
// Returns the regionalized orderForm
|
|
1014
1056
|
const getOrderForm = async (id, session, {
|
|
1015
1057
|
clients: {
|
|
@@ -1058,11 +1100,9 @@ const validateCart = async (_, {
|
|
|
1058
1100
|
},
|
|
1059
1101
|
session
|
|
1060
1102
|
}, ctx) => {
|
|
1103
|
+
var _ref;
|
|
1061
1104
|
const {
|
|
1062
|
-
|
|
1063
|
-
} = ctx.storage.flags;
|
|
1064
|
-
const {
|
|
1065
|
-
orderNumber,
|
|
1105
|
+
orderNumber: orderNumberFromCart,
|
|
1066
1106
|
acceptedOffer,
|
|
1067
1107
|
shouldSplitItem
|
|
1068
1108
|
} = order;
|
|
@@ -1072,7 +1112,8 @@ const validateCart = async (_, {
|
|
|
1072
1112
|
},
|
|
1073
1113
|
loaders: {
|
|
1074
1114
|
skuLoader
|
|
1075
|
-
}
|
|
1115
|
+
},
|
|
1116
|
+
headers
|
|
1076
1117
|
} = ctx;
|
|
1077
1118
|
const channel = session == null ? void 0 : session.channel;
|
|
1078
1119
|
const locale = session == null ? void 0 : session.locale;
|
|
@@ -1082,17 +1123,17 @@ const validateCart = async (_, {
|
|
|
1082
1123
|
if (locale) {
|
|
1083
1124
|
mutateLocaleContext(ctx, locale);
|
|
1084
1125
|
}
|
|
1126
|
+
const orderNumberFromSession = await getOrderNumberFromSession(headers, commerce);
|
|
1127
|
+
const orderNumber = (_ref = orderNumberFromSession != null ? orderNumberFromSession : orderNumberFromCart) != null ? _ref : '';
|
|
1085
1128
|
// Step1: Get OrderForm from VTEX Commerce
|
|
1086
1129
|
const orderForm = await getOrderForm(orderNumber, session, ctx);
|
|
1087
1130
|
// Step1.5: Check if another system changed the orderForm with this orderNumber
|
|
1088
1131
|
// If so, this means the user interacted with this cart elsewhere and expects
|
|
1089
1132
|
// to see this new cart state instead of what's stored on the user's browser.
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
return orderFormToCart(newOrderForm, skuLoader);
|
|
1095
|
-
}
|
|
1133
|
+
const isStale = isOrderFormStale(orderForm);
|
|
1134
|
+
if (isStale && orderNumber) {
|
|
1135
|
+
const newOrderForm = await setOrderFormEtag(orderForm, commerce).then(joinItems);
|
|
1136
|
+
return orderFormToCart(newOrderForm, skuLoader);
|
|
1096
1137
|
}
|
|
1097
1138
|
// Step2: Process items from both browser and checkout so they have the same shape
|
|
1098
1139
|
const browserItemsById = groupById(acceptedOffer);
|
|
@@ -1145,7 +1186,7 @@ const validateCart = async (_, {
|
|
|
1145
1186
|
shouldSplitItem
|
|
1146
1187
|
})
|
|
1147
1188
|
// update orderForm etag so we know last time we touched this orderForm
|
|
1148
|
-
.then(form =>
|
|
1189
|
+
.then(form => setOrderFormEtag(form, commerce)).then(joinItems);
|
|
1149
1190
|
// Step5: If no changes detected before/after updating orderForm, the order is validated
|
|
1150
1191
|
if (equals(order, updatedOrderForm)) {
|
|
1151
1192
|
return null;
|