2017-09-26 03:41:11 +00:00
|
|
|
import { handleJSONResponse } from 'api/utils';
|
|
|
|
|
2017-11-15 05:51:09 +00:00
|
|
|
export const rateSymbols = ['USD', 'EUR', 'GBP', 'BTC', 'CHF', 'REP', 'ETH'];
|
|
|
|
const rateSymbolsArg = rateSymbols.join(',');
|
2017-09-26 03:41:11 +00:00
|
|
|
// TODO - internationalize
|
|
|
|
const ERROR_MESSAGE = 'Could not fetch rate data.';
|
|
|
|
const CCApi = 'https://min-api.cryptocompare.com';
|
|
|
|
|
2017-11-15 05:51:09 +00:00
|
|
|
const CCRates = (symbol: string) => {
|
|
|
|
return `${CCApi}/data/price?fsym=${symbol}&tsyms=${rateSymbolsArg}`;
|
|
|
|
};
|
2017-09-26 03:41:11 +00:00
|
|
|
|
|
|
|
export interface CCResponse {
|
2017-11-15 05:51:09 +00:00
|
|
|
symbol: string;
|
|
|
|
rates: {
|
|
|
|
BTC: number;
|
|
|
|
EUR: number;
|
|
|
|
GBP: number;
|
|
|
|
CHF: number;
|
|
|
|
REP: number;
|
|
|
|
ETH: number;
|
|
|
|
};
|
2017-09-26 03:41:11 +00:00
|
|
|
}
|
|
|
|
|
2017-11-15 05:51:09 +00:00
|
|
|
export const fetchRates = (symbol: string): Promise<CCResponse> =>
|
|
|
|
fetch(CCRates(symbol))
|
|
|
|
.then(response => handleJSONResponse(response, ERROR_MESSAGE))
|
|
|
|
.then(rates => ({
|
|
|
|
symbol,
|
|
|
|
rates
|
|
|
|
}));
|