@blotoutio/providers-shop-gpt-sdk 1.22.2 → 1.22.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.
@@ -4,6 +4,92 @@ if (typeof window != 'undefined') {
4
4
  (_a$1 = window[registryKey]) !== null && _a$1 !== void 0 ? _a$1 : (window[registryKey] = {});
5
5
  }
6
6
 
7
+ const CACHE_KEY = 'shop_gpt_currency_rates';
8
+ const CACHE_DURATION = 14400000; // 4 hours
9
+ let ratesCache = null;
10
+ let ratesPromise = null;
11
+ const getCachedRatesFromStorage = () => {
12
+ try {
13
+ const cached = localStorage.getItem(CACHE_KEY);
14
+ if (!cached) {
15
+ return null;
16
+ }
17
+ const data = JSON.parse(cached);
18
+ if (Date.now() > data.expiresAt) {
19
+ localStorage.removeItem(CACHE_KEY);
20
+ return null;
21
+ }
22
+ return data.rates;
23
+ }
24
+ catch (error) {
25
+ return null;
26
+ }
27
+ };
28
+ const setCachedRatesToStorage = (rates) => {
29
+ try {
30
+ const data = {
31
+ rates,
32
+ timestamp: Date.now(),
33
+ expiresAt: Date.now() + CACHE_DURATION,
34
+ };
35
+ localStorage.setItem(CACHE_KEY, JSON.stringify(data));
36
+ }
37
+ catch (error) {
38
+ // Storage might be full or disabled
39
+ }
40
+ };
41
+ const fetchRatesFromShopify = async () => {
42
+ try {
43
+ const response = await fetch('https://cdn.shopify.com/s/javascripts/currencies.js');
44
+ if (!response.ok) {
45
+ throw new Error(`HTTP error! status: ${response.status} ${response.statusText}`);
46
+ }
47
+ const text = await response.text();
48
+ const ratesMatch = text.match(/rates:\{([^}]+)\}/);
49
+ if (!ratesMatch) {
50
+ throw new Error('Could not parse currency rates from response');
51
+ }
52
+ const ratesString = ratesMatch[1];
53
+ const rates = {};
54
+ const ratePairs = ratesString.split(',');
55
+ for (const pair of ratePairs) {
56
+ const [currency, rate] = pair.split(':');
57
+ if (currency && rate) {
58
+ const rawRate = parseFloat(rate.trim());
59
+ rates[currency.trim()] = 1 / rawRate;
60
+ }
61
+ }
62
+ setCachedRatesToStorage(rates);
63
+ return rates;
64
+ }
65
+ catch (error) {
66
+ return {};
67
+ }
68
+ };
69
+ const getRatesLazy = async () => {
70
+ if (ratesCache) {
71
+ return ratesCache;
72
+ }
73
+ const storedRates = getCachedRatesFromStorage();
74
+ if (storedRates) {
75
+ ratesCache = storedRates;
76
+ return storedRates;
77
+ }
78
+ if (ratesPromise) {
79
+ return ratesPromise;
80
+ }
81
+ ratesPromise = fetchRatesFromShopify();
82
+ ratesCache = await ratesPromise;
83
+ return ratesCache;
84
+ };
85
+ const ensureRatesLoaded = async () => {
86
+ const rates = await getRatesLazy();
87
+ return rates;
88
+ };
89
+ if (typeof window !== 'undefined') {
90
+ getRatesLazy();
91
+ }
92
+
7
93
  var _a, _b;
8
94
  var _c;
9
95
  const addItemToCart = (fetchOverride, itemsToAdd) => fetchOverride(`${window.Shopify.routes.root}cart/add.js`, {
@@ -23,11 +109,33 @@ const createShopApi = (fetchOverride = window.fetch) => ({
23
109
  throw new Error(`Could not add items`, { cause: await response.text() });
24
110
  }
25
111
  },
26
- getSiteCurrency() {
112
+ async getSiteCurrency() {
27
113
  var _a, _b, _c, _d;
114
+ const currency = ((_b = (_a = window.Shopify) === null || _a === void 0 ? void 0 : _a.currency) === null || _b === void 0 ? void 0 : _b.active) || 'USD';
115
+ const shopifyRate = (_d = (_c = window.Shopify) === null || _c === void 0 ? void 0 : _c.currency) === null || _d === void 0 ? void 0 : _d.rate;
116
+ if (currency === 'USD') {
117
+ return {
118
+ currency: 'USD',
119
+ rate: 1,
120
+ };
121
+ }
122
+ if (shopifyRate) {
123
+ return {
124
+ currency,
125
+ rate: parseFloat(shopifyRate),
126
+ };
127
+ }
128
+ const rates = await ensureRatesLoaded();
129
+ if (rates === null || rates === void 0 ? void 0 : rates[currency]) {
130
+ const rate = rates[currency];
131
+ return {
132
+ currency,
133
+ rate,
134
+ };
135
+ }
28
136
  return {
29
- currency: ((_b = (_a = window.Shopify) === null || _a === void 0 ? void 0 : _a.currency) === null || _b === void 0 ? void 0 : _b.active) || 'USD',
30
- rate: parseFloat(((_d = (_c = window.Shopify) === null || _c === void 0 ? void 0 : _c.currency) === null || _d === void 0 ? void 0 : _d.rate) || '1'),
137
+ currency: 'USD',
138
+ rate: 1,
31
139
  };
32
140
  },
33
141
  getCurrentProductHandle() {