swell-js 4.0.1 → 4.0.3

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.1',
138
+ version: '4.0.3',
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';