finerio-pfm-unnax
Version:
This SDK lets you connect to [Finerio PFM API Unnax](http://ec2-3-16-174-50.us-east-2.compute.amazonaws.com:8082/swagger-ui/index.html#/) in an easier way.
61 lines (60 loc) • 2.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const models_1 = require("../models");
class Categories {
constructor(fcSdk) {
this.fcSdk = fcSdk;
this.path = "/categories";
}
get(id) {
const uri = `${this.path}/${id}`;
return this.fcSdk.doGet(uri, this.processResponse);
}
update(id, updateObject) {
const uri = `${this.path}/${id}`;
return this.fcSdk.doPut(uri, updateObject.plainObject, this.processResponse);
}
create(createCategory) {
const uri = `${this.path}`;
return this.fcSdk.doPost(uri, createCategory.plainObject, this.processResponse);
}
processResponse(response) {
return new models_1.Category(response);
}
delete(id) {
const uri = `${this.path}/${id}`;
return this.fcSdk.doDelete(uri, this.processDeleteResponse);
}
processDeleteResponse(status) {
return status === "" ? 204 : 500;
}
list() {
const uri = `${this.path}`;
return this.fcSdk.doGet(uri, this.processlistResponse);
}
processlistResponse(response) {
return response.data
? [...response.data].reverse().map((cat) => new models_1.Category(cat))
: [];
}
listWithSubcategories() {
const uri = `${this.path}`;
return this.fcSdk.doGet(uri, this.processListWithSubcategoriesResponse);
}
processListWithSubcategoriesResponse(response) {
let categories = [];
if (response.data) {
const catsOrd = [...response.data].reverse();
categories = catsOrd
.filter((cat) => !cat.parentCategoryId)
.map((cat) => new models_1.ParentCategory(cat));
categories.forEach((parcat) => {
parcat.subcategories = catsOrd
.filter((rescat) => rescat.parentCategoryId === parcat.id)
.map((cat) => new models_1.Category(cat));
});
}
return categories;
}
}
exports.default = Categories;