@becomes/cms-cloud-client
Version:
SDK for accessing BCMS Cloud API
184 lines (183 loc) • 6.82 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InstanceFjeHandler = void 0;
const queue_1 = require("@banez/queue");
const types_1 = require("@banez/queue/types");
class InstanceFjeHandler {
sdk;
baseUrl = (instanceId) => {
return `/v2/instance/${instanceId}/fje`;
};
storeKey = 'instanceFje';
getAllLatch = {};
getAllQueue = (0, queue_1.createQueue)();
getQueue = (0, queue_1.createQueue)();
constructor(sdk) {
this.sdk = sdk;
}
async getAll(data) {
const queue = await this.getAllQueue({
name: 'getAll',
handler: async () => {
if (!data.skipCache && this.getAllLatch[data.instanceId]) {
return this.sdk.store[this.storeKey].findMany((e) => e.instanceId === data.instanceId);
}
const result = await this.sdk.send({
url: `${this.baseUrl(data.instanceId)}/all`,
method: 'GET',
});
this.sdk.store[this.storeKey].set(result.items);
this.getAllLatch[data.instanceId] = true;
return result.items;
},
}).wait;
if (queue instanceof types_1.QueueError) {
throw queue.error;
}
return queue.data;
}
async getAllWithCode(data) {
const queue = await this.getAllQueue({
name: 'getAll',
handler: async () => {
if (!data.skipCache && this.getAllLatch[data.instanceId]) {
const items = this.sdk.store[this.storeKey].findMany((e) => e.instanceId === data.instanceId);
if (!items.find((e) => !e.code)) {
return items;
}
}
const result = await this.sdk.send({
url: `${this.baseUrl(data.instanceId)}/all/code`,
method: 'GET',
});
this.sdk.store[this.storeKey].set(result.items);
this.getAllLatch[data.instanceId] = true;
return result.items;
},
}).wait;
if (queue instanceof types_1.QueueError) {
throw queue.error;
}
return queue.data;
}
async getAllByType(data) {
const queue = await this.getAllQueue({
name: 'getAll',
handler: async () => {
const key = `${data.instanceId}_${data.type}`;
if (!data.skipCache && this.getAllLatch[key]) {
return this.sdk.store[this.storeKey].findMany((e) => e.instanceId === data.instanceId);
}
const result = await this.sdk.send({
url: `${this.baseUrl(data.instanceId)}/all/${data.type}`,
method: 'GET',
});
this.sdk.store[this.storeKey].set(result.items);
this.getAllLatch[key] = true;
return result.items;
},
}).wait;
if (queue instanceof types_1.QueueError) {
throw queue.error;
}
return queue.data;
}
async getAllByTypWithCode(data) {
const queue = await this.getAllQueue({
name: 'getAll',
handler: async () => {
const key = `${data.instanceId}_${data.type}`;
if (!data.skipCache && this.getAllLatch[key]) {
const items = this.sdk.store[this.storeKey].findMany((e) => e.instanceId === data.instanceId);
if (!items.find((e) => !e.code)) {
return items;
}
}
const result = await this.sdk.send({
url: `${this.baseUrl(data.instanceId)}/all/${data.type}/code`,
method: 'GET',
});
this.sdk.store[this.storeKey].set(result.items);
this.getAllLatch[key] = true;
return result.items;
},
}).wait;
if (queue instanceof types_1.QueueError) {
throw queue.error;
}
return queue.data;
}
async get(data) {
const queue = await this.getQueue({
name: 'get',
handler: async () => {
if (!data.skipCache) {
const cacheHit = this.sdk.store[this.storeKey].find((e) => e.instanceId === data.instanceId && e._id === data.id);
if (cacheHit) {
return cacheHit;
}
}
const result = await this.sdk.send({
url: `${this.baseUrl(data.instanceId)}/${data.id}`,
method: 'GET',
});
this.sdk.store[this.storeKey].set(result.item);
return result.item;
},
}).wait;
if (queue instanceof types_1.QueueError) {
throw queue.error;
}
return queue.data;
}
async getWithCode(data) {
const queue = await this.getQueue({
name: 'get',
handler: async () => {
if (!data.skipCache) {
const cacheHit = this.sdk.store[this.storeKey].find((e) => e.instanceId === data.instanceId && e._id === data.id);
if (cacheHit && cacheHit.code) {
return cacheHit;
}
}
const result = await this.sdk.send({
url: `${this.baseUrl(data.instanceId)}/${data.id}/code`,
method: 'GET',
});
this.sdk.store[this.storeKey].set(result.item);
return result.item;
},
}).wait;
if (queue instanceof types_1.QueueError) {
throw queue.error;
}
return queue.data;
}
async create(data) {
const result = await this.sdk.send({
url: `${this.baseUrl(data.instanceId)}`,
method: 'POST',
data,
});
this.sdk.store[this.storeKey].set(result.item);
return result.item;
}
async update(data) {
const result = await this.sdk.send({
url: `${this.baseUrl(data.instanceId)}`,
method: 'PUT',
data,
});
this.sdk.store[this.storeKey].set(result.item);
return result.item;
}
async deleteById(data) {
const result = await this.sdk.send({
url: `${this.baseUrl(data.instanceId)}/${data.id}`,
method: 'DELETE',
});
this.sdk.store[this.storeKey].remove(result.item._id);
return result.item;
}
}
exports.InstanceFjeHandler = InstanceFjeHandler;