| --- |
| sidebar_position: 2 |
| --- |
| |
| |
|
|
| The **Script** tab is designed for placing JavaScript code that handles the node's programmatic logic. Here you define what exactly your node does - whether it processes text, calls APIs, transforms data, or performs calculations. |
|
|
|  |
|
|
| |
|
|
| All Piper nodes, including custom ones, follow a specific JavaScript structure: |
|
|
| ```javascript |
| export async function run({ inputs }) { |
| const { FatalError, NextNode } = DEFINITIONS; |
| |
| // Your code here |
| |
| return NextNode.from({ |
| outputs: { |
| // Your results |
| } |
| }); |
| } |
| ``` |
|
|
| This structure ensures that your node can properly receive data from previous nodes and pass results to the next ones in your pipeline. |
|
|
| |
| The basic **Input Text** node has this simple structure: |
|
|
| ```javascript |
| export async function run({ inputs }) { |
| const { FatalError, NextNode } = DEFINITIONS; |
|
|
| const text = inputs.input_text; |
|
|
| return NextNode.from({ |
| outputs: { |
| output_text: text |
| } |
| }); |
| } |
| ``` |
|
|
| |
| We need to replace this with logic that processes multiple string inputs and creates a JSON array: |
|
|
| ```javascript |
| export async function run({ inputs }) { |
| const { FatalError, NextNode } = DEFINITIONS; |
|
|
| const jsonArray = []; |
|
|
| if (inputs.string1) { |
| jsonArray.push(inputs.string1); |
| } |
| if (inputs.string2) { |
| jsonArray.push(inputs.string2); |
| } |
| if (inputs.string3) { |
| jsonArray.push(inputs.string3); |
| } |
| if (inputs.string4) { |
| jsonArray.push(inputs.string4); |
| } |
|
|
| return NextNode.from({ |
| outputs: { |
| json_output: jsonArray |
| } |
| }); |
| } |
| ``` |
|
|
| |
|
|
| The custom script implements the following logic: |
|
|
| 1. **Initialize empty array** - `const jsonArray = []` creates an empty array to store our text strings |
| 2. **Check each input** - `if` conditions check that each input has a value before adding |
| 3. **Build array** - Only non-empty inputs are added to `jsonArray` using `push()` |
| 4. **Return JSON output** - The final array is returned as `json_output`, matching our configuration in the Design tab |
|
|
| This approach ensures that: |
| - Empty or undefined inputs are ignored |
| - Only valid text strings are included in the final JSON array |
| - The output format matches the JSON type we defined in the Design tab |
|
|
| |
|
|
| Choose the appropriate execution type based on how much time your node requires to work: |
|
|
| | Type | Time | Description | |
| |------------|-------------|-----------------------| |
| | Rapid | 0-20 sec | Fast operations | |
| | Regular | 21-60 sec | Standard processing | |
| | Deferred | 60-120 sec | Complex operations | |
| | Protracted | 120-300 sec | Heavy processing tasks | |
|
|
| For our **Merge text to JSON** node, **Rapid** execution is suitable since we're only manipulating text strings and there are no API calls to external services. |
|
|
| |
|
|
| Always save changes to the script by clicking the **Save** button at the bottom of the interface. Any changes to the code should be saved by pressing the Save button at the bottom of the interface. |
|
|
| The Script tab is the core of your node's functionality, where you transform input data into output using JavaScript code. |