Getting Started

This guide walks you through creating and executing your first action.

import { 
  ActionConfigBase, 
  ActionExecutionResult, 
  ActionRegistration, 
  ActionRegistry, 
  ActionExecutor 
} from '@schema-engine/actions';
import { z } from 'zod';
 
// 1. Define Domain Types
type MyTrigger = 'submit' | 'change';
interface MyContext {
  id: string;
  data: Record<string, unknown>;
}
 
// 2. Define Action Config
interface MyActionConfig extends ActionConfigBase {
  $type: string;
  message?: string;
}
 
// 3. Create Action
const logAction: ActionRegistration<'log', MyContext, MyTrigger> = {
  $type: 'log',
  schema: z.object({
    $type: z.literal('log'),
    message: z.string(),
  }),
  metadata: {
    name: 'Log Action',
    defaultTriggers: ['submit'],
  },
  execute: (context) => {
    // context.context is the MyContext
    // context.trigger is MyTrigger
    console.log('Executing log:', context);
    return { success: true };
  },
};
 
// 4. Setup & Execute
const registry = new ActionRegistry<MyContext, MyTrigger>();
registry.register(logAction);
 
const executor = new ActionExecutor(registry);
 
await executor.execute(
  [{ id: '1', $type: 'log', message: 'Hello', enabled: true }], // Actions
  'submit',                                                   // Trigger
  { id: 'ctx-1', data: {} }                                   // Context
);

Next Steps