import type { z } from 'zod' import { fieldsFromSchema, humanize, type Field, type FieldConfig } from './fields.js' export interface ResourceConfig { /** Plural machine name, e.g. 'projects'. */ name: string label?: string /** Entity schema — drives table columns. */ schema: z.ZodObject /** Input schema for creation — drives the create form and validation. */ createSchema?: z.ZodObject /** Input schema for edits. Defaults to createSchema. */ updateSchema?: z.ZodObject /** Ordered subset of fields shown in the table. Defaults to all. */ columns?: string[] /** Identifier field. Default: 'id'. */ idField?: string /** * Display overrides keyed by field name, for every schema of the resource. * * Labels otherwise come from the field name through `humanize()`, which is an * English transformation of an identifier — `taxId` reads 'Tax Id' — and enum * options come out as the stored values. Neither is presentable in an * application written in another language, or wherever the field name is a * developer's name for the thing rather than the reader's. * * ```ts * fields: { taxId: { label: 'NIF' }, kind: { options: { person: 'Pessoa' } } } * ``` */ fields?: Record } export type FormMode = 'create' | 'update' export interface ValidationResult { success: boolean data?: unknown /** One message per field, keyed by field name. */ errors?: Record } export class Resource { readonly name: string readonly label: string readonly idField: string constructor(private readonly config: ResourceConfig) { this.name = config.name this.label = config.label ?? humanize(config.name) this.idField = config.idField ?? 'id' } /** All entity fields. */ fields(): Field[] { return fieldsFromSchema(this.config.schema, this.config.fields) } /** Table columns, honoring the configured order/subset. */ columns(): Field[] { const all = this.fields() if (!this.config.columns) return all return this.config.columns .map((name) => all.find((field) => field.name === name)) .filter((field): field is Field => field !== undefined) } /** Form fields for the given mode (falls back to entity fields minus the id). */ formFields(mode: FormMode = 'create'): Field[] { const schema = this.schemaFor(mode) return schema ? fieldsFromSchema(schema, this.config.fields) : this.fields().filter((f) => f.name !== this.idField) } /** Validates input against the mode's schema, mapping errors to field names. */ validate(input: unknown, mode: FormMode = 'create'): ValidationResult { const schema = this.schemaFor(mode) if (!schema) return { success: true, data: input } const result = schema.safeParse(input) if (result.success) return { success: true, data: result.data } const errors: Record = {} for (const issue of result.error.issues) { const key = String(issue.path[0] ?? '_') if (!(key in errors)) errors[key] = issue.message } return { success: false, errors } } private schemaFor(mode: FormMode): z.ZodObject | undefined { if (mode === 'update') return this.config.updateSchema ?? this.config.createSchema return this.config.createSchema } } export function defineResource(config: ResourceConfig): Resource { return new Resource(config) } // --- view models (headless — the React layer renders these) -------------- export interface TableView { columns: Field[] rows: Record[] } export function tableView(resource: Resource, rows: Record[]): TableView { return { columns: resource.columns(), rows } } export interface FormView { fields: Field[] values: Record mode: FormMode } export function formView( resource: Resource, values: Record = {}, mode: FormMode = 'create', ): FormView { return { fields: resource.formFields(mode), values, mode } }