Docs‎ > ‎Logic Designer‎ > ‎REST Resources‎ > ‎

JavaScript Resources


You can create JavaScript Resource Types for full control over returned JSON as shown here.

In this example, we are retrieving Resource information from another Rest Server (line 2) as the data for the OrdersSvc Sub Resource.

This enables you to deliver JSON results combining managed data (here, Customers) with external service results (OrdersSvc).











The actual code:

var restCaller = new com.kahuna.logic.lib.rest.RestCaller(this);
var url = req.baseUrl;              // e.g. http://houston.d.espressologic.com/rest/abl/sample/ 
url = url.replace("sample","demo"); // compute baseURL of target system
url = url + "v1/purchaseorder";     // add version/endPoint
var key = containingRow.name;       // containingRow is system supplied
log.debug("..using key: " + key + ", and url: " + url);
url = url + "?filter=customer_name=%22" + key + "%22";
url = url.replace(/ /g, "%20");
var response = restCaller.get(url, null, {headers:{Authorization: "Espresso demo_full:123"}} );
return JSON.parse(response);

Output

The system inserts your returned JSON (OrdersSvc) into the larger result, as shown below:


Context

As shown here, your code has pre-supplied context for:
  • containingRow - you can access data from the containing row, here a Customer row from the CustOrdersSvc containing Resource (line 3).  This enables you to "parameterize" the external service using locally managed data.

  • log - for debugging (such information is visible in the log)

  • restCaller.get - you can use this to invoke REST services
  • req - this is the RestRequest object, which has a number of useful properties:
return {
    baseUrl: "" + req.baseUrl,  // The URL of this call up to the project
    fullBaseUrl: "" + req.getFullBaseURL(),
    accountName: "" + req.account.getName(),
    accountUrl: "" + req.account.getUrlName(),
    projectName: "" + req.project.getName(),
    projectUrl: "" + req.project.getUrlName(),
    apiName: "" + req.apiVersion.getName(),
    userIdentifier: "" + req.userIdentifier, // May be null
    params: JSON.parse("" + req.urlParameters), // Parameters from URL, e.g. ?abc=123&def=foo
    userProps: JSON.parse("" + req.userProperties), // User props from authentication
    verb: "" + req.verb, // GET, POST, PUT, DELETE, etc...
    clientAddress: "" + req.clientAddress  // The IP address of the caller
};

Usage

The system does not understand the internals of the external content, including its mapping to underlying data, its primary keys, its logic its security and so forth.   The system is therefore not able to supply advanced automation services such as pagination or update.

Using local services

If you are making calls to a local service (i.e. an Espresso Logic REST service on the same server), you should probably use SysUtility.getResource instead of making a full network call:

var custsJson = SysUtility.getResource("Customers", {filter: {"name like '%foo%' "});
var custs = JSON.parse(custsJson);
for (var i = 0; i < custs.length; i++) {
    log.debug("Customer: " + JSON.stringify(custs[i]));
}

This is more efficient, and it uses the same authentication as the original call. If you need to use different authentication, you will need to make a network call.

Please note that, depending on whether you are using a load balancer or not, a call to your usual REST URL may or may not work. You may have to make a call to localhost, port 8080, instead.