UNPKG

mdxlayer

Version:

Transform your MDX content into typed, JSON-powered data with flexible schema validation.

32 lines (29 loc) 806 B
import { t } from 'tyne'; import { cache } from '../../cache/index.js'; import { transformFile } from '../../utils/transform.js'; const toTypesDts = ({ resolvedFields, frontmatterSchema, docType }) => { const defaultSchema = { _body: t.object({ code: t.string(), raw: t.string() }), _filePath: t.string(), _id: t.string() }; if (resolvedFields) { for (const [key, { type }] of Object.entries(resolvedFields)) { defaultSchema[key] = type; } } const types = t.object({ ...frontmatterSchema.shape, ...defaultSchema }).toDts(); const isChanged = cache.changed(types, "types.d.ts"); if (isChanged) { transformFile({ doc: `export type ${docType} = ${types}`, filename: "types.d.ts", subpath: "generated" }); } }; export { toTypesDts };