diff --git a/src/fiat-api/FiatApi.ts b/src/fiat-api/FiatApi.ts index f4dfc6c..1f4807d 100644 --- a/src/fiat-api/FiatApi.ts +++ b/src/fiat-api/FiatApi.ts @@ -281,6 +281,12 @@ export function setCoingeckoApiUrl(url: string) { API_URL = url; } +const coingeckoExtraHeaders = new Map(); + +export function setCoingeckoApiExtraHeader(name: string, value: string) { + coingeckoExtraHeaders.set(name, value); +} + type VsCurrency = FiatApiSupportedFiatCurrency | FiatApiBridgedFiatCurrency | FiatApiSupportedCryptoCurrency; export async function getExchangeRates( cryptoCurrencies: C[], @@ -510,13 +516,15 @@ function _findTimestampChunk( }; } -async function _fetch(input: RequestInfo, init?: RequestInit): Promise { +async function _fetch(input: RequestInfo): Promise { let result: T | null = null; do { let retry = true; try { // eslint-disable-next-line no-await-in-loop - const response = await fetch(input, init); // throws when user is offline + const response = await fetch(input, { + headers: [...coingeckoExtraHeaders.entries()], + }); // throws when user is offline if (!response.ok) { if (response.status === 400) { retry = false; diff --git a/tests/FiatApi.spec.ts b/tests/FiatApi.spec.ts index c084171..df806ce 100644 --- a/tests/FiatApi.spec.ts +++ b/tests/FiatApi.spec.ts @@ -10,12 +10,19 @@ import { FiatApiSupportedFiatCurrency, getExchangeRates, getHistoricExchangeRates, + setCoingeckoApiUrl, + setCoingeckoApiExtraHeader, } from '../src/fiat-api/FiatApi'; // If tests are failing locally, increase the timeout const timeout = /* use default timeout of 5000 */ 0; // const timeout = 300000; +setCoingeckoApiUrl('https://nq-coingecko-proxy.deno.dev/api/v3'); +if (process.env.COINGECKO_PROXY_AUTHORIZATION_HEADER && process.env.CI_AUTHORIZATION_TOKEN) { + setCoingeckoApiExtraHeader(process.env.COINGECKO_PROXY_AUTHORIZATION_HEADER, process.env.CI_AUTHORIZATION_TOKEN); +} + describe('FiatApi', () => { it('can fetch current USD rate for BTC', async () => { const rate = await getExchangeRates(