From 4b918f5f709e83d69b99b359cc09b4257924147e Mon Sep 17 00:00:00 2001 From: hshaosf <11635576+hshaosf@users.noreply.github.com> Date: Thu, 16 Dec 2021 22:19:36 -0800 Subject: [PATCH 1/4] jsonata eval --- .vscode/settings.json | 2 +- eval/function.json | 19 +++++++++++++++++++ eval/index.js | 16 ++++++++++++++++ package-lock.json | 10 +++++++++- package.json | 4 +++- 5 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 eval/function.json create mode 100644 eval/index.js diff --git a/.vscode/settings.json b/.vscode/settings.json index 3f445e7..75f1834 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,7 +2,7 @@ "azureFunctions.deploySubpath": ".", "azureFunctions.postDeployTask": "npm install (functions)", "azureFunctions.projectLanguage": "JavaScript", - "azureFunctions.projectRuntime": "~3", + "azureFunctions.projectRuntime": "~4", "debug.internalConsoleOptions": "neverOpen", "azureFunctions.preDeployTask": "npm prune (functions)" } \ No newline at end of file diff --git a/eval/function.json b/eval/function.json new file mode 100644 index 0000000..7eb1f8f --- /dev/null +++ b/eval/function.json @@ -0,0 +1,19 @@ +{ + "bindings": [ + { + "authLevel": "anonymous", + "type": "httpTrigger", + "direction": "in", + "name": "req", + "methods": [ + "get", + "post" + ] + }, + { + "type": "http", + "direction": "out", + "name": "res" + } + ] +} diff --git a/eval/index.js b/eval/index.js new file mode 100644 index 0000000..62ce0c2 --- /dev/null +++ b/eval/index.js @@ -0,0 +1,16 @@ +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 + }; +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 38978ef..28285fe 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,13 @@ { "name": "jsonata-fn-js", "version": "1.0.0", - "lockfileVersion": 1 + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "jsonata": { + "version": "1.8.5", + "resolved": "https://registry.npmjs.org/jsonata/-/jsonata-1.8.5.tgz", + "integrity": "sha512-ilDyTBkg6qhNoNVr8PUPzz5GYvRK+REKOM5MdOGzH2y6V4yvPRMegSvbZLpbTtI0QAgz09QM7drDhSHUlwp9pA==" + } + } } diff --git a/package.json b/package.json index 88f90ba..9a46af6 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,8 @@ "start": "func start", "test": "echo \"No tests yet...\"" }, - "dependencies": {}, + "dependencies": { + "jsonata": "^1.8.5" + }, "devDependencies": {} } From e0fecc3b695d030cf19aafe5201e0ed530103742 Mon Sep 17 00:00:00 2001 From: hshaosf <11635576+hshaosf@users.noreply.github.com> Date: Wed, 8 Jun 2022 15:17:45 -0700 Subject: [PATCH 2/4] license and readme files --- LICENSE | 21 +++++++++++++++++++++ README.md | 0 2 files changed, 21 insertions(+) create mode 100644 LICENSE create mode 100644 README.md diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..30177cc --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 San Francisco Digital Services + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 From b2950617f7999f2e0e5cc77c1c518aca1ea3db69 Mon Sep 17 00:00:00 2001 From: hshaosf <11635576+hshaosf@users.noreply.github.com> Date: Wed, 8 Jun 2022 15:29:30 -0700 Subject: [PATCH 3/4] updated Readme --- README.md | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/README.md b/README.md index e69de29..4fd00b6 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,100 @@ +# jsonata-fn-js +JSONATA API Serverless Function + +## About JSONata +[JSONata](https://jsonata.org/) is query and transformation language. +[[try](http://try.jsonata.org/)] [[documentation](http://docs.jsonata.org/)] +* Lightweight query and transformation language for JSON data +* Inspired by the location path semantics of XPath 3.1 +* Sophisticated query expressions with minimal syntax +* Built in operators and functions for manipulating and combining data +* Create user-defined functions +* Format query results into any JSON output structure + +## Usage + +### Example +Request +``` +curl --request POST 'http://localhost:7071/api/eval?jsonata={%22sum%22:%20$sum(Account.Order.Product.(Price%20*%20Quantity))}' \ +--header 'Content-Type: application/json' \ +--data-raw '{ + "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 +} +``` From b6542ab5bf8efc76bb89c62cb6bbb085fee0cca2 Mon Sep 17 00:00:00 2001 From: hshaosf <11635576+hshaosf@users.noreply.github.com> Date: Wed, 8 Jun 2022 15:29:44 -0700 Subject: [PATCH 4/4] github action --- .github/workflows/azure.yml | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 .github/workflows/azure.yml diff --git a/.github/workflows/azure.yml b/.github/workflows/azure.yml new file mode 100644 index 0000000..e00f29a --- /dev/null +++ b/.github/workflows/azure.yml @@ -0,0 +1,50 @@ +name: Deploy Node.js project to Azure Function App + +on: + [push] + +# CONFIGURATION +# For help, go to https://github.com/Azure/Actions +# +# 1. Set up the following secrets in your repository: +# AZURE_FUNCTIONAPP_PUBLISH_PROFILE +# +# 2. Change these variables for your configuration: +env: + AZURE_FUNCTIONAPP_NAME: jsonata-fn-js # set this to your application's name + AZURE_FUNCTIONAPP_SLOT_NAME: staging # This is the slot name to be deployed to + AZURE_FUNCTIONAPP_PACKAGE_PATH: '.' # set this to the path to your web app project, defaults to the repository root + NODE_VERSION: '10.x' # set this to the node version to use (supports 8.x, 10.x, 12.x) + +jobs: + build-and-deploy: + runs-on: ubuntu-latest + environment: dev + steps: + - name: 'Checkout GitHub Action' + uses: actions/checkout@master + + - name: Setup Node ${{ env.NODE_VERSION }} Environment + uses: actions/setup-node@v1 + with: + node-version: ${{ env.NODE_VERSION }} + + - name: 'Resolve Project Dependencies Using Npm' + shell: bash + run: | + pushd './${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }}' + npm install + npm run build --if-present + npm run test --if-present + popd + + - name: 'Run Azure Functions Action' + uses: Azure/functions-action@v1 + id: fa + with: + app-name: ${{ env.AZURE_FUNCTIONAPP_NAME }} + slot-name: ${{ env.AZURE_FUNCTIONAPP_SLOT_NAME }} + package: ${{ env.AZURE_FUNCTIONAPP_PACKAGE_PATH }} + publish-profile: ${{ secrets.AZURE_FUNCTIONAPP_PUBLISH_PROFILE }} + +# For more samples to get started with GitHub Action workflows to deploy to Azure, refer to https://github.com/Azure/actions-workflow-samples \ No newline at end of file