This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from bennycode/ts-node-starter
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add endpoint to retrieve exchange rates (#767)
- Loading branch information
Showing
6 changed files
with
884 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import nock from 'nock'; | ||
import getExchangeRates from '../test/fixtures/rest/v2/exchange-rates/GET-200.json'; | ||
import getExchangeRatesEUR from '../test/fixtures/rest/v2/exchange-rates/GET-200-EUR.json'; | ||
import {ExchangeRateAPI} from './ExchangeRateAPI'; | ||
|
||
describe('ExchangeRateAPI', () => { | ||
afterEach(() => nock.cleanAll()); | ||
|
||
beforeEach(() => { | ||
nock.cleanAll(); | ||
}); | ||
|
||
describe('getExchangeRates', () => { | ||
it('returns the exchange rates based on USD by default', async () => { | ||
nock('https://api.coinbase.com') | ||
.get(ExchangeRateAPI.URL.V2_EXCHANGE_RATES) | ||
.query({currency: 'USD'}) | ||
.reply(200, JSON.stringify(getExchangeRates)); | ||
|
||
const rates = await global.client.rest.exchangeRate.getExchangeRates(); | ||
expect(rates.currency).toBe('USD'); | ||
expect(rates.rates['EUR']).toBe('1.0208'); | ||
}); | ||
|
||
it('returns the exchange rates for a specific currency', async () => { | ||
nock('https://api.coinbase.com') | ||
.get(ExchangeRateAPI.URL.V2_EXCHANGE_RATES) | ||
.query({currency: 'EUR'}) | ||
.reply(200, JSON.stringify(getExchangeRatesEUR)); | ||
|
||
const rates = await global.client.rest.exchangeRate.getExchangeRates('EUR'); | ||
expect(rates.currency).toBe('EUR'); | ||
expect(rates.rates['IOTX']).toBe('29.4767870302137067'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import axios from 'axios'; | ||
|
||
export interface ExchangeRate { | ||
currency: string; | ||
rates: {[currency: string]: string}; | ||
} | ||
|
||
/** | ||
* @see https://docs.cloud.coinbase.com/sign-in-with-coinbase/docs/api-exchange-rates | ||
*/ | ||
export class ExchangeRateAPI { | ||
static readonly URL = { | ||
V2_EXCHANGE_RATES: `/v2/exchange-rates`, | ||
}; | ||
|
||
constructor(private readonly baseURL: string = 'https://api.coinbase.com') {} | ||
|
||
/** | ||
* Get current exchange rates. Default base currency is USD, but it can be defined as any supported currency. | ||
* Returned rates will define the exchange rate for one unit of the base currency. | ||
* | ||
* @see https://docs.cloud.coinbase.com/sign-in-with-coinbase/docs/api-exchange-rates#get-exchange-rates | ||
*/ | ||
async getExchangeRates(currency: string = 'USD'): Promise<ExchangeRate> { | ||
const response = await axios.get<{ | ||
data: ExchangeRate; | ||
}>(`${this.baseURL}${ExchangeRateAPI.URL.V2_EXCHANGE_RATES}`, {params: {currency}}); | ||
return response.data.data; | ||
} | ||
} |
Oops, something went wrong.