Overview
Initialization
NPX
To create a node, run the following cli command:
Run the command
Follow the interactive prompts
Install dependencies and start coding
This will create a new node in the nodes/
directory of the project. To learn how to create a new project, visit Quickstart or NPX documentation.__
Directory Structure
Dependencies - package.json
The package.json
includes necessary scripts and dependencies for building, testing, and running the node. When working on the core logic of your node in the index.ts
file you can install any dependencides of this node there.
Creating logic - index.ts
It is the main file where the core logic of the node is written. You can import any installed packagse and use like any other typescript file. However, there are a few rules to follow:
- Each node needs to have a
handle
method. - If a node receives input from the previous node or request body, it should be defined in the
inputSchema
property. Otherwise, a type ofany
will be used. - If a node returns a response, it should be defined in the
outputSchema
property. Otherwise, a type ofany
will be used. - The
handle
method should return the output data or a promise that resolves to the output data. NodeError
class should be used to throw errors in the node.
Here is an example of a node structure that has been created via npx nanoctl@latest create node
:
handle
method
Each node needs to have a handle
method where all the core logic of the node is written. It accepts two parameters: ctx
and inputs
and returns a promise of NanoServiceResponse
.
Contains context received by the node from the workflow when it is executed. To learn more about CTX, visit CTX.
Is the data that is being passed to the node when it is executed (from previous nodes) or instantiated (from the static configurations of the workflows)
inputSchema
and outputSchema
These properties define the input and output data that is being passed to the node when it is executed. The inputSchema
and outputSchema
are optional and can be defined as any
if not required. Both of them follow JSON Schema format and are in the constructor of the main node class.
constructor
accept config
parameter that can be used to pass the configuration to the node in case certain properties need to be defined on build@nanoservice-ts/runner
methods, classes, and interfaces
nanoservice-ts
also contains a number of helpers methods and classes that can be used in the node. To learn more, visit Reference.
Configuration - config.json
Defines the JSON Schema for the node configuration. This file defines inputs, outputs, and configurations of each node to ensure that a user passes the required parameters.
config.json
file is not used during the node creation and workflows run, but only inside Deskree platfomr. However, it is recommended to define the configuration schema there as it will be used in the near futureFull example
Definitions
The name of the node
A brief explanation of what the workflow does.
The version of the workflow, useful for managing updates.
A category of the node used for internal classification.
A JSON Schema that defines the node configuration, which is the data that is being passed to the node when it is instantiated, unlike input
that is being passed to the node when it is executed.
A JSON Schema that defines the input data that is being passed to the node when it is executed.
A JSON Schema that defines the output data that is being returned from the node when it is executed.
TBD
TBD
Documenting
It is highly recommended to document the node for better understanding and maintainability. In the near future, we will streamle the process of sharing the nodes with the community, hence the documentation will become a vital part of the project. Currently, the documentation is supported via the following files:
README.md
CHANGELOG.md
Testing, Debugging & Running
Nodes created via npx
command or templates include a test folder and a sample test file (index.test.ts) to validate your node’s logic.
Directory Structure
Writing Test Cases
Use tools like Jest or Mocha (based on the provided template). The default template includes a basic example to validate your node logic. Example of an index.test.ts
file:
To run the test, use the following command:
IF you need to debug the node, you can run it in the development mode using the following command:
This command uses nodemon
to restart your node every time you save a change, simplifying the development process. If you want to adjust the process, you access the configuration in the nodemon.json
file:
Debugging Techniques
Logging Output
Use console.log()
statements to log variables or responses during execution. Example:
Manual Testing
If an error occurs, the BlueprintNode class captures the stack trace and returns a structured error. Check the error logs to locate issues:
Manual Testing
Publishing nodes to npm
Publishing your custom nodes to npm allows other developers to easily install and use them in their workflows.
Follow these steps to publish your custom node to the npm registry:
Create an npm Account
If you don’t already have an npm account, sign up at https://www.npmjs.com/signup. Run the following command in your terminal to log in to your npm account:
Provide your username, password, and email.
Update the package.json File
Ensure that your package.json is correctly configured:
Set the name of your node (use a scoped name like @yourusername/node-name for clarity).
Add the appropriate version following semantic versioning.
Provide a description and keywords to make your node discoverable.
Example package.json:
Build the Node
If your project uses TypeScript, ensure it is built into JavaScript before publishing:
Publish to npm
Run the following command in your project directory:
Use the —access public flag to ensure the package is publicly accessible.
Verify Your Node
After publishing:
- Verify your node is available on npm by visiting https://www.npmjs.com/package/@yourusername/node-name.
- Test the node by installing it in a fresh project:
Keep Your Node Updated
When making changes:
- Update the version number in package.json (e.g., 1.0.0 -> 1.0.1).
- Rebuild the project.
- Run
npm publish
again to push updates.