UNPKG

open-banking-pfm-sdk

Version:

The Open Banking PFM SDK uses Client classes and with **Promises** to get responses from the Open Banking PFM API in an easier way and structured as data models.

67 lines (66 loc) 2.54 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const Transaction_1 = __importDefault(require("../models/Transaction")); const Client_1 = __importDefault(require("./Client")); class TransactionsClient extends Client_1.default { constructor() { super(...arguments); this._path = '/open-finances/transactions'; } getUriParams(objectReq) { let params = ''; let index = 0; for (let [key, value] of Object.entries(objectReq)) { params += '&'; if (Array.isArray(value)) params += `${key}=${value.join('&' + key + '=')}`; else params += `${key}=${value}`; index++; } return params; } processResponse(response) { return new Transaction_1.default(response); } processListResponseBuild() { return (response) => { if (!response.data) { return { data: [], currentPage: 0, totalPages: 0, totalItems: 0 }; } return { data: response.data.map((transaction) => new Transaction_1.default(transaction)), currentPage: response.currentPage || 0, totalPages: response.totalPages || 0, totalItems: response.totalItems || 0 }; }; } getList(accountIds, listOptions) { const params = listOptions ? this.getUriParams(listOptions) : ''; const accountIdParams = typeof accountIds === 'number' ? `accountIds=${accountIds}` : `accountIds=${accountIds.join(',')}`; const uri = this._path + `?${accountIdParams}${params}`; return this.apiCore.doGet(uri, this.processListResponseBuild()); } get(id) { return this.apiCore.doGet(`${this._path}/${id}`, this.processResponse); } create(transactionToCreate) { return this.apiCore.doPost(this._path, transactionToCreate.toObject(), this.processResponse); } edit(id, transactionToUpdate) { return this.apiCore.doPut(`${this._path}/${id}`, transactionToUpdate.toObject(), this.processResponse); } delete(id) { return this.apiCore.doDelete(`${this._path}/${id}`); } deleteAll(accountId) { return this.apiCore.doDelete(`${this._path}?accountId=${accountId}`); } } exports.default = TransactionsClient;