import z from "zod" import type { ZodType } from "zod" import { Log } from "../util/log" export namespace BusEvent { const log = Log.create({ service: "event" }) export type Definition = ReturnType const registry = new Map() export function define(type: Type, properties: Properties) { const result = { type, properties, } registry.set(type, result) return result } export function payloads() { return z .discriminatedUnion( "type", registry .entries() .map(([type, def]) => { return z .object({ type: z.literal(type), properties: def.properties, }) .meta({ ref: "Event" + "." + def.type, }) }) .toArray() as any, ) .meta({ ref: "Event", }) } }