Skyone
Skyone
English
English
  • Home
  • Data
    • Getting Started
      • Create an account
      • Recover Password
      • Quick Platform Guide
      • How to test the platform for free
      • Workspace
        • Creating a new Workspace
        • Find a Workspace
        • Sending an invitation to a Workspace
        • Editing a Workspace
      • Organizations
        • Creating an Organization
        • Organization Overview
        • Organization Management
        • Organization Monitoring
      • Settings and Preferences
        • Profile
        • Notifications
        • Usage and Billing
        • Users and Permissions
    • Modules
      • Module management
        • Creating a Module
        • Importing a Module
          • IAC Files - Integration as Code
        • Editing a module
        • Module Options
      • Settings and Operations
        • Module settings
          • Connectivity: Database
          • Connectivity: Email
          • Connectivity: REST
          • Connectivity: SOAP
          • Connectivity: File
          • Connectivity: RFC
          • Connected Account Management
        • Operations
          • Importing operations into REST Modules
          • Operation Management
        • Flows Using This Module
    • Monitoring
    • API Gateway
    • Terminals & Agent
      • Agent
        • Versions supported by Agent
        • How to Update the Agent Version
        • How to back up Agent files
      • Terminals
    • Data
      • Data Stack
        • Process Control
        • Data Stack Upload
        • File Actions
        • File Jobs
        • Data Job Parameters
        • Data Store
        • Data Share Features
        • ODBC
        • How to use the Data Engine Proxy
    • Integrations
      • Integration Management
        • Create integration
        • Import Integration
        • Edit Integration
        • Integration Options
        • Flows of this integration
      • Flows
        • Flow management
          • Creating a flow
          • Flow options
          • Flow Canva: configuring and editing the flow
            • Flow Canva: overview
            • Exception Handler
              • Exception Handler - Configuration
              • Exception Handler - Cases
            • Multicontext Flows
              • Example: Multicontext with an API Gateway
              • Example: Multicontext with a Time Trigger
            • Flow Settings
        • Triggers of a flow
          • API Gateway Triggers: Adding and Setting
          • AS2 Triggers: Adding and Setting
          • Queue Triggers: Adding and Setting
          • Flow Triggers: Adding and Setting
          • Time Triggers: Adding and Setting
          • Webhook Triggers: Adding and Setting
        • Tool Modules
          • AS2 Module
          • CSV Module
          • Data Transform Module
          • Data Balancer Module
          • EDI Module
          • Flow Call Module
          • IF Module
          • JavaScript Module
          • Log Module
          • Loop Do While Module
          • Loop For Module
          • Return Module
          • XML Module
          • Other Tool Modules
        • Module Header
        • Connecting components of a flow
        • Editing triggers and modules
        • Data Operations
          • Object Handling
            • Practical example: Handling variables
          • SMOP (Small Operations)
          • Parameterization rules
    • How to
      • Insert JSON into databases
      • Flattening: Data transformation using JSONata
      • How to use Form Data
      • Understanding recursion in JSONata
      • REST Module Output Consolidation
      • Isolated in execution: concept and application in variables
      • URL Parameters in API Gateway
      • Use case: API Gateway trigger parameters
      • Use case: Exception Handler in financial transactions
      • Use case: using Groups to manage access to flows
      • How to create a download endpoint and integrate with Power BI
      • Is it possible to use two triggers in a single flow?
      • How to set up WhatsApp in Skyone Studio
    • FAQ
    • GIGS: The complete guide
    • Glossary
  • Support
    • How do I request support?
    • Case Severity Levels
    • SLAs
    • Help & Resources
Powered by GitBook
On this page
  • Definition
  • Configuration of the JavaScript module
  • Use examples
  1. Data
  2. Integrations
  3. Flows
  4. Tool Modules

JavaScript Module

PreviousIF ModuleNextLog Module

Last updated 4 months ago

Definition

The "JavaScript" module, the platform's standard module for use in any flow, allows you to run JavaScript code safely within a controlled environment. This module is ideal for performing data transformations, custom calculations, and other JavaScript-specific programmatic operations.

Check below for what is available for use, what is restricted, and practical examples of how to use this feature.

✅ Usage possibilities

  • Standard JavaScript code execution: You can use all the standard JavaScript functionalities, including variable declarations, loops, conditionals, object manipulation and arrays.

  • Asynchronous functions and Promises: Support for async/await and Promises, allowing asynchronous operations such as simulated data requests or operations that rely on timers.

  • Timers:

    • setTimeout: Available to schedule asynchronous operations after a specified delay.

    • clearTimeout: Available to cancel timers created with setTimeout.

  • String and number manipulation: Use of global functions such as parseInt, parseFloat, Number, String, and others to manipulate numbers and strings.

  • Native JavaScript functions: All native JavaScript functions, such as Math, Date, JSON, Array, Object, and RegExp, are available.

  • Memory: You can use up to 60MB of memory to execute the code.

🚫 Usage restrictions

  • Module Import: It is not possible to import modules using require or import. This includes both native Node.js modules and third-party libraries, ensuring a secure and isolated environment.

  • System Access: There is no access to the file system, network, or other system operations that could compromise the security of the platform.

  • Process Handling: Functions for manipulating processes, such as process.exit() and process.kill(), are not available.

  • Restricted Keywords: Certain keywords and operations, such as eval and Function, are restricted to prevent the execution of code that could escape the secure environment.

  • Access to Global Variables: The code executed is isolated from the global context, with no access to global variables external to the sandbox.

Configuration of the JavaScript module

The "JavaScript" module appears in the Tools option when editing a flow. As soon as you click to add it, the modal opens for configuration:

Variable Configuration

Fill in the following fields:

  • Key: variable identifier

  • Value: type or drag a variable

  • Type: choose the most appropriate data type. Options are: text, number, boolean, object, and array.

After completing the configurations, click "Next".

Result

On this screen, you can execute and view the data transformation information and manipulate it with JavaScript.

Parameters

All parameters are passed within the "data" field. In practice, this means that all parameters are within a "data" object, which can be accessed by the JavaScript code.

Structure and Content of data

  • Data Type: "data" is a JSON object. This means it can contain key-value pairs, where the keys are strings and the values can be data types such as strings, numbers, arrays, objects, booleans, or null.

  • Custom Fields: The content of data is completely customizable and can contain any information the user’s code needs to function. There are no specific restrictions on the structure of the data, other than being JSON serializable.

Examples of Fields:

  • Numbers and Strings: Numeric or text values that can be used for calculations or manipulations.

  • Arrays and Objects: More complex data structures that can contain lists of items or other nested objects.

  • Configuration Data: Specific configuration information that determines how the code should behave.

Use examples

Example 1: Synchronous Code with Two Parameters:

Imagine a scenario where we have two parameters. In this example, we will call them "number1" and "number2". We input the data and use the following code in JavaScript:

function addNumbers(data) {
  return data.number1 + data.number2;
}
return addNumbers(data);

Next, we click "Run." The result will be the sum of the parameters, as shown in the image below:

Example 2: Asynchronous Code with Promises:

In this example, we will work with an asynchronous scenario using Promises in JavaScript.

Imagine we want to process the data after a certain time. We will use two parameters, which are inside "data," and the function will return a Promise.

The code below simulates an asynchronous operation using setTimeout to wait for one second before resolving the Promise with a message and the provided data, after clicking "Run":

function fetchData(data) {
return new Promise((resolve) => {
setTimeout(() => {
resolve({ message: 'Data processed successfully', data });
}, 1000);
});
}

return fetchData(data);

The result can be seen in the image below:

Example 3: Creating a token using the signJwtToken function

We will create a JWT token using the JavaScript module in this example.

To do this, we will use the signJwtToken function provided by the platform within the module context. This function accepts three parameters:

  • payload: This field is required and must be an object. Its value corresponds to the token body.

  • secret: This field is required and must be a string. Its value is the secret key used to generate and validate the token.

The signJwtToken function takes a payload (data we want to include in the token) and a secret key to sign the token.

Usage example

In the input data, you can configure a parameter or variable if needed. For instance:

Example of input data:

{
  "data": {
    "payload": {
      "id": 1,
      "first_name": "Kristos",
      "last_name": "Amiranda",
      "email": "kamiranda0@hugedomains.com",
      "gender": "Male",
      "ip_address": "183.129.112.78"
    },
    "secret": "687398ee-8c99-48fe-93b5-5ade50d6e22f"
  }
}

We insert the code below into the JavaScript Code area:

const {payload, secret} = data;

return signJwtToken(payload, secret, {expiresIn: '1d'})

After execution, the result can be seen in the image below:

In case of an error, the response will be different, as shown below:

{
	"error": "simulate error",
	"success": "false"
}

Example 4: Decoding a token using the decodeJwtToken function

In this example, we will decode a JWT token to verify and access the data stored within it.

We will use a function provided by the module called decodeJwtToken. This function requires two parameters:

  • token: This field is required and must be a string. It represents the token to be decoded and verified.

  • secret: This field is required and must be a string. It represents the secret key used to sign the token.

Usage example

For the input, we used the following data:

{
  "data": {
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiZmlyc3RfbmFtZSI6IktyaXN0b3MiLCJsYXN0X25hbWUiOiJBbWlyYW5kYSIsImVtYWlsIjoia2FtaXJhbmRhMEBodWdlZG9tYWlucy5jb20iLCJnZW5kZXIiOiJNYWxlIiwiaXBfYWRkcmVzcyI6IjE4My4xMjkuMTEyLjc4IiwiaWF0IjoxNzM3NjQwNzk1LCJleHAiOjE3Mzc3MjcxOTV9.Isi8OlBpc-yUa5EsJa9IOA8wgmAFivgrn_pRClssVGE",
    "secret": "687398ee-8c99-48fe-93b5-5ade50d6e22f"
  }
}

We inserted the code below into the JavaScript Code area:

const {token, secret} = data;

return decodeJwtToken(token, secret)

After execution, the result can be seen in the image below:

In case of an error, the response will be different, as shown below:

{
    "error": "simulate error",
    "success": "false"
}

Best Practices

  • Input Validation: Always validate input data to ensure correct code execution and avoid unexpected errors.

  • Performance: Avoid infinite loops or deep recursions that can cause excessive memory usage.

.

To finalize the module configuration, click "Save".

options: This field is optional and must be an object. For more details about its configuration, refer to the JavaScript library documentation: .

Considering that the parameters payload and secret are required, additional options can be included. In this example, we used expiresIn. To view other available options, .

Definition
Usage possibilities
Usage restrictions
Module configuration
Variable configuration
Result
Use examples
Synchronous Code with Two Parameters
Asynchronous Code with Promises
Creating a token using the signJwtToken function
Decoding a token using the decodeJwtToken function
Best Practices
Find out about the Module Header here
jsonwebtoken
refer to the documentation
Configuration example
"Result" screen