Use Case

In this guide, we'll go through the steps to create a new use case in your SoapJS project. You can create use cases using CLI commands, interactive forms, or by defining them in a JSON file.

export class ProcessOrderUseCase {
  static Token = "ProcessOrderUseCase";

  public async execute(
    orderItems: Product[],
    customerDetails: Customer,
    paymentMethod: string,
  ): Promise<Result<OrderConfirmation>> {
    let confirmation: OrderConfirmation;
    // logic ...
    return Result.withContent(confirmation);
  }
}

Creating new use case

Using CLI Command with options

To create a new use cases directly via the CLI, use the following command:

soap new use-case -n "ProcessOrder" -e "shop" -i "orderItems:Array<Entity<Product>>,customerDetails:Customer,paymentMethod:string" -o "Entity<OrderConfirmation>" -w -f

Options explained:

  • -n: Name of the use case (e.g., "ProcessOrder").

  • -e: Endpoint associated with the use case (e.g., "shop").

  • -i: Input of the use case in "name:type" format, separated by commas.

  • -o: Type of the use case output.

  • --no-tests: Skip test generation.

  • --no-rel: Skip generating related files. You can also specify specific groups.

  • --force: Force the creation, overwrite files if necessary.

  • --patch: Add content to the files if they exists.

Using Interactive Form

If you prefer to use an interactive form to specify your use case details, simply run:

soap new use-case -w -f

Follow the prompts to enter your use case's details.

Using JSON Configuration

Alternatively, you can define your use cases in a JSON file. Here is an example structure:

{
  "use_cases": [
    {
      "name": "ProcessOrder",
      "endpoint": "shop",
      "input": [
        {
          "name": "orderItems",
          "type": "Array<Entity<Product>>"
        },
        {
          "name": "customerDetails",
          "type": "Entity<Customer>"
        },
        {
          "name": "paymentMethod",
          "type": "string"
        }
      ],
      "output": "Entity<OrderConfirmation>"
    }
  ]
}

Save this to a file, for example api.json, and run:

soap new --json "./path/to/api.json" -w -f

Options:

  • --json: Path to your JSON configuration file.

  • -w: Generate with dependencies included.

  • -f: Force the creation, overwrite files if necessary.

File Structure

After creating your use cases, your file structure (assuming default configuration settings) should look like this:

src/
  - shop/
    - domain/
      - entities/
        - product.ts
        - customer.ts
        - order-confirmation.ts
        - index.ts
      - use-cases/
        - process-order.use-case.ts
        - index.ts
...

This structure helps maintain a clean separation of your domain logic, keeping your use cases organized within the domain/use-cases/ directory.

Last updated