current-currency
Version:
Making it easy to work with currencies and cryptos!
28 lines (27 loc) • 1.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.convert = void 0;
const tslib_1 = require("tslib");
const axios_1 = tslib_1.__importDefault(require("axios"));
/**
* Converts one currency to another.
* @param fromCurrency The original currency
* @param amount The amount of money in the original currency
* @param toCurrency The currency to convert to
* @return An object containing the new currency and value
*/
function convert(fromCurrency, amount, toCurrency) {
return new Promise((resolve, reject) => {
if (fromCurrency === toCurrency) {
return reject("fromCurrency cannot be the same as toCurrency");
}
axios_1.default
.get(`https://api.coinbase.com/v2/exchange-rates?currency=${fromCurrency}`)
.then((body) => {
return resolve({ currency: toCurrency, amount: amount * body.data.data.rates[toCurrency] });
}).catch((err) => {
return reject(err.response.data.errors[0].message);
});
});
}
exports.convert = convert;