UNPKG

@nitrogenbuilder/client-core

Version:

Nitrogen Builder Core Client

139 lines (138 loc) 3.83 kB
const PROP_COPY_KEYS = [ 'label', 'default', 'responsive', 'divider', 'clearable', 'conditions', 'allowOther', 'options', 'accept', 'ctrlType', 'useUnit', 'step', 'sources', 'dialogTitle', 'collection', 'initialLimitDefault', 'totalLimitDefault', 'loadMoreLimitDefault', 'useLoadMoreDefault', 'usePaginationDefault', 'config', 'hasToolbar', 'defaultLinkState', 'layout', 'layoutMode', 'startLabel', 'endLabel', 'min', 'max', ]; function sanitizeValue(value) { if (value === undefined) return undefined; if (value === null || typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') { return value; } if (Array.isArray(value)) { return value .map((entry) => sanitizeValue(entry)) .filter((entry) => entry !== undefined); } if (typeof value === 'function') { return undefined; } if (typeof value === 'object') { const next = {}; for (const [key, entry] of Object.entries(value)) { const serialized = sanitizeValue(entry); if (serialized !== undefined) { next[key] = serialized; } } return next; } return undefined; } function sanitizeOptions(options) { const serialized = sanitizeValue(options); if (!Array.isArray(serialized)) return undefined; return serialized; } function normalizeProp(prop) { const next = { type: prop.type, }; for (const key of PROP_COPY_KEYS) { const value = sanitizeValue(prop[key]); if (value !== undefined) { if (key === 'options') { next.options = sanitizeOptions(value); continue; } next[key] = value; } } if ('props' in prop && prop.props) { next.props = normalizePropMap(prop.props); } if (prop.type === 'asset-folder' && 'files' in prop) { next.meta = { assetCount: Object.keys(prop.files || {}).length, }; } return next; } function normalizePropMap(props) { return Object.fromEntries(Object.entries(props).map(([propKey, prop]) => [propKey, normalizeProp(prop)])); } function normalizeGroup(groupKey, group) { return { key: groupKey, label: group.label, props: normalizePropMap(group.props), }; } function normalizeCategory(categoryKey, category) { return { key: categoryKey, label: category.label, groups: Object.fromEntries(Object.entries(category.groups).map(([groupKey, group]) => [ groupKey, normalizeGroup(groupKey, group), ])), }; } function normalizeChildren(children) { const serialized = sanitizeValue(children); if (serialized === undefined) return undefined; return serialized; } export function createComponentManifestEntry({ name, scope, settings, }) { return { name, scope, description: settings.description, sidebarCategory: settings.options?.category, children: normalizeChildren(settings.options?.children), categories: Object.fromEntries(Object.entries(settings.categories).map(([categoryKey, category]) => [ categoryKey, normalizeCategory(categoryKey, category), ])), }; } export function createComponentManifest(components, options) { return { components: [...components] .sort((a, b) => a.name.localeCompare(b.name)) .map((component) => createComponentManifestEntry(component)), generatedAt: options?.generatedAt ?? new Date().toISOString(), source: options?.source, }; }