/** * Czech National Bank (ČNB) exchange rate service. * Fetches daily rates and caches them. * API: https://api.cnb.cz/cnbapi/exrates/daily */ interface CnbRate { currencyCode: string; rate: number; amount: number; } const rateCache: Record> = {}; async function fetchRatesForDate( date?: string, ): Promise> { const key = date || "today"; if (rateCache[key]) return rateCache[key]; try { let url = "https://api.cnb.cz/cnbapi/exrates/daily?lang=EN"; if (date) url += `&date=${date}`; const response = await fetch(url); if (!response.ok) throw new Error(`CNB API: ${response.status}`); const data = (await response.json()) as { rates: CnbRate[] }; const rates: Record = { CZK: 1 }; for (const r of data.rates) { rates[r.currencyCode] = r.rate / r.amount; } rateCache[key] = rates; return rates; } catch (err) { console.error("Failed to fetch CNB exchange rates:", err); if (rateCache["today"]) return rateCache["today"]; return { CZK: 1, EUR: 25, USD: 22, GBP: 28 }; } } /** Convert an amount from a given currency to CZK using CNB rates */ export async function toCzk( amount: number, currency: string, date?: string, ): Promise { if (currency === "CZK") return amount; const rates = await fetchRatesForDate(date); const rate = rates[currency]; if (!rate) return amount; return Math.round(amount * rate * 100) / 100; } /** Get CNB rate for a currency (CZK per 1 unit), optionally for a specific date */ export async function getRate( currency: string, date?: string, ): Promise { if (currency === "CZK") return 1; const rates = await fetchRatesForDate(date); return rates[currency] || 1; }