@@ -8,7 +8,19 @@ import { type Type, type } from "arktype";
88import { standard } from "./standard" ;
99import type { BaseEventType , SchemaInput , ValueOf } from "./types" ;
1010
11- type ArktypeObjectType = Type < object , object > ;
11+ type ArktypeEmptyObject = Type < object , object > ;
12+
13+ type ArktypeEventObject <
14+ $$EventName extends string ,
15+ $$Body extends ArktypeEmptyObject ,
16+ > = Type < {
17+ eventId : string ;
18+ eventCreatedAt : string ;
19+ entityName : string ;
20+ entityId : string ;
21+ eventName : $$EventName ;
22+ body : $$Body [ "infer" ] ;
23+ } > ;
1224
1325/**
1426 * Creates an ArkType schema provider for Ventyd.
@@ -93,15 +105,25 @@ type ArktypeObjectType = Type<object, object>;
93105export function arktype <
94106 $$EntityName extends string ,
95107 $$EventBodyArktypeDefinition extends {
96- [ eventName : string ] : ArktypeObjectType ;
108+ [ eventName : string ] : ArktypeEmptyObject ;
97109 } ,
98- $$StateArktypeDefinition extends ArktypeObjectType ,
110+ $$StateArktypeDefinition extends ArktypeEmptyObject ,
99111 $$NamespaceSeparator extends string = ":" ,
100112> ( args : {
101113 event : $$EventBodyArktypeDefinition ;
102114 state : $$StateArktypeDefinition ;
103115 namespaceSeparator ?: $$NamespaceSeparator ;
104116} ) {
117+ type $$EventArktypeDefinition = {
118+ [ key in Extract <
119+ keyof $$EventBodyArktypeDefinition ,
120+ string
121+ > ] : ArktypeEventObject <
122+ `${$$EntityName } ${$$NamespaceSeparator } ${key } `,
123+ $$EventBodyArktypeDefinition [ key ]
124+ > ;
125+ } ;
126+
105127 type $$EventStandardDefinition = {
106128 [ key in Extract <
107129 keyof $$EventBodyArktypeDefinition ,
@@ -124,46 +146,56 @@ export function arktype<
124146 > ;
125147 type $$StateType = StandardSchemaV1 . InferOutput < $$StateStandardDefinition > ;
126148
127- type $$SchemaInput = SchemaInput < $$EntityName , $$EventType , $$StateType > ;
149+ type $$SchemaInput = SchemaInput <
150+ $$EntityName ,
151+ $$EventType ,
152+ $$StateType ,
153+ {
154+ event : $$EventArktypeDefinition ;
155+ state : $$StateArktypeDefinition ;
156+ }
157+ > ;
128158
129159 const input : $$SchemaInput = ( context ) => {
130160 const namespaceSeparator = args . namespaceSeparator ?? ":" ;
131161
132- const event = Object . entries ( args . event ) . reduce ( ( acc , [ key , body ] ) => {
133- const eventName = `${ context . entityName } ${ namespaceSeparator } ${ key } ` ;
134-
135- // ArkType's intersection to combine base event and body
136- const arktypeSchema = type ( {
137- eventId : "string" ,
138- eventCreatedAt : "string" ,
139- entityName : "string" ,
140- entityId : "string" ,
141- eventName : `'${ eventName } '` ,
142- body,
143- } ) ;
144-
145- // ArkType natively implements Standard Schema V1
146- // We can use it directly as a StandardSchemaV1
147- const standardSchema = arktypeSchema as unknown as StandardSchemaV1 ;
162+ const eventSchema = Object . entries ( args . event ) . reduce (
163+ ( acc , [ key , body ] ) => {
164+ const eventName = `${ context . entityName } ${ namespaceSeparator } ${ key } ` ;
148165
149- return {
150- // biome-ignore lint/performance/noAccumulatingSpread: readonly acc
151- ...acc ,
152- [ eventName ] : standardSchema ,
153- } ;
154- } , { } as $$EventStandardDefinition ) ;
166+ // ArkType's intersection to combine base event and body
167+ const arktypeSchema = type ( {
168+ eventId : "string" ,
169+ eventCreatedAt : "string" ,
170+ entityName : "string" ,
171+ entityId : "string" ,
172+ eventName : `'${ eventName } '` ,
173+ body,
174+ } ) ;
155175
156- // ArkType state schema is already a Standard Schema V1
157- const state = args . state as unknown as $$StateStandardDefinition ;
176+ return {
177+ // biome-ignore lint/performance/noAccumulatingSpread: readonly acc
178+ ...acc ,
179+ [ eventName ] : arktypeSchema ,
180+ } ;
181+ } ,
182+ { } as $$EventArktypeDefinition ,
183+ ) ;
184+ const stateSchema = args . state ;
158185
159- return standard <
160- $$EntityName ,
161- $$EventStandardDefinition ,
162- $$StateStandardDefinition
163- > ( {
164- event,
165- state,
166- } ) ( context ) ;
186+ return {
187+ event : eventSchema ,
188+ state : stateSchema ,
189+ ...standard <
190+ $$EntityName ,
191+ $$EventStandardDefinition ,
192+ $$StateStandardDefinition
193+ > ( {
194+ // ArkType natively implements Standard Schema V1
195+ event : eventSchema as unknown as $$EventStandardDefinition ,
196+ state : stateSchema as unknown as $$StateStandardDefinition ,
197+ } ) ( context ) ,
198+ } ;
167199 } ;
168200
169201 return input ;
0 commit comments