Micro Gateway only

JavaScript (javascript)

Use the JavaScript policy to execute a specified JavaScript program.

Restriction: The JavaScript policy can be used only with the Micro Gateway.
Important: IBM® API Connect Micro Gateway is deprecated in IBM API Connect Version 5.0.8 in favor of DataPower® Gateway. From 1 April 2020, Micro Gateway, and associated toolkit CLI commands, will no longer be supported. Existing users can migrate their API definitions to IBM DataPower Gateways. For information on supported API policies, see Built-in policies.

About

You can attach this policy to the following API flows:
  • REST
  • SOAP
You can use the JavaScript policy to execute a snippet of JavaScript code, however, the policy has the following limitations:
  • The require() parameter is not available.
  • Global objects of node.js are not available.
  • The use of strict mode ("use strict") inside the JavaScript code is not supported. Therefore, block-scoped declarations cannot be used.

Properties

The following table lists the policy properties, indicates whether a property is required, specifies the valid and default values for input, and specifies the data type of the values.

Table 1. JavaScript policy properties
Property label Required Description Data type
Title Yes The title of the policy.

The default value is javascript.

string
Description No A description of the policy. string
Source Yes The JavaScript source code to execute. string

Examples

The following snippet of JavaScript code shows how the properties of a context object can be accessed or modified directly:
if (request.verb === 'POST') {
  //perform some business logic when the request is POST
}
The following example shows how to throw an error object that contains the error information, and changes the flow after:
if (request.body.order === undefined) {
  throw { name : 'IncorrectOrder', message: 'the payload should contain valid order' };
}
The following example shows how the error object from the previous example can be caught by a catch assembly:
catch:
  - errors:
      - 'IncorrectOrder'
    execute:
      - set-variable:
        actions:
          - set: 'message.body'
            value: '{ "error" : "found an incorrect order" }'

Errors

The following errors can be thrown while the policy is being executed:
  • JavaScriptError - a generic error that captures all errors that occur during the execution of the policy.
  • A custom error.