type Abstractapi = {
apiKey: string
}
export async function main(
resource: Abstractapi,
baseCurrency: string,
targetCurrency: string,
amountToConvert: number,
exchangeDate?: `${number}-${number}-${number}` // YYYY-MM-DD
) {
const queryParams = new URLSearchParams({
api_key: resource.apiKey,
base: baseCurrency,
target: targetCurrency,
base_amount: amountToConvert.toString()
})
if (exchangeDate) {
queryParams.append('date', exchangeDate)
}
const endpoint = `https://exchange-rates.abstractapi.com/v1/convert?${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 43 days ago