Skip to content

Commit

Permalink
Use Coingecko proxy with special CI auth token for FiatApi tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sisou committed Apr 8, 2024
1 parent b15cfff commit 389dfda
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/fiat-api/FiatApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,12 @@ export function setCoingeckoApiUrl(url: string) {
API_URL = url;
}

const coingeckoExtraHeaders = new Map<string, string>();

export function setCoingeckoApiExtraHeader(name: string, value: string) {
coingeckoExtraHeaders.set(name, value);
}

type VsCurrency = FiatApiSupportedFiatCurrency | FiatApiBridgedFiatCurrency | FiatApiSupportedCryptoCurrency;
export async function getExchangeRates<C extends FiatApiSupportedCryptoCurrency, V extends VsCurrency>(
cryptoCurrencies: C[],
Expand Down Expand Up @@ -510,13 +516,15 @@ function _findTimestampChunk(
};
}

async function _fetch<T>(input: RequestInfo, init?: RequestInit): Promise<T> {
async function _fetch<T>(input: RequestInfo): Promise<T> {
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;
Expand Down
7 changes: 7 additions & 0 deletions tests/FiatApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 389dfda

Please sign in to comment.