28 lines
686 B
JavaScript
Raw Normal View History

import { SUPPORTED_CURRENCIES } from '@/lib/currency'
import { cachedFetcher } from '@/lib/fetch'
const getPrice = cachedFetcher(async (fiat) => {
2023-05-07 10:44:57 -05:00
fiat ??= 'USD'
const url = `https://api.coinbase.com/v2/prices/BTC-${fiat}/spot`
try {
const res = await fetch(url)
const body = await res.json()
return parseFloat(body.data.amount)
} catch (err) {
console.error(err)
return -1
2023-05-07 10:44:57 -05:00
}
}, {
maxSize: SUPPORTED_CURRENCIES.length,
cacheExpiry: 60 * 1000, // 1 minute
forceRefreshThreshold: 0 // never force refresh
})
2023-05-07 10:44:57 -05:00
export default {
Query: {
price: async (parent, { fiatCurrency }, ctx) => {
return await getPrice(fiatCurrency) || -1
2023-01-27 18:08:58 -06:00
}
}
}