@faststore/api 2.0.82-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.esm.js CHANGED
@@ -18,6 +18,16 @@ const fetchAPI = async (info, init) => {
18
18
  throw new Error(text);
19
19
  };
20
20
 
21
+ const getCookie = (name, cookie) => {
22
+ const value = `; ${cookie}`;
23
+ const parts = value.split(`; ${name}=`);
24
+ if (parts.length === 2) {
25
+ var _parts$pop$split$shif, _parts$pop;
26
+ 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 : '';
27
+ }
28
+ return '';
29
+ };
30
+
21
31
  const BASE_INIT = {
22
32
  method: 'POST',
23
33
  headers: {
@@ -141,13 +151,34 @@ const VtexCommerce = ({
141
151
  session: search => {
142
152
  const params = new URLSearchParams(search);
143
153
  params.set('items', 'profile.id,profile.email,profile.firstName,profile.lastName,store.channel,store.countryCode,store.cultureInfo,store.currencyCode,store.currencySymbol');
144
- return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
145
- method: 'POST',
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',
146
178
  headers: {
147
179
  'content-type': 'application/json',
148
180
  cookie: ctx.headers.cookie
149
- },
150
- body: '{}'
181
+ }
151
182
  });
152
183
  },
153
184
  subscribeToNewsletter: data => {
@@ -833,23 +864,6 @@ const subscribeToNewsletter = async (_, {
833
864
  };
834
865
  };
835
866
 
836
- const md5 = payload => crypto.createHash('md5').update(payload).digest('hex');
837
-
838
- const VALUE_REFERENCES = {
839
- attachment: 'ATTACHMENT',
840
- specification: 'SPECIFICATION'
841
- };
842
- function attachmentToPropertyValue(attachment) {
843
- return {
844
- name: attachment.name,
845
- value: attachment.content,
846
- valueReference: VALUE_REFERENCES.attachment
847
- };
848
- }
849
- function getPropertyId(item) {
850
- return md5(`${item.name}:${JSON.stringify(item.value)}:${item.valueReference}`);
851
- }
852
-
853
867
  class ChannelMarshal {
854
868
  static parse(channelString) {
855
869
  try {
@@ -877,6 +891,23 @@ const mutateLocaleContext = (ctx, locale) => {
877
891
  ctx.storage.locale = locale;
878
892
  };
879
893
 
894
+ const md5 = payload => crypto.createHash('md5').update(payload).digest('hex');
895
+
896
+ const VALUE_REFERENCES = {
897
+ attachment: 'ATTACHMENT',
898
+ specification: 'SPECIFICATION'
899
+ };
900
+ function attachmentToPropertyValue(attachment) {
901
+ return {
902
+ name: attachment.name,
903
+ value: attachment.content,
904
+ valueReference: VALUE_REFERENCES.attachment
905
+ };
906
+ }
907
+ function getPropertyId(item) {
908
+ return md5(`${item.name}:${JSON.stringify(item.value)}:${item.valueReference}`);
909
+ }
910
+
880
911
  const isAttachment = value => value.valueReference === VALUE_REFERENCES.attachment;
881
912
  const getId = item => {
882
913
  var _item$itemOffered$add;
@@ -1006,6 +1037,17 @@ const isOrderFormStale = form => {
1006
1037
  const newEtag = getOrderFormEtag(form);
1007
1038
  return newEtag !== oldEtag;
1008
1039
  };
1040
+ async function getOrderNumberFromSession(headers = {}, commerce) {
1041
+ const cookieSession = getCookie('vtex_session', headers.cookie);
1042
+ if (cookieSession) {
1043
+ var _namespaces$checkout, _namespaces$checkout$;
1044
+ const {
1045
+ namespaces
1046
+ } = await commerce.getSessionOrder();
1047
+ return (_namespaces$checkout = namespaces.checkout) == null ? void 0 : (_namespaces$checkout$ = _namespaces$checkout.orderFormId) == null ? void 0 : _namespaces$checkout$.value;
1048
+ }
1049
+ return;
1050
+ }
1009
1051
  // Returns the regionalized orderForm
1010
1052
  const getOrderForm = async (id, session, {
1011
1053
  clients: {
@@ -1054,11 +1096,9 @@ const validateCart = async (_, {
1054
1096
  },
1055
1097
  session
1056
1098
  }, ctx) => {
1099
+ var _ref;
1057
1100
  const {
1058
- enableOrderFormSync
1059
- } = ctx.storage.flags;
1060
- const {
1061
- orderNumber,
1101
+ orderNumber: orderNumberFromCart,
1062
1102
  acceptedOffer,
1063
1103
  shouldSplitItem
1064
1104
  } = order;
@@ -1068,7 +1108,8 @@ const validateCart = async (_, {
1068
1108
  },
1069
1109
  loaders: {
1070
1110
  skuLoader
1071
- }
1111
+ },
1112
+ headers
1072
1113
  } = ctx;
1073
1114
  const channel = session == null ? void 0 : session.channel;
1074
1115
  const locale = session == null ? void 0 : session.locale;
@@ -1078,17 +1119,17 @@ const validateCart = async (_, {
1078
1119
  if (locale) {
1079
1120
  mutateLocaleContext(ctx, locale);
1080
1121
  }
1122
+ const orderNumberFromSession = await getOrderNumberFromSession(headers, commerce);
1123
+ const orderNumber = (_ref = orderNumberFromSession != null ? orderNumberFromSession : orderNumberFromCart) != null ? _ref : '';
1081
1124
  // Step1: Get OrderForm from VTEX Commerce
1082
1125
  const orderForm = await getOrderForm(orderNumber, session, ctx);
1083
1126
  // Step1.5: Check if another system changed the orderForm with this orderNumber
1084
1127
  // If so, this means the user interacted with this cart elsewhere and expects
1085
1128
  // to see this new cart state instead of what's stored on the user's browser.
1086
- if (enableOrderFormSync === true) {
1087
- const isStale = isOrderFormStale(orderForm);
1088
- if (isStale === true && orderNumber) {
1089
- const newOrderForm = await setOrderFormEtag(orderForm, commerce).then(joinItems);
1090
- return orderFormToCart(newOrderForm, skuLoader);
1091
- }
1129
+ const isStale = isOrderFormStale(orderForm);
1130
+ if (isStale && orderNumber) {
1131
+ const newOrderForm = await setOrderFormEtag(orderForm, commerce).then(joinItems);
1132
+ return orderFormToCart(newOrderForm, skuLoader);
1092
1133
  }
1093
1134
  // Step2: Process items from both browser and checkout so they have the same shape
1094
1135
  const browserItemsById = groupById(acceptedOffer);
@@ -1141,7 +1182,7 @@ const validateCart = async (_, {
1141
1182
  shouldSplitItem
1142
1183
  })
1143
1184
  // update orderForm etag so we know last time we touched this orderForm
1144
- .then(form => enableOrderFormSync ? setOrderFormEtag(form, commerce) : form).then(joinItems);
1185
+ .then(form => setOrderFormEtag(form, commerce)).then(joinItems);
1145
1186
  // Step5: If no changes detected before/after updating orderForm, the order is validated
1146
1187
  if (equals(order, updatedOrderForm)) {
1147
1188
  return null;