UNPKG

@commercelayer/sdk-utils

Version:
66 lines (63 loc) 2.24 kB
'use strict'; var sdk = require('@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 (sdk.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 = sdk.CommerceLayerStatic.isSingleton(res) ? res.slice(0, -1) : res; const resApi = cl[resField]; if (resApi?.type && sdk.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; exports.ApiResourceClient = ApiResourceClient; exports.ApiSdkUtils = ApiSdkUtils; exports.CommerceLayerUtils = CommerceLayerUtils; exports.init_default = init_default; //# sourceMappingURL=chunk-MLE76YHX.cjs.map //# sourceMappingURL=chunk-MLE76YHX.cjs.map