magicbell
Version:
MagicBell API wrapper
52 lines • 1.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Resource = void 0;
const utils_js_1 = require("../lib/utils.js");
const method_js_1 = require("./method.js");
const paginate_js_1 = require("./paginate.js");
function isEmptyPayload(data) {
if (!data)
return true;
if (Array.isArray(data))
return data.length === 0;
if (typeof data === 'object')
return Object.keys(data).length === 0;
return false;
}
class Resource {
path;
entity;
client;
constructor(client) {
this.client = client;
}
request({ method, paged, path: tplPath }, ...args) {
const { path, data, params, options } = (0, method_js_1.normalizeArgs)({
path: (0, utils_js_1.joinUrlSegments)(this.path, tplPath),
method,
args,
});
const makeRequest = ({ data, params }) => {
const entity = this.entity || this.path;
data = isEmptyPayload(data) ? undefined : { [entity]: data };
params = isEmptyPayload(params) ? undefined : params;
return this.client
.request({ method, path, data, params }, options)
.then((response) => response?.[entity] || response);
};
if (paged) {
return (0, paginate_js_1.autoPaginate)(makeRequest, {
data,
params,
});
}
return makeRequest({ data, params });
}
assertFeatureFlag(flag) {
if (!this.client.hasFlag(flag)) {
throw new Error(`This is a beta feature, please enable it by providing the "${flag}" feature flag.`);
}
}
}
exports.Resource = Resource;
//# sourceMappingURL=resource.js.map