@faststore/api 1.12.37 → 1.12.39

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.
@@ -12,8 +12,76 @@ var graphql = require('graphql');
12
12
  var language = require('graphql/language');
13
13
  var utils = require('@graphql-tools/utils');
14
14
 
15
+ var name = "@faststore/api";
16
+ var version = "1.12.38";
17
+ var license = "MIT";
18
+ var main = "dist/index.js";
19
+ var typings = "dist/index.d.ts";
20
+ var module$1 = "dist/api.esm.js";
21
+ var files = [
22
+ "dist",
23
+ "src"
24
+ ];
25
+ var browserslist = "node 12";
26
+ var engines = {
27
+ node: ">=12"
28
+ };
29
+ var scripts = {
30
+ develop: "tsdx watch --entry ./local/server.ts --target node --onSuccess \"node ./dist\"",
31
+ "develop:no-server": "concurrently \"yarn generate -w\" \"tsdx watch\"",
32
+ build: "graphql-codegen --config codegen.yml && tsdx build",
33
+ lint: "eslint src/**/*.ts",
34
+ test: "tsdx test",
35
+ generate: "graphql-codegen --config codegen.yml"
36
+ };
37
+ var dependencies = {
38
+ "@graphql-tools/schema": "^8.2.0",
39
+ "@rollup/plugin-graphql": "^1.0.0",
40
+ dataloader: "^2.1.0",
41
+ "fast-deep-equal": "^3.1.3",
42
+ "isomorphic-unfetch": "^3.1.0",
43
+ "p-limit": "^3.1.0"
44
+ };
45
+ var devDependencies = {
46
+ "@faststore/shared": "^1.12.37",
47
+ "@graphql-codegen/cli": "2.2.0",
48
+ "@graphql-codegen/typescript": "2.2.2",
49
+ concurrently: "^6.2.1",
50
+ express: "^4.17.3",
51
+ "express-graphql": "^0.12.0",
52
+ graphql: "^15.6.0",
53
+ "jest-transform-graphql": "^2.1.0",
54
+ "ts-jest": "25.5.1",
55
+ tsdx: "^0.14.1",
56
+ tslib: "^2.3.1",
57
+ typescript: "^4.4.2"
58
+ };
59
+ var peerDependencies = {
60
+ graphql: "^15.6.0"
61
+ };
62
+ var packageJson = {
63
+ name: name,
64
+ version: version,
65
+ license: license,
66
+ main: main,
67
+ typings: typings,
68
+ module: module$1,
69
+ files: files,
70
+ browserslist: browserslist,
71
+ engines: engines,
72
+ scripts: scripts,
73
+ dependencies: dependencies,
74
+ devDependencies: devDependencies,
75
+ peerDependencies: peerDependencies
76
+ };
77
+
78
+ const USER_AGENT = `${packageJson.name}@${packageJson.version}`;
15
79
  const fetchAPI = async (info, init) => {
16
- const response = await fetch(info, init);
80
+ const response = await fetch(info, { ...init,
81
+ headers: { ...(init == null ? void 0 : init.headers),
82
+ 'User-Agent': USER_AGENT
83
+ }
84
+ });
17
85
 
18
86
  if (response.ok) {
19
87
  return response.status !== 204 ? response.json() : undefined;
@@ -24,6 +92,19 @@ const fetchAPI = async (info, init) => {
24
92
  throw new Error(text);
25
93
  };
26
94
 
95
+ const getCookie = (name, cookie) => {
96
+ const value = `; ${cookie}`;
97
+ const parts = value.split(`; ${name}=`);
98
+
99
+ if (parts.length === 2) {
100
+ var _parts$pop$split$shif, _parts$pop;
101
+
102
+ 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 : '';
103
+ }
104
+
105
+ return '';
106
+ };
107
+
27
108
  const BASE_INIT = {
28
109
  method: 'POST',
29
110
  headers: {
@@ -141,13 +222,35 @@ const VtexCommerce = ({
141
222
  session: search => {
142
223
  const params = new URLSearchParams(search);
143
224
  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',
225
+
226
+ if (getCookie('vtex_session', ctx.headers.cookie)) {
227
+ // cookie set
228
+ return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
229
+ method: 'GET',
230
+ headers: {
231
+ 'content-type': 'application/json',
232
+ cookie: ctx.headers.cookie
233
+ }
234
+ });
235
+ } else {
236
+ // cookie unset -> create session
237
+ return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
238
+ method: 'POST',
239
+ headers: {
240
+ 'content-type': 'application/json',
241
+ cookie: ctx.headers.cookie
242
+ },
243
+ body: '{}'
244
+ });
245
+ }
246
+ },
247
+ getSessionOrder: () => {
248
+ return fetchAPI(`${base}/api/sessions?items=checkout.orderFormId`, {
249
+ method: 'GET',
146
250
  headers: {
147
251
  'content-type': 'application/json',
148
252
  cookie: ctx.headers.cookie
149
- },
150
- body: '{}'
253
+ }
151
254
  });
152
255
  },
153
256
  subscribeToNewsletter: data => {
@@ -944,6 +1047,13 @@ const subscribeToNewsletter = async (_, {
944
1047
  };
945
1048
  };
946
1049
 
1050
+ const mutateChannelContext = (ctx, channelString) => {
1051
+ ctx.storage.channel = ChannelMarshal.parse(channelString);
1052
+ };
1053
+ const mutateLocaleContext = (ctx, locale) => {
1054
+ ctx.storage.locale = locale;
1055
+ };
1056
+
947
1057
  const md5 = payload => crypto.createHash('md5').update(payload).digest('hex');
948
1058
 
949
1059
  const VALUE_REFERENCES = {
@@ -961,13 +1071,6 @@ function getPropertyId(item) {
961
1071
  return md5(`${item.name}:${JSON.stringify(item.value)}:${item.valueReference}`);
962
1072
  }
963
1073
 
964
- const mutateChannelContext = (ctx, channelString) => {
965
- ctx.storage.channel = ChannelMarshal.parse(channelString);
966
- };
967
- const mutateLocaleContext = (ctx, locale) => {
968
- ctx.storage.locale = locale;
969
- };
970
-
971
1074
  const isAttachment = value => value.valueReference === VALUE_REFERENCES.attachment;
972
1075
 
973
1076
  const getId = item => {
@@ -1114,7 +1217,22 @@ const isOrderFormStale = form => {
1114
1217
 
1115
1218
  const newEtag = getOrderFormEtag(form);
1116
1219
  return newEtag !== oldEtag;
1117
- }; // Returns the regionalized orderForm
1220
+ };
1221
+
1222
+ async function getOrderNumberFromSession(headers = {}, commerce) {
1223
+ const cookieSession = getCookie('vtex_session', headers.cookie);
1224
+
1225
+ if (cookieSession) {
1226
+ var _namespaces$checkout, _namespaces$checkout$;
1227
+
1228
+ const {
1229
+ namespaces
1230
+ } = await commerce.getSessionOrder();
1231
+ return (_namespaces$checkout = namespaces.checkout) == null ? void 0 : (_namespaces$checkout$ = _namespaces$checkout.orderFormId) == null ? void 0 : _namespaces$checkout$.value;
1232
+ }
1233
+
1234
+ return;
1235
+ } // Returns the regionalized orderForm
1118
1236
 
1119
1237
 
1120
1238
  const getOrderForm = async (id, session, {
@@ -1170,11 +1288,10 @@ const validateCart = async (_, {
1170
1288
  },
1171
1289
  session
1172
1290
  }, ctx) => {
1291
+ var _ref;
1292
+
1173
1293
  const {
1174
- enableOrderFormSync
1175
- } = ctx.storage.flags;
1176
- const {
1177
- orderNumber,
1294
+ orderNumber: orderNumberFromCart,
1178
1295
  acceptedOffer
1179
1296
  } = order;
1180
1297
  const {
@@ -1183,7 +1300,8 @@ const validateCart = async (_, {
1183
1300
  },
1184
1301
  loaders: {
1185
1302
  skuLoader
1186
- }
1303
+ },
1304
+ headers
1187
1305
  } = ctx;
1188
1306
  const channel = session == null ? void 0 : session.channel;
1189
1307
  const locale = session == null ? void 0 : session.locale;
@@ -1194,20 +1312,20 @@ const validateCart = async (_, {
1194
1312
 
1195
1313
  if (locale) {
1196
1314
  mutateLocaleContext(ctx, locale);
1197
- } // Step1: Get OrderForm from VTEX Commerce
1315
+ }
1198
1316
 
1317
+ const orderNumberFromSession = await getOrderNumberFromSession(headers, commerce);
1318
+ const orderNumber = (_ref = orderNumberFromSession != null ? orderNumberFromSession : orderNumberFromCart) != null ? _ref : ''; // Step1: Get OrderForm from VTEX Commerce
1199
1319
 
1200
1320
  const orderForm = await getOrderForm(orderNumber, session, ctx); // Step1.5: Check if another system changed the orderForm with this orderNumber
1201
1321
  // If so, this means the user interacted with this cart elsewhere and expects
1202
1322
  // to see this new cart state instead of what's stored on the user's browser.
1203
1323
 
1204
- if (enableOrderFormSync === true) {
1205
- const isStale = isOrderFormStale(orderForm);
1324
+ const isStale = isOrderFormStale(orderForm);
1206
1325
 
1207
- if (isStale === true && orderNumber) {
1208
- const newOrderForm = await setOrderFormEtag(orderForm, commerce).then(joinItems);
1209
- return orderFormToCart(newOrderForm, skuLoader);
1210
- }
1326
+ if (isStale && orderNumber) {
1327
+ const newOrderForm = await setOrderFormEtag(orderForm, commerce).then(joinItems);
1328
+ return orderFormToCart(newOrderForm, skuLoader);
1211
1329
  } // Step2: Process items from both browser and checkout so they have the same shape
1212
1330
 
1213
1331
 
@@ -1260,7 +1378,7 @@ const validateCart = async (_, {
1260
1378
  id: orderForm.orderFormId,
1261
1379
  orderItems: changes
1262
1380
  }) // update orderForm etag so we know last time we touched this orderForm
1263
- .then(form => enableOrderFormSync ? setOrderFormEtag(form, commerce) : form).then(joinItems); // Step5: If no changes detected before/after updating orderForm, the order is validated
1381
+ .then(form => setOrderFormEtag(form, commerce)).then(joinItems); // Step5: If no changes detected before/after updating orderForm, the order is validated
1264
1382
 
1265
1383
  if (equals(order, updatedOrderForm)) {
1266
1384
  return null;