Skip to end of metadata
Go to start of metadata

This document describes a Codeblock flow-node and provides information on Codeblock flow-node configuration, metadata, and functionality.

NameDescription
CodeblockA 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)

PropertyDescriptionRequiredType
methodThe name of the Codeblock to execute.yesstring

The typical usage of code blocks in a flow involves setting parameters, mapping responses, and setting required configs as noted in the example below.

Configuration example

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

name

The name of the Codeblock

yes

string

description

The description of the Codeblock

yes

string

path

The relative path to the function to be invoked.

yes

string

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.

Icon

Most of the time errors should be returned in the callback as a standard response. Using the first parameter to return errors is the same as throwing an error in the Codeblock and should rarely need to be used.

Functionality example

 

1 Comment

  1. Configuration example

    Metadata example

    Those to example are a bit confusing and as the way they are separated it feels like they should be in two diff files.

    "block.0": {
     "type": "Codeblock",
     "config": {
       "method": "Greet"
     },

    also this part is not correct at least I could NOT make this work following the steps from this document

     

    COMPLETE / COMBINED (of Configuration and Metadata ) EXAMPLE
    {
        "schemaVersion": "1",
        "name": "Greet",
        "description": "Some codeblock to run with Greet flow",
        "path": "Greet.js",
        "parameter": {
            "type": "object",
            "properties": {
                "username": {
                    "type": "string"
                }
            },
            "required": [
                "username"
            ],
            "additionalProperties": false
        },
        "outputs": {
            "next": {
                "description": "The codeblock completed.",
                "context": "$.greeting",
                "schema": {
                    "$ref": "schema:///schema/foo/greeting"
                }
            },
            "error": {
                "description": "The codeblock failed to complete.",
                "context": "$.error",
                "schema": {
                    "$ref": "schema:///schema/foo/error"
                }
            }
        }
    }
    This is what works just fine I'd suggest to merge the two examples.