node-mnb
Version:
Node package that retrieves information from the Hungarian National Bank's (MNB) SOAP API.
68 lines • 3.01 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getHistoricalRates = exports.getMnbDateInterval = exports.getMnbCurrencies = exports.getMnbRates = exports.getMnbStatus = void 0;
const xml2js_1 = __importDefault(require("xml2js"));
const _1 = require(".");
async function getMnbStatus() {
const res = await (0, _1.wsdlRequestHandler)(_1.soapServiceUrl, 'GetInfo', {});
const infoRoot = await xml2js_1.default.parseStringPromise(res.response.GetInfoResult);
const info = infoRoot.MNBExchangeRatesQueryValues;
const formattedInfo = {
firstDate: info.FirstDate[0],
lastDate: info.LastDate[0],
currencies: info.Currencies[0].Curr,
};
return formattedInfo;
}
exports.getMnbStatus = getMnbStatus;
async function getMnbRates() {
const res = await (0, _1.wsdlRequestHandler)(_1.soapServiceUrl, 'GetCurrentExchangeRates', {});
const ratesRoot = await xml2js_1.default.parseStringPromise(res.response.GetCurrentExchangeRatesResult);
const rawRates = ratesRoot.MNBCurrentExchangeRates.Day[0].Rate;
const rates = {};
for (const rate of rawRates) {
rates[rate.$.curr] = +rate._.replace(',', '.') / +rate.$.unit;
}
return rates;
}
exports.getMnbRates = getMnbRates;
async function getMnbCurrencies() {
const res = await (0, _1.wsdlRequestHandler)(_1.soapServiceUrl, 'GetCurrencies', {});
const currsRoot = await xml2js_1.default.parseStringPromise(res.response.GetCurrenciesResult);
const currs = currsRoot.MNBCurrencies.Currencies[0].Curr;
return currs;
}
exports.getMnbCurrencies = getMnbCurrencies;
async function getMnbDateInterval() {
const res = await (0, _1.wsdlRequestHandler)(_1.soapServiceUrl, 'GetDateInterval', {});
const intervalRoot = await xml2js_1.default.parseStringPromise(res.response.GetDateIntervalResult);
const interval = intervalRoot.MNBStoredInterval.DateInterval[0].$;
const formattedInterval = {
startDate: interval.startdate,
endDate: interval.enddate,
};
return formattedInterval;
}
exports.getMnbDateInterval = getMnbDateInterval;
async function getHistoricalRates(startDate, endDate, currencies) {
const res = await (0, _1.wsdlRequestHandler)(_1.soapServiceUrl, 'GetExchangeRates', {
startDate: startDate,
endDate: endDate,
currencyNames: currencies.replace(' ', ''),
});
const historyRoot = await xml2js_1.default.parseStringPromise(res.response.GetExchangeRatesResult);
const historical = historyRoot.MNBExchangeRates.Day[0].Rate;
const history = {};
for (const rate of historical) {
history[rate.$.curr] = {
unit: Number(rate.$.unit),
rate: Number(rate._.replace(',', '.')),
};
}
return history;
}
exports.getHistoricalRates = getHistoricalRates;
//# sourceMappingURL=mnb.js.map