ya, I guess if I were doing this I would write some JS to fixup that broken payload, using RegExp or other string matching logic. I’m sure it would be brittle and hard to maintain, though.
The development process will be iterative. If you follow the process of embedding the JS into a proxy as a JS callout, and then deploying the proxy, waiting for that to finish, then sending in a request… The Edit-Build-Test-Debug loop will be very tedious. You will need to wait 60s or more for deployment to succeed. Lots of steps. Not good. Ideally you would be able to run and test the JS you are building, outside the Apigee environment. Like, from your terminal.
As you know the Apigee JS callout relies on the Mozilla Rhino JS engine. It’s not v8, and it’s not node or deno. One implication of that is that you cannot include arbitrary npm modules, and you cannot rely on arrow functions or spread syntax, promises, and more things that have been added into EcmaScript over time. A second implication is that … you cannot use node to run/test your JS!
You need a way to run the JS, directly in the Rhino engine.
So, to aid in development of JS callouts for Apigee, when the JavaScript logic is not completely straightforward, such as in this case, it’s good to get a stand-alone Rhino environment, where you can run Rhino on your own workstation, from the command line. This allows you to write JS, then run it directly in Rhino, so the Edit-Build-Test-Debug loop is pretty efficient. For each change you make, You do not have to embed the JS into a proxy then deploy the proxy, wait for that to finish, then send in a request. You can do it all from a terminal.
Here’s what I did to set up my environment.
-
Download Mozilla Rhino from the github repo. There is a releases page. Download the zip from there. Per the Apigee documentation, Apigee X relies on v1.7.13 of Rhino, so you might want to download THAT release, as opposed to a later one. Not sure which version of Rhino is used by OPDK or Apigee Edge.
To use v1.7.13 from your terminal, you need to download rhino-1.7.13.jar and rhino-runtime-1.7.13.jar
-
you also need Java, probably Java8 or Java11. I haven’t tried running Rhino in later JVMs.
$ java -version
openjdk version "1.8.0_382"
OpenJDK Runtime Environment (build 1.8.0_382-bre_2023_09_05_21_07-b00)
OpenJDK 64-Bit Server VM (build 25.382-b00, mixed mode)
-
write your JS
-
run/test it like this:
$ java -cp rhino-runtime-1.7.13.jar:rhino-1.7.13.jar \
org.mozilla.javascript.tools.shell.Main -f ./my-source-file.js
You can use print() to see output. There is also debugger support in Rhino, but I don’t know how to use that.
This has been a huge time-saver for me, as I build logic in JS that runs in Rhino.
One tip
- I found that if I define a local variable named context, in my script… Rhino will choke. I forget the exact problem, but the script will fail to run. That was true in v1.7.11, not sure if still true in v1.7.13. Anyway, just avoid defining a variable of that name. This presents a minor challenge if you want to mock the message context object that is present in Apigee, in your script. In Apigee, that thing is available as variable context. To avoid this, I just use a different variable name for my mock - something like ctx. And then I have to replace names when I deploy the script into Apigee.