@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.
package/dist/api.esm.js CHANGED
@@ -8,8 +8,76 @@ import { GraphQLScalarType, print } from 'graphql';
8
8
  import { Kind } from 'graphql/language';
9
9
  import { mapSchema, MapperKind, getDirective } from '@graphql-tools/utils';
10
10
 
11
+ var name = "@faststore/api";
12
+ var version = "1.12.38";
13
+ var license = "MIT";
14
+ var main = "dist/index.js";
15
+ var typings = "dist/index.d.ts";
16
+ var module = "dist/api.esm.js";
17
+ var files = [
18
+ "dist",
19
+ "src"
20
+ ];
21
+ var browserslist = "node 12";
22
+ var engines = {
23
+ node: ">=12"
24
+ };
25
+ var scripts = {
26
+ develop: "tsdx watch --entry ./local/server.ts --target node --onSuccess \"node ./dist\"",
27
+ "develop:no-server": "concurrently \"yarn generate -w\" \"tsdx watch\"",
28
+ build: "graphql-codegen --config codegen.yml && tsdx build",
29
+ lint: "eslint src/**/*.ts",
30
+ test: "tsdx test",
31
+ generate: "graphql-codegen --config codegen.yml"
32
+ };
33
+ var dependencies = {
34
+ "@graphql-tools/schema": "^8.2.0",
35
+ "@rollup/plugin-graphql": "^1.0.0",
36
+ dataloader: "^2.1.0",
37
+ "fast-deep-equal": "^3.1.3",
38
+ "isomorphic-unfetch": "^3.1.0",
39
+ "p-limit": "^3.1.0"
40
+ };
41
+ var devDependencies = {
42
+ "@faststore/shared": "^1.12.37",
43
+ "@graphql-codegen/cli": "2.2.0",
44
+ "@graphql-codegen/typescript": "2.2.2",
45
+ concurrently: "^6.2.1",
46
+ express: "^4.17.3",
47
+ "express-graphql": "^0.12.0",
48
+ graphql: "^15.6.0",
49
+ "jest-transform-graphql": "^2.1.0",
50
+ "ts-jest": "25.5.1",
51
+ tsdx: "^0.14.1",
52
+ tslib: "^2.3.1",
53
+ typescript: "^4.4.2"
54
+ };
55
+ var peerDependencies = {
56
+ graphql: "^15.6.0"
57
+ };
58
+ var packageJson = {
59
+ name: name,
60
+ version: version,
61
+ license: license,
62
+ main: main,
63
+ typings: typings,
64
+ module: module,
65
+ files: files,
66
+ browserslist: browserslist,
67
+ engines: engines,
68
+ scripts: scripts,
69
+ dependencies: dependencies,
70
+ devDependencies: devDependencies,
71
+ peerDependencies: peerDependencies
72
+ };
73
+
74
+ const USER_AGENT = `${packageJson.name}@${packageJson.version}`;
11
75
  const fetchAPI = async (info, init) => {
12
- const response = await fetch(info, init);
76
+ const response = await fetch(info, { ...init,
77
+ headers: { ...(init == null ? void 0 : init.headers),
78
+ 'User-Agent': USER_AGENT
79
+ }
80
+ });
13
81
 
14
82
  if (response.ok) {
15
83
  return response.status !== 204 ? response.json() : undefined;
@@ -20,6 +88,19 @@ const fetchAPI = async (info, init) => {
20
88
  throw new Error(text);
21
89
  };
22
90
 
91
+ const getCookie = (name, cookie) => {
92
+ const value = `; ${cookie}`;
93
+ const parts = value.split(`; ${name}=`);
94
+
95
+ if (parts.length === 2) {
96
+ var _parts$pop$split$shif, _parts$pop;
97
+
98
+ 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 : '';
99
+ }
100
+
101
+ return '';
102
+ };
103
+
23
104
  const BASE_INIT = {
24
105
  method: 'POST',
25
106
  headers: {
@@ -137,13 +218,35 @@ const VtexCommerce = ({
137
218
  session: search => {
138
219
  const params = new URLSearchParams(search);
139
220
  params.set('items', 'profile.id,profile.email,profile.firstName,profile.lastName,store.channel,store.countryCode,store.cultureInfo,store.currencyCode,store.currencySymbol');
140
- return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
141
- method: 'POST',
221
+
222
+ if (getCookie('vtex_session', ctx.headers.cookie)) {
223
+ // cookie set
224
+ return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
225
+ method: 'GET',
226
+ headers: {
227
+ 'content-type': 'application/json',
228
+ cookie: ctx.headers.cookie
229
+ }
230
+ });
231
+ } else {
232
+ // cookie unset -> create session
233
+ return fetchAPI(`${base}/api/sessions?${params.toString()}`, {
234
+ method: 'POST',
235
+ headers: {
236
+ 'content-type': 'application/json',
237
+ cookie: ctx.headers.cookie
238
+ },
239
+ body: '{}'
240
+ });
241
+ }
242
+ },
243
+ getSessionOrder: () => {
244
+ return fetchAPI(`${base}/api/sessions?items=checkout.orderFormId`, {
245
+ method: 'GET',
142
246
  headers: {
143
247
  'content-type': 'application/json',
144
248
  cookie: ctx.headers.cookie
145
- },
146
- body: '{}'
249
+ }
147
250
  });
148
251
  },
149
252
  subscribeToNewsletter: data => {
@@ -940,6 +1043,13 @@ const subscribeToNewsletter = async (_, {
940
1043
  };
941
1044
  };
942
1045
 
1046
+ const mutateChannelContext = (ctx, channelString) => {
1047
+ ctx.storage.channel = ChannelMarshal.parse(channelString);
1048
+ };
1049
+ const mutateLocaleContext = (ctx, locale) => {
1050
+ ctx.storage.locale = locale;
1051
+ };
1052
+
943
1053
  const md5 = payload => crypto.createHash('md5').update(payload).digest('hex');
944
1054
 
945
1055
  const VALUE_REFERENCES = {
@@ -957,13 +1067,6 @@ function getPropertyId(item) {
957
1067
  return md5(`${item.name}:${JSON.stringify(item.value)}:${item.valueReference}`);
958
1068
  }
959
1069
 
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
1070
  const isAttachment = value => value.valueReference === VALUE_REFERENCES.attachment;
968
1071
 
969
1072
  const getId = item => {
@@ -1110,7 +1213,22 @@ const isOrderFormStale = form => {
1110
1213
 
1111
1214
  const newEtag = getOrderFormEtag(form);
1112
1215
  return newEtag !== oldEtag;
1113
- }; // Returns the regionalized orderForm
1216
+ };
1217
+
1218
+ async function getOrderNumberFromSession(headers = {}, commerce) {
1219
+ const cookieSession = getCookie('vtex_session', headers.cookie);
1220
+
1221
+ if (cookieSession) {
1222
+ var _namespaces$checkout, _namespaces$checkout$;
1223
+
1224
+ const {
1225
+ namespaces
1226
+ } = await commerce.getSessionOrder();
1227
+ return (_namespaces$checkout = namespaces.checkout) == null ? void 0 : (_namespaces$checkout$ = _namespaces$checkout.orderFormId) == null ? void 0 : _namespaces$checkout$.value;
1228
+ }
1229
+
1230
+ return;
1231
+ } // Returns the regionalized orderForm
1114
1232
 
1115
1233
 
1116
1234
  const getOrderForm = async (id, session, {
@@ -1166,11 +1284,10 @@ const validateCart = async (_, {
1166
1284
  },
1167
1285
  session
1168
1286
  }, ctx) => {
1287
+ var _ref;
1288
+
1169
1289
  const {
1170
- enableOrderFormSync
1171
- } = ctx.storage.flags;
1172
- const {
1173
- orderNumber,
1290
+ orderNumber: orderNumberFromCart,
1174
1291
  acceptedOffer
1175
1292
  } = order;
1176
1293
  const {
@@ -1179,7 +1296,8 @@ const validateCart = async (_, {
1179
1296
  },
1180
1297
  loaders: {
1181
1298
  skuLoader
1182
- }
1299
+ },
1300
+ headers
1183
1301
  } = ctx;
1184
1302
  const channel = session == null ? void 0 : session.channel;
1185
1303
  const locale = session == null ? void 0 : session.locale;
@@ -1190,20 +1308,20 @@ const validateCart = async (_, {
1190
1308
 
1191
1309
  if (locale) {
1192
1310
  mutateLocaleContext(ctx, locale);
1193
- } // Step1: Get OrderForm from VTEX Commerce
1311
+ }
1194
1312
 
1313
+ const orderNumberFromSession = await getOrderNumberFromSession(headers, commerce);
1314
+ const orderNumber = (_ref = orderNumberFromSession != null ? orderNumberFromSession : orderNumberFromCart) != null ? _ref : ''; // Step1: Get OrderForm from VTEX Commerce
1195
1315
 
1196
1316
  const orderForm = await getOrderForm(orderNumber, session, ctx); // Step1.5: Check if another system changed the orderForm with this orderNumber
1197
1317
  // If so, this means the user interacted with this cart elsewhere and expects
1198
1318
  // to see this new cart state instead of what's stored on the user's browser.
1199
1319
 
1200
- if (enableOrderFormSync === true) {
1201
- const isStale = isOrderFormStale(orderForm);
1320
+ const isStale = isOrderFormStale(orderForm);
1202
1321
 
1203
- if (isStale === true && orderNumber) {
1204
- const newOrderForm = await setOrderFormEtag(orderForm, commerce).then(joinItems);
1205
- return orderFormToCart(newOrderForm, skuLoader);
1206
- }
1322
+ if (isStale && orderNumber) {
1323
+ const newOrderForm = await setOrderFormEtag(orderForm, commerce).then(joinItems);
1324
+ return orderFormToCart(newOrderForm, skuLoader);
1207
1325
  } // Step2: Process items from both browser and checkout so they have the same shape
1208
1326
 
1209
1327
 
@@ -1256,7 +1374,7 @@ const validateCart = async (_, {
1256
1374
  id: orderForm.orderFormId,
1257
1375
  orderItems: changes
1258
1376
  }) // update orderForm etag so we know last time we touched this orderForm
1259
- .then(form => enableOrderFormSync ? setOrderFormEtag(form, commerce) : form).then(joinItems); // Step5: If no changes detected before/after updating orderForm, the order is validated
1377
+ .then(form => setOrderFormEtag(form, commerce)).then(joinItems); // Step5: If no changes detected before/after updating orderForm, the order is validated
1260
1378
 
1261
1379
  if (equals(order, updatedOrderForm)) {
1262
1380
  return null;