@cloudparker/moldex.js 4.1.13 → 4.1.16
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/services/utils/currency-service.d.ts +64 -0
- package/dist/services/utils/currency-service.js +155 -0
- package/dist/views/extra/loaders/country-loader.svelte +15 -1
- package/dist/views/extra/loaders/country-loader.svelte.d.ts +5 -0
- package/dist/views/extra/loaders/state-loader.svelte +7 -0
- package/dist/views/extra/loaders/state-loader.svelte.d.ts +1 -0
- package/package.json +1 -1
|
@@ -43,6 +43,28 @@ export declare enum CurrencySymbols {
|
|
|
43
43
|
VND = "\u20AB",
|
|
44
44
|
ZAR = "R"
|
|
45
45
|
}
|
|
46
|
+
export interface CurrencyRates {
|
|
47
|
+
[currencyCode: string]: number;
|
|
48
|
+
}
|
|
49
|
+
export interface CurrencyApiResponse {
|
|
50
|
+
result: string;
|
|
51
|
+
provider: string;
|
|
52
|
+
documentation: string;
|
|
53
|
+
terms_of_use: string;
|
|
54
|
+
time_last_update_unix: number;
|
|
55
|
+
time_last_update_utc: string;
|
|
56
|
+
time_next_update_unix: number;
|
|
57
|
+
time_next_update_utc: string;
|
|
58
|
+
time_eol_unix: number;
|
|
59
|
+
base_code: string;
|
|
60
|
+
rates: CurrencyRates;
|
|
61
|
+
}
|
|
62
|
+
export interface CurrencyData {
|
|
63
|
+
baseCode: string;
|
|
64
|
+
rates: CurrencyRates;
|
|
65
|
+
lastUpdate: string;
|
|
66
|
+
nextUpdate: string;
|
|
67
|
+
}
|
|
46
68
|
/**
|
|
47
69
|
* Converts a number into words based on the Indian numbering system.
|
|
48
70
|
* Supports up to Crore level and works for numbers up to 99 Crore.
|
|
@@ -89,3 +111,45 @@ export declare function toCurrency(value?: number, symbol?: string): string;
|
|
|
89
111
|
* @returns A string formatted with commas as per the Indian numbering system.
|
|
90
112
|
*/
|
|
91
113
|
export declare function formatIndianCurrency(amount: number | string): string;
|
|
114
|
+
/**
|
|
115
|
+
* Fetches currency exchange rates for a given base currency
|
|
116
|
+
* @param baseCurrency - The base currency code (e.g., 'USD', 'EUR', 'INR')
|
|
117
|
+
* @param forceRefresh - Force a refresh even if cached data exists (default: false)
|
|
118
|
+
* @returns Currency data with exchange rates
|
|
119
|
+
* @throws Error if the API request fails
|
|
120
|
+
*/
|
|
121
|
+
export declare function getCurrencyRates(baseCurrency?: string, forceRefresh?: boolean): Promise<CurrencyData>;
|
|
122
|
+
/**
|
|
123
|
+
* Converts an amount from one currency to another
|
|
124
|
+
* @param amount - The amount to convert
|
|
125
|
+
* @param fromCurrency - The source currency code
|
|
126
|
+
* @param toCurrency - The target currency code
|
|
127
|
+
* @returns The converted amount
|
|
128
|
+
*/
|
|
129
|
+
export declare function convertCurrency(amount: number, fromCurrency: string, toCurrency: string): Promise<number>;
|
|
130
|
+
/**
|
|
131
|
+
* Gets a specific exchange rate between two currencies
|
|
132
|
+
* @param fromCurrency - The source currency code
|
|
133
|
+
* @param toCurrency - The target currency code
|
|
134
|
+
* @returns The exchange rate
|
|
135
|
+
*/
|
|
136
|
+
export declare function getExchangeRate(fromCurrency: string, toCurrency: string): Promise<number>;
|
|
137
|
+
/**
|
|
138
|
+
* Clears the currency cache for a specific currency or all currencies
|
|
139
|
+
* @param baseCurrency - Optional base currency to clear, if not provided clears all
|
|
140
|
+
*/
|
|
141
|
+
export declare function clearCurrencyCache(baseCurrency?: string): void;
|
|
142
|
+
/**
|
|
143
|
+
* Gets the cached currency data without fetching
|
|
144
|
+
* @param baseCurrency - The base currency code
|
|
145
|
+
* @returns Cached currency data or null if not cached
|
|
146
|
+
*/
|
|
147
|
+
export declare function getCachedCurrencyRates(baseCurrency: string): CurrencyData | null;
|
|
148
|
+
/**
|
|
149
|
+
* Formats a currency amount with the appropriate symbol and formatting
|
|
150
|
+
* @param amount - The amount to format
|
|
151
|
+
* @param currencyCode - The currency code (e.g., 'USD', 'EUR', 'INR')
|
|
152
|
+
* @param locale - The locale for formatting (defaults to 'en-US')
|
|
153
|
+
* @returns Formatted currency string
|
|
154
|
+
*/
|
|
155
|
+
export declare function formatCurrency(amount: number, currencyCode: string, locale?: string): string;
|
|
@@ -47,6 +47,8 @@ export var CurrencySymbols;
|
|
|
47
47
|
CurrencySymbols["VND"] = "\u20AB";
|
|
48
48
|
CurrencySymbols["ZAR"] = "R";
|
|
49
49
|
})(CurrencySymbols || (CurrencySymbols = {}));
|
|
50
|
+
const CURRENCY_API_BASE = 'https://open.er-api.com/v6/latest';
|
|
51
|
+
const currencyCache = new Map();
|
|
50
52
|
/**
|
|
51
53
|
* Converts a number into words based on the Indian numbering system.
|
|
52
54
|
* Supports up to Crore level and works for numbers up to 99 Crore.
|
|
@@ -138,3 +140,156 @@ export function formatIndianCurrency(amount) {
|
|
|
138
140
|
}
|
|
139
141
|
return otherDigits + (otherDigits ? "," : "") + lastThreeDigits + "." + decimalPart;
|
|
140
142
|
}
|
|
143
|
+
/**
|
|
144
|
+
* Checks if cached currency data is still valid
|
|
145
|
+
* @param cachedData - The cached currency data
|
|
146
|
+
* @returns true if cache is valid, false otherwise
|
|
147
|
+
*/
|
|
148
|
+
function isCacheValid(cachedData) {
|
|
149
|
+
const nowUnix = Math.floor(Date.now() / 1000);
|
|
150
|
+
return cachedData.nextUpdateUnix > nowUnix;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Fetches currency exchange rates for a given base currency
|
|
154
|
+
* @param baseCurrency - The base currency code (e.g., 'USD', 'EUR', 'INR')
|
|
155
|
+
* @param forceRefresh - Force a refresh even if cached data exists (default: false)
|
|
156
|
+
* @returns Currency data with exchange rates
|
|
157
|
+
* @throws Error if the API request fails
|
|
158
|
+
*/
|
|
159
|
+
export async function getCurrencyRates(baseCurrency = 'USD', forceRefresh = false) {
|
|
160
|
+
const cacheKey = baseCurrency.toUpperCase();
|
|
161
|
+
// Check cache first
|
|
162
|
+
if (!forceRefresh) {
|
|
163
|
+
const cached = currencyCache.get(cacheKey);
|
|
164
|
+
if (cached && isCacheValid(cached)) {
|
|
165
|
+
console.log(`[currency.service] Using cached rates for ${cacheKey}`);
|
|
166
|
+
return {
|
|
167
|
+
baseCode: cached.baseCode,
|
|
168
|
+
rates: cached.rates,
|
|
169
|
+
lastUpdate: cached.lastUpdate,
|
|
170
|
+
nextUpdate: cached.nextUpdate
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
try {
|
|
175
|
+
console.log(`[currency.service] Fetching fresh rates for ${cacheKey}`);
|
|
176
|
+
const response = await fetch(`${CURRENCY_API_BASE}/${cacheKey}`);
|
|
177
|
+
if (!response.ok) {
|
|
178
|
+
throw new Error(`Currency API request failed: ${response.status} ${response.statusText}`);
|
|
179
|
+
}
|
|
180
|
+
const data = await response.json();
|
|
181
|
+
if (data.result !== 'success') {
|
|
182
|
+
throw new Error('Currency API returned unsuccessful result');
|
|
183
|
+
}
|
|
184
|
+
const currencyData = {
|
|
185
|
+
baseCode: data.base_code,
|
|
186
|
+
rates: data.rates,
|
|
187
|
+
lastUpdate: data.time_last_update_utc,
|
|
188
|
+
nextUpdate: data.time_next_update_utc,
|
|
189
|
+
nextUpdateUnix: data.time_next_update_unix
|
|
190
|
+
};
|
|
191
|
+
// Store in cache
|
|
192
|
+
currencyCache.set(cacheKey, currencyData);
|
|
193
|
+
return {
|
|
194
|
+
baseCode: currencyData.baseCode,
|
|
195
|
+
rates: currencyData.rates,
|
|
196
|
+
lastUpdate: currencyData.lastUpdate,
|
|
197
|
+
nextUpdate: currencyData.nextUpdate
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
catch (error) {
|
|
201
|
+
console.error('[currency.service] Failed to fetch currency rates:', error);
|
|
202
|
+
// If fetch fails, try to return stale cache as fallback
|
|
203
|
+
const cached = currencyCache.get(cacheKey);
|
|
204
|
+
if (cached) {
|
|
205
|
+
console.warn('[currency.service] Returning stale cached data due to fetch error');
|
|
206
|
+
return {
|
|
207
|
+
baseCode: cached.baseCode,
|
|
208
|
+
rates: cached.rates,
|
|
209
|
+
lastUpdate: cached.lastUpdate,
|
|
210
|
+
nextUpdate: cached.nextUpdate
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
throw error;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Converts an amount from one currency to another
|
|
218
|
+
* @param amount - The amount to convert
|
|
219
|
+
* @param fromCurrency - The source currency code
|
|
220
|
+
* @param toCurrency - The target currency code
|
|
221
|
+
* @returns The converted amount
|
|
222
|
+
*/
|
|
223
|
+
export async function convertCurrency(amount, fromCurrency, toCurrency) {
|
|
224
|
+
const data = await getCurrencyRates(fromCurrency);
|
|
225
|
+
const rate = data.rates[toCurrency.toUpperCase()];
|
|
226
|
+
if (!rate) {
|
|
227
|
+
throw new Error(`Exchange rate not found for ${toCurrency}`);
|
|
228
|
+
}
|
|
229
|
+
return amount * rate;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Gets a specific exchange rate between two currencies
|
|
233
|
+
* @param fromCurrency - The source currency code
|
|
234
|
+
* @param toCurrency - The target currency code
|
|
235
|
+
* @returns The exchange rate
|
|
236
|
+
*/
|
|
237
|
+
export async function getExchangeRate(fromCurrency, toCurrency) {
|
|
238
|
+
const data = await getCurrencyRates(fromCurrency);
|
|
239
|
+
const rate = data.rates[toCurrency.toUpperCase()];
|
|
240
|
+
if (!rate) {
|
|
241
|
+
throw new Error(`Exchange rate not found for ${toCurrency}`);
|
|
242
|
+
}
|
|
243
|
+
return rate;
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Clears the currency cache for a specific currency or all currencies
|
|
247
|
+
* @param baseCurrency - Optional base currency to clear, if not provided clears all
|
|
248
|
+
*/
|
|
249
|
+
export function clearCurrencyCache(baseCurrency) {
|
|
250
|
+
if (baseCurrency) {
|
|
251
|
+
currencyCache.delete(baseCurrency.toUpperCase());
|
|
252
|
+
console.log(`[currency.service] Cleared cache for ${baseCurrency.toUpperCase()}`);
|
|
253
|
+
}
|
|
254
|
+
else {
|
|
255
|
+
currencyCache.clear();
|
|
256
|
+
console.log('[currency.service] Cleared all currency cache');
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Gets the cached currency data without fetching
|
|
261
|
+
* @param baseCurrency - The base currency code
|
|
262
|
+
* @returns Cached currency data or null if not cached
|
|
263
|
+
*/
|
|
264
|
+
export function getCachedCurrencyRates(baseCurrency) {
|
|
265
|
+
const cached = currencyCache.get(baseCurrency.toUpperCase());
|
|
266
|
+
if (!cached)
|
|
267
|
+
return null;
|
|
268
|
+
return {
|
|
269
|
+
baseCode: cached.baseCode,
|
|
270
|
+
rates: cached.rates,
|
|
271
|
+
lastUpdate: cached.lastUpdate,
|
|
272
|
+
nextUpdate: cached.nextUpdate
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Formats a currency amount with the appropriate symbol and formatting
|
|
277
|
+
* @param amount - The amount to format
|
|
278
|
+
* @param currencyCode - The currency code (e.g., 'USD', 'EUR', 'INR')
|
|
279
|
+
* @param locale - The locale for formatting (defaults to 'en-US')
|
|
280
|
+
* @returns Formatted currency string
|
|
281
|
+
*/
|
|
282
|
+
export function formatCurrency(amount, currencyCode, locale = 'en-US') {
|
|
283
|
+
try {
|
|
284
|
+
return new Intl.NumberFormat(locale, {
|
|
285
|
+
style: 'currency',
|
|
286
|
+
currency: currencyCode.toUpperCase(),
|
|
287
|
+
minimumFractionDigits: 2,
|
|
288
|
+
maximumFractionDigits: 2
|
|
289
|
+
}).format(amount);
|
|
290
|
+
}
|
|
291
|
+
catch (error) {
|
|
292
|
+
console.error('[currency.service] Failed to format currency:', error);
|
|
293
|
+
return `${currencyCode.toUpperCase()} ${amount.toFixed(2)}`;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
|
|
2
1
|
<script lang="ts">
|
|
3
2
|
import EasyScriptLoader from '@cloudparker/easy-script-loader-svelte';
|
|
4
3
|
import type { Snippet } from 'svelte';
|
|
@@ -14,6 +13,21 @@
|
|
|
14
13
|
|
|
15
14
|
let EasyCountryData: any;
|
|
16
15
|
|
|
16
|
+
export async function getCountry({
|
|
17
|
+
isoCode,
|
|
18
|
+
dialCode,
|
|
19
|
+
name
|
|
20
|
+
}: {
|
|
21
|
+
isoCode?: string;
|
|
22
|
+
dialCode?: string;
|
|
23
|
+
name?: string;
|
|
24
|
+
}) {
|
|
25
|
+
if (EasyCountryData) {
|
|
26
|
+
return EasyCountryData.getCountry({ isoCode, dialCode, name });
|
|
27
|
+
}
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
|
|
17
31
|
export async function loadCountries() {
|
|
18
32
|
if (EasyCountryData) {
|
|
19
33
|
countries = EasyCountryData.getCountries();
|
|
@@ -6,6 +6,11 @@ type Props = {
|
|
|
6
6
|
children?: Snippet;
|
|
7
7
|
};
|
|
8
8
|
declare const CountryLoader: import("svelte").Component<Props, {
|
|
9
|
+
getCountry: ({ isoCode, dialCode, name }: {
|
|
10
|
+
isoCode?: string;
|
|
11
|
+
dialCode?: string;
|
|
12
|
+
name?: string;
|
|
13
|
+
}) => Promise<any>;
|
|
9
14
|
loadCountries: () => Promise<void>;
|
|
10
15
|
}, "countries">;
|
|
11
16
|
type CountryLoader = ReturnType<typeof CountryLoader>;
|
|
@@ -19,6 +19,13 @@
|
|
|
19
19
|
|
|
20
20
|
let EasyCountryStateData: any;
|
|
21
21
|
|
|
22
|
+
export async function getState(id:string){
|
|
23
|
+
if(EasyCountryStateData){
|
|
24
|
+
return EasyCountryStateData.getState(id);
|
|
25
|
+
}
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
|
|
22
29
|
export async function loadStates(countryCode?: string) {
|
|
23
30
|
if (EasyCountryStateData) {
|
|
24
31
|
states = EasyCountryStateData.getStates(countryCode || null);
|
|
@@ -11,6 +11,7 @@ type Props = {
|
|
|
11
11
|
children?: Snippet;
|
|
12
12
|
};
|
|
13
13
|
declare const StateLoader: import("svelte").Component<Props, {
|
|
14
|
+
getState: (id: string) => Promise<any>;
|
|
14
15
|
loadStates: (countryCode?: string) => Promise<void>;
|
|
15
16
|
}, "states">;
|
|
16
17
|
type StateLoader = ReturnType<typeof StateLoader>;
|