@@ -2,6 +2,7 @@ import path from 'path';
22import { JSDOM , VirtualConsole } from 'jsdom' ;
33import fs from 'fs-extra' ;
44import { getPlatform , type Elb } from '@walkeros/core' ;
5+ import { schemas } from '@walkeros/core/dev' ;
56import {
67 createCommandLogger ,
78 createLogger ,
@@ -40,22 +41,34 @@ export async function pushCommand(options: PushCommandOptions): Promise<void> {
4041 name : 'event' ,
4142 } ) ;
4243
43- // Validate event format
44- if (
45- ! event ||
46- typeof event !== 'object' ||
47- ! ( 'name' in event ) ||
48- typeof event . name !== 'string'
49- ) {
50- throw new Error (
51- 'Event must be an object with a "name" property (string)' ,
52- ) ;
44+ // Validate event format using Zod schema
45+ const eventResult = schemas . PartialEventSchema . safeParse ( event ) ;
46+ if ( ! eventResult . success ) {
47+ const errors = eventResult . error . issues
48+ . map ( ( issue ) => `${ String ( issue . path . join ( '.' ) ) } : ${ issue . message } ` )
49+ . join ( ', ' ) ;
50+ throw new Error ( `Invalid event: ${ errors } ` ) ;
5351 }
5452
55- // Warn about event naming format
56- if ( ! event . name . includes ( ' ' ) ) {
53+ const parsedEvent = eventResult . data as {
54+ name ?: string ;
55+ data ?: Record < string , unknown > ;
56+ } ;
57+ if ( ! parsedEvent . name ) {
58+ throw new Error ( 'Invalid event: Missing required "name" property' ) ;
59+ }
60+
61+ // Create typed event object for execution
62+ const validatedEvent : { name : string ; data : Record < string , unknown > } =
63+ {
64+ name : parsedEvent . name ,
65+ data : ( parsedEvent . data || { } ) as Record < string , unknown > ,
66+ } ;
67+
68+ // Warn about event naming format (walkerOS business logic)
69+ if ( ! validatedEvent . name . includes ( ' ' ) ) {
5770 logger . warn (
58- `Event name "${ event . name } " should follow "ENTITY ACTION" format (e.g., "page view")` ,
71+ `Event name "${ validatedEvent . name } " should follow "ENTITY ACTION" format (e.g., "page view")` ,
5972 ) ;
6073 }
6174
@@ -108,10 +121,10 @@ export async function pushCommand(options: PushCommandOptions): Promise<void> {
108121
109122 if ( platform === 'web' ) {
110123 logger . info ( '🌐 Executing in web environment (JSDOM)...' ) ;
111- result = await executeWebPush ( tempPath , event , logger ) ;
124+ result = await executeWebPush ( tempPath , validatedEvent , logger ) ;
112125 } else if ( platform === 'server' ) {
113126 logger . info ( '🖥️ Executing in server environment (Node.js)...' ) ;
114- result = await executeServerPush ( tempPath , event , logger ) ;
127+ result = await executeServerPush ( tempPath , validatedEvent , logger ) ;
115128 } else {
116129 throw new Error ( `Unsupported platform: ${ platform } ` ) ;
117130 }
@@ -199,12 +212,20 @@ export async function pushCommand(options: PushCommandOptions): Promise<void> {
199212 ) ;
200213}
201214
215+ /**
216+ * Typed event input for push command
217+ */
218+ interface PushEventInput {
219+ name : string ;
220+ data : Record < string , unknown > ;
221+ }
222+
202223/**
203224 * Execute push for web platform using JSDOM with real APIs
204225 */
205226async function executeWebPush (
206227 bundlePath : string ,
207- event : Record < string , unknown > ,
228+ event : PushEventInput ,
208229 logger : Logger ,
209230) : Promise < PushResult > {
210231 const startTime = Date . now ( ) ;
@@ -244,8 +265,7 @@ async function executeWebPush(
244265
245266 // Push event
246267 logger . info ( `Pushing event: ${ event . name } ` ) ;
247- const eventData = ( event . data || { } ) as Record < string , unknown > ;
248- const elbResult = await elb ( event . name as string , eventData ) ;
268+ const elbResult = await elb ( event . name , event . data ) ;
249269
250270 return {
251271 success : true ,
@@ -266,7 +286,7 @@ async function executeWebPush(
266286 */
267287async function executeServerPush (
268288 bundlePath : string ,
269- event : Record < string , unknown > ,
289+ event : PushEventInput ,
270290 logger : Logger ,
271291 timeout : number = 60000 , // 60 second default timeout
272292) : Promise < PushResult > {
@@ -305,13 +325,12 @@ async function executeServerPush(
305325
306326 // Push event
307327 logger . info ( `Pushing event: ${ event . name } ` ) ;
308- const eventData = ( event . data || { } ) as Record < string , unknown > ;
309328 const elbResult = await (
310329 elb as (
311330 name : string ,
312331 data : Record < string , unknown > ,
313332 ) => Promise < Elb . PushResult >
314- ) ( event . name as string , eventData ) ;
333+ ) ( event . name , event . data ) ;
315334
316335 return {
317336 success : true ,
0 commit comments