1
0
Fork 0
mirror of synced 2024-08-15 18:11:40 +12:00

Merge branch 'feature/environment-variables' of github.com:Budibase/budibase into feature/environment-variables

This commit is contained in:
Peter Clement 2023-01-27 15:25:21 +00:00
commit c1f270ef3a
4 changed files with 34 additions and 6 deletions

View file

@ -11,11 +11,12 @@ import { getEnvironmentVariables } from "../../utils"
import { getDefinitions } from "../../../integrations"
const ENV_VAR_PREFIX = "env."
const USER_PREFIX = "user"
async function enrichDatasourceWithValues(datasource: Datasource) {
const cloned = cloneDeep(datasource)
const env = await getEnvironmentVariables()
const processed = processObjectSync(cloned, { env })
const processed = processObjectSync(cloned, { env }, { onlyFound: true })
return {
datasource: processed as Datasource,
envVars: env as Record<string, string>,
@ -48,7 +49,9 @@ export async function getWithEnvVars(datasourceId: string) {
export function isValid(datasource: Datasource) {
const blocks = findHBSBlocks(JSON.stringify(datasource))
const validList = blocks.filter(block => block.includes(ENV_VAR_PREFIX))
const validList = blocks.filter(
block => block.includes(ENV_VAR_PREFIX) || block.includes(USER_PREFIX)
)
return blocks.length === validList.length
}

View file

@ -32,11 +32,15 @@ const HELPERS = [
// javascript helper
new Helper(HelperFunctionNames.JS, processJS, false),
// this help is applied to all statements
new Helper(HelperFunctionNames.ALL, (value, { __opts }) => {
new Helper(HelperFunctionNames.ALL, (value, inputs) => {
const { __opts } = inputs
if (isObject(value)) {
return new SafeString(JSON.stringify(value))
}
// null/undefined values produce bad results
if (__opts && __opts.onlyFound && value == null) {
return __opts.input
}
if (value == null || typeof value !== "string") {
return value == null ? "" : value
}

View file

@ -146,16 +146,31 @@ module.exports.processStringSync = (string, context, opts) => {
if (typeof string !== "string") {
throw "Cannot process non-string types."
}
try {
const template = createTemplate(string, opts)
function process(stringPart) {
const template = createTemplate(stringPart, opts)
const now = Math.floor(Date.now() / 1000) * 1000
return processors.postprocess(
template({
now: new Date(now).toISOString(),
__opts: opts,
__opts: {
...opts,
input: stringPart,
},
...context,
})
)
}
try {
if (opts && opts.onlyFound) {
const blocks = exports.findHBSBlocks(string)
for (let block of blocks) {
const outcome = process(block)
string = string.replace(block, outcome)
}
return string
} else {
return process(string)
}
} catch (err) {
return input
}

View file

@ -221,3 +221,9 @@ describe("check find hbs blocks function", () => {
})
})
describe("should leave HBS blocks if not found using option", () => {
it("should replace one, leave one", async () => {
const output = await processString("{{ a }}, {{ b }}", { b: 1 }, { onlyFound: true })
expect(output).toBe("{{ a }}, 1")
})
})