UNPKG

@becomes/cms-cloud-client

Version:

SDK for accessing BCMS Cloud API

90 lines (89 loc) 3.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InstanceDomainHandler = void 0; const queue_1 = require("@banez/queue"); const types_1 = require("@banez/queue/types"); class InstanceDomainHandler { sdk; baseUrl = (instanceId) => { return `/v2/instance/${instanceId}/domain`; }; storeKey = 'instanceDomain'; 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.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 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.InstanceDomainHandler = InstanceDomainHandler;