UNPKG

@commercelayer/sdk-utils

Version:
61 lines (59 loc) 2.13 kB
import { CommerceLayerStatic } from '@commercelayer/sdk'; // src/init.ts var CommerceLayerUtilsConfig = class { #sdk; #api = {}; constructor(cl, resources) { if (cl === void 0 || cl === null) throw Error("Invalid Commerce Layer client provided"); this.#sdk = cl; if (resources && resources.length > 0) this.addApiResources(...resources); } get sdk() { if (!this.#sdk) throw Error("CommerceLayer Utils not initialized"); return this.#sdk; } addApiResources(...resources) { if (!resources || resources.length === 0) throw Error("Invalid resources array provided"); resources.forEach((r) => { this.addApiResource(r); }); return this; } addApiResource(resource) { const type = resource.type(); if (CommerceLayerStatic.resources().includes(type)) this.#api[type] = resource; else throw Error(`Invalid resource: [${type}]`); return this; } api(resourceType) { if (!this.#api) throw Error("CommerceLayer Utils API resources not initialized"); const res = this.#api[resourceType]; if (!res) throw Error(`Resource [${resourceType}] not available`); return res; } }; var clUtilsConfig; function CommerceLayerUtils(cl, resources) { if (cl) { const resList = resources || []; if (resList.length === 0) { for (const res of cl.resources()) { const resField = CommerceLayerStatic.isSingleton(res) ? res.slice(0, -1) : res; const resApi = cl[resField]; if (resApi?.type && CommerceLayerStatic.resources().includes(resApi.type())) resList.push(resApi); } } clUtilsConfig = new CommerceLayerUtilsConfig(cl, resList); } else if (resources) throw new Error("CommerceLayer SDK is required to initialize resources"); return clUtilsConfig; } function ApiResourceClient(resourceType) { return CommerceLayerUtils().api(resourceType); } function ApiSdkUtils() { return CommerceLayerUtils().sdk; } var init_default = CommerceLayerUtils; export { ApiResourceClient, ApiSdkUtils, CommerceLayerUtils, init_default }; //# sourceMappingURL=chunk-XGJBRQ3X.js.map //# sourceMappingURL=chunk-XGJBRQ3X.js.map