UNPKG

@becomes/cms-cloud-client

Version:

SDK for accessing BCMS Cloud API

76 lines (75 loc) 2.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InstanceEnvHandler = void 0; const queue_1 = require("@banez/queue"); const types_1 = require("@banez/queue/types"); class InstanceEnvHandler { sdk; baseUrl = (instanceId) => { return `/v2/instance/${instanceId}/env`; }; storeKey = 'instanceEnv'; 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)}`, method: 'POST', data, }); this.sdk.store[this.storeKey].set(result.items); if (data.remove) { this.sdk.store[this.storeKey].remove(data.remove.map((e) => e._id)); } return result.items; } } exports.InstanceEnvHandler = InstanceEnvHandler;