mirror of
https://gitea.toothfairyai.com/ToothFairyAI/tf_code.git
synced 2026-04-04 08:03:14 +00:00
44 lines
984 B
TypeScript
44 lines
984 B
TypeScript
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<typeof define>
|
|
|
|
const registry = new Map<string, Definition>()
|
|
|
|
export function define<Type extends string, Properties extends ZodType>(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",
|
|
})
|
|
}
|
|
}
|