Skip to content

Commit

Permalink
Merge pull request #2 from SFDigitalServices/eval_body
Browse files Browse the repository at this point in the history
EPR-466 take jsonata and data in POST body
  • Loading branch information
hshaosf authored Feb 11, 2023
2 parents 0e69273 + d68e854 commit 9f88283
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 21 deletions.
89 changes: 88 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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' \
Expand Down Expand Up @@ -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
}
```
33 changes: 18 additions & 15 deletions eval/index.js
Original file line number Diff line number Diff line change
@@ -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
};
}
26 changes: 22 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"test": "echo \"No tests yet...\""
},
"dependencies": {
"jsonata": "^1.8.5"
"jsonata": "^1.8.6"
},
"devDependencies": {}
}

0 comments on commit 9f88283

Please sign in to comment.