Skip to content

Commit

Permalink
Merge pull request #1 from SFDigitalServices/jsonata
Browse files Browse the repository at this point in the history
JSONata API
  • Loading branch information
hshaosf authored Oct 19, 2022
2 parents bcb8ad9 + b6542ab commit 0e69273
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 3 deletions.
50 changes: 50 additions & 0 deletions .github/workflows/azure.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
100 changes: 100 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
}
```
19 changes: 19 additions & 0 deletions eval/function.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"bindings": [
{
"authLevel": "anonymous",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
16 changes: 16 additions & 0 deletions eval/index.js
Original file line number Diff line number Diff line change
@@ -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
};
}
10 changes: 9 additions & 1 deletion package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"start": "func start",
"test": "echo \"No tests yet...\""
},
"dependencies": {},
"dependencies": {
"jsonata": "^1.8.5"
},
"devDependencies": {}
}

0 comments on commit 0e69273

Please sign in to comment.