Define JavaScript Resource Types


You can get full control over returned JSON by defining JavaScript resource types. This enables you to create resources that are not explicitly bound to tables, such as other web services.

The following image shows a JavaScript resource type on the Create, Resources, Details tab:

This example illustrates how you can deliver JSON results combining managed data (Customers) with external service results (OrdersSvc) by retrieving resource information from another Rest server (line 2) as the data for the OrdersSvc sub-resource. 

The following code snippet shows the actual code:

var url = req.baseUrl;// e.g. http://server.acme.com/rest/abl/sample/
url = url.replace("sample","demo");// compute baseURL of target system
url = url + "v1/PurchaseOrders";// add version/endPoint
var key = containingRow.name;// containingRow is system supplied
log.debug("..using key: " + key + ", and url: " + url);
key = key.replace(/'/g, "''");
var params= {
   filter: "customer_name='" + key + "'"
};
//special apikey in this project to allow access from url (?auth=demo_full:1)
var settings = {
   headers: {
      Authorization: "CALiveAPICreator demo_full:1"
   }
}
//////////// Built in utility to make REST GET call
var response =  SysUtility.restGet(url, params, settings);
return JSON.parse(response);

Output

Your returned JSON (OrdersRest) is inserted into the larger result, as shown in the following code snippet:

{
    "name": "Alpha and Sons",
    "balance": 4484,
    "@metadata": {
      "href": "http://localhost:8080/APIServer/rest/el-local/demo/v1/CustomerJSOrdersREST/Alpha%20and%20Sons",
      "checksum": "A:e86aea2e0a4e74bf"
    },
    "OrdersRest": [
      {
        "order_number": 1,
        "amount_total": 1079,
        "paid": false,
        "notes": "This is a small order",
        "customer_name": "Alpha and Sons",
        "salesrep_id": 2,
        "@metadata": {
          "href": "http://localhost:8080/APIServer/rest/el-local/demo/v1/PurchaseOrders/1",
          "checksum": "A:5c157e88a42aa592"
        },
        "LineItems": [
          {
            "lineitem_id": 12,
            "product_number": 2,
            "order_number": 1,

Context

As shown in this example, 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). You can "parameterize" the external service using locally managed data using containingRow.
  • log. For debugging. You can view debugging information in the log.
    For more information about the log, see View Logging Information.
  • restCaller.get. You can use this to invoke REST services.
  • req. This is the request object, which has the following 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
};

For more information about the context variables that are set when Live API Creator invokes JavaScript logic, see the Quick Reference.

Usage

Live API Creator does not understand the internals of the external content, including its mapping to underlying data, its primary keys, its logic, and its security. It cannot supply advanced automation services such as pagination or update.

Using Local Services

If you are making calls to a local service (for instance, an APICreator REST service on the same server), use SysUtility.getResource instead of making a full network call. For example:

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, make a network call.

Note: 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.

Supports Get Only

JavaScript resources provide support for Get, not Put or Post. This is because the normal resource/object mapping logic cannot be performed, since Live API Creator has no knowledge of the resource/table mapping.

For more information about resource/object mapping, see Logic Execution.