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.
@@ -13182,7 +13182,30 @@
13182
13182
 
13183
13183
  const FORMATTERS = {};
13184
13184
 
13185
+ // A currency code is three letters. Anything else, such as a pending settings
13186
+ // Promise that was stringified to "[object Promise]", is not a currency.
13187
+ function isCurrencyCode(value) {
13188
+ return typeof value === 'string' && /^[a-z]{3}$/i.test(value);
13189
+ }
13190
+
13191
+ function defaultLocale() {
13192
+ return typeof navigator === 'object' ? navigator.language : 'en-US';
13193
+ }
13194
+
13185
13195
  function methods$1(api, opt) {
13196
+ // Reads a store setting without ever returning a Promise. Until settings
13197
+ // load, this starts the request and reports `loaded: false`.
13198
+ function readSetting(path, def) {
13199
+ const value = opt.api.settings.get(path, def);
13200
+
13201
+ if (value && typeof value.then === 'function') {
13202
+ value.catch(() => {}); // callers that await settings report the error
13203
+ return { loaded: false, value: def };
13204
+ }
13205
+
13206
+ return { loaded: true, value };
13207
+ }
13208
+
13186
13209
  return {
13187
13210
  code: null,
13188
13211
  state: null,
@@ -13200,52 +13223,72 @@
13200
13223
 
13201
13224
  selected() {
13202
13225
  if (!this.code) {
13203
- this.set(
13204
- opt.getCookie('swell-currency') ||
13205
- opt.api.settings.get('store.currency'),
13206
- );
13226
+ const cookieCode = opt.getCookie('swell-currency');
13227
+
13228
+ if (isCurrencyCode(cookieCode)) {
13229
+ this.set(cookieCode);
13230
+ } else {
13231
+ const storeCode = readSetting('store.currency');
13232
+
13233
+ if (!storeCode.loaded) {
13234
+ // Settings are still loading: use a stand-in without saving it,
13235
+ // so the store currency is picked up once they arrive.
13236
+ return 'USD';
13237
+ }
13238
+
13239
+ this.set(isCurrencyCode(storeCode.value) ? storeCode.value : 'USD');
13240
+ }
13207
13241
  }
13208
13242
 
13209
13243
  return this.code;
13210
13244
  },
13211
13245
 
13212
13246
  get() {
13213
- if (!this.code) {
13214
- this.code = this.selected();
13247
+ const code = this.selected();
13248
+
13249
+ if (this.state) {
13250
+ return this.state;
13215
13251
  }
13216
- if (!this.state) {
13217
- this.state = this.set(this.code);
13252
+
13253
+ if (!readSetting('store.currencies').loaded) {
13254
+ // Not cached, so the currency's rate and decimals apply once
13255
+ // settings load.
13256
+ return { code };
13218
13257
  }
13219
- return this.state;
13258
+
13259
+ return this.set(code);
13220
13260
  },
13221
13261
 
13222
13262
  set(code = 'USD') {
13223
- this.code = code;
13224
- this.state = find$1(this.list(), { code }) || { code };
13263
+ if (!isCurrencyCode(code)) {
13264
+ // Never store or persist something that isn't a currency code.
13265
+ return this.state || { code: this.code || 'USD' };
13266
+ }
13225
13267
 
13226
- this.locale = String(
13227
- opt.api.settings.get(
13228
- 'store.locale',
13229
- typeof navigator === 'object' ? navigator.language : 'en-US',
13230
- ),
13231
- );
13268
+ const list = readSetting('store.currencies', []);
13269
+ const state = (list.loaded && find$1(list.value, { code })) || { code };
13270
+
13271
+ this.code = code;
13272
+ // Only cache a state built from the loaded currency list.
13273
+ this.state = list.loaded ? state : null;
13274
+ this.locale = String(readSetting('store.locale', defaultLocale()).value);
13232
13275
 
13233
13276
  opt.setCookie('swell-currency', code);
13234
13277
 
13235
- return this.state;
13278
+ return state;
13236
13279
  },
13237
13280
 
13238
13281
  format(amount, params = {}) {
13239
13282
  let state = this.get();
13240
13283
  if (params.code && params.code !== state.code) {
13241
- const list = this.list();
13284
+ const list = readSetting('store.currencies', []).value;
13242
13285
  state = find$1(list, { code: params.code }) || { code: params.code };
13243
13286
  }
13244
13287
 
13245
13288
  const { code = 'USD', type, decimals, rate } = state;
13246
13289
  const formatCode = params.code || code;
13247
13290
  const formatRate = params.rate || rate;
13248
- const formatLocale = params.locale || this.locale;
13291
+ const formatLocale = params.locale || this.locale || defaultLocale();
13249
13292
  const formatDecimals = 'decimals' in params ? params.decimals : decimals;
13250
13293
  const { convert = true } = params;
13251
13294
 
@@ -13470,7 +13513,7 @@
13470
13513
  const api = {};
13471
13514
 
13472
13515
  Object.assign(api, {
13473
- version: '5.9.1',
13516
+ version: '5.9.2',
13474
13517
  options,
13475
13518
  request,
13476
13519
 
@@ -13586,8 +13629,10 @@
13586
13629
 
13587
13630
  const session = allOptions.session || allOptions.getCookie('swell-session');
13588
13631
  const locale = allOptions.locale || allOptions.getCookie('swell-locale');
13632
+ const cookieCurrency = allOptions.getCookie('swell-currency');
13589
13633
  const currency =
13590
- allOptions.currency || allOptions.getCookie('swell-currency');
13634
+ allOptions.currency ||
13635
+ (isCurrencyCode(cookieCurrency) ? cookieCurrency : undefined);
13591
13636
  const path = allOptions.path || '/api';
13592
13637
 
13593
13638
  const baseUrl = `${allOptions.url}${allOptions.base || ''}${path}`;