Logic Libraries
JavaScript invocations typically provide the row and logic context for your programmatic access. When you write rules in API Creator using JavaScript, you will often benefit from using libraries. Libraries are JavaScript or Java files of re-usable solutions for patterns. For example, libraries can include functions, such as date routines.
lac42
JavaScript invocations typically provide the row and logic context for your programmatic access. When you write rules in API Creator using JavaScript, you will often benefit from using libraries. Libraries are JavaScript or Java files of re-usable solutions for patterns. For example, libraries can include functions, such as date routines.
In this article:
Add JavaScript Libraries to APIs
You can add only JavaScript libraries to your API using API Creator. After you have added a library to your API, you can reference its facilities in your rules by invoking classes and methods directly from JavaScript.
Best Practice:
There is cost in CPU and memory associated with each library you make available. Select only those libraries your API references.Follow these steps:
- With your API open, in the Create section, clickAPI Properties.The Details tab appears by default.
- Click theLibrariestab.The Your libraries page appears by default. This page displays a list of your logic libraries.
- ClickCreate New Library.The Logic library window opens.
- Complete the following fields and then save your changes:NameEnter a name for your library.CodeThe Logic library window closes.Find your JavaScript library by clickingChoose File, finding and selecting your JavaScript library, and then clickingOpen.
- Define the library as available to JavaScript event programming and used in JavaScript logic by selecting theUsedcheckbox and then save your changes.TheUsedcheckbox must be selected so that you can reference the functions that are defined within the library.
The JavaScript library is added to your API.
Add Java Libraries
You can add a Java library to your API by instantiating Java objects inside JavaScript. You can use Java JAR files in your APIs. Before you start API Server, copy your Java JAR files into the following directory:
- (The self-contained, single user version ofLayer7 Live API Creatorbased on Jetty) Thecaliveapicreator/lib/extdirectory.
- (Tomcat) Thetomcat/apache-tomcat-<version>/libdirectory.
If you have installed
Layer7 Live API Creator
as a Docker container, see the information about how to load libraries at container startup in Install as a Docker Container.For more information:
- About how to start API Server, see:
- (The self-contained, single user version ofLayer7 Live API Creatorbased on Jetty) Install the Single-User Demonstration Package.
- (Tomcat) Install on Apache Tomcat.
- About how to add a Java library to your API by instantiating Java objects inside JavaScript, see JavaScript.
Define the Available Libraries
To use a library, you must define the library as available to JavaScript event programming and used in JavaScript logic. For example, many APIs use date arithmetic. The
Moment.js
library is a JavaScript date library for parsing, validating, manipulating, and formatting dates. You can make this library available inside your API.Best Practice:
There is cost in CPU and memory associated with each library you make available. Define only those libraries your API references as available.System libraries are the standard set of logic libraries that API Creator includes. Your APIs share the system libraries. Your libraries are available on the Your libraries page.
Do
one
of the following:- On the Your libraries page, select theUsedcheckbox for the library you want to define as available and then save your changes.
- To define a system library as available, complete the following:
- Click theSystem librariestab.The System libraries page appears, displaying the JavaScript libraries for your API.The first several libraries that are listed are selected and available by default. You cannot clear theUsedcheckbox for these libraries.
- Select theUsedcheckbox for the library you want to define as available and then save your changes.
The library is defined as available.
Export JavaScript Libraries
You can export your JavaScript libraries so that you can import them and can reference them in other APIs.
Follow these steps:
- On the Your Libraries page, clickEditfor the library you want to export.The code editor expands to reveal more options.
- ClickExport Code.
- Save your changes.
The JavaScript library is exported as a JavaScript text file.
Reload Updated Libraries
If you update your library code, reload the updated library into your API.
Follow these steps:
- With your API open, in the Create section, clickAPI Properties.The Details tab displays by default.
- Click theLibrariestab.The Your libraries page appears by default.
- ClickChangefor the library that you have updated.The Logic library dialog opens.
- In the Code field, clickChoose File, select the updated JavaScript file, and then save your changes.
The updated library is reloaded into your API.
Call Libraries from Event Rules: Commit Event Example
The following code snippet shows a commit event rule in the Business to Business (B2B) sample. This commit event rule is on the
Orders
object for the INSERT operation:var salesRep = row.EmployeeID; // object model (row) provides accessors to related objectsif (salesRep !== null) { var mail = B2B.sendEmail(); // this is a loadable library var config = { to: '[email protected]', // better, find this from DB from: '[email protected]acme.com', title: 'Congratulations on your new Order', text: 'An order was placed for you, totaling ' + row.AmountTotal }; mail.configure(config); var msg = mail.send(); logicContext.logDebug('Sending Mail - msg: ' + msg);}
You can initialize your library from a commit even rule. For more information about how to call reusable solutions from rules, see B2B API Sample.
Load Libraries: moment.js Library Example
In this example, you have an Orders table with the
order_date
and shipping_date
date attributes. You would like the processing_delay
computed attribute to show the time that is elapsed between these two dates. You can use the moment.js
library–a JavaScript date library for parsing, validating, manipulating, and formatting dates–or use Java objects.The following example uses the
moment.js
library:var orderDate = currentBean.order_date.getTime();var shippingDate = currentBean.shipping_date.getTime();currentBean.processing_delay = moment(orderData).from(shippingData, true);
For more information about the
moment.js
library, see the moment.js site.Use Library Facilities in your Rules
Prerequisite:
You have added a library to your API.You can use library facilities in your rules by invoking methods inside classes from your JavaScript.
Example:
The following
myClass
Java class contains the add()
method:public class MyClass { public static int add(int a, int b) { return a+b; }}
You can invoke the
add()
method by adding the following code snippet to your JavaScript with the Java.type
keyword:var MyClassObject = Java.type("MyClass");var myResult = MyClassObject.add(1,2);
If your Java class is part of a package, for example
foo.bar.MyClass
, then provide the full name of the class while accessing the Class
object.For more information about using Java inside JavaScript, see JavaScript.
Logic Libraries Best Practices
Define libraries as available to JavaScript event programming and used in JavaScript logic only for those that your API uses. There is CPU cost and memory associated with each library you mark as
Used
.