1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00
0 Design: Node Generator
Kazuhito Yokoi edited this page 2018-08-08 15:26:28 +09:00
This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Node generator function can automatically generate original nodes from Swagger definition, JavaScript code in function node and other sources. Currently, the command line version of node generator is available on the following GitHub repository.

Node Generator: https://github.com/node-red/node-red-nodegen

Using the tool, node developers can dramatically reduce their time to create original nodes. And they can easily publish them on npm repository. In the future plan, Node-RED flow editor UI will support the node generation functionality. This design note contains details of use cases, additional API design and UI of Node-RED flow editor.

Use Cases

(1) Swagger definition

Swagger is one of the famous specification for REST API. And its used for API documentation (Swagger UI) and client/server code generation (Swagger code generator). Node generator provides code generator function of Node-RED nodes using Swagger definitions. If cloud providers have Swagger definitions for their cloud services, they can create original nodes without JavaScript and HTML coding.

(2) Function node

In general, Node-RED users write JavaScript code in function node when they want to realize custom handling or algorism. Current function node isnt suitable for reusing nodes because the way to share their function node is only flow exporting function using JSON data. To solve the problem, Node generator supports creating original nodes from JavaScript code in function node. Node-RED users can easily create original nodes and publish them on npm library to share their nodes with other Node-RED users.

(3) Subflow

In terms of reuse, subflow unit is also suitable for sharing with other Node-RED users. In the future, Node generator will support to create original node from subflow. Node-RED users can wrap flow as original nodes. For example, template node which has authentication header and http request node which has URL are a typical pair which a lot of Node-RED users use to connect to cloud services. Node generator can generate original nodes from subflow which has the flow. And they can easily share them with other Node-RED users via npm repository.

Flow editor UI design

In the first version, Node-RED flow editor will support to generate nodes from Swagger definition and function node. “Creating nodes” item will be added under “Export” in the menu.

To select the type of node generation, theres the pull down menu on the creating nodes UI.

(1) Swagger definition

If you select “swagger” in “Type” pull down menu, UI shows the following items.

  • Node name: Input area to type module name of the generated node (The functionality internally adds the prefix, “node-red-contrib-“ at the beginning of the name)
  • Version: Input area to type version number of the generated node module
  • Source: Text area to paste the text of Swagger definition

(2) Function node

If you select “function” in “Type” pull down menu, UI shows the following items. The list of function nodes on workplace will be shown in the “target” pull down menu.

  • Target: list of function nodes on workspace (If Node-RED user select the item on the list, the Node name and Source fields are automatically inputted using the settings of the selected function node)
  • Node name: (Same as Swagger definition case)
  • Version: (Same as Swagger definition case)

Build button

Once Node-RED users input source data, node name and version, Build button will be enabled. After clicking the button, these data will be sent to Node-RED runtime and run build process to create original node internally.

Download UI

When Node-RED runtime finished generating original node as tgz file, Node-RED flow editor shows the notification message which has download button. After clicking the button, Node-RED users can download the file to their local PC.

To install the original node into their Node-RED environment, Node-RED users need to run the following command on Node-RED home directory which contains flow.json, settings.js and other files.

> cd ~/.node-red
> npm install node-red-conrib-<node name>-<version>.tgz
> node-red

Because tgz file is package of Node-RED node, Node-RED users can share it with other users as tgz file or via npm repository.

Feature flag of node generator UI

As the default, creating nodes UI will be disabled because only advanced developers (they have knowledge of Swagger or skills to write JavaScript code) will use the functionality. To enable the functionality, node developers need to change "editorTheme.menu.menu-item-creating-nodes" from false to true in setting file, “settings.js”.

API design

To realize node generator functionality on Node-RED flow editor, Node-RED runtime has the following additional API endpoints.

Request endpoint

  • Path: nodes/create
  • Method: POST
  • Request data
{
    type: “<template type of node generation(swagger or function node)>”, 
    data: {
        name: “<Node name which Node-RED user inputted on UI>”,
        version: “<Version which Node-RED user inputted on UI>”
    }
}
  • Content type: application/json

Once Node-RED runtime generate node internally, the endpoint returns random ID and module name of the generated node to identify.

  • Response data:
{
    id: <ID to identify generated node>
    module: <file name of tgz file>
}

Download endpoint

After generating node, Node-RED users can download the node via the following endpoint. Node-RED flow editor should set the ID of the generated node in the path of the endpoint.

  • Path: /nodes/download/:id (:id is ID of generated node)
  • Method: GET

When Node-RED editor accesses to the endpoint, it can download the generated node as tgz file.

Home directory design

To generate nodes in Node-RED runtime, home directory of Node-RED(default: ~/.node-red) is used. For the directory path, the directory which has random id will be created.

  • File path: ~/.node-red/nodegen/id

When creating node, 'node-red-contrib-' is used as the default prefix of the node module.