alinea
Version:
Headless git-based CMS
95 lines (93 loc) • 2.43 kB
JavaScript
import {
array,
boolean,
object,
string
} from "../chunks/chunk-WD7H5L2L.js";
import "../chunks/chunk-NZLE2WMY.js";
// src/core/Root.ts
import { getRoot, hasRoot, internalRoot } from "./Internal.js";
import { Schema } from "./Schema.js";
import { Type } from "./Type.js";
var Root;
((Root2) => {
function label(root2) {
return getRoot(root2).label;
}
Root2.label = label;
function contains(root2) {
return getRoot(root2).contains ?? [];
}
Root2.contains = contains;
function data(root2) {
return getRoot(root2);
}
Root2.data = data;
function preview(root2) {
return getRoot(root2).preview;
}
Root2.preview = preview;
function defaultLocale(root2) {
return getRoot(root2).i18n?.locales[0];
}
Root2.defaultLocale = defaultLocale;
function isRoot(value) {
return Boolean(value && hasRoot(value));
}
Root2.isRoot = isRoot;
function isMediaRoot(root2) {
return Boolean(getRoot(root2).isMediaRoot);
}
Root2.isMediaRoot = isMediaRoot;
const RootOptions = object({
label: string,
i18n: object({
locales: array(string)
}).optional,
view: string.optional,
isMediaRoot: boolean.optional
});
function validate(root2, workspaceLabel, schema) {
const { contains: contains2 } = getRoot(root2);
const keyOfType = Schema.typeNames(schema);
if (contains2) {
for (const inner of contains2) {
if (typeof inner === "string") {
if (!schema[inner])
throw new Error(
`Root "${label(
root2
)}" in workspace "${workspaceLabel}" contains "${inner}", but that Type does not exist`
);
} else {
const hasType = keyOfType.has(inner);
if (!hasType)
throw new Error(
`Root "${label(
root2
)}" in workspace "${workspaceLabel}" contains "${Type.label(
inner
)}", but that Type does not exist`
);
}
}
}
}
Root2.validate = validate;
function referencedViews(root2) {
const { view } = data(root2);
return typeof view === "string" ? [view] : [];
}
Root2.referencedViews = referencedViews;
})(Root || (Root = {}));
function root(label, config = {}) {
const instance = {
...config.children,
[internalRoot]: { ...config, label }
};
return instance;
}
export {
Root,
root
};