REST-EZ supports three ways of writing custom JS:
These types have access to different Context
REST-EZ utilizes the following special yaml types to enable the use of JS functions:
!!js/function
an syncronous JS function!js/asyncFunction
an asynchronous JS functionBasic usage:
1link# basic function (synchronous)
2linkrun_type: inline
3linkinline:
4link function: !!js/function >
5link function () {
6link // ...
7link }
Use the arguments
property to pass an array of arguments:
1link# basic function (synchronous)
2linkrun_type: inline
3linkinline:
4link arguments:
5link - Kiran Mandadi
6link function: !!js/function >
7link function (name) {
8link console.log(`Hello, ${name}.`);
9link }
10link
Basic usage:
1link# async function
2linkrun_type: inline
3linkinline:
4link function: !js/asyncFunction >
5link async function () {
6link // return new Promise((resolve, reject) => { ... });
7link // -- or --
8link // return await doSomething();
9link }
Use the arguments
property to pass an array of arguments:
1link# async function
2linkrun_type: inline
3linkinline:
4link arguments:
5link - name: Matthew
6link dob: 1984-03-02
7link function: !js/asyncFunction >
8link async function (options) {
9link // return await doSomething(options.name, options.dob);
10link // -- or --
11link // return new Promise((resolve, reject) => { ... });
12link }
⚠️ NOTE: using require or import inside inline function is not yet supported. Use the JS modules approach below:
You can run code from any external file:
1linkrun_type: module
2linkmodule:
3link module_path: 'path/to/a/file.js'
4link function_name: nameOfSomeFunction
1linkmodule.exports = {
2link someFunction: function () {
3link // do something
4link }
5link};
Use the arguments
property to pass an array of arguments:
1linkrun_type: module
2linkmodule:
3link arguments:
4link - Argument one is a string.
5link - { name: 'Person', age: 72 }
6link - [ 'argument', 3, 'is', an', 'array' ]
7link module_path: 'path/to/a/file.js'
8link function_name: nameOfSomeFunction
1linkmodule.exports = {
2link someFunction: function (arg1, arg2) {
3link // do something
4link }
5link};