type Abstractapi = {
apiKey: string
}
export async function main(
resource: Abstractapi,
baseCurrency: string,
date: `${number}-${number}-${number}`, // YYYY-MM-DD
targetCurrencies?: string[]
) {
const queryParams = new URLSearchParams({
api_key: resource.apiKey,
base: baseCurrency,
date: date
})
// If target currencies are provided, convert them to a comma-separated string
if (targetCurrencies && targetCurrencies.length > 0) {
queryParams.append('target', targetCurrencies.join(','))
}
const endpoint = `https://exchange-rates.abstractapi.com/v1/historical?${queryParams.toString()}`
const response = await fetch(endpoint, {
method: 'GET'
})
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const data = await response.json()
return data
}
Submitted by hugo697 72 days ago