mnb-exchange-rate
Version:
[Docs - mnb.hu](https://www.mnb.hu/sajtoszoba/sajtokozlemenyek/2015-evi-sajtokozlemenyek/tajekoztatas-az-arfolyam-webservice-mukodeserol)
107 lines • 3.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Client = exports.getClient = exports.WSDL_URL = void 0;
const soap_1 = require("soap");
const fast_xml_parser_1 = require("fast-xml-parser");
exports.WSDL_URL = 'https://www.mnb.hu/arfolyamok.asmx?wsdl';
let _client = null;
async function getClient() {
if (!_client) {
const soapClient = await (0, soap_1.createClientAsync)(exports.WSDL_URL);
_client = new Client(soapClient);
}
return _client;
}
exports.getClient = getClient;
class Client {
constructor(soap) {
this.soap = soap;
}
async fetch(name, root, input = {}) {
const [res] = await this.soap[`${name}Async`](input);
const parser = new fast_xml_parser_1.XMLParser({
ignoreAttributes: false,
attributeNamePrefix: '@',
isArray: (tagName, jPath, isLeafNode, isAttribute) => !isAttribute,
});
const xml = parser.parse(res[`${name}Result`]);
return xml[root];
}
async GetInfo(input = {}) {
const [raw] = await this.fetch('GetInfo', 'MNBExchangeRatesQueryValues', input);
return {
FirstDate: raw?.FirstDate[0],
LastDate: raw?.LastDate[0],
Currencies: raw?.Currencies[0]?.Curr ?? [],
};
}
async GetCurrencies(input = {}) {
const raw = await this.fetch('GetCurrencies', 'MNBCurrencies', input);
return {
Currencies: raw?.[0]?.Currencies?.[0]?.Curr ?? [],
};
}
async GetCurrencyUnits(input = { currencies: [] }) {
const raw = await this.fetch('GetCurrencyUnits', 'MNBCurrencyUnits', {
currencyNames: input.currencies.join(','),
});
const entries = raw?.[0]?.Units?.[0]?.Unit?.map((e) => [
e['@curr'],
parseFloat(e['#text']),
]);
return Object.fromEntries(entries);
}
async GetDateInterval(input = {}) {
const raw = await this.fetch('GetDateInterval', 'MNBStoredInterval', input);
const el = raw?.[0]?.DateInterval?.[0];
if (!el) {
throw new Error('Invalid response');
}
return {
FirstDate: el['@startdate'],
LastDate: el['@enddate'],
};
}
async GetCurrentExchangeRates(input = {}) {
const raw = await this.fetch('GetCurrentExchangeRates', 'MNBCurrentExchangeRates', input);
const el = raw?.[0]?.Day?.[0];
if (!el) {
throw new Error('Invalid response');
}
return Object.fromEntries(el.Rate?.map((e) => [
e['@curr'],
parseFloat(e['#text']) / parseFloat(e['@unit']),
]));
}
async GetExchangeRates(input) {
if (input.startDate === input.endDate) {
throw new Error('Invalid logic: startDate and doneDate cannot be the same');
}
const raw = await this.fetch('GetExchangeRates', 'MNBExchangeRates', {
startDate: input.startDate,
endDate: input.endDate,
currencyNames: input.currencies.join(','),
});
const r = {};
if (raw) {
raw.forEach((n) => {
if (!n.Day) {
return;
}
n.Day.forEach((dn) => {
r[dn['@date']] = {};
if (!dn.Rate) {
return;
}
dn.Rate.forEach((rn) => {
r[dn['@date']][rn['@curr']] =
parseFloat(rn['#text']) / parseFloat(rn['@unit']);
});
});
});
}
return r;
}
}
exports.Client = Client;
//# sourceMappingURL=client.js.map