Skip to content

Commit 4c0a28d

Browse files
committed
Use Coingecko proxy with special CI auth token for FiatApi tests
1 parent b15cfff commit 4c0a28d

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

.github/workflows/complete-check.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,7 @@ jobs:
1616
- name: Install modules
1717
run: yarn
1818
- name: Run
19+
env:
20+
COINGECKO_PROXY_AUTHORIZATION_HEADER: ${{ secrets.COINGECKO_PROXY_AUTHORIZATION_HEADER }}
21+
CI_AUTHORIZATION_TOKEN: ${{ secrets.CI_AUTHORIZATION_TOKEN }}
1922
run: yarn pr

src/fiat-api/FiatApi.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,12 @@ export function setCoingeckoApiUrl(url: string) {
281281
API_URL = url;
282282
}
283283

284+
const coingeckoExtraHeaders = new Map<string, string>();
285+
286+
export function setCoingeckoApiExtraHeader(name: string, value: string) {
287+
coingeckoExtraHeaders.set(name, value);
288+
}
289+
284290
type VsCurrency = FiatApiSupportedFiatCurrency | FiatApiBridgedFiatCurrency | FiatApiSupportedCryptoCurrency;
285291
export async function getExchangeRates<C extends FiatApiSupportedCryptoCurrency, V extends VsCurrency>(
286292
cryptoCurrencies: C[],
@@ -510,13 +516,15 @@ function _findTimestampChunk(
510516
};
511517
}
512518

513-
async function _fetch<T>(input: RequestInfo, init?: RequestInit): Promise<T> {
519+
async function _fetch<T>(input: RequestInfo): Promise<T> {
514520
let result: T | null = null;
515521
do {
516522
let retry = true;
517523
try {
518524
// eslint-disable-next-line no-await-in-loop
519-
const response = await fetch(input, init); // throws when user is offline
525+
const response = await fetch(input, {
526+
headers: [...coingeckoExtraHeaders.entries()],
527+
}); // throws when user is offline
520528
if (!response.ok) {
521529
if (response.status === 400) {
522530
retry = false;

tests/FiatApi.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,25 @@ import {
1010
FiatApiSupportedFiatCurrency,
1111
getExchangeRates,
1212
getHistoricExchangeRates,
13+
setCoingeckoApiUrl,
14+
setCoingeckoApiExtraHeader,
1315
} from '../src/fiat-api/FiatApi';
1416

1517
// If tests are failing locally, increase the timeout
1618
const timeout = /* use default timeout of 5000 */ 0;
1719
// const timeout = 300000;
1820

1921
describe('FiatApi', () => {
22+
beforeEach(() => {
23+
setCoingeckoApiUrl('https://nq-coingecko-proxy.deno.dev/api/v3');
24+
if (process.env.COINGECKO_PROXY_AUTHORIZATION_HEADER && process.env.CI_AUTHORIZATION_TOKEN) {
25+
setCoingeckoApiExtraHeader(
26+
process.env.COINGECKO_PROXY_AUTHORIZATION_HEADER,
27+
process.env.CI_AUTHORIZATION_TOKEN,
28+
);
29+
}
30+
});
31+
2032
it('can fetch current USD rate for BTC', async () => {
2133
const rate = await getExchangeRates(
2234
[FiatApiSupportedCryptoCurrency.BTC],

0 commit comments

Comments
 (0)