Surface / foldkit-metadata
metadata
When one package builds a description (such as which Model fields a screen reads) and another package attaches requirements to it (such as which server queries those fields need), the carrier package cannot hardcode fields for every downstream consumer. Putting plain string properties on an object also risks name collisions between unrelated packages.
foldkit-metadata solves this by giving each consumer a private, typed key (Metadata.key). A carrier merges and forwards entries with Metadata.combine without knowing what they mean, and only code holding the original key object can read its entries back.
It also provides Evidence, which records why a declared transition keeps a law so foldkit-entity and foldkit-surface can share those records without depending on each other.
When to write a Metadata key
Use foldkit-metadata when you are building a carrier (a description other packages decorate, such as a Surface Projection) or an interpreter (a package that attaches facts to someone else's description, such as foldkit-remote recording server data needs).
Applications rarely import this package directly. foldkit-surface re-exports Metadata, so an app using Surface already has it. Metadata describes static declarations at definition time; it lives outside your Foldkit Model and update.
- The key defines the entry type, how duplicates
merge, and how an entry issummarized as text. - The carrier decides where metadata sits and merges parts with
Metadata.combine, without inspecting entries. - Lookup uses key object identity rather than the string
name, so two packages picking the same name stay isolated.
Install
pnpm add foldkit-metadata effectA first Metadata key
This declares a Flags key with a deduplicating merge rule, combines two metadata values, and reads back both the typed entries and their text summary:
import { Metadata } from 'foldkit-metadata'
// An interpreter declares its key once at module level.
const Flags = Metadata.key<string>('flags', {
merge: flags => [...new Set(flags)],
summarize: flag => flag,
})
// A carrier merges parts without knowing which keys are inside.
const combined = Metadata.combine([Flags.of('beta'), Flags.of('beta', 'gamma')])
Flags.get(combined) // ['beta', 'gamma']
Metadata.summarize(combined) // [{ name: 'flags', entries: ['beta', 'gamma'] }]Flags.of and Metadata.combine construct immutable values and run merge eagerly, so Flags.get(combined) returns ['beta', 'gamma'] with 'beta' deduplicated.
Writing a merge rule
A carrier can combine parts that were already combined. Write merge to be associative so regrouping parts preserves the same result: a set union deduplicates repeated flags, whereas array concatenation keeps order and duplicates.
Flags.get(Metadata.empty) returns []. Another key also named 'flags' reads [] from combined because names only label summarize output while object identity controls lookup. Treat a key as an integration token rather than a security boundary against code that already holds that key reference.
merge and summarize are your own callbacks; if either throws, the exception propagates directly to the caller.