@schema-engine/actions

Overview

@schema-engine/actions provides a generic, serializable, type-safe action execution framework. It can be used in any domai or forms, workflows, state machines, UI interactions and is completely independent from any specific framework.

Key Features:

  • Domain Agnostic: No dependencies on specific frameworks or domains
  • Type Safe: Full TypeScript support with generic constraints
  • Sequential Execution: Actions execute in order, preventing race conditions
  • Pluggable: Registry-based architecture for extensibility
  • Testable: Clean separation of concerns makes testing straightforward
  • Zero Dependencies: Only requires Zod for schema validation

Installation

bun add @schema-engine/actions zod

Quick Start

import { ActionExecutor, ActionRegistration, ActionRegistry } from '@schema-engine/actions';
import { z } from 'zod';
 
// 1. Define your domain context and triggers
type AppContext = {
  userId: string;
  metadata?: Record<string, unknown>;
};
type AppTrigger = 'click' | 'submit' | 'auto';
 
// 2. Create the registry
const registry = new ActionRegistry<AppContext, AppTrigger>();
 
// 3. Register an action
const logAction: ActionRegistration<'log', AppContext, AppTrigger> = {
  $type: 'log',
  schema: z.object({
    $type: z.literal('log'),
    message: z.string(),
  }),
  metadata: {
    name: 'Log Message',
    description: 'Logs a message to stdout',
    defaultTriggers: ['click'],
  },
  execute: (context) => {
    console.log(`[${context.userId}]`, context);
    return { success: true };
  },
};
 
registry.register(logAction);
 
// 4. Execute
const executor = new ActionExecutor(registry);
 
await executor.execute(
  [{ id: '1', $type: 'log', enabled: true }],
  'click',
  { userId: 'user_123' }
);

Next Steps