@becomes/cms-cloud-client
Version:
SDK for accessing BCMS Cloud API
83 lines (82 loc) • 3.01 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.InstancePluginHandler = void 0;
const queue_1 = require("@banez/queue");
const types_1 = require("@banez/queue/types");
const buffer_1 = require("buffer");
class InstancePluginHandler {
sdk;
baseUrl = (instanceId) => {
return `/v2/instance/${instanceId}/plugin`;
};
storeKey = 'instancePlugin';
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 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.instanceId}`,
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 set(data) {
const result = await this.sdk.send({
url: `${this.baseUrl(data.instanceId)}/set/${data.name}/${buffer_1.Buffer.from(data.tag).toString('hex')}/${data.version}`,
method: 'POST',
data: data.formData,
onUploadProgress: data.onUploadProgress,
});
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.InstancePluginHandler = InstancePluginHandler;