Custom scripts

2 min read

You can use TypeScript for added flexibility in both the mapping dialog and custom Script blocks in flows. Both methods allow you to use the same function library.

Scripts in mapping

You can input scripts directly in the mapping dialog using the code fields on the bottom-left. This is especially handy for simple type conversions, string concatenations, or quick calculations. Scripts entered here can contain multiple lines, but must be a single expression, for example:

input.a + input.b

You cannot write multiple commands directly. Imagine that this code is substituted in const output = (...);. Then you see that code like const output = (const a = 3;a +b); is invalid. However, you can wrap your code in a function call to create a single expression:

({
  const v = input.a + input.b;
  return v;
})();

However, the script block is more suited for writing complex scripts.

Script blocks

After adding a script block to a flow, you can click on its configure button and choose "Edit script". It will lead you to a separate page with a code editor and test capabilities.

The script entered here is a complete function call. The empty script looks like this:

export async function script1({}: {
  // Input definitions.
}): Promise<{
  // Output definitions.
}> {
  // Your code here.
  return {};
}

This function has a single input parameter which is always an object. Its output must also be an object and both are defined using TypeScript. Also note that it an async function, which means we can use await in the function body. A full example of a function is:

export async function script1({a, b}: {
  a: number;
  b: number;
}): Promise<{
  c: number;
}> {
  const c = Math.sqrt(a ** 2 + b ** 2);
  return {c};
}

Flowlet will automatically update the block's input and output definitions in the flow based on the types defined in the script.