fio-api-handler
Version:
Unofficial handler for Fio Bank API
339 lines (261 loc) • 8.61 kB
JavaScript
'use strict';
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var _defineProperty = _interopDefault(require('@babel/runtime/helpers/defineProperty'));
var restApiHandler = require('rest-api-handler');
var luxon = require('luxon');
class FioApiException extends Error {
/**
* Response from server that throwed an error.
*/
/**
* Constructor.
*
* @param response - Processed response from server.
*/
constructor(response) {
super(`Fio exception: ${JSON.stringify(response.data)}`);
_defineProperty(this, "response", void 0);
this.response = response;
}
getResponse() {
return this.response;
}
getRequest() {
return this.response.request;
}
}
class FioApi extends restApiHandler.Api {
constructor(apiKey, format = 'json') {
super('https://www.fio.cz/ib_api/rest', [new restApiHandler.DefaultResponseProcessor(FioApiException)]);
_defineProperty(this, "apiKey", void 0);
_defineProperty(this, "format", void 0);
_defineProperty(this, "dateFormat", 'yyyy-MM-dd');
this.apiKey = apiKey;
this.format = format;
}
getTransactionOverview(year, overviewNumber) {
return this.get(this.getTransactionsApiUrl('by-id', `${year}/${overviewNumber}`));
}
getTransactions(start, end) {
return this.get(this.getTransactionsApiUrl('periods', `${start.toFormat(this.dateFormat)}/${end.toFormat(this.dateFormat)}`));
}
getLastTransactions() {
return this.get(this.getTransactionsApiUrl('last'));
}
setLastTransactionById(id) {
return this.get(this.getLastTransactionSetApiUrl('id', id.toString()));
}
setLastTransactionBydate(date) {
return this.get(this.getLastTransactionSetApiUrl('date', date.toFormat(this.dateFormat)));
}
getApiUrl(action, namespace, format = this.format) {
return `${action}/${this.apiKey}/${namespace}${format ? `.${format}` : '/'}`;
}
getTransactionsApiUrl(action, namespace) {
return this.getApiUrl(action, `${namespace ? `/${namespace}/` : ''}transactions`);
}
getLastTransactionSetApiUrl(action, value) {
return this.getApiUrl(`set-last-${action}`, value, undefined);
}
}
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
class Info {
constructor(source) {
_defineProperty(this, "data", void 0);
_defineProperty(this, "source", void 0);
const dateFormat = 'yyyy-MM-ddZZZ';
this.source = source; // $FlowFixMe
this.data = _objectSpread(_objectSpread({}, source), {}, {
accountId: Number(source.accountId),
bankId: Number(source.bankId),
dateStart: luxon.DateTime.fromFormat(source.dateStart, dateFormat),
dateEnd: luxon.DateTime.fromFormat(source.dateEnd, dateFormat)
});
}
getAccountId() {
return this.data.accountId;
}
getBankId() {
return this.data.bankId;
}
getCurrency() {
return this.data.currency;
}
getIban() {
return this.data.iban;
}
getBic() {
return this.data.bic;
}
getOpeningBalance() {
return this.data.openingBalance;
}
getClosingBalance() {
return this.data.closingBalance;
}
getStart() {
return this.data.dateStart;
}
getEnd() {
return this.data.dateEnd;
}
getYearList() {
return this.data.yearList;
}
getIdList() {
return this.data.idList;
}
getIdFrom() {
return this.data.idFrom;
}
getIdTo() {
return this.data.idTo;
}
getIdLastDownload() {
return this.data.idLastDownload;
}
getData() {
return this.data;
}
getSource() {
return this.source;
}
}
const PLATBA_KARTOU = 'Platba kartou';
class Transaction {
// eslint-disable-next-line complexity,sonarjs/cognitive-complexity
constructor(source) {
_defineProperty(this, "data", void 0);
_defineProperty(this, "source", void 0);
this.source = source;
const comment = source.column25 ? source.column25.value : null;
const type = source.column8.value;
const issueDate = luxon.DateTime.fromFormat(source.column0.value, 'yyyy-MM-ddZZZ');
const createdDate = type === PLATBA_KARTOU && comment ? luxon.DateTime.fromFormat(comment.split(/dne (\d*\.\d*\.\d{4})/g)[1], 'd.M.yyyy') : issueDate;
const amount = source.column1.value;
const currency = source.column14.value;
const message = source.column16 ? source.column16.value : null;
this.data = {
issueDate,
createdDate,
type,
comment,
transactionId: source.column22.value,
approvalId: source.column17.value,
amount,
originalAmount: amount,
currency,
originalCurrency: currency,
message,
vs: source.column5 ? source.column5.value : null,
ks: source.column4 ? source.column4.value : null,
ss: source.column6 ? source.column6.value : null,
info: source.column7 ? source.column7.value : null,
author: source.column9 ? source.column9.value : null,
sendToAccountNumber: source.column2 ? source.column2.value : null,
sendToAccountName: source.column10 ? source.column10.value : null,
sendToBankCode: source.column3 ? source.column3.value : null,
sendToBank: source.column12 ? source.column12.value : null
};
if (type === PLATBA_KARTOU && message) {
const [// eslint-disable-next-line no-unused-vars
text, originalAmount, originalCurrency] = message.match(/částka {2}(\d*\.\d{2}) (([a-zA-Z]|\u010D){2,3})/);
this.data.originalAmount = Number(originalAmount) * (amount / Math.abs(amount));
this.data.originalCurrency = originalCurrency === 'Kč' ? 'CZK' : originalCurrency;
}
}
getIssueDate() {
return this.data.issueDate;
}
getCreatedDate() {
return this.data.createdDate;
}
getTransactionId() {
return this.data.transactionId;
}
getApprovalId() {
return this.data.approvalId;
}
getAmount() {
return this.data.amount;
}
getCurrency() {
return this.data.currency;
}
getOriginalAmount() {
return this.data.originalAmount;
}
getOriginalCurrency() {
return this.data.originalCurrency;
}
getCounterpartyAccountNumber() {
return this.data.sendToAccountNumber;
}
getCounterpartyAccountName() {
return this.data.sendToAccountName;
}
getCounterpartyBankName() {
return this.data.sendToBank;
}
getCounterpartyBankNumber() {
return this.data.sendToBankCode;
}
getVs() {
return this.data.vs;
}
getKs() {
return this.data.ks;
}
getSs() {
return this.data.ss;
}
getInfo() {
return this.data.info;
}
getMessage() {
return this.data.message;
}
getAuthor() {
return this.data.author;
}
getData() {
return this.data;
}
}
class EnhacedFioApi extends FioApi {
// eslint-disable-next-line no-useless-constructor
constructor(key) {
super(key);
}
processTransactionResponse(data) {
const {
accountStatement
} = data;
return {
info: new Info(accountStatement.info),
transactions: accountStatement.transactionList.transaction.map(transaction => {
return new Transaction(transaction);
})
};
}
async getTransactions(start, end) {
const {
data
} = await FioApi.prototype.getTransactions.call(this, start, end);
return this.processTransactionResponse(data);
}
async getLastTransactions() {
const {
data
} = await FioApi.prototype.getLastTransactions.call(this);
return this.processTransactionResponse(data);
}
async getTransactionOverview(year, overviewNumber) {
const {
data
} = await FioApi.prototype.getTransactionOverview.call(this, year, overviewNumber);
return this.processTransactionResponse(data);
}
}
module.exports = EnhacedFioApi;