Service for registering and executing commands using the command pattern.

The CommandBus is the central hub for the command pattern implementation. It manages command registration, handler lookup, and command execution. Commands are decoupled from their handlers, allowing for flexible middleware chains and cross-cutting concerns like logging, validation, and authorization.

Key responsibilities:

  • Register commands with their handlers
  • Execute commands through their registered handlers
  • Support middleware for command pipeline
  • Dispatch command lifecycle events
  • Provide command metadata and discovery

Example

// Register and execute a command
const commandBus = platform.get(PlatformServiceName.CommandBus);

if (commandBus.isSupported(SendEmailCommand)) {
const command = new SendEmailCommand(context, 'Subject', 'Body');
await commandBus.handle(command);
}

Example

// Register a custom command handler
const commandBus = platform.get(PlatformServiceName.CommandBus);

const handler = {
handle: async (command: GenerateReportCommand) => {
console.log('Generating report...');
return { success: true, reportUrl: '/reports/123.pdf' };
}
};

commandBus.register(
GenerateReportCommand,
handler,
{
title: 'Generate Report',
description: 'Creates a PDF report',
icon: 'file-pdf'
}
);

See

Hierarchy

Methods

  • Create a command instance from a CommandConfig

    Throws

    Thrown if the command has not been registered yet

    Type Parameters

    • Key extends keyof CommandRegistry

    Parameters

    • config: CommandConfig<CommandRegistry[Key], Key>

      The command configuration

    Returns CommandRegistry[Key]

  • Type Parameters

    • T = any

    • Key extends string = string

    Parameters

    Returns T

  • Execute the given command with it's registered command handler

    Returns

    result from the command handler

    Parameters

    Returns any

  • Check if a command is supported

    Returns

    true if the command is supported, false otherwise

    Parameters

    • commandId: CommandIdentifier<AnyCommand>

      identifier of the command. Can be either the class or the string the class was registered with

    Returns boolean