From 389dfdad3d11d097ec465202d95ed39f3dd83085 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren?= Date: Mon, 8 Apr 2024 16:46:24 +0200 Subject: [PATCH] Use Coingecko proxy with special CI auth token for FiatApi tests --- src/fiat-api/FiatApi.ts | 12 ++++++++++-- tests/FiatApi.spec.ts | 7 +++++++ 2 files changed, 17 insertions(+), 2 deletions(-) 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(