UNPKG

@paroicms/server

Version:
53 lines 2.59 kB
import { documentTypeHasData, getDocumentTypeByName } from "@paroicms/internal-anywhere-lib"; import { parseLNodeId, } from "@paroicms/public-anywhere-lib"; import { loadParentRoutingClusterFromNode } from "../connector/db-init/load-routing-cluster.queries.js"; import { createLiquidDrop } from "../liquidjs-tools/liquidjs-drop.js"; import { createDocPayloadDrop } from "./create-doc-drop.js"; import { getDocItem } from "./doc-values.queries.js"; export function toTpClusterPayload(renderingContext, language, cluster) { const { siteContext } = renderingContext; return createLiquidDrop(renderingContext, { renderingCacheKey: `cluster:${cluster.nodeId}:${language}`, values: { kind: "cluster", routing: toTpRoutingClusterNode(renderingContext, cluster, language), parentCluster: cluster.kind === "home" ? undefined : async () => { const parent = await loadParentRoutingClusterFromNode(siteContext, cluster); if (!parent) { throw new Error(`Missing parent routing cluster for node '${cluster.nodeId}'`); } return toTpRoutingClusterNode(renderingContext, parent, language); }, }, }); } export function toTpRoutingClusterNode(renderingContext, clusterNode, language) { const { siteContext } = renderingContext; const documentId = clusterNode.lNodeIds[language]; if (documentId === undefined) { throw new Error(`missing language '${language}' in cluster node '${clusterNode.nodeId}'`); } const documentType = getDocumentTypeByName(siteContext.siteSchema, clusterNode.typeName); const result = { id: documentId, doc: documentTypeHasData(documentType) ? createDocPayloadDrop(renderingContext, () => getDocItem(siteContext, renderingContext.tracker, parseLNodeId(documentId))) : undefined, }; for (const [childTypeName, childIds] of Object.entries(clusterNode.children ?? {})) { result[childTypeName] = toTpRoutingClusterNode(renderingContext, childIds, language); } return result; } export function getClusterNodeOf(cluster, routingDocumentNodeId) { if (cluster.nodeId === routingDocumentNodeId) return cluster; for (const childClusterNode of Object.values(cluster.children ?? {})) { const child = getClusterNodeOf(childClusterNode, routingDocumentNodeId); if (child) return child; } } //# sourceMappingURL=cluster-payloads.js.map