swell-js 5.9.3 → 5.9.4

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.
@@ -5111,7 +5111,7 @@
5111
5111
  vaultRequest: vaultRequest
5112
5112
  });
5113
5113
 
5114
- function methods$e(options) {
5114
+ function methods$f(options) {
5115
5115
  return {
5116
5116
  async createToken(data) {
5117
5117
  let error = null;
@@ -5328,7 +5328,7 @@
5328
5328
 
5329
5329
  const RECORD_TIMEOUT = 5000;
5330
5330
 
5331
- function methods$d(opt) {
5331
+ function methods$e(opt) {
5332
5332
  let VALUES = {
5333
5333
  /*
5334
5334
  [model]: {
@@ -5490,7 +5490,7 @@
5490
5490
  };
5491
5491
  }
5492
5492
 
5493
- function methods$c(api) {
5493
+ function methods$d(api) {
5494
5494
  const { get, list } = defaultMethods(api, '/attributes', ['list', 'get']);
5495
5495
 
5496
5496
  return {
@@ -5502,7 +5502,7 @@
5502
5502
  };
5503
5503
  }
5504
5504
 
5505
- function methods$b(api, opt) {
5505
+ function methods$c(api, opt) {
5506
5506
  const { get, list } = defaultMethods(api, '/products', ['list', 'get']);
5507
5507
  return {
5508
5508
  get: (id, ...args) => {
@@ -5711,7 +5711,7 @@
5711
5711
  }
5712
5712
 
5713
5713
  async function getFilterableAttributeFilters(api, products, options) {
5714
- const { results: filterableAttributes } = await methods$c(api).list({
5714
+ const { results: filterableAttributes } = await methods$d(api).list({
5715
5715
  filterable: true,
5716
5716
  });
5717
5717
 
@@ -5895,7 +5895,7 @@
5895
5895
  };
5896
5896
  }
5897
5897
 
5898
- function methods$a(api, options) {
5898
+ function methods$b(api, options) {
5899
5899
  return {
5900
5900
  state: null,
5901
5901
  order: null,
@@ -6080,7 +6080,7 @@
6080
6080
  };
6081
6081
  }
6082
6082
 
6083
- function methods$9(api) {
6083
+ function methods$a(api) {
6084
6084
  return {
6085
6085
  state: null,
6086
6086
 
@@ -6254,7 +6254,7 @@
6254
6254
  }
6255
6255
 
6256
6256
  async updateCart(data) {
6257
- const cart = await methods$a(this.api, this.options).update(data);
6257
+ const cart = await methods$b(this.api, this.options).update(data);
6258
6258
 
6259
6259
  this.setProps({ cart });
6260
6260
  }
@@ -6490,7 +6490,7 @@
6490
6490
  }
6491
6491
  }
6492
6492
 
6493
- function methods$8(api) {
6493
+ function methods$9(api) {
6494
6494
  const { get, list } = defaultMethods(api, '/categories', ['list', 'get']);
6495
6495
 
6496
6496
  return {
@@ -6502,7 +6502,7 @@
6502
6502
  };
6503
6503
  }
6504
6504
 
6505
- function methods$7(api) {
6505
+ function methods$8(api) {
6506
6506
  const { get, list } = defaultMethods(api, '/subscriptions', ['list', 'get']);
6507
6507
  return {
6508
6508
  get: (id, ...args) => {
@@ -6567,7 +6567,7 @@
6567
6567
  };
6568
6568
  }
6569
6569
 
6570
- function methods$6(api) {
6570
+ function methods$7(api) {
6571
6571
  const { get, list } = defaultMethods(api, '/invoices', ['list', 'get']);
6572
6572
  return {
6573
6573
  get: (id, ...args) => {
@@ -6578,7 +6578,7 @@
6578
6578
  };
6579
6579
  }
6580
6580
 
6581
- function methods$5(api, opt) {
6581
+ function methods$6(api, opt) {
6582
6582
  return {
6583
6583
  get: (type, id, query) => {
6584
6584
  return api.cache.getFetch(`content_${type}`, id, () =>
@@ -6594,7 +6594,7 @@
6594
6594
  };
6595
6595
  }
6596
6596
 
6597
- function methods$4(api, opt) {
6597
+ function methods$5(api, opt) {
6598
6598
  return {
6599
6599
  state: null,
6600
6600
  menuState: null,
@@ -6889,7 +6889,7 @@
6889
6889
  }
6890
6890
  }
6891
6891
 
6892
- function methods$3(api, opt) {
6892
+ function methods$4(api, opt) {
6893
6893
  return {
6894
6894
  /**
6895
6895
  * Get the decoded session as an object of session values
@@ -7277,7 +7277,7 @@
7277
7277
  * @returns {Promise<Cart>}
7278
7278
  */
7279
7279
  async getCart() {
7280
- const cart = await methods$a(this.api, this.options).get();
7280
+ const cart = await methods$b(this.api, this.options).get();
7281
7281
 
7282
7282
  if (!cart) {
7283
7283
  throw new Error('Cart not found');
@@ -7305,7 +7305,7 @@
7305
7305
  }
7306
7306
  }
7307
7307
 
7308
- const updatedCart = await methods$a(this.api, this.options).update(
7308
+ const updatedCart = await methods$b(this.api, this.options).update(
7309
7309
  updateData,
7310
7310
  );
7311
7311
 
@@ -7318,7 +7318,7 @@
7318
7318
  * @returns {Promise<object>}
7319
7319
  */
7320
7320
  async getSettings() {
7321
- return methods$4(this.api, this.options).get();
7321
+ return methods$5(this.api, this.options).get();
7322
7322
  }
7323
7323
 
7324
7324
  /**
@@ -12934,7 +12934,7 @@
12934
12934
  }
12935
12935
 
12936
12936
  async _getPaymentMethods() {
12937
- const paymentMethods = await methods$4(this.api, this.options).payments();
12937
+ const paymentMethods = await methods$5(this.api, this.options).payments();
12938
12938
 
12939
12939
  if (paymentMethods.error) {
12940
12940
  throw new Error(paymentMethods.error);
@@ -13164,7 +13164,7 @@
13164
13164
  }
13165
13165
  }
13166
13166
 
13167
- function methods$2(api, opt) {
13167
+ function methods$3(api, opt) {
13168
13168
  return {
13169
13169
  code: null,
13170
13170
  state: null,
@@ -13220,7 +13220,7 @@
13220
13220
  return typeof navigator === 'object' ? navigator.language : 'en-US';
13221
13221
  }
13222
13222
 
13223
- function methods$1(api, opt) {
13223
+ function methods$2(api, opt) {
13224
13224
  // Reads a store setting without ever returning a Promise. Until settings
13225
13225
  // load, this starts the request and reports `loaded: false`.
13226
13226
  function readSetting(path, def) {
@@ -13441,7 +13441,7 @@
13441
13441
  };
13442
13442
  }
13443
13443
 
13444
- function methods(api, _opt) {
13444
+ function methods$1(api, _opt) {
13445
13445
  return {
13446
13446
  /**
13447
13447
  * Make a request to an app function and greceiveet a response
@@ -13510,6 +13510,85 @@
13510
13510
  };
13511
13511
  }
13512
13512
 
13513
+ const PURPOSES = ['analytics', 'marketing', 'sale_of_data'];
13514
+
13515
+ /**
13516
+ * The page the shopper landed on, which the consent call sends along: the
13517
+ * first URL of this document and its referrer. Read when needed, so nothing
13518
+ * is kept before the shopper answers.
13519
+ *
13520
+ * @returns {{url: string, referrer: string} | undefined}
13521
+ */
13522
+ function getLanding() {
13523
+ if (isServer()) {
13524
+ return undefined;
13525
+ }
13526
+
13527
+ let url = window.location.href;
13528
+
13529
+ try {
13530
+ const [navigation] = window.performance.getEntriesByType('navigation');
13531
+ if (navigation?.name) {
13532
+ url = navigation.name;
13533
+ }
13534
+ } catch {
13535
+ // Navigation timing is not available
13536
+ }
13537
+
13538
+ return { url, referrer: window.document.referrer || '' };
13539
+ }
13540
+
13541
+ function methods(api) {
13542
+ // Calls run one after another. Consent tools fire several events for one
13543
+ // answer, and the first call may set the visitor cookie that the next one
13544
+ // has to send: in parallel, each would start a visitor of its own.
13545
+ let queue = Promise.resolve();
13546
+
13547
+ return {
13548
+ /**
13549
+ * Records the shopper's consent for attribution, from the storefront's
13550
+ * consent banner. Purposes left out stay as they are.
13551
+ *
13552
+ * Calls the storefront's own origin, so that Swell sees the page's
13553
+ * cookies and sets the visitor cookie on the storefront's domain.
13554
+ *
13555
+ * @param {{analytics?: boolean, marketing?: boolean, sale_of_data?: boolean}} consent
13556
+ */
13557
+ set(consent = {}) {
13558
+ if (isServer()) {
13559
+ return Promise.reject(new Error('consent.set() runs in the browser'));
13560
+ }
13561
+
13562
+ const data = {};
13563
+
13564
+ for (const purpose of PURPOSES) {
13565
+ // camelCase clients pass saleOfData
13566
+ const camel = purpose.replace(/_(\w)/g, (_m, c) => c.toUpperCase());
13567
+ const value = consent[purpose] ?? consent[camel];
13568
+
13569
+ if (typeof value === 'boolean') {
13570
+ data[purpose] = value;
13571
+ }
13572
+ }
13573
+
13574
+ const result = queue.then(() =>
13575
+ api.request(
13576
+ 'put',
13577
+ '/session/consent',
13578
+ { ...data, landing: getLanding() },
13579
+ undefined,
13580
+ { fullUrl: `${window.location.origin}/api/session/consent` },
13581
+ ),
13582
+ );
13583
+
13584
+ // A failed call doesn't hold back the next one
13585
+ queue = result.catch(() => undefined);
13586
+
13587
+ return result;
13588
+ },
13589
+ };
13590
+ }
13591
+
13513
13592
  /**
13514
13593
  * @template {'snake' | 'camel'} C
13515
13594
  * @typedef {import('../types').InitOptions<C>} InitOptions<C>
@@ -13541,7 +13620,7 @@
13541
13620
  const api = {};
13542
13621
 
13543
13622
  Object.assign(api, {
13544
- version: '5.9.3',
13623
+ version: '5.9.4',
13545
13624
  options,
13546
13625
  request,
13547
13626
 
@@ -13595,39 +13674,41 @@
13595
13674
  return api.request('delete', url, data);
13596
13675
  },
13597
13676
 
13598
- cache: methods$d(options),
13677
+ cache: methods$e(options),
13599
13678
 
13600
- card: methods$e(options),
13679
+ card: methods$f(options),
13601
13680
 
13602
- cart: methods$a(api, options),
13681
+ cart: methods$b(api, options),
13603
13682
 
13604
- account: methods$9(api),
13683
+ account: methods$a(api),
13605
13684
 
13606
13685
  app: new AppController(api, options),
13607
13686
 
13608
- products: methods$b(api, options),
13687
+ products: methods$c(api, options),
13609
13688
 
13610
- categories: methods$8(api),
13689
+ categories: methods$9(api),
13611
13690
 
13612
- attributes: methods$c(api),
13691
+ attributes: methods$d(api),
13613
13692
 
13614
- subscriptions: methods$7(api),
13693
+ subscriptions: methods$8(api),
13615
13694
 
13616
- invoices: methods$6(api),
13695
+ invoices: methods$7(api),
13617
13696
 
13618
- content: methods$5(api, options),
13697
+ content: methods$6(api, options),
13619
13698
 
13620
- settings: methods$4(api, options),
13699
+ settings: methods$5(api, options),
13621
13700
 
13622
13701
  payment: new PaymentController(api, options),
13623
13702
 
13624
- locale: methods$2(api, options),
13703
+ locale: methods$3(api, options),
13625
13704
 
13626
- currency: methods$1(api, options),
13705
+ currency: methods$2(api, options),
13627
13706
 
13628
- session: methods$3(api, options),
13707
+ session: methods$4(api, options),
13629
13708
 
13630
- functions: methods(api),
13709
+ functions: methods$1(api),
13710
+
13711
+ consent: methods(api),
13631
13712
 
13632
13713
  utils,
13633
13714
 
@@ -13701,6 +13782,13 @@
13701
13782
  reqHeaders['X-Session'] = session;
13702
13783
  }
13703
13784
 
13785
+ // The storefront's visitor, for attribution: the cookie is on the
13786
+ // storefront's domain and doesn't reach the store's API host
13787
+ const visitor = allOptions.getCookie('swell-visitor');
13788
+ if (visitor) {
13789
+ reqHeaders['X-Visitor'] = visitor;
13790
+ }
13791
+
13704
13792
  if (locale) {
13705
13793
  reqHeaders['X-Locale'] = locale;
13706
13794
  }