UNPKG

fio-api-handler

Version:
85 lines (60 loc) 2.21 kB
'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'); 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); } } module.exports = FioApi;