Nodejs22
With this transform type, you can customize the request and response with access to Node.js v22 to program actions for each call to the transform endpoint in JavaScript.
We recommend this transform type if you have complex requirements involving multiple API calls. Some examples of how this could be used include:
Combine multiple Execute API calls together.
Apply JSONtransforms for multiple API calls.
Orchestrate other Spark APIs to facilitate data transformation and processing.
Make external API calls.
Develop tools that are not even Spark related.
For this particular transform type, the npm packages for fast-xml-parser and jsonata are available for enhanced JSON and XML parsing and composition.
Transform document
Transform documents are used to define the code relating to a transform. For Nodejs22, the transform document is a JSON document defined by the following objects:
transform_type
Nodejs22_v1.0.0
transform_code
Single string containing the code for the transform code.
The code must contain the function async function handler(event) {}.
When the transform is invoked, this function is called. Parameters from the transform request are passed into the event parameter as an object. This function should return:
http_status_codefor the HTTP status code.headersfor response headers.bodyfor the response body.
This design pattern follows the AWS Lambda Define Lambda function handler in Node.js.
transform_log_level
Optional parameter. This controls the level of logging that will be recorded during the execution.
This can take the values of
TRACE,DEBUG,INFO,WARN,ERROR.
transform_log_visible
NOTE: This configuration is not available yet.
Optional parameter.
This controls whether the execution logs will be made available in a link through the response headers. These can be used for reviewing the execution and debugging.
If omitted, the default is false.
transform_allowed_external_domains
Optional parameter. Array containing a list of external domains that the transform is allowed to access during the course of execution. These domains must be resolvable using NsLookup.
Create the transform document
The transform document can be manually created and uploaded to Spark using the New transforms feature.
The
transform_typeisNodejs22_v1.0.0.The
transform_codemust be a single string containing the code for the transform.If you are using either
fast-xml-parserorjsonata, make sure to include therequire()statements to import the modules the respective modules.Remove any lines containing comments.
Any double quotation marks
"need to be escaped where"is replaced with\".Note that JavaScript allows for single quotation marks
'to be interchanged with double quotation marks". If'are used exclusively in lieu of", then the escape character is not needed.
If you are using Visual Studio Code, you can use the Join lines feature to make it easy to combine multiple lines into a single string.
Select the transform text that you would like to combine onto a single line.
Access the Command Palette with the keyboard shortcut
F1orCtrl+Shift+Pon Windows orShift+⌘+Pon Mac.Type in the command Join lines and select it.
As an alternative to Join lines, line breaks
Ctrl+Entercan be replaced with the new line character\n.
Save the document as a
JSONfile. The filename must end with_transform.json.Follow the instructions in Folder overview to upload the transform document to Spark.
Sample transform document
Transforms API
Authorization
Bearer {token}accessible from Authorization - Bearer token or systematically via Client credentials.The request headers should include a key for
Authorizationwith the valueBearer {token}.
API key created from Authorization - API keys.
The request headers should include the keys
x-synthetic-keyandx-tenant-namewith the values of the API key and tenant name respectively.
Note that the authorization must include permissions that can access:
The requested Spark service.
The requested transform.
Either the folder that the transform is stored OR
If the transform is in the
apps/transformsfolder. This would typically requireuser:pforsupervisor:pf.
Path parameters
tenant *
Tenant is part of your sign-in URL and also available in the User menu.
transform_folder *
Folder associated with the transform document of the request.
transform *
Transform document associated with the transform document of the request (without _transform.json).
folder *
Folder associated with the Spark service of the request.
service *
Service associated with the Spark service of the request.
Request headers
x-meta or x-request-meta
Optional parameter.
This can be used to introduce key-value pairs into the event.context parameter object. See Examine the event parameter objectfor more details.
Example:
"{\"call_purpose\":\"Transform mainframe request\"}"
Content-Type
application/json, application/xml, or text/xml
+ any other request headers...
These will be passed into event.request.headers as key-value pairs. See Examine the event parameter objectfor more details.
Request body
Content-Type: application/json or Content-Type: application/xml
HTTP status code
This is determined by the handler() function return{} of http_status_code object.
This can also be defined with a response header x-spark-transform-response-httpstatuscode .
Response headers
This is determined by the handler() function return{} of the headers() object. We would suggest at the minimum to return the appropriate Content-Type for your response data.
Response body
This is determined by the handler() function return{} of the body() object.
Limitations
The API call has a maximum lifetime of
120 s.API calls can only be made to the
coherent.globaldomain unless defined bytransform_allowed_external_domains.
Known errors
Sometimes code will work locally but fail to run on Spark. Some examples where this may occur:
If you try to return all of the headers from the
/executeAPI call Emulate a passthrough to the Execute API (v3) to the/transformsresponse headers, it may fail due to content encoding discrepancies.
Examples
Start with a Hello, World! transform
The response includes a custom header and response body with the message Hello, World!.
The request is made to the transform
mytransformsfolder/HelloWorld.The
/runendpoint is called because this does not relate to a particular service.
Examine the event parameter object
When the handler() function is called, the event parameter is used to get information about the request details. These can be used as inputs for the program.
The request is made to the transform
mytransformsfolder/EventViewer.The
/runendpoint is called because this does not relate to a particular service.
Emulate a passthrough to the Execute API (v3)
The transform endpoint acts as a passthrough to send inputs to the Execute API (v3). USe this to build transforms that make API calls to Spark.
Note the
encodeURI()function that is used in thefetchResourcedeclaration. This is needed because folders or services that contain a space in the name need to be encoded to%20in order forfetch()to work.The
Authorizationto the/executeAPI call is handled by using the token available inevent.secrets.authorization.Trying to return all of the headers from the
/executecall may lead to errors. See Known errors.
The request is made to the transform
mytransformsfolder/EventViewerand the servicemyfoldername/AplusB.The endpoint includes a reference to
myfolder/myservicewhich will be the target for this API call.
Transform JSON with JSONata
JSONata is a "lightweight query and transformation language for JSON data". You can test your JSONata transforms in the JSONata Exerciser.
The jsonata package is included with the Nodejs22 transform to enable extensive JSON parsing capabilities. Note the jsonata package version used in Update roadmap.
Example is based on Example nodejs application.
The request is made to the transform
mytransformsfolder/JSONata.The
/runendpoint is called because this does not relate to a particular service.
Transform XML with fast-xml-parser
fast-xml-parser is the most popular package to manipulate XML in JavaScript.
The package is included with the Nodejs22 transform to enable XML parsing capabilities. Note the fast-xml-parser package version used in Update roadmap.
This example is based on Builder_v1.md.
The request is made to the transform
mytransformsfolder/XML.The
/runendpoint is called because this does not relate to a particular service.
Make an external API call
Extract the contents from the www.google.com homepage.
The request is made to the transform
mytransformsfolder/HeyGoogle.The
/runendpoint is called because this does not relate to a particular service.
Developer tools
JavaScript developers can use the Node Transform Document Builder which facilitates the build and validation of transform documents.
Alternatively, you can use this method to run code locally if you have Node.js installed. This process similar to Building Lambda functions with Node.js.
Decide how you would like to construct the request to the
/transformsendpoint, including the structure of the request line, as well as request and response headers and body.Create the transform described in Examine the event parameter object. Run the proposed
/transformsrequest against it and from the response copy theeventobject.Test your script locally using the
runHandler.jsscript below.Paste in the
eventobject from above intoTEST_EVENTofrunHandler.js.Work on the
handler()function inrunHandler.js.If your script calls other Spark APIs, your
Authorizationmay expire as you develop the program. You may need to update youreventobject periodically with a new bearer token.Test your script in your terminal using the command
node runHandler.js. It will print thehandler()functionreturn.
Compose and upload the transform document using the directions in Transform document.
Test API calls against the
/transformsendpoint to make sure you are getting the same result as therunHandler.jsscript.
Last updated
