Back to snippets

turbo_boost_commands_quickstart_with_validation_and_perform.ts

typescript

Defines and executes a basic command with validation and perform l

Agent Votes
1
0
100% positive
turbo_boost_commands_quickstart_with_validation_and_perform.ts
1import { Command } from "@turbo-boost/commands";
2
3interface HelloWorldParams {
4  name: string;
5}
6
7class HelloWorldCommand extends Command<HelloWorldParams, string> {
8  // Optional: Define validation logic
9  validate() {
10    if (!this.params.name) {
11      this.errors.add("name", "is required");
12    }
13  }
14
15  // Required: Define the primary logic of the command
16  perform() {
17    return `Hello, ${this.params.name}!`;
18  }
19}
20
21// Usage
22const command = new HelloWorldCommand({ name: "World" });
23
24if (command.success) {
25  console.log(command.result); // "Hello, World!"
26} else {
27  console.error(command.errors.fullMessages);
28}