@croct/plug
Version:
A fully-featured devkit for building natively personalized applications.
64 lines (63 loc) • 2.31 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var fetchContent_exports = {};
__export(fetchContent_exports, {
fetchContent: () => fetchContent
});
module.exports = __toCommonJS(fetchContent_exports);
var import_contentFetcher = require("@croct/sdk/contentFetcher");
var import_error = require("@croct/sdk/error");
var import_content = require("@croct/content");
function fetchContent(slotId, options) {
const {
apiKey,
appId,
fallback,
baseEndpointUrl,
logger,
preferredLocale = "",
...fetchOptions
} = options ?? {};
const auth = { appId, apiKey };
const [id, version = "latest"] = slotId.split("@");
const normalizedLocale = preferredLocale === "" ? void 0 : preferredLocale;
const promise = new import_contentFetcher.ContentFetcher({ ...auth, baseEndpointUrl }).fetch(id, {
...fetchOptions,
...normalizedLocale !== void 0 ? { preferredLocale: normalizedLocale } : {},
...version !== "latest" ? { version } : {}
});
return promise.catch(
async (error) => {
if (logger !== void 0) {
logger.error(`Failed to fetch content for slot "${id}@${version}": ${(0, import_error.formatCause)(error)}`);
}
if (fallback !== void 0) {
return { content: fallback };
}
const staticContent = await (0, import_content.loadSlotContent)(id, normalizedLocale);
if (staticContent === null) {
throw error;
}
return { content: staticContent };
}
);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
fetchContent
});