@becomes/cms-cloud-client
Version:
SDK for accessing BCMS Cloud API
71 lines (70 loc) • 2.27 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OrgHandler = void 0;
const queue_1 = require("@banez/queue");
const types_1 = require("@banez/queue/types");
class OrgHandler {
sdk;
baseUrl = '/v2/org';
getAllLatch = false;
getQueue = (0, queue_1.createQueue)();
getAllQueue = (0, queue_1.createQueue)();
constructor(sdk) {
this.sdk = sdk;
}
async get(data) {
const queue = await this.getQueue({
name: 'get',
handler: async () => {
if (!data.skipCache && this.getAllLatch) {
const cacheHit = this.sdk.store.org.findById(data.id);
if (cacheHit) {
return cacheHit;
}
}
const result = await this.sdk.send({
url: `${this.baseUrl}/${data.id}`,
method: 'GET',
});
this.sdk.store.org.set(result.item);
return result.item;
},
}).wait;
if (queue instanceof types_1.QueueError) {
throw queue.error;
}
return queue.data;
}
async getAll(data) {
const queue = await this.getAllQueue({
name: 'get',
handler: async () => {
if ((!data || !data.skipCache) && this.getAllLatch) {
return this.sdk.store.org.items();
}
const result = await this.sdk.send({
url: `${this.baseUrl}/all`,
method: 'GET',
});
this.sdk.store.org.set(result.items);
this.getAllLatch = true;
return result.items;
},
}).wait;
if (queue instanceof types_1.QueueError) {
throw queue.error;
}
return queue.data;
}
// public create(data: OrgCreateData): Promise<Org> {}
async update(data) {
const result = await this.sdk.send({
url: `${this.baseUrl}/${data.id}`,
method: 'GET',
data,
});
this.sdk.store.org.set(result.item);
return result.item;
}
}
exports.OrgHandler = OrgHandler;