stacker.news/api/resolvers/price.js

28 lines
734 B
JavaScript
Raw Normal View History

import { SUPPORTED_CURRENCIES } from '@/lib/currency'
import { cachedFetcher } from '@/lib/fetch'
2024-10-09 00:26:29 +00:00
const getPrice = cachedFetcher(async function fetchPrice (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 15:44:57 +00:00
}
}, {
maxSize: SUPPORTED_CURRENCIES.length,
cacheExpiry: 60 * 1000, // 1 minute
2024-10-06 22:58:46 +00:00
forceRefreshThreshold: 0, // never force refresh
keyGenerator: (fiat = 'USD') => fiat
})
2023-05-07 15:44:57 +00:00
export default {
Query: {
price: async (parent, { fiatCurrency }, ctx) => {
return await getPrice(fiatCurrency) || -1
2023-01-28 00:08:58 +00:00
}
}
}