This document describes a Codeblock
flow-node and provides information on Codeblock
flow-node configuration, metadata, and functionality.
Name
|
Description
|
---|---|
Codeblock |
A flow-node that can be used for executing user code and returns the response. This flow-node allows custom business logic to be executed as part of the flow. |
Instance configuration (config)
Property
|
Description
|
Required
|
Type
|
---|---|---|---|
method |
The name of the Codeblock to execute. | yes | string |
The typical usage of code blocks in a flow involves setting parameters, mapping responses, and setting required configs as noted in the example below.
Metadata
Codeblock metadata should be included in the /codeblocks
directory of an API Builder Project. It is defined as a JSON file
with the following properties:
Property |
Description |
Required |
Type |
---|---|---|---|
|
The name of the Codeblock |
yes |
string |
|
The description of the Codeblock |
yes |
string |
|
The relative path to the function to be invoked. |
yes |
string |
Configuration and metadata example
Functionality
The functional part of a Codeblock should be a .js
file which exports a function of the following signature:
invoke(arrow, params, cb);
-
arrow <Arrow>
- The API Builder instance. -
params <Object>
- Key/value pairs of parameters passed to the flow-node instance. -
cb <Function>
- Callback.-
err
- Error. Passing this will cause the flow to cease processing and a 500 error to be returned from the endpoint which called it. -
response
- The data to be returned as the flow-node response.
-
Functionality example
function invoke(arrow, params, cb) { const salutation = arrow.config.helloworld.salutation; if (!params.username) { return cb( null , { error: 'Invalid name' }); } const body = salutation + ' ' + params.username; cb( null , body); } exports = module.exports = invoke |