koboprice/lib/rates.js

40 lines
888 B
JavaScript
Raw Permalink Normal View History

2024-07-14 22:36:08 +03:00
import { isToday } from "date-fns";
import { getRates, cacheRates } from "./cache";
export const baseCurrency = "usd";
export const loadCurrencyRates = async () => {
try {
2024-07-15 14:32:15 +03:00
const url = `https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/${baseCurrency}.json`;
l("loading currency rates", url);
const res = await fetch(url);
2024-07-14 22:36:08 +03:00
return await res.json();
} catch (e) {
l("loadCurrencyRates", e);
return null;
}
};
export const getRate = async (currency) => {
const cached = getRates();
if (cached && isToday(cached.date)) {
l("found rates in cache", cached);
return cached[baseCurrency][currency];
}
const newRates = await loadCurrencyRates();
if (!newRates) {
l("failed to download rates");
return 0;
}
l("new rates", newRates);
cacheRates(newRates);
return newRates[baseCurrency][currency];
};