From 84f1a95a8bf872a183e2a17b3c90ebe2312d326c Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 15 Jan 2025 17:05:32 +0000 Subject: [PATCH] Implementing JSONValue type for passing binding results around. --- .../common/bindings/EvaluationSidePanel.svelte | 13 ++++++------- packages/frontend-core/src/utils/jsonFormatter.ts | 15 ++++++--------- packages/types/src/core/common.ts | 7 +++++++ packages/types/src/core/index.ts | 1 + 4 files changed, 20 insertions(+), 16 deletions(-) create mode 100644 packages/types/src/core/common.ts diff --git a/packages/builder/src/components/common/bindings/EvaluationSidePanel.svelte b/packages/builder/src/components/common/bindings/EvaluationSidePanel.svelte index e48b68aef8..c8bf5529ad 100644 --- a/packages/builder/src/components/common/bindings/EvaluationSidePanel.svelte +++ b/packages/builder/src/components/common/bindings/EvaluationSidePanel.svelte @@ -4,10 +4,10 @@ import { Helpers } from "@budibase/bbui" import { fade } from "svelte/transition" import { UserScriptError } from "@budibase/string-templates" + import type { JSONValue } from "@budibase/types" // this can be essentially any primitive response from the JS function - export let expressionResult: string | boolean | object | number | undefined = - undefined + export let expressionResult: JSONValue | undefined = undefined export let expressionError: string | undefined = undefined export let evaluating = false export let expression: string | null = null @@ -24,21 +24,20 @@ return err.toString() } + // json can be any primitive type const highlight = (json?: any | null) => { if (json == null) { return "" } // Attempt to parse and then stringify, in case this is valid result - let jsonString: string try { - jsonString = JSON.stringify(JSON.parse(json), null, 2) + json = JSON.stringify(JSON.parse(json), null, 2) } catch (err) { - // Ignore - jsonString = json + // couldn't parse/stringify, just treat it as the raw input } - return JsonFormatter.format(jsonString, { + return JsonFormatter.format(json, { keyColor: "#e06c75", numberColor: "#e5c07b", stringColor: "#98c379", diff --git a/packages/frontend-core/src/utils/jsonFormatter.ts b/packages/frontend-core/src/utils/jsonFormatter.ts index 14a8989fd2..ba9f8fc748 100644 --- a/packages/frontend-core/src/utils/jsonFormatter.ts +++ b/packages/frontend-core/src/utils/jsonFormatter.ts @@ -1,3 +1,5 @@ +import { JSONValue } from "@budibase/types" + export type ColorsOptions = { keyColor?: string numberColor?: string @@ -32,15 +34,10 @@ function escapeHtml(html: string) { }) } -export function format(json: string | object, colorOptions = {}) { +export function format(json: JSONValue, colorOptions: ColorsOptions = {}) { const valueType = typeof json - if (valueType !== "string") { - json = JSON.stringify(json, null, 2) || valueType - } - let jsonString: string = - valueType !== "string" - ? JSON.stringify(json, null, 2) || valueType - : (json as string) + let jsonString = + typeof json === "string" ? json : JSON.stringify(json, null, 2) || valueType let colors = Object.assign({}, defaultColors, colorOptions) jsonString = jsonString .replace(/&/g, "&") @@ -48,7 +45,7 @@ export function format(json: string | object, colorOptions = {}) { .replace(/>/g, ">") return jsonString.replace( /("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+]?\d+)?)/g, - match => { + (match: string) => { let color = colors.numberColor let style = "" if (/^"/.test(match)) { diff --git a/packages/types/src/core/common.ts b/packages/types/src/core/common.ts new file mode 100644 index 0000000000..c61fc3255d --- /dev/null +++ b/packages/types/src/core/common.ts @@ -0,0 +1,7 @@ +export type JSONValue = + | string + | number + | boolean + | null + | { [key: string]: JSONValue } + | JSONValue[] diff --git a/packages/types/src/core/index.ts b/packages/types/src/core/index.ts index 73cc7d35e0..ba9c1b907c 100644 --- a/packages/types/src/core/index.ts +++ b/packages/types/src/core/index.ts @@ -1,2 +1,3 @@ export * from "./installation" export * from "./events" +export * from "./common"