UNPKG

magicbell

Version:
48 lines 1.57 kB
import { joinUrlSegments } from '../lib/utils.js'; import { normalizeArgs } from './method.js'; import { autoPaginate } from './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; } export class Resource { path; entity; client; constructor(client) { this.client = client; } request({ method, paged, path: tplPath }, ...args) { const { path, data, params, options } = normalizeArgs({ path: 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 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.`); } } } //# sourceMappingURL=resource.js.map