@paroicms/server
Version:
The ParoiCMS server
121 lines • 4.99 kB
JavaScript
import { getNodeTypeByName } from "@paroicms/internal-anywhere-lib";
import { createNoTracker } from "@paroicms/internal-server-lib";
import { encodeLNodeId } from "@paroicms/public-anywhere-lib";
import { ApiError } from "@paroicms/public-server-lib";
import { getOneDocument } from "../admin-backend/document/load-documents.queries.js";
import { getFieldTypesByNames, loadFieldValues, } from "../admin-backend/fields/load-fields.queries.js";
import { findOneNode } from "../admin-backend/node/node.queries.js";
import { readPartSeedsOf } from "../admin-backend/part/part.queries.js";
import { getUrlOfDocument } from "../rendered-site/page-route/make-url.js";
import { loadRelatedTerms } from "./document-info.queries.js";
import { getLabelingFieldTypes } from "./programmatic-api-helpers.js";
export async function getFullDocumentProgrammaticApi(siteContext, lNodeId) {
const { nodeId, language } = lNodeId;
const doc = await getOneDocument(siteContext, lNodeId);
const node = await findOneNode(siteContext, nodeId);
const docType = getNodeTypeByName(siteContext.siteSchema, node.typeName);
if (docType.kind !== "document") {
throw new ApiError(`Node "${nodeId}" is not a document`, 400);
}
if (!node.parentId) {
throw new ApiError(`Document "${nodeId}" has no parent node`, 500);
}
const fieldValues = await loadDocumentFieldValues(siteContext, node.typeName, nodeId, language);
const parts = await loadAndOrganizeParts(siteContext, nodeId, language);
const url = await getUrlOfDocument(siteContext, createNoTracker(), lNodeId, {
returnUndef: true,
absoluteUrl: true,
});
const labelingFields = getLabelingFieldTypes(siteContext, node.typeName);
const relatedTerms = await loadRelatedTerms(siteContext, labelingFields, nodeId, language);
return {
documentId: encodeLNodeId(lNodeId),
nodeId,
parentNodeId: node.parentId,
relativeId: node.relativeId,
typeName: node.typeName,
publishDate: node.publishDate?.toISOString(),
ready: doc.ready,
language,
title: doc.title,
slug: doc.slug,
url,
relatedTerms,
fieldValues,
parts,
};
}
async function loadDocumentFieldValues(siteContext, typeName, nodeId, language) {
const fieldTypes = getFieldTypesByNames(siteContext, { typeName });
const fieldValuesRaw = await loadFieldValues(siteContext, {
fieldTypes,
nodeId,
language,
publishedOnly: false,
});
return stripAttachedData(fieldValuesRaw);
}
function stripAttachedData(fieldValuesRaw) {
const fieldValues = {};
for (const [key, value] of Object.entries(fieldValuesRaw)) {
if (Array.isArray(value)) {
fieldValues[key] = value[0] ?? undefined;
}
else {
fieldValues[key] = value;
}
}
return fieldValues;
}
async function loadAndOrganizeParts(siteContext, documentNodeId, language) {
const partSeeds = await readPartSeedsOf(siteContext, { documentNodeId, language });
const fullPartsByNodeId = await buildFullPartsMap(siteContext, partSeeds, language);
return organizePartsIntoTree(fullPartsByNodeId, documentNodeId);
}
async function buildFullPartsMap(siteContext, partSeeds, language) {
const fullPartsByNodeId = new Map();
for (const seed of partSeeds) {
if (!("partId" in seed))
continue;
const partSeed = seed;
const partFieldTypes = getFieldTypesByNames(siteContext, { typeName: partSeed.typeName });
const partFieldValuesRaw = await loadFieldValues(siteContext, {
fieldTypes: partFieldTypes,
nodeId: partSeed.nodeId,
language,
publishedOnly: false,
});
const fullPart = {
partId: partSeed.partId,
nodeId: partSeed.nodeId,
parentNodeId: partSeed.parentNodeId,
typeName: partSeed.typeName,
relativeId: "",
listName: partSeed.listName,
publishDate: partSeed.publishDate?.toISOString(),
ready: partSeed.ready,
language: partSeed.language,
fieldValues: stripAttachedData(partFieldValuesRaw),
};
fullPartsByNodeId.set(partSeed.nodeId, fullPart);
}
return fullPartsByNodeId;
}
function organizePartsIntoTree(fullPartsByNodeId, documentNodeId) {
const parts = {};
for (const [, fullPart] of fullPartsByNodeId) {
if (fullPart.parentNodeId === documentNodeId) {
parts[fullPart.listName] ??= [];
parts[fullPart.listName].push(fullPart);
}
else {
const parentPart = fullPartsByNodeId.get(fullPart.parentNodeId);
if (parentPart) {
parentPart.children ??= [];
parentPart.children.push(fullPart);
}
}
}
return parts;
}
//# sourceMappingURL=get-document-api.js.map