diff --git a/README.md b/README.md index 4fd00b6..0a43e22 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ JSONATA API Serverless Function ## Usage ### Example -Request +Request with `jsonata` as query parameter and data in POST body ``` curl --request POST 'http://localhost:7071/api/eval?jsonata={%22sum%22:%20$sum(Account.Order.Product.(Price%20*%20Quantity))}' \ --header 'Content-Type: application/json' \ @@ -98,3 +98,90 @@ Response "sum": 536.36 } ``` +Request with `jsonata` and `data` in POST body +``` +curl --location --request POST 'http://localhost:7071/api/eval' \ +--header 'Content-Type: application/json' \ +--data-raw '{ + "jsonata": "{'\''sum'\'': $sum(Account.Order.Product.(Price * Quantity))}", + "data": { + "Account": { + "Account Name": "Firefly", + "Order": [ + { + "OrderID": "order103", + "Product": [ + { + "Product Name": "Bowler Hat", + "ProductID": 858383, + "SKU": "0406654608", + "Description": { + "Colour": "Purple", + "Width": 300, + "Height": 200, + "Depth": 210, + "Weight": 0.75 + }, + "Price": 134.45, + "Quantity": 2 + }, + { + "Product Name": "Trilby hat", + "ProductID": 858236, + "SKU": "0406634348", + "Description": { + "Colour": "Orange", + "Width": 300, + "Height": 200, + "Depth": 210, + "Weight": 0.6 + }, + "Price": 21.67, + "Quantity": 1 + } + ] + }, + { + "OrderID": "order104", + "Product": [ + { + "Product Name": "Bowler Hat", + "ProductID": 858383, + "SKU": "040657863", + "Description": { + "Colour": "Purple", + "Width": 300, + "Height": 200, + "Depth": 210, + "Weight": 0.75 + }, + "Price": 34.45, + "Quantity": 4 + }, + { + "ProductID": 345664, + "SKU": "0406654603", + "Product Name": "Cloak", + "Description": { + "Colour": "Black", + "Width": 30, + "Height": 20, + "Depth": 210, + "Weight": 2 + }, + "Price": 107.99, + "Quantity": 1 + } + ] + } + ] + } + } +}' +``` +Response +``` +{ + "sum": 536.36 +} +``` \ No newline at end of file diff --git a/eval/index.js b/eval/index.js index 62ce0c2..66b5a3a 100644 --- a/eval/index.js +++ b/eval/index.js @@ -1,16 +1,19 @@ -const jsonata = require("jsonata"); - -module.exports = async function (context, req) { - - context.log('JavaScript JSONata function processed a request.'); - - const has_data = (req.query.jsonata && req.body); - const responseMessage = has_data - ? jsonata(req.query.jsonata).evaluate(req.body) - : "This JSONata function executed successfully. Pass a jsonata in the query string and JSON in the request body for an evaluated response."; - - context.res = { - // status: 200, /* Defaults to 200 */ - body: responseMessage - }; +const jsonata = require("jsonata"); + +module.exports = async function (context, req) { + + context.log('JavaScript JSONata function processed a request.'); + + const has_data = (req.query.jsonata && req.body); + const responseMessage = req.body ? has_data + ? jsonata(req.query.jsonata).evaluate(req.body) + : (req.body.jsonata && req.body.data) + ? jsonata(req.body.jsonata).evaluate(req.body.data) + : "This JSONata function executed successfully. Pass jsonata and data JSON for an evaluated response." + : "This JSONata function executed successfully. Pass in paramenters for an evaluated response." + + context.res = { + // status: 200, /* Defaults to 200 */ + body: responseMessage + }; } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 28285fe..03124fc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,13 +1,31 @@ { "name": "jsonata-fn-js", "version": "1.0.0", - "lockfileVersion": 1, + "lockfileVersion": 2, "requires": true, + "packages": { + "": { + "name": "jsonata-fn-js", + "version": "1.0.0", + "dependencies": { + "jsonata": "^1.8.6" + }, + "devDependencies": {} + }, + "node_modules/jsonata": { + "version": "1.8.6", + "resolved": "https://registry.npmjs.org/jsonata/-/jsonata-1.8.6.tgz", + "integrity": "sha512-ZH2TPYdNP2JecOl/HvrH47Xc+9imibEMQ4YqKy/F/FrM+2a6vfbGxeCX23dB9Fr6uvGwv+ghf1KxWB3iZk09wA==", + "engines": { + "node": ">= 8" + } + } + }, "dependencies": { "jsonata": { - "version": "1.8.5", - "resolved": "https://registry.npmjs.org/jsonata/-/jsonata-1.8.5.tgz", - "integrity": "sha512-ilDyTBkg6qhNoNVr8PUPzz5GYvRK+REKOM5MdOGzH2y6V4yvPRMegSvbZLpbTtI0QAgz09QM7drDhSHUlwp9pA==" + "version": "1.8.6", + "resolved": "https://registry.npmjs.org/jsonata/-/jsonata-1.8.6.tgz", + "integrity": "sha512-ZH2TPYdNP2JecOl/HvrH47Xc+9imibEMQ4YqKy/F/FrM+2a6vfbGxeCX23dB9Fr6uvGwv+ghf1KxWB3iZk09wA==" } } } diff --git a/package.json b/package.json index 9a46af6..aaaf993 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "test": "echo \"No tests yet...\"" }, "dependencies": { - "jsonata": "^1.8.5" + "jsonata": "^1.8.6" }, "devDependencies": {} }