Script Modules

Script modules are used to define commonly used library functions. Modules can be called from pre work, post work like server environment, or form and validation rule like client environment.

Modules are always executed in an isolated environment, and do not automatically import any process related variable.

Define a Script Module

  1. Click "+" icon from "Script Modules"
  2. Specify the module name. Because the module name is used in a script environment, whitespace or other punctuation chars cannot be used for it.
  3. Modify the module content as a following script:
function privateSum(a,b) {
    return a+b;
}


function publicSum(a,b) {
	return privateSum(a,b);
}

exports.sum = publicSum;

Parameter Types

Module parameters must be serializable primitive types like string, date, number or object. Other complex types like xml node or functions cannot be used as parameters.

Exporting Module Functions

Modules may contain one or more functions to structure content and reusability, but not all functions are exported to the outside of a module. If you want to expose a function to the outside world, function must be added to "exports" object.

Calling Module Function

Module functions should be called in "ModuleName.FunctionName(arguments)" syntax. For the example above, that function is called as following:

var result = MyModule.sum(1,2);

Client Side Module Calls

Modules are always executed as synchronous context in server environment, but because of the browser limitations, client side modules can be renamed as "functionAsync" and return a javascript promise object to support asynchronous context. For example, the same module as the one above, can be called as following:

MyModule.sumAsync(1,2).then(function(result) {
    // result = 3
});

Library Modules

Module scripts can be shared with other processes if desired to build a common use library.

Sharing Module Script

Selecting "Is Public" flag from module options allows the publish module script to other processes. Public module contents are dynamically updated when module content is change.

Using Shared Module

To import a shared module;

  • Select the "Add Module" from designer
  • Select the shared module script from list

Module functions can be called same as private module functions.

Library Variables

Rest Service Library Variables

When module script is called from Rest Services in addition to standard variables following variables are automatically defined.

$ActiveUser
Specifies the authenticated user identity.

$GrantedScopes
Specifies the comma separated granted api scopes in string value.

$Headers
Specifies the http context request headers as object instance.

 Request Example
Http Request$Headers

POST /rest/v1/executeModule HTTP/1.1
Host: mydomain.emakin.com
Authorization: Bearer F8C......8B
X-My-Header: MyValue

{
     "apiKey": "49......72",
     "logonId": "myuser@emakin.com",
     "logonProvider": "Organization",
     "process" :"c6acc319-76cd-4265-abc1-d4ee2698dfbd",
     "module": "Module",
     "function" :"myFunction",
     "arguments" : []
}

$Headers defined as;

$Headers = {
  "host": ["mydomain.emakin.com"],
  "x-my-header": ["MyValue"],
  "remote_addr": ["67.3.2.1"],
  "remote_port": ["80"]
}

$Cookies
Specifies the http context request cookies as object instance.

 Cookies Example
Http Request$Cookie

POST /rest/v1/executeModule HTTP/1.1
Host: mydomain.emakin.com
Authorization: Bearer F8C......8B
Cookie: MyCookie=1; MyOtherCookie=2


{
     "apiKey": "49......72",
     "logonId": "myuser@emakin.com",
     "logonProvider": "Organization",
     "process" :"c6acc319-76cd-4265-abc1-d4ee2698dfbd",
     "module": "Module",
     "function" :"myFunction",
     "arguments" : []
}

$Cookie defined as;

$Cookie = {
  "mycookie": "1",
  "myothercookie": "2" 
}

Copyright © 2010 - 2023 Emakin. All rights reserved.