Public
Script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import { fetch } from "https://esm.town/v/std/fetch";
function fetchJSON(url) {
return fetch(url).then(res => res.json());
}
export let currency = async (desired, base = "usd", amount = 1) => {
let { rates } = await fetchJSON(`https://open.er-api.com/v6/latest/${base}`);
if (rates && rates[desired.toUpperCase()]) return amount * (rates[desired.toUpperCase()]);
else {
let { rates } = await fetchJSON("https://api.coingecko.com/api/v3/exchange_rates");
return amount * rates[desired.toLowerCase()]?.value / rates[base.toLowerCase()]?.value;
}
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
May 21, 2024