UNPKG

@paroicms/server

Version:
61 lines 2.74 kB
import { getDocumentTypeByName } from "@paroicms/internal-anywhere-lib"; import { parseLNodeId, } from "@paroicms/public-anywhere-lib"; import { assignLoadDescriptor } from "@paroicms/public-server-lib"; import { loadParentRoutingClusterFromNode } from "../connector/db-init/load-routing-cluster.queries.js"; import { createLiquidDrop } from "../liquidjs-tools/liquidjs-drop.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 toTpClusterPayload(renderingContext, language, parent); }, }, }); } 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 = assignLoadDescriptor({ id: documentId, children: assignLoadDescriptor({}, { load: "list", nodeKind: "document", descriptorName: "children", parentDocumentId: parseLNodeId(documentId), parentDocumentTypeName: documentType.typeName, }), }, { load: "one", nodeKind: "document", descriptorName: "id", documentId: parseLNodeId(documentId), }); 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