Define JavaScript Resource Types

You can get full control over returned JSON by defining JavaScript resource types. JavaScript resources are not explicitly bound to tables, such as other web services.
lac32
You can get full control over returned JSON by defining JavaScript resource types. JavaScript resources are not explicitly bound to tables, such as other web services.
In this article:
 
 
The following image shows the 
CustomerJSOrdersREST
 JavaScript resource that is included with the Demo API:
  CA Technologies  
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 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);
For more information about the Demo API sample, see Business Logic API Project Sample.
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
    . This object is 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 object to invoke REST services.
  •  
    req
    . This object 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 
CA Live API Creator
 invokes JavaScript logic, see the Quick Reference.
Usage
 
CA 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.
Use Local Services
If you are making calls to a local service (for instance, an API Creator 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.
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.
GET or POST to JavaScript Resources
You can GET or POST to JavaScript resources. If you use POST, you can access the payload using 
req.json
 in your JavaScript code. API Creator performs logic only on resources of type Normal that are mapped to underlying tables.
For more information about resource/object mapping, see Logic Execution.