UNPKG

@alauda/doom

Version:

Doctor Doom making docs.

64 lines (63 loc) 4.69 kB
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime"; import { FallbackHeading } from '@rspress/core/theme'; import { Fragment, useMemo } from 'react'; import { resolveRef } from '../utils.js'; import { Markdown } from './Markdown.js'; import { APIReferenceLink } from './_APIReferenceLink.js'; import { X } from './_X.js'; const getSchemaValue = (schema, key = 'type') => { if (schema[key]) { return schema[key]; } if (schema.allOf?.length) { const allOf = schema.allOf[0]; const value = allOf[key]; if (value) { return value; } } if (key === 'type' && schema.oneOf?.length) { return schema.oneOf.map((it) => it[key]); } }; const NESTED_SCHEMA_TYPES = new Set(['object', 'array']); const METADATA = 'metadata'; const DEFAULT_METADATA_DESCRIPTION = 'ObjectMeta is metadata that all persisted resources must have, which includes all objects users must create.'; const derefSchema = (schema, fullSchema) => { const $ref = getSchemaValue(schema, '$ref'); if (fullSchema && $ref) { return resolveRef(fullSchema, $ref); } return schema; }; const typeCode = (type) => (_jsx("code", { children: Array.isArray(type) ? type.join('|') : type })); export const K8sAPISchemaItemBasic = ({ schema, }) => { const description = getSchemaValue(schema, 'description'); return (_jsxs("dl", { children: [description && (_jsxs(_Fragment, { children: [_jsx("dt", { children: "Description" }), _jsx("dd", { children: description })] })), _jsx("dt", { children: "Type" }), _jsx("dd", { children: typeCode(getSchemaValue(schema)) }), schema.required && (_jsxs(_Fragment, { children: [_jsx("dt", { children: "Required" }), _jsx("dd", { children: schema.required.map((it) => (_jsx("code", { children: it }, it))) })] }))] })); }; export const K8sAPISchemaItem = ({ schema, fullSchema, propertyPath = '', }) => { const schemaType = useMemo(() => getSchemaValue(schema), [schema]); const isArraySchema = schemaType === 'array'; const nestedSchema = useMemo(() => isArraySchema ? derefSchema(schema.items, fullSchema) : schema, [fullSchema, isArraySchema, schema]); const propertiesEntry = useMemo(() => { const properties = getSchemaValue(nestedSchema, 'properties'); return (properties && Object.entries(properties).map(([key, value]) => [key, derefSchema(value, fullSchema)])); }, [fullSchema, nestedSchema]); return (_jsxs(_Fragment, { children: [_jsx(K8sAPISchemaItemBasic, { schema: schema }), !propertyPath && _jsx(FallbackHeading, { level: 2, title: "Specification" }), isArraySchema && (_jsxs(_Fragment, { children: [_jsx("div", { className: "rp-toc-exclude", children: _jsx(FallbackHeading, { level: 3, title: `${propertyPath}[]` }) }), _jsx(K8sAPISchemaItemBasic, { schema: nestedSchema })] })), propertiesEntry && (_jsxs(_Fragment, { children: [_jsxs(X.table, { children: [_jsx("thead", { children: _jsxs("tr", { children: [_jsx("th", { children: "Property" }), _jsx("th", { children: "Type" }), _jsx("th", { children: "Description" })] }) }), _jsx("tbody", { children: propertiesEntry.map(([key, value]) => { const description = getSchemaValue(value, 'description'); const isMetadata = key === METADATA; return (_jsxs("tr", { children: [_jsx("td", { children: _jsx("code", { children: key }) }), _jsx("td", { children: isMetadata ? (_jsx("code", { children: _jsx(APIReferenceLink, { name: "ObjectMeta" }) })) : (typeCode(getSchemaValue(value))) }), _jsx("td", { children: _jsx(Markdown, { children: description || (isMetadata ? DEFAULT_METADATA_DESCRIPTION : '') }) })] }, key)); }) })] }), propertiesEntry.map(([key, value]) => { const type = getSchemaValue(value); if (!NESTED_SCHEMA_TYPES.has(type) || key === METADATA) { return; } const nestedPropertyPath = `${propertyPath}${isArraySchema ? '[]' : ''}.${key}`; return (_jsxs(Fragment, { children: [_jsx("div", { className: "rp-toc-exclude", children: _jsx(FallbackHeading, { level: 3, title: nestedPropertyPath }) }), _jsx(K8sAPISchemaItem, { schema: value, fullSchema: fullSchema, propertyPath: nestedPropertyPath })] }, key)); })] }))] })); }; export const K8sAPISchema = ({ schema, fullSchema }) => (_jsx(K8sAPISchemaItem, { schema: schema, fullSchema: fullSchema }));