From 74297b6e131ccb734f634330eef23e3dfad0920f Mon Sep 17 00:00:00 2001 From: Martin Patino Date: Fri, 9 Feb 2024 17:22:31 -0700 Subject: [PATCH] chore: udpate based on jon changes --- package-lock.json | 27 +++++++++----- package.json | 3 +- src/core/config/Categories.json | 1 + src/core/operations/Jsonata.mjs | 65 +++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 10 deletions(-) create mode 100644 src/core/operations/Jsonata.mjs diff --git a/package-lock.json b/package-lock.json index 110c94a1..0c6c1ab1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -50,6 +50,7 @@ "js-sha3": "^0.9.3", "jsesc": "^3.0.2", "json5": "^2.2.3", + "jsonata": "^2.0.3", "jsonpath-plus": "^8.0.0", "jsonwebtoken": "8.5.1", "jsqr": "^1.4.0", @@ -114,7 +115,7 @@ "babel-plugin-dynamic-import-node": "^2.3.3", "babel-plugin-transform-builtin-extend": "1.1.2", "base64-loader": "^1.0.0", - "chromedriver": "^121.0.0", + "chromedriver": "^114.0.2", "cli-progress": "^3.12.0", "colors": "^1.4.0", "copy-webpack-plugin": "^12.0.2", @@ -4896,25 +4897,25 @@ } }, "node_modules/chromedriver": { - "version": "121.0.0", - "resolved": "https://registry.npmjs.org/chromedriver/-/chromedriver-121.0.0.tgz", - "integrity": "sha512-ZIKEdZrQAfuzT/RRofjl8/EZR99ghbdBXNTOcgJMKGP6N/UL6lHUX4n6ONWBV18pDvDFfQJ0x58h5AdOaXIOMw==", + "version": "114.0.3", + "resolved": "https://registry.npmjs.org/chromedriver/-/chromedriver-114.0.3.tgz", + "integrity": "sha512-Qy5kqsAUrCDwpovM5pIWFkb3X3IgJLoorigwFEDgC1boL094svny3N7yw06marJHAuyX4CE/hhd25RarIcKvKg==", "dev": true, "hasInstallScript": true, "dependencies": { - "@testim/chrome-version": "^1.1.4", - "axios": "^1.6.5", - "compare-versions": "^6.1.0", + "@testim/chrome-version": "^1.1.3", + "axios": "^1.4.0", + "compare-versions": "^6.0.0", "extract-zip": "^2.0.1", "https-proxy-agent": "^5.0.1", "proxy-from-env": "^1.1.0", - "tcp-port-used": "^1.0.2" + "tcp-port-used": "^1.0.1" }, "bin": { "chromedriver": "bin/chromedriver" }, "engines": { - "node": ">=18" + "node": ">=16" } }, "node_modules/ci-info": { @@ -9782,6 +9783,14 @@ "node": ">=6" } }, + "node_modules/jsonata": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/jsonata/-/jsonata-2.0.3.tgz", + "integrity": "sha512-Up2H81MUtjqI/dWwWX7p4+bUMfMrQJVMN/jW6clFMTiYP528fBOBNtRu944QhKTs3+IsVWbgMeUTny5fw2VMUA==", + "engines": { + "node": ">= 8" + } + }, "node_modules/jsonpath-plus": { "version": "8.0.0", "resolved": "https://registry.npmjs.org/jsonpath-plus/-/jsonpath-plus-8.0.0.tgz", diff --git a/package.json b/package.json index 31aa2f1c..986c372a 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,7 @@ "babel-plugin-dynamic-import-node": "^2.3.3", "babel-plugin-transform-builtin-extend": "1.1.2", "base64-loader": "^1.0.0", - "chromedriver": "^121.0.0", + "chromedriver": "^114.0.2", "cli-progress": "^3.12.0", "colors": "^1.4.0", "copy-webpack-plugin": "^12.0.2", @@ -133,6 +133,7 @@ "js-sha3": "^0.9.3", "jsesc": "^3.0.2", "json5": "^2.2.3", + "jsonata": "^2.0.3", "jsonpath-plus": "^8.0.0", "jsonwebtoken": "8.5.1", "jsqr": "^1.4.0", diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index ac1f81d6..9804ac84 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -330,6 +330,7 @@ "Regular expression", "XPath expression", "JPath expression", + "Jsonata Query", "CSS selector", "Extract EXIF", "Extract ID3", diff --git a/src/core/operations/Jsonata.mjs b/src/core/operations/Jsonata.mjs new file mode 100644 index 00000000..1651e093 --- /dev/null +++ b/src/core/operations/Jsonata.mjs @@ -0,0 +1,65 @@ +/** + * @author Jon K (jon@ajarsoftware.com) + * @copyright Crown Copyright 2016 + * @license Apache-2.0 + */ + +import jsonata from "jsonata"; +import Operation from "../Operation.mjs"; +import OperationError from "../errors/OperationError.mjs"; + +/** + * Jsonata Query operation + */ +class JsonataQuery extends Operation { + /** + * JsonataQuery constructor + */ + constructor() { + super(); + + this.name = "Jsonata Query"; + this.module = "Code"; + this.description = + "Query and transform JSON data with a jsonata query."; + this.infoURL = "https://docs.jsonata.org/overview.html"; + this.inputType = "string"; + this.outputType = "string"; + this.args = [ + { + name: "Query", + type: "text", + value: "string", + }, + ]; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + async run(input, args) { + const [query] = args; + let result, jsonObj; + + try { + jsonObj = JSON.parse(input); + } catch (err) { + throw new OperationError(`Invalid input JSON: ${err.message}`); + } + + try { + const expression = jsonata(query); + result = await expression.evaluate(jsonObj); + } catch (err) { + throw new OperationError( + `Invalid Jsonata Expression: ${err.message}` + ); + } + + return JSON.stringify(result); + } +} + +export default JsonataQuery;