@reintent/catalog
Declaring a host catalog (docs/design/0043 §2): the kinds a host draws, each with what it is (`about`), its data props as a Zod object, its children and its events.
exportCatalog gives the contract every host reads, JSON in a JSON Schema subset (0043 §3); @reintent/core checks frames and events against that contract and does not depend on Zod.
A Zod feature the subset cannot say (refine, transform, a pattern, an exclusive bound) is an error when the catalog is defined, not left out of the contract. A union exports as anyOf, as Zod reads it: the first branch that takes the value.
New.
Classes
InvalidCatalog
Defined in: packages/catalog/src/index.ts
A catalog's kinds are not what the contract can hold. errors says each, as data.
Extends
Error
Constructors
Constructor
new InvalidCatalog(errors): InvalidCatalog;Defined in: packages/catalog/src/index.ts
Parameters
| Parameter | Type |
|---|---|
errors | CatalogError[] |
Returns
Overrides
Error.constructorProperties
| Property | Modifier | Type |
|---|---|---|
errors | readonly | CatalogError[] |
Type Aliases
Catalog
type Catalog<K> = object;Defined in: packages/catalog/src/index.ts
A catalog as declared: its id and version, its kinds. exportCatalog gives its contract.
Type Parameters
| Type Parameter | Default type |
|---|---|
K extends object | object |
Properties
| Property | Modifier | Type |
|---|---|---|
[CATALOG] | readonly | HostCatalog |
id | readonly | string |
kinds | readonly | K |
version | readonly | number |
CatalogElements
type CatalogElements<C> = { [K in keyof C["kinds"] & string]: KindProps<C["kinds"][K]> };Defined in: packages/catalog/src/index.ts
A catalog's kinds as elements, by name: what JSX.IntrinsicElements holds for its entry.
Type Parameters
| Type Parameter |
|---|
C extends Catalog |
Children
type Children = "none" | "text" | "nodes";Defined in: packages/catalog/src/index.ts
What a kind's children are: none, strings and numbers, or host nodes and text.
EventDef
type EventDef<S, O> = object;Defined in: packages/catalog/src/index.ts
An event of a kind: the sends a node may put in the prop (one of them), and whether it may leave it out.
Type Parameters
Properties
| Property | Modifier | Type | Description |
|---|---|---|---|
omittable | readonly | O | Whether a node may leave the prop out (.optional()). |
sends | readonly | readonly S[] | - |
Methods
optional()
optional(): EventDef<S, true>;Defined in: packages/catalog/src/index.ts
A node may leave the prop out.
Returns
EventDef<S, true>
or()
or<T>(other): EventDef<S | T, O>;Defined in: packages/catalog/src/index.ts
This send or the other: a node puts exactly one in the prop.
Type Parameters
| Type Parameter |
|---|
T extends Send |
Parameters
| Parameter | Type |
|---|---|
other | EventDef<T, false> |
Returns
EventDef<S | T, O>
KindDef
type KindDef<P, E, C> = object;Defined in: packages/catalog/src/index.ts
A kind as declared: about for a model and a person, data props, children, events.
Type Parameters
| Type Parameter | Default type |
|---|---|
P extends z.ZodObject | z.ZodObject |
E extends object | object |
C extends Children | Children |
Properties
KindProps
type KindProps<D> = PropsOf<D, "in"> & EventProps<D["events"]> & ChildrenProps<D["children"]>;Defined in: packages/catalog/src/index.ts
The props a node of a kind is written with: its data props as the Zod object takes them (a default makes a prop optional), its events, its children. For a catalog's JSX entry (0043 §4).
Type Parameters
| Type Parameter |
|---|
D extends KindDef |
Send
type Send =
| {
send: "action";
value?: z.ZodType;
}
| {
args?: z.ZodType;
send: "control";
};Defined in: packages/catalog/src/index.ts
What a node sends back on an event: an action, or an application control.
Functions
action()
function action<V>(opts?): EventDef<{
send: "action";
value: V;
}, false>;Defined in: packages/catalog/src/index.ts
An event that sends a HostAction. With value, a decide without a value takes one the host
gives, within value, and a decide with its value holds to value; other actions are sent as
they are. Without value, a node's decide carries its value. The value cannot be .optional()
(optional-value when the catalog is defined): a decide the host sends without one has nothing
to decide.
Type Parameters
| Type Parameter | Default type |
|---|---|
V extends | ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>> | undefined | undefined |
Parameters
| Parameter | Type |
|---|---|
opts | { value?: V; } |
opts.value? | V |
Returns
EventDef<{
send: "action";
value: V;
}, false>
control()
function control<A>(opts?): EventDef<{
args: A;
send: "control";
}, false>;Defined in: packages/catalog/src/index.ts
An event that sends an application control (undo, a review, a sentence told; 0043 §5). The
node holds { name, args? }; with args, the host gives args that args allows when the
node does not fix them. Its handler is the session's, not the catalog's.
Type Parameters
| Type Parameter | Default type |
|---|---|
A extends | ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>> | undefined | undefined |
Parameters
| Parameter | Type |
|---|---|
opts | { args?: A; } |
opts.args? | A |
Returns
EventDef<{
args: A;
send: "control";
}, false>
defineCatalog()
function defineCatalog<S>(meta, ...sources): Catalog<Merge<KindsOf<S[number]>> extends K ? K : never>;Defined in: packages/catalog/src/index.ts
Declare a catalog from kinds, and from other catalogs: a kind named twice is an error, not
the later one winning. The catalog is checked as it is defined, export included, and throws
InvalidCatalog with every problem.
Type Parameters
| Type Parameter |
|---|
S extends readonly object[] |
Parameters
| Parameter | Type |
|---|---|
meta | { id: string; version: number; } |
meta.id | string |
meta.version | number |
...sources | S |
Returns
Catalog<Merge<KindsOf<S[number]>> extends K ? K : never>
Example
import { z } from 'zod';
import { action, control, defineCatalog, exportCatalog, kind } from '@reintent/catalog';
const card = defineCatalog(
{ id: 'card', version: 1 },
{
stack: kind({
about: 'children in a row or a column',
props: z.object({ axis: z.enum(['h', 'v']), gap: z.number().min(0).default(8) }),
children: 'nodes',
}),
button: kind({
about: 'a button a person presses',
props: z.object({ label: z.string() }),
events: { press: action().or(control({ args: z.string().optional() })) },
}),
},
);
console.log(exportCatalog(card).kinds.stack.props?.properties?.gap);
// → { type: 'number', minimum: 0, default: 8 }
try {
defineCatalog({ id: 'more', version: 1 }, card, {
stack: kind({ about: 'another stack' }),
});
} catch (e) {
console.log((e as Error).message);
// → 'the catalog does not hold:\n /kinds/stack: declared twice (card, and source 2)'
}defineComponent()
function defineComponent<S>(schema, render): Component<input<S> & object> & object;Defined in: packages/catalog/src/index.ts
Declares an intent component's data props with Zod (0059). Input and render props are inferred
from the schema; defaults are filled before rendering. Uses the catalog's JSON Schema subset
and checker: unsupported schemas and reserved key/children fields are refused at declaration,
and undeclared data props are refused at evaluation. JSX children pass through separately.
The contract carries JSON props for the model and a pure parser. reprogrammable retains it
for every replacement; requests expose scope.propsSchema and importSchemas. Validation
covers actual calls, not all future inputs or the TypeScript type of generated source.
Type Parameters
| Type Parameter |
|---|
S extends ZodObject<$ZodLooseShape, $strip> |
Parameters
| Parameter | Type |
|---|---|
schema | S |
render | Component<output<S> & object> |
Returns
Component<input<S> & object> & object
Example
import { z } from 'zod';
import { defineComponent } from '@reintent/catalog';
import { createIntent, host, type HostNode } from '@reintent/core';
const Heading = defineComponent(
z.object({ text: z.string().min(1), size: z.number().int().min(1).default(2) }),
({ text, size }) => host('heading', { text, size }),
);
const intent = createIntent({ program: <Heading text="Hello" /> });
console.log((intent.realize().tree as HostNode).props); // → { text: 'Hello', size: 2 }exportCatalog()
function exportCatalog(catalog): HostCatalog;Defined in: packages/catalog/src/index.ts
The catalog's contract (0043 §3): JSON, the props and event values in the JSON Schema subset.
This is what a native host, a model and checkFrame read. A copy each time.
Parameters
| Parameter | Type |
|---|---|
catalog | Catalog |
Returns
hostOf()
function hostOf<C>(catalog): <K>(kind, props) => IntentElement;Defined in: packages/catalog/src/index.ts
host for one catalog: a node of one of its kinds, with the props TypeScript checks against the
catalog (CatalogElements). The function form of the catalog's JSX entry (0043 §4).
Type Parameters
| Type Parameter |
|---|
C extends Catalog<{ [kind: string]: KindDef<ZodObject<$ZodLooseShape, $strip>, { [name: string]: EventDef<Send, boolean>; }, Children>; }> |
Parameters
| Parameter | Type |
|---|---|
catalog | C |
Returns
<K>(kind, props) => IntentElement
Example
import { z } from 'zod';
import { action, defineCatalog, exportCatalog, hostOf, kind } from '@reintent/catalog';
import { action as act, checkFrame, createIntent } from '@reintent/core';
const card = defineCatalog(
{ id: 'card', version: 1 },
{
button: kind({
about: 'a button a person presses',
props: z.object({ label: z.string() }),
events: { press: action() },
}),
},
);
const node = hostOf(card);
const intent = createIntent({
program: node('button', { label: 'Go', press: act.tell('go') }),
});
console.log(checkFrame(exportCatalog(card), intent.realize().tree).errors); // → []
// node('button', { label: 3 }) does not compile: label is a string, and press is required.
intent.dispose();jsxEntry()
function jsxEntry(entry): string;Defined in: packages/catalog/src/index.ts
The source of a catalog's JSX entry (0043 §4): write it to jsx-runtime.ts next to the
catalog, export it as the package's ./jsx-runtime and ./jsx-dev-runtime, and a file with
@jsxImportSource of the package writes that catalog's kinds and nothing else. Its JSX is
intent JSX (@reintent/core/intent/jsx-runtime): the entry only narrows the types.
Parameters
| Parameter | Type | Description |
|---|---|---|
entry | { from: string; name: string; } | - |
entry.from | string | The module that exports the catalog, from the entry's file. |
entry.name | string | The catalog's export name. |
Returns
string
Example
import { jsxEntry } from '@reintent/catalog';
const source = jsxEntry({ from: './catalog.js', name: 'softoy' });
console.log(source.includes("import type { softoy } from './catalog.js';")); // → truekind()
function kind<P, E, C>(def): KindDef<P, E, C>;Defined in: packages/catalog/src/index.ts
Declare a kind. Its children are none unless it says otherwise.
Type Parameters
| Type Parameter | Default type |
|---|---|
P extends ZodObject<$ZodLooseShape, $strip> | ZodObject<Record<never, never>, $strip> |
E extends object | Record<never, never> |
C extends Children | "none" |
Parameters
| Parameter | Type |
|---|---|
def | { about: string; children?: C; events?: E; props?: P; } |
def.about | string |
def.children? | C |
def.events? | E |
def.props? | P |
Returns
KindDef<P, E, C>
opaque()
function opaque<S>(schema): ZodOptional<S>;Defined in: packages/catalog/src/index.ts
A prop the host takes as it is (0049): any data, the contract's {} with the schema's
description, { "description": … }. schema only gives TypeScript the prop's type and is not
exported or checked: as with any, the one who writes the value answers for it. For a host
extension's catalog (the DOM catalog's style); an application's common kinds write a schema
for every prop, so that each host knows how to draw it.
It is optional, and a prop of a kind itself: in an object or an array of a prop, or in an event,
it is opaque-position when the catalog is defined; with .default(), opaque-default; with
.nullable(), opaque-nullable (it takes null already). schema itself is left as it is.
Type Parameters
| Type Parameter |
|---|
S extends ZodType<unknown, unknown, $ZodTypeInternals<unknown, unknown>> |
Parameters
| Parameter | Type |
|---|---|
schema | S |
Returns
ZodOptional<S>
Example
import { z } from 'zod';
import { defineCatalog, exportCatalog, kind, opaque } from '@reintent/catalog';
const web = defineCatalog(
{ id: 'web', version: 1 },
{
box: kind({
about: 'a block of content',
props: z.object({
style: opaque(z.record(z.string(), z.string()).describe('CSS properties')),
}),
children: 'nodes',
}),
},
);
console.log(exportCatalog(web).kinds.box.props);
// → { type: 'object', properties: { style: { description: 'CSS properties' } }, additionalProperties: false }Last updated on
Edit on GitHub