Toolset

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

export class DateToolset {
  static getFirstDayOfMonth(date: Date): Date {
    return new Date(date.getFullYear(), date.getMonth(), 1);
  }
  
  static getLastDayOfMonth(date: Date): Date {
    return new Date(date.getFullYear(), date.getMonth() + 1, 0);
  }
  // more date-related utility methods here...
}

Creating new toolset

Using CLI Command with options

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

soap new toolset -n "Date" -e "shop" -l "domain" -m "getFirstDayOfMonth(date:Date):Date, getLastDayOfMonth(date:Date):Date"

Options explained:

  • -n: Name of the toolset (e.g., "Date").

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

  • -l: The layer/folder (e.g., "domain", "data") in which the toolset is to be placed.

  • -m: Method name(s)/ declaration(s), separated by commas.

  • -w: Generate with dependencies included.

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

Using Interactive Form

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

soap new toolset -w -f

Follow the prompts to enter your toolset's details.

Using JSON Configuration

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

{
  "toolsets": [
    {
      "name": "DateToolset",
      "endpoint": "shop",
      "layer": "domain",
      "methods": [
        {
          "name": "getFirstDayOfMonth",
          "is_static": true,
          "params": ["date:Date"],
          "return_type": "Date"
        },
        {
          "name": "getLastDayOfMonth",
          "is_static": true,
          "params": [
            {
              "name":"date",
              "type":"Date"
            }
          ],
          "return_type": "Date"
        },
      ]
    }
  ]
}

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.

  • --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.

File Structure

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

src/
  - shop/
    - domain/
      - toolsets/
        - date.toolset.ts
        - index.ts
...

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

Last updated