@becomes/cms-cloud-client
Version:
SDK for accessing BCMS Cloud API
88 lines (87 loc) • 2.8 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LimitHandler = void 0;
const queue_1 = require("@banez/queue");
const types_1 = require("@banez/queue/types");
class LimitHandler {
sdk;
baseUrl = '/v2/limit';
storeKey = 'limit';
getAllLatch = false;
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 || !data.skipCache) && this.getAllLatch) {
return this.sdk.store[this.storeKey].items();
}
const result = await this.sdk.send({
url: `${this.baseUrl}/all`,
method: 'GET',
});
this.sdk.store[this.storeKey].set(result.items);
this.getAllLatch = 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._id === data.id);
if (cacheHit) {
return cacheHit;
}
}
const result = await this.sdk.send({
url: `${this.baseUrl}/${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}`,
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}`,
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.id}`,
method: 'DELETE',
});
this.sdk.store[this.storeKey].remove(result.item._id);
return result.item;
}
}
exports.LimitHandler = LimitHandler;