swell-js 5.9.1 → 5.9.2

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
@@ -15,7 +15,7 @@ import methods$b from './content.mjs';
15
15
  import methods$c from './settings.mjs';
16
16
  import PaymentController from './payment.mjs';
17
17
  import methods$d from './locale.mjs';
18
- import methods$e from './currency.mjs';
18
+ import methods$e, { isCurrencyCode } from './currency.mjs';
19
19
  import './round.c68b8be5.mjs';
20
20
 
21
21
  class AppComponent {
@@ -455,7 +455,7 @@ function swell(initStore = undefined, initKey, initOptions = {}) {
455
455
  const api = {};
456
456
 
457
457
  Object.assign(api, {
458
- version: '5.9.1',
458
+ version: '5.9.2',
459
459
  options,
460
460
  request,
461
461
 
@@ -571,8 +571,10 @@ function swell(initStore = undefined, initKey, initOptions = {}) {
571
571
 
572
572
  const session = allOptions.session || allOptions.getCookie('swell-session');
573
573
  const locale = allOptions.locale || allOptions.getCookie('swell-locale');
574
+ const cookieCurrency = allOptions.getCookie('swell-currency');
574
575
  const currency =
575
- allOptions.currency || allOptions.getCookie('swell-currency');
576
+ allOptions.currency ||
577
+ (isCurrencyCode(cookieCurrency) ? cookieCurrency : undefined);
576
578
  const path = allOptions.path || '/api';
577
579
 
578
580
  const baseUrl = `${allOptions.url}${allOptions.base || ''}${path}`;
package/dist/currency.mjs CHANGED
@@ -5,7 +5,30 @@ import 'fast-case';
5
5
 
6
6
  const FORMATTERS = {};
7
7
 
8
+ // A currency code is three letters. Anything else, such as a pending settings
9
+ // Promise that was stringified to "[object Promise]", is not a currency.
10
+ function isCurrencyCode(value) {
11
+ return typeof value === 'string' && /^[a-z]{3}$/i.test(value);
12
+ }
13
+
14
+ function defaultLocale() {
15
+ return typeof navigator === 'object' ? navigator.language : 'en-US';
16
+ }
17
+
8
18
  function methods(api, opt) {
19
+ // Reads a store setting without ever returning a Promise. Until settings
20
+ // load, this starts the request and reports `loaded: false`.
21
+ function readSetting(path, def) {
22
+ const value = opt.api.settings.get(path, def);
23
+
24
+ if (value && typeof value.then === 'function') {
25
+ value.catch(() => {}); // callers that await settings report the error
26
+ return { loaded: false, value: def };
27
+ }
28
+
29
+ return { loaded: true, value };
30
+ }
31
+
9
32
  return {
10
33
  code: null,
11
34
  state: null,
@@ -23,52 +46,72 @@ function methods(api, opt) {
23
46
 
24
47
  selected() {
25
48
  if (!this.code) {
26
- this.set(
27
- opt.getCookie('swell-currency') ||
28
- opt.api.settings.get('store.currency'),
29
- );
49
+ const cookieCode = opt.getCookie('swell-currency');
50
+
51
+ if (isCurrencyCode(cookieCode)) {
52
+ this.set(cookieCode);
53
+ } else {
54
+ const storeCode = readSetting('store.currency');
55
+
56
+ if (!storeCode.loaded) {
57
+ // Settings are still loading: use a stand-in without saving it,
58
+ // so the store currency is picked up once they arrive.
59
+ return 'USD';
60
+ }
61
+
62
+ this.set(isCurrencyCode(storeCode.value) ? storeCode.value : 'USD');
63
+ }
30
64
  }
31
65
 
32
66
  return this.code;
33
67
  },
34
68
 
35
69
  get() {
36
- if (!this.code) {
37
- this.code = this.selected();
70
+ const code = this.selected();
71
+
72
+ if (this.state) {
73
+ return this.state;
38
74
  }
39
- if (!this.state) {
40
- this.state = this.set(this.code);
75
+
76
+ if (!readSetting('store.currencies').loaded) {
77
+ // Not cached, so the currency's rate and decimals apply once
78
+ // settings load.
79
+ return { code };
41
80
  }
42
- return this.state;
81
+
82
+ return this.set(code);
43
83
  },
44
84
 
45
85
  set(code = 'USD') {
46
- this.code = code;
47
- this.state = find(this.list(), { code }) || { code };
86
+ if (!isCurrencyCode(code)) {
87
+ // Never store or persist something that isn't a currency code.
88
+ return this.state || { code: this.code || 'USD' };
89
+ }
48
90
 
49
- this.locale = String(
50
- opt.api.settings.get(
51
- 'store.locale',
52
- typeof navigator === 'object' ? navigator.language : 'en-US',
53
- ),
54
- );
91
+ const list = readSetting('store.currencies', []);
92
+ const state = (list.loaded && find(list.value, { code })) || { code };
93
+
94
+ this.code = code;
95
+ // Only cache a state built from the loaded currency list.
96
+ this.state = list.loaded ? state : null;
97
+ this.locale = String(readSetting('store.locale', defaultLocale()).value);
55
98
 
56
99
  opt.setCookie('swell-currency', code);
57
100
 
58
- return this.state;
101
+ return state;
59
102
  },
60
103
 
61
104
  format(amount, params = {}) {
62
105
  let state = this.get();
63
106
  if (params.code && params.code !== state.code) {
64
- const list = this.list();
107
+ const list = readSetting('store.currencies', []).value;
65
108
  state = find(list, { code: params.code }) || { code: params.code };
66
109
  }
67
110
 
68
111
  const { code = 'USD', type, decimals, rate } = state;
69
112
  const formatCode = params.code || code;
70
113
  const formatRate = params.rate || rate;
71
- const formatLocale = params.locale || this.locale;
114
+ const formatLocale = params.locale || this.locale || defaultLocale();
72
115
  const formatDecimals = 'decimals' in params ? params.decimals : decimals;
73
116
  const { convert = true } = params;
74
117
 
@@ -193,4 +236,4 @@ function methods(api, opt) {
193
236
  };
194
237
  }
195
238
 
196
- export { methods as default };
239
+ export { methods as default, isCurrencyCode };
package/dist/swell.cjs CHANGED
@@ -13037,7 +13037,30 @@ function methods$2(api, opt) {
13037
13037
 
13038
13038
  const FORMATTERS = {};
13039
13039
 
13040
+ // A currency code is three letters. Anything else, such as a pending settings
13041
+ // Promise that was stringified to "[object Promise]", is not a currency.
13042
+ function isCurrencyCode(value) {
13043
+ return typeof value === 'string' && /^[a-z]{3}$/i.test(value);
13044
+ }
13045
+
13046
+ function defaultLocale() {
13047
+ return typeof navigator === 'object' ? navigator.language : 'en-US';
13048
+ }
13049
+
13040
13050
  function methods$1(api, opt) {
13051
+ // Reads a store setting without ever returning a Promise. Until settings
13052
+ // load, this starts the request and reports `loaded: false`.
13053
+ function readSetting(path, def) {
13054
+ const value = opt.api.settings.get(path, def);
13055
+
13056
+ if (value && typeof value.then === 'function') {
13057
+ value.catch(() => {}); // callers that await settings report the error
13058
+ return { loaded: false, value: def };
13059
+ }
13060
+
13061
+ return { loaded: true, value };
13062
+ }
13063
+
13041
13064
  return {
13042
13065
  code: null,
13043
13066
  state: null,
@@ -13055,52 +13078,72 @@ function methods$1(api, opt) {
13055
13078
 
13056
13079
  selected() {
13057
13080
  if (!this.code) {
13058
- this.set(
13059
- opt.getCookie('swell-currency') ||
13060
- opt.api.settings.get('store.currency'),
13061
- );
13081
+ const cookieCode = opt.getCookie('swell-currency');
13082
+
13083
+ if (isCurrencyCode(cookieCode)) {
13084
+ this.set(cookieCode);
13085
+ } else {
13086
+ const storeCode = readSetting('store.currency');
13087
+
13088
+ if (!storeCode.loaded) {
13089
+ // Settings are still loading: use a stand-in without saving it,
13090
+ // so the store currency is picked up once they arrive.
13091
+ return 'USD';
13092
+ }
13093
+
13094
+ this.set(isCurrencyCode(storeCode.value) ? storeCode.value : 'USD');
13095
+ }
13062
13096
  }
13063
13097
 
13064
13098
  return this.code;
13065
13099
  },
13066
13100
 
13067
13101
  get() {
13068
- if (!this.code) {
13069
- this.code = this.selected();
13102
+ const code = this.selected();
13103
+
13104
+ if (this.state) {
13105
+ return this.state;
13070
13106
  }
13071
- if (!this.state) {
13072
- this.state = this.set(this.code);
13107
+
13108
+ if (!readSetting('store.currencies').loaded) {
13109
+ // Not cached, so the currency's rate and decimals apply once
13110
+ // settings load.
13111
+ return { code };
13073
13112
  }
13074
- return this.state;
13113
+
13114
+ return this.set(code);
13075
13115
  },
13076
13116
 
13077
13117
  set(code = 'USD') {
13078
- this.code = code;
13079
- this.state = find$1(this.list(), { code }) || { code };
13118
+ if (!isCurrencyCode(code)) {
13119
+ // Never store or persist something that isn't a currency code.
13120
+ return this.state || { code: this.code || 'USD' };
13121
+ }
13080
13122
 
13081
- this.locale = String(
13082
- opt.api.settings.get(
13083
- 'store.locale',
13084
- typeof navigator === 'object' ? navigator.language : 'en-US',
13085
- ),
13086
- );
13123
+ const list = readSetting('store.currencies', []);
13124
+ const state = (list.loaded && find$1(list.value, { code })) || { code };
13125
+
13126
+ this.code = code;
13127
+ // Only cache a state built from the loaded currency list.
13128
+ this.state = list.loaded ? state : null;
13129
+ this.locale = String(readSetting('store.locale', defaultLocale()).value);
13087
13130
 
13088
13131
  opt.setCookie('swell-currency', code);
13089
13132
 
13090
- return this.state;
13133
+ return state;
13091
13134
  },
13092
13135
 
13093
13136
  format(amount, params = {}) {
13094
13137
  let state = this.get();
13095
13138
  if (params.code && params.code !== state.code) {
13096
- const list = this.list();
13139
+ const list = readSetting('store.currencies', []).value;
13097
13140
  state = find$1(list, { code: params.code }) || { code: params.code };
13098
13141
  }
13099
13142
 
13100
13143
  const { code = 'USD', type, decimals, rate } = state;
13101
13144
  const formatCode = params.code || code;
13102
13145
  const formatRate = params.rate || rate;
13103
- const formatLocale = params.locale || this.locale;
13146
+ const formatLocale = params.locale || this.locale || defaultLocale();
13104
13147
  const formatDecimals = 'decimals' in params ? params.decimals : decimals;
13105
13148
  const { convert = true } = params;
13106
13149
 
@@ -13325,7 +13368,7 @@ function swell(initStore = undefined, initKey, initOptions = {}) {
13325
13368
  const api = {};
13326
13369
 
13327
13370
  Object.assign(api, {
13328
- version: '5.9.1',
13371
+ version: '5.9.2',
13329
13372
  options,
13330
13373
  request,
13331
13374
 
@@ -13441,8 +13484,10 @@ function swell(initStore = undefined, initKey, initOptions = {}) {
13441
13484
 
13442
13485
  const session = allOptions.session || allOptions.getCookie('swell-session');
13443
13486
  const locale = allOptions.locale || allOptions.getCookie('swell-locale');
13487
+ const cookieCurrency = allOptions.getCookie('swell-currency');
13444
13488
  const currency =
13445
- allOptions.currency || allOptions.getCookie('swell-currency');
13489
+ allOptions.currency ||
13490
+ (isCurrencyCode(cookieCurrency) ? cookieCurrency : undefined);
13446
13491
  const path = allOptions.path || '/api';
13447
13492
 
13448
13493
  const baseUrl = `${allOptions.url}${allOptions.base || ''}${path}`;