Workflow
Workflows are central to nanoservice-ts
, defining the orchestration of Nodes to implement business logic. The nanoctl
CLI helps in scaffolding new workflow definition files, primarily through the create workflow
command.
create workflow
This command is used to generate a new workflow definition file within your Nanoservice-TS project.
Purpose: To create a boilerplate JSON file for a new workflow, placing it in the workflows/
directory of your project. This file will contain a basic structure that you can then customize to define your specific sequence of nodes and trigger configuration.
Usage:
To create a new workflow, run the following command:
1. Interactive Prompts & Options
When you run nanoctl create workflow
, you might be prompted for:
- Workflow Name: Provide a unique name for your workflow.
2. Understand the Workflow Structure
A workflow consists of three main sections:
- Trigger: Defines how the workflow is executed.
- Steps: Specifies the sequence of nodes executed in order.
- Nodes: Configures each node used in the steps.
3. Example Workflow (Fetching Data)
Below is an example of a workflow named “fetch-cat-facts” that makes an HTTP request to fetch cat facts. Save this file as workflows/json/cats.json
:
4. Workflow Structure Breakdown
1. Trigger Section
The trigger
defines how the workflow is executed:
method
: Specifies the HTTP method (e.g.,GET
,POST
, etc.). Use"*"
to accept any method.path
: Defines the URL path. Supports path parameters:"/:id"
→ Required parameter (e.g.,/cats/123
)."/:id?"
→ Optional parameter (e.g.,/cats
or/cats/123
).
accept
: Defines the expected response format (application/json
by default).
2. Steps Section
The steps
define the sequence of nodes executed in the workflow:
name
: Unique name for the step.node
: The node to execute (e.g.,@nanoservice-ts/api-call
).type
: Specifies the type of node. The available options are:module
: Used for nodes installed as dependencies (e.g., from npm) or nodes created using theClass
type.local
: Used for nodes created locally, typically intended for publishing to npm.
3. Nodes Section
The nodes
section configures each node used in the workflow:
inputs
: Configuration values for the node.url
: API endpoint.method
: HTTP method (GET
,POST
, etc.).headers
: Optional request headers.
5. Run & Test the Workflow
-
Start the nanoservice project:
-
Test the workflow using
curl
or Postman:✔ If everything is set up correctly, the response will contain a random cat fact fetched from the API. The
/cats
path corresponds to the workflow JSON fileworkflows/json/cats.json
.
Summary
- ✅ Workflows consist of Triggers, Steps, and Nodes.
- ✅ HTTP triggers support method filtering (
GET
,POST
, etc.) and dynamic paths (/:id
). - ✅ Nodes execute nanoservices and are configured in the nodes section.
- ✅ Workflows can be tested via
curl
or Postman.