Library scripts
To reuse an existing script, create a library script containing the functionality you want to reuse and reference it from a next-generation script.
A library script can take the format of any JavaScript code. You can also import functionality from another library script.
For example:
-
Create a library script using a minified third-party JavaScript utility library, such as
lodash.js.Only import scripts from trusted third parties that you know take security seriously. It is your responsibility to ensure that third-party code is secure and to maintain it.
-
Write your own reusable snippet that enhances Advanced Identity Cloud debugging functionality.
|
Modules that use file systems, such as |
Create a library script
-
In the Advanced Identity Cloud admin console, create a script of type
Library. -
In the JavaScript editor, paste the contents of a minified third-party JavaScript library or write your own code.
Expose the reusable functions of your library script by defining properties on the
exportsobject.For this example,
myExampleLibrarydefines and exports three functions:function add(i, j) { return i + j; } function logTotal(i) { logger.info("Total so far: " + i); } // export functions exports.add = add; exports.logTotal = logTotal; // export a constant exports.PI = 3.14; // direct export using an inline declaration exports.encodeURL = (url) => { return utils.base64url.encode(url); }For similar functionality to library scripts, refer to the CommonJS modules.
You can’t create or export classes in library scripts, only functions and constants.
As a next-generation script, a library script has access to all the next-generation common bindings. You can also pass in parameters.
Make sure you don’t use the same name for a local variable as that of a common binding in your script. These names are reserved for common bindings only.
If you have already defined a local variable with the same name as one that’s added to common bindings in a more recent version of Advanced Identity Cloud; for example,
utils, you must rename the variable in your scripts. -
Save your changes.
Import a library script
-
In the Advanced Identity Cloud admin console, create or edit a next-generation script.
Only next-generation scripts support the use of library scripts.
Alternatively, create or edit a
Libraryscript to nest library scripts. -
In the JavaScript editor, load the library using the
require(LIBRARY_SCRIPT)notation; for example:var mylib = require('myExampleLibrary'); -
Access the exported functions and constants using the library variable; in this case,
mylib:var i = mylib.add(10, mylib.PI); mylib.logTotal(i); var encoded = mylib.encodeURL("http://maths.example.com"); -
Save your changes.