swell-js 4.0.2 → 4.0.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.
package/dist/api.mjs CHANGED
@@ -7,7 +7,7 @@ import methods$5 from './products.mjs';
7
7
  import methods$6 from './categories.mjs';
8
8
  import methods$7 from './attributes.mjs';
9
9
  import methods$8 from './subscriptions.mjs';
10
- import { d as defaultMethods, z as trimEnd, A as utils, B as trimStart, C as trimBoth, t as toSnake, D as stringifyQuery, E as base64Encode, a as toCamel, F as setOptions } from './index.78b6af02.mjs';
10
+ import { d as defaultMethods, z as trimEnd, A as utils, B as trimStart, C as trimBoth, t as toSnake, D as stringifyQuery, E as base64Encode, a as toCamel, F as setOptions } from './index.6938673d.mjs';
11
11
  import methods$9 from './content.mjs';
12
12
  import methods$a from './settings.mjs';
13
13
  import PaymentController from './payment.mjs';
@@ -135,7 +135,7 @@ const options = {
135
135
  };
136
136
 
137
137
  const api = {
138
- version: '4.0.2',
138
+ version: '4.0.4',
139
139
  options,
140
140
  request,
141
141
 
@@ -1,4 +1,4 @@
1
- import { d as defaultMethods } from './index.78b6af02.mjs';
1
+ import { d as defaultMethods } from './index.6938673d.mjs';
2
2
  import cacheApi from './cache.mjs';
3
3
  import 'qs';
4
4
  import './find.fd7de00e.mjs';
package/dist/cache.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { s as set, m as merge, a as toCamel, h as toCamelPath, j as getOptions } from './index.78b6af02.mjs';
1
+ import { s as set, m as merge, a as toCamel, h as toCamelPath, j as getOptions } from './index.6938673d.mjs';
2
2
  import { g as get } from './find.fd7de00e.mjs';
3
3
  import 'qs';
4
4
  import './round.a606b844.mjs';
package/dist/card.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { t as toSnake, v as vaultRequest } from './index.78b6af02.mjs';
1
+ import { t as toSnake, v as vaultRequest } from './index.6938673d.mjs';
2
2
  import 'qs';
3
3
  import './find.fd7de00e.mjs';
4
4
  import './round.a606b844.mjs';
package/dist/cart.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import 'qs';
2
- import { c as cloneDeep } from './index.78b6af02.mjs';
2
+ import { c as cloneDeep } from './index.6938673d.mjs';
3
3
  import 'deepmerge';
4
4
  import 'fast-case';
5
5
  import { cleanProductOptions } from './products.mjs';
@@ -38,9 +38,12 @@ function methods(request, options) {
38
38
 
39
39
  const { handler, resolve, reject } = this.pendingRequests.shift();
40
40
 
41
- return Promise.resolve().then(handler).then(resolve, reject).finally(() => {
42
- this.nextRequest();
43
- });
41
+ return Promise.resolve()
42
+ .then(handler)
43
+ .then(resolve, reject)
44
+ .finally(() => {
45
+ this.nextRequest();
46
+ });
44
47
  },
45
48
 
46
49
  async requestStateSync(handler) {
@@ -1,4 +1,4 @@
1
- import { d as defaultMethods } from './index.78b6af02.mjs';
1
+ import { d as defaultMethods } from './index.6938673d.mjs';
2
2
  import cacheApi from './cache.mjs';
3
3
  import 'qs';
4
4
  import './find.fd7de00e.mjs';
package/dist/content.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import cacheApi from './cache.mjs';
2
- import './index.78b6af02.mjs';
2
+ import './index.6938673d.mjs';
3
3
  import 'qs';
4
4
  import './find.fd7de00e.mjs';
5
5
  import './round.a606b844.mjs';
package/dist/cookie.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { i as isServer } from './index.78b6af02.mjs';
1
+ import { i as isServer } from './index.6938673d.mjs';
2
2
  import 'qs';
3
3
  import './find.fd7de00e.mjs';
4
4
  import './round.a606b844.mjs';
package/dist/currency.mjs CHANGED
@@ -25,7 +25,8 @@ function methods(request, opt) {
25
25
  selected() {
26
26
  if (!this.code) {
27
27
  this.set(
28
- opt.getCookie('swell-currency') || opt.api.settings.get('store.currency'),
28
+ opt.getCookie('swell-currency') ||
29
+ opt.api.settings.get('store.currency'),
29
30
  );
30
31
  }
31
32
 
@@ -69,8 +70,7 @@ function methods(request, opt) {
69
70
  const formatCode = params.code || code;
70
71
  const formatRate = params.rate || rate;
71
72
  const formatLocale = params.locale || this.locale;
72
- const formatDecimals =
73
- 'decimals' in params ? params.decimals : decimals;
73
+ const formatDecimals = 'decimals' in params ? params.decimals : decimals;
74
74
  const { convert = true } = params;
75
75
 
76
76
  let formatAmount = amount;
@@ -171,8 +171,8 @@ function methods(request, opt) {
171
171
  ? 'up'
172
172
  : 'down'
173
173
  : diff <= -0.5
174
- ? 'down'
175
- : 'up'
174
+ ? 'down'
175
+ : 'up'
176
176
  : config.round;
177
177
 
178
178
  switch (direction) {
@@ -1590,64 +1590,62 @@ function defaultMethods(request, uri, methods) {
1590
1590
  };
1591
1591
  }
1592
1592
 
1593
- async function vaultRequest(method, url, data, opt = undefined) {
1594
- const vaultUrl = options.vaultUrl;
1595
- const timeout = options.timeout;
1596
- const requestId = vaultRequestId();
1597
- const callback = `swell_vault_response_${requestId}`;
1598
-
1599
- data = {
1593
+ async function vaultRequest(method, url, data) {
1594
+ const { vaultUrl, timeout, key } = options;
1595
+ const requestData = {
1600
1596
  $jsonp: {
1601
1597
  method,
1602
- callback,
1598
+ callback: 'none',
1603
1599
  },
1604
1600
  $data: data,
1605
- $key: options.key,
1601
+ $key: key,
1606
1602
  };
1607
-
1608
- return new Promise((resolve, reject) => {
1609
- const script = document.createElement('script');
1610
- script.type = 'text/javascript';
1611
- script.src = `${trimEnd(vaultUrl)}/${trimStart(url)}?${serializeData(
1612
- data,
1613
- )}`;
1614
-
1615
- const errorTimeout = setTimeout(() => {
1616
- window[callback]({
1617
- $error: `Request timed out after ${timeout / 1000} seconds`,
1618
- $status: 500,
1619
- });
1620
- }, timeout);
1621
-
1622
- window[callback] = (result) => {
1623
- clearTimeout(errorTimeout);
1624
- if (result && result.$error) {
1625
- const err = new Error(result.$error);
1626
- err.code = 'request_error';
1627
- err.status = result.$status;
1628
- reject(err);
1629
- } else if (!result || result.$status >= 300) {
1630
- const err = new Error(
1631
- 'A connection error occurred while making the request',
1603
+ const requestUrl = `${trimEnd(vaultUrl)}/${trimStart(url)}?${serializeData(
1604
+ requestData,
1605
+ )}`;
1606
+
1607
+ const abortController = new AbortController();
1608
+ const id = setTimeout(() => abortController.abort(), timeout);
1609
+
1610
+ const result = await fetch(requestUrl, {
1611
+ signal: abortController.signal,
1612
+ })
1613
+ .then((response) => response.json())
1614
+ .catch((error) => {
1615
+ if (error.name === 'AbortError') {
1616
+ const timeoutError = new Error(
1617
+ `Request timed out after ${timeout / 1000} seconds`,
1632
1618
  );
1633
- err.code = 'connection_error';
1634
- err.status = result.$status;
1635
- reject(err);
1636
- } else {
1637
- resolve(result.$data);
1619
+
1620
+ timeoutError.status = 500;
1621
+
1622
+ throw timeoutError;
1638
1623
  }
1639
- delete window[callback];
1640
- script.parentNode.removeChild(script);
1641
- };
1642
1624
 
1643
- document.getElementsByTagName('head')[0].appendChild(script);
1644
- });
1645
- }
1625
+ throw new Error(error.message);
1626
+ });
1627
+
1628
+ clearTimeout(id);
1646
1629
 
1647
- function vaultRequestId() {
1648
- window.__swell_vault_request_id = window.__swell_vault_request_id || 0;
1649
- window.__swell_vault_request_id++;
1650
- return window.__swell_vault_request_id;
1630
+ if (result?.$error) {
1631
+ const requestError = new Error(result.$error);
1632
+
1633
+ requestError.code = 'request_error';
1634
+ requestError.status = result.$status;
1635
+
1636
+ throw requestError;
1637
+ } else if (!result || result.$status >= 300) {
1638
+ const connectionError = new Error(
1639
+ 'A connection error occurred while making the request',
1640
+ );
1641
+
1642
+ connectionError.code = 'connection_error';
1643
+ connectionError.status = result?.$status;
1644
+
1645
+ throw connectionError;
1646
+ }
1647
+
1648
+ return result.$data;
1651
1649
  }
1652
1650
 
1653
1651
  function serializeData(data) {
@@ -1666,7 +1664,9 @@ function serializeData(data) {
1666
1664
  }
1667
1665
  return s.join('&').replace(' ', '+');
1668
1666
  }
1667
+
1669
1668
  const rbracket = /\[\]$/;
1669
+
1670
1670
  function buildParams(key, obj, add) {
1671
1671
  let name;
1672
1672
  if (obj instanceof Array) {
package/dist/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  export { default } from './api.mjs';
2
2
  import './card.mjs';
3
- import './index.78b6af02.mjs';
3
+ import './index.6938673d.mjs';
4
4
  import 'qs';
5
5
  import './find.fd7de00e.mjs';
6
6
  import './round.a606b844.mjs';
package/dist/payment.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import methods from './cart.mjs';
2
2
  import methods$1 from './settings.mjs';
3
- import { b as isObject, k as isFunction, l as loadScript, c as cloneDeep, t as toSnake, v as vaultRequest, p as pick, n as toLower, o as isEmpty, r as reduce, q as isLiveMode, w as map, x as getLocationParams, y as removeUrlParams, a as toCamel } from './index.78b6af02.mjs';
3
+ import { b as isObject, k as isFunction, l as loadScript, c as cloneDeep, t as toSnake, v as vaultRequest, p as pick, n as toLower, o as isEmpty, r as reduce, q as isLiveMode, w as map, x as getLocationParams, y as removeUrlParams, a as toCamel } from './index.6938673d.mjs';
4
4
  import { g as get, t as toNumber } from './find.fd7de00e.mjs';
5
5
  import 'qs';
6
6
  import 'deepmerge';
@@ -638,8 +638,8 @@ function setBancontactOwner(source, data) {
638
638
  ...(billingData.phone
639
639
  ? { phone: billingData.phone }
640
640
  : account.phone
641
- ? { phone: account.phone }
642
- : {}),
641
+ ? { phone: account.phone }
642
+ : {}),
643
643
  ...(!isEmpty(billingAddress) ? { address: billingAddress } : {}),
644
644
  };
645
645
  }
@@ -2053,9 +2053,8 @@ class BraintreeGooglePayment extends Payment {
2053
2053
 
2054
2054
  async _onClick(googlePayment, paymentDataRequest) {
2055
2055
  try {
2056
- const paymentData = await this.googleClient.loadPaymentData(
2057
- paymentDataRequest,
2058
- );
2056
+ const paymentData =
2057
+ await this.googleClient.loadPaymentData(paymentDataRequest);
2059
2058
 
2060
2059
  if (paymentData) {
2061
2060
  await this._submitPayment(googlePayment, paymentData);
package/dist/products.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { d as defaultMethods, r as reduce, t as toSnake, a as toCamel, f as snakeCase, u as uniq, g as isEqual } from './index.78b6af02.mjs';
1
+ import { d as defaultMethods, r as reduce, t as toSnake, a as toCamel, f as snakeCase, u as uniq, g as isEqual } from './index.6938673d.mjs';
2
2
  import cacheApi from './cache.mjs';
3
3
  import methods$1 from './attributes.mjs';
4
4
  import { g as get, f as find } from './find.fd7de00e.mjs';
@@ -191,8 +191,8 @@ function findPurchaseOption(product, purchaseOption) {
191
191
  typeof purchaseOption === 'string'
192
192
  ? purchaseOption
193
193
  : plan !== undefined
194
- ? 'subscription'
195
- : 'standard',
194
+ ? 'subscription'
195
+ : 'standard',
196
196
  );
197
197
  let option = get(product, `purchase_options.${type}`);
198
198
  if (!option && type !== 'standard') {
package/dist/settings.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { s as set, a as toCamel, m as merge, c as cloneDeep, b as isObject, e as camelCase } from './index.78b6af02.mjs';
1
+ import { s as set, a as toCamel, m as merge, c as cloneDeep, b as isObject, e as camelCase } from './index.6938673d.mjs';
2
2
  import { g as get, f as find } from './find.fd7de00e.mjs';
3
3
  import 'qs';
4
4
  import './round.a606b844.mjs';
@@ -1,5 +1,5 @@
1
1
  import { cleanProductOptions } from './products.mjs';
2
- import { d as defaultMethods } from './index.78b6af02.mjs';
2
+ import { d as defaultMethods } from './index.6938673d.mjs';
3
3
  import cacheApi from './cache.mjs';
4
4
  import './attributes.mjs';
5
5
  import './find.fd7de00e.mjs';
package/dist/swell.cjs CHANGED
@@ -4614,64 +4614,62 @@ function defaultMethods(request, uri, methods) {
4614
4614
  };
4615
4615
  }
4616
4616
 
4617
- async function vaultRequest(method, url, data, opt = undefined) {
4618
- const vaultUrl = options$1.vaultUrl;
4619
- const timeout = options$1.timeout;
4620
- const requestId = vaultRequestId();
4621
- const callback = `swell_vault_response_${requestId}`;
4622
-
4623
- data = {
4617
+ async function vaultRequest(method, url, data) {
4618
+ const { vaultUrl, timeout, key } = options$1;
4619
+ const requestData = {
4624
4620
  $jsonp: {
4625
4621
  method,
4626
- callback,
4622
+ callback: 'none',
4627
4623
  },
4628
4624
  $data: data,
4629
- $key: options$1.key,
4625
+ $key: key,
4630
4626
  };
4627
+ const requestUrl = `${trimEnd(vaultUrl)}/${trimStart(url)}?${serializeData(
4628
+ requestData,
4629
+ )}`;
4630
+
4631
+ const abortController = new AbortController();
4632
+ const id = setTimeout(() => abortController.abort(), timeout);
4633
+
4634
+ const result = await fetch(requestUrl, {
4635
+ signal: abortController.signal,
4636
+ })
4637
+ .then((response) => response.json())
4638
+ .catch((error) => {
4639
+ if (error.name === 'AbortError') {
4640
+ const timeoutError = new Error(
4641
+ `Request timed out after ${timeout / 1000} seconds`,
4642
+ );
4631
4643
 
4632
- return new Promise((resolve, reject) => {
4633
- const script = document.createElement('script');
4634
- script.type = 'text/javascript';
4635
- script.src = `${trimEnd(vaultUrl)}/${trimStart(url)}?${serializeData(
4636
- data,
4637
- )}`;
4644
+ timeoutError.status = 500;
4638
4645
 
4639
- const errorTimeout = setTimeout(() => {
4640
- window[callback]({
4641
- $error: `Request timed out after ${timeout / 1000} seconds`,
4642
- $status: 500,
4643
- });
4644
- }, timeout);
4645
-
4646
- window[callback] = (result) => {
4647
- clearTimeout(errorTimeout);
4648
- if (result && result.$error) {
4649
- const err = new Error(result.$error);
4650
- err.code = 'request_error';
4651
- err.status = result.$status;
4652
- reject(err);
4653
- } else if (!result || result.$status >= 300) {
4654
- const err = new Error(
4655
- 'A connection error occurred while making the request',
4656
- );
4657
- err.code = 'connection_error';
4658
- err.status = result.$status;
4659
- reject(err);
4660
- } else {
4661
- resolve(result.$data);
4646
+ throw timeoutError;
4662
4647
  }
4663
- delete window[callback];
4664
- script.parentNode.removeChild(script);
4665
- };
4666
4648
 
4667
- document.getElementsByTagName('head')[0].appendChild(script);
4668
- });
4669
- }
4649
+ throw new Error(error.message);
4650
+ });
4651
+
4652
+ clearTimeout(id);
4653
+
4654
+ if (result?.$error) {
4655
+ const requestError = new Error(result.$error);
4656
+
4657
+ requestError.code = 'request_error';
4658
+ requestError.status = result.$status;
4670
4659
 
4671
- function vaultRequestId() {
4672
- window.__swell_vault_request_id = window.__swell_vault_request_id || 0;
4673
- window.__swell_vault_request_id++;
4674
- return window.__swell_vault_request_id;
4660
+ throw requestError;
4661
+ } else if (!result || result.$status >= 300) {
4662
+ const connectionError = new Error(
4663
+ 'A connection error occurred while making the request',
4664
+ );
4665
+
4666
+ connectionError.code = 'connection_error';
4667
+ connectionError.status = result?.$status;
4668
+
4669
+ throw connectionError;
4670
+ }
4671
+
4672
+ return result.$data;
4675
4673
  }
4676
4674
 
4677
4675
  function serializeData(data) {
@@ -4690,7 +4688,9 @@ function serializeData(data) {
4690
4688
  }
4691
4689
  return s.join('&').replace(' ', '+');
4692
4690
  }
4691
+
4693
4692
  const rbracket = /\[\]$/;
4693
+
4694
4694
  function buildParams(key, obj, add) {
4695
4695
  let name;
4696
4696
  if (obj instanceof Array) {
@@ -5343,8 +5343,8 @@ function findPurchaseOption(product, purchaseOption) {
5343
5343
  typeof purchaseOption === 'string'
5344
5344
  ? purchaseOption
5345
5345
  : plan !== undefined
5346
- ? 'subscription'
5347
- : 'standard',
5346
+ ? 'subscription'
5347
+ : 'standard',
5348
5348
  );
5349
5349
  let option = get(product, `purchase_options.${type}`);
5350
5350
  if (!option && type !== 'standard') {
@@ -5602,9 +5602,12 @@ function methods$a(request, options) {
5602
5602
 
5603
5603
  const { handler, resolve, reject } = this.pendingRequests.shift();
5604
5604
 
5605
- return Promise.resolve().then(handler).then(resolve, reject).finally(() => {
5606
- this.nextRequest();
5607
- });
5605
+ return Promise.resolve()
5606
+ .then(handler)
5607
+ .then(resolve, reject)
5608
+ .finally(() => {
5609
+ this.nextRequest();
5610
+ });
5608
5611
  },
5609
5612
 
5610
5613
  async requestStateSync(handler) {
@@ -6901,8 +6904,8 @@ function setBancontactOwner(source, data) {
6901
6904
  ...(billingData.phone
6902
6905
  ? { phone: billingData.phone }
6903
6906
  : account.phone
6904
- ? { phone: account.phone }
6905
- : {}),
6907
+ ? { phone: account.phone }
6908
+ : {}),
6906
6909
  ...(!isEmpty(billingAddress) ? { address: billingAddress } : {}),
6907
6910
  };
6908
6911
  }
@@ -8316,9 +8319,8 @@ class BraintreeGooglePayment extends Payment {
8316
8319
 
8317
8320
  async _onClick(googlePayment, paymentDataRequest) {
8318
8321
  try {
8319
- const paymentData = await this.googleClient.loadPaymentData(
8320
- paymentDataRequest,
8321
- );
8322
+ const paymentData =
8323
+ await this.googleClient.loadPaymentData(paymentDataRequest);
8322
8324
 
8323
8325
  if (paymentData) {
8324
8326
  await this._submitPayment(googlePayment, paymentData);
@@ -9810,7 +9812,8 @@ function methods$1(request, opt) {
9810
9812
  selected() {
9811
9813
  if (!this.code) {
9812
9814
  this.set(
9813
- opt.getCookie('swell-currency') || opt.api.settings.get('store.currency'),
9815
+ opt.getCookie('swell-currency') ||
9816
+ opt.api.settings.get('store.currency'),
9814
9817
  );
9815
9818
  }
9816
9819
 
@@ -9854,8 +9857,7 @@ function methods$1(request, opt) {
9854
9857
  const formatCode = params.code || code;
9855
9858
  const formatRate = params.rate || rate;
9856
9859
  const formatLocale = params.locale || this.locale;
9857
- const formatDecimals =
9858
- 'decimals' in params ? params.decimals : decimals;
9860
+ const formatDecimals = 'decimals' in params ? params.decimals : decimals;
9859
9861
  const { convert = true } = params;
9860
9862
 
9861
9863
  let formatAmount = amount;
@@ -9956,8 +9958,8 @@ function methods$1(request, opt) {
9956
9958
  ? 'up'
9957
9959
  : 'down'
9958
9960
  : diff <= -0.5
9959
- ? 'down'
9960
- : 'up'
9961
+ ? 'down'
9962
+ : 'up'
9961
9963
  : config.round;
9962
9964
 
9963
9965
  switch (direction) {
@@ -10057,7 +10059,7 @@ const options = {
10057
10059
  };
10058
10060
 
10059
10061
  const api = {
10060
- version: '4.0.2',
10062
+ version: '4.0.4',
10061
10063
  options,
10062
10064
  request,
10063
10065