Added fixes for query body binding. Query parameter fixes and pretty bindings added

This commit is contained in:
Dean 2022-07-03 17:33:25 +01:00
parent cad3d7dead
commit 6be703d99a
6 changed files with 150 additions and 57 deletions

View File

@ -97,13 +97,17 @@ export const toBindingsArray = (valueMap, prefix) => {
if (!valueMap) { if (!valueMap) {
return [] return []
} }
return Object.keys(valueMap).map(binding => { return Object.keys(valueMap).reduce((acc, binding) => {
return { if (!binding || !valueMap[binding]) {
return acc
}
acc.push({
type: "context", type: "context",
runtimeBinding: binding, runtimeBinding: binding,
readableBinding: `${prefix}.${binding}`, readableBinding: `${prefix}.${binding}`,
}
}) })
return acc
}, [])
} }
/** /**

View File

@ -53,6 +53,7 @@
bind:object={parsedHeaders} bind:object={parsedHeaders}
on:change={evt => onDefaultHeaderUpdate(evt.detail)} on:change={evt => onDefaultHeaderUpdate(evt.detail)}
noAddButton noAddButton
bindings={getRestBindings()}
/> />
<div> <div>
<ActionButton icon="Add" on:click={() => addHeader.addEntry()}> <ActionButton icon="Add" on:click={() => addHeader.addEntry()}>

View File

@ -2,7 +2,7 @@
import { onMount } from "svelte" import { onMount } from "svelte"
import { ModalContent, Layout, Select, Body, Input } from "@budibase/bbui" import { ModalContent, Layout, Select, Body, Input } from "@budibase/bbui"
import { AUTH_TYPE_LABELS, AUTH_TYPES } from "./authTypes" import { AUTH_TYPE_LABELS, AUTH_TYPES } from "./authTypes"
import DrawerBindableCombobox from "components/common/bindings/DrawerBindableCombobox.svelte" import BindableCombobox from "components/common/bindings/BindableCombobox.svelte"
import { getAuthBindings } from "builderStore/dataBinding" import { getAuthBindings } from "builderStore/dataBinding"
export let configs export let configs
@ -205,7 +205,7 @@
/> />
{/if} {/if}
{#if form.type === AUTH_TYPES.BEARER} {#if form.type === AUTH_TYPES.BEARER}
<DrawerBindableCombobox <BindableCombobox
label="Token" label="Token"
value={form.bearer.token} value={form.bearer.token}
bindings={getAuthBindings()} bindings={getAuthBindings()}

View File

@ -0,0 +1,68 @@
<script>
import { Combobox } from "@budibase/bbui"
import {
readableToRuntimeBinding,
runtimeToReadableBinding,
} from "builderStore/dataBinding"
import { createEventDispatcher } from "svelte"
import { isJSBinding } from "@budibase/string-templates"
export let value = ""
export let bindings = []
export let placeholder
export let label
export let disabled = false
export let options
export let appendBindingsAsOptions = true
export let error
const dispatch = createEventDispatcher()
$: readableValue = runtimeToReadableBinding(bindings, value)
$: isJS = isJSBinding(value)
$: allOptions = buildOptions(options, bindings, appendBindingsAsOptions)
const onChange = (value, optionPicked) => {
// Add HBS braces if picking binding
if (optionPicked && !options?.includes(value)) {
value = `{{ ${value} }}`
}
dispatch("change", readableToRuntimeBinding(bindings, value))
}
const buildOptions = (options, bindings, appendBindingsAsOptions) => {
if (!appendBindingsAsOptions) {
return options
}
return []
.concat(options || [])
.concat(bindings?.map(binding => binding.readableBinding) || [])
}
</script>
<div class="control" class:disabled>
<Combobox
{label}
{disabled}
readonly={isJS}
value={isJS ? "(JavaScript function)" : readableValue}
on:type={e => onChange(e.detail, false)}
on:pick={e => onChange(e.detail, true)}
on:blur={() => dispatch("blur")}
{placeholder}
options={allOptions}
{error}
/>
</div>
<style>
.control {
flex: 1;
position: relative;
}
.control:not(.disabled) :global(.spectrum-Textfield-input) {
padding-right: 40px;
}
</style>

View File

@ -18,7 +18,6 @@
export let options export let options
export let allowJS = true export let allowJS = true
export let appendBindingsAsOptions = true export let appendBindingsAsOptions = true
export let drawerEnabled = false
export let error export let error
const dispatch = createEventDispatcher() const dispatch = createEventDispatcher()
@ -66,7 +65,7 @@
options={allOptions} options={allOptions}
{error} {error}
/> />
{#if !disabled && drawerEnabled} {#if !disabled}
<div <div
class="icon" class="icon"
on:click={bindingDrawer.show} on:click={bindingDrawer.show}
@ -76,8 +75,8 @@
</div> </div>
{/if} {/if}
</div> </div>
{#if !drawerEnabled}
<Drawer bind:this={bindingDrawer} {title}> <Drawer bind:this={bindingDrawer} {title}>
<svelte:fragment slot="description"> <svelte:fragment slot="description">
Add the objects on the left to enrich your text. Add the objects on the left to enrich your text.
</svelte:fragment> </svelte:fragment>
@ -91,8 +90,7 @@
{bindings} {bindings}
{allowJS} {allowJS}
/> />
</Drawer> </Drawer>
{/if}
<style> <style>
.control { .control {

View File

@ -59,6 +59,7 @@
let restBindings = getRestBindings() let restBindings = getRestBindings()
$: staticVariables = datasource?.config?.staticVariables || {} $: staticVariables = datasource?.config?.staticVariables || {}
$: customRequestBindings = toBindingsArray(requestBindings, "Binding") $: customRequestBindings = toBindingsArray(requestBindings, "Binding")
$: dynamicRequestBindings = toBindingsArray(dynamicVariables, "Dynamic") $: dynamicRequestBindings = toBindingsArray(dynamicVariables, "Dynamic")
$: dataSourceStaticBindings = toBindingsArray( $: dataSourceStaticBindings = toBindingsArray(
@ -73,10 +74,6 @@
...dataSourceStaticBindings, ...dataSourceStaticBindings,
] ]
$: mergedAndCompleteBindings = mergedBindings.filter(binding => {
return binding.runtimeBinding && binding.readableBinding
})
$: datasourceType = datasource?.source $: datasourceType = datasource?.source
$: integrationInfo = $integrations[datasourceType] $: integrationInfo = $integrations[datasourceType]
$: queryConfig = integrationInfo?.query $: queryConfig = integrationInfo?.query
@ -92,10 +89,7 @@
Object.keys(schema || {}).length !== 0 || Object.keys(schema || {}).length !== 0 ||
Object.keys(query?.schema || {}).length !== 0 Object.keys(query?.schema || {}).length !== 0
$: runtimeUrlQueries = readableToRuntimeMap( $: runtimeUrlQueries = readableToRuntimeMap(mergedBindings, breakQs)
mergedAndCompleteBindings,
breakQs
)
function getSelectedQuery() { function getSelectedQuery() {
const cloneQuery = cloneDeep( const cloneQuery = cloneDeep(
@ -110,18 +104,6 @@
queryVerb: "read", queryVerb: "read",
} }
) )
if (cloneQuery?.fields?.requestBody) {
cloneQuery.fields.requestBody =
typeof cloneQuery.fields.requestBody === "object"
? runtimeToReadableMap(
mergedAndCompleteBindings,
cloneQuery.fields.requestBody
)
: runtimeToReadableBinding(
mergedAndCompleteBindings,
cloneQuery.fields.requestBody
)
}
return cloneQuery return cloneQuery
} }
@ -137,7 +119,7 @@
return base return base
} }
const qs = restUtils.buildQueryString( const qs = restUtils.buildQueryString(
runtimeToReadableMap(mergedAndCompleteBindings, qsObj) runtimeToReadableMap(mergedBindings, qsObj)
) )
let newUrl = base let newUrl = base
if (base.includes("?")) { if (base.includes("?")) {
@ -150,16 +132,11 @@
const newQuery = cloneDeep(query) const newQuery = cloneDeep(query)
const queryString = restUtils.buildQueryString(runtimeUrlQueries) const queryString = restUtils.buildQueryString(runtimeUrlQueries)
newQuery.parameters = restUtils.keyValueToQueryParameters(requestBindings)
newQuery.fields.requestBody = newQuery.fields.requestBody =
typeof newQuery.fields.requestBody === "object" typeof newQuery.fields.requestBody === "object"
? readableToRuntimeMap( ? readableToRuntimeMap(mergedBindings, newQuery.fields.requestBody)
mergedAndCompleteBindings, : readableToRuntimeBinding(mergedBindings, newQuery.fields.requestBody)
newQuery.fields.requestBody
)
: readableToRuntimeBinding(
mergedAndCompleteBindings,
newQuery.fields.requestBody
)
newQuery.fields.path = url.split("?")[0] newQuery.fields.path = url.split("?")[0]
newQuery.fields.queryString = queryString newQuery.fields.queryString = queryString
@ -181,6 +158,13 @@
datasource.config.dynamicVariables = rebuildVariables(saveId) datasource.config.dynamicVariables = rebuildVariables(saveId)
datasource = await datasources.save(datasource) datasource = await datasources.save(datasource)
} }
prettifyQueryRequestBody(
query,
requestBindings,
dynamicVariables,
staticVariables,
restBindings
)
} catch (err) { } catch (err) {
notifications.error(`Error saving query`) notifications.error(`Error saving query`)
} }
@ -297,6 +281,36 @@
} }
} }
const prettifyQueryRequestBody = (
query,
requestBindings,
dynamicVariables,
staticVariables,
restBindings
) => {
let customRequestBindings = toBindingsArray(requestBindings, "Binding")
let dynamicRequestBindings = toBindingsArray(dynamicVariables, "Dynamic")
let dataSourceStaticBindings = toBindingsArray(
staticVariables,
"Datasource.Static"
)
const prettyBindings = [
...restBindings,
...customRequestBindings,
...dynamicRequestBindings,
...dataSourceStaticBindings,
]
//Parse the body here as now all bindings have been updated.
if (query?.fields?.requestBody) {
query.fields.requestBody =
typeof query.fields.requestBody === "object"
? runtimeToReadableMap(prettyBindings, query.fields.requestBody)
: runtimeToReadableBinding(prettyBindings, query.fields.requestBody)
}
}
onMount(async () => { onMount(async () => {
query = getSelectedQuery() query = getSelectedQuery()
@ -311,7 +325,7 @@
const datasourceUrl = datasource?.config.url const datasourceUrl = datasource?.config.url
const qs = query?.fields.queryString const qs = query?.fields.queryString
breakQs = restUtils.breakQueryString(qs) breakQs = restUtils.breakQueryString(qs)
breakQs = runtimeToReadableMap(restBindings, breakQs) breakQs = runtimeToReadableMap(mergedBindings, breakQs)
const path = query.fields.path const path = query.fields.path
if ( if (
@ -354,6 +368,14 @@
query.fields.pagination = {} query.fields.pagination = {}
} }
dynamicVariables = getDynamicVariables(datasource, query._id) dynamicVariables = getDynamicVariables(datasource, query._id)
prettifyQueryRequestBody(
query,
requestBindings,
dynamicVariables,
staticVariables,
restBindings
)
}) })
</script> </script>
@ -425,7 +447,7 @@
bind:object={breakQs} bind:object={breakQs}
name="param" name="param"
headings headings
bindings={mergedAndCompleteBindings} bindings={mergedBindings}
/> />
</Tab> </Tab>
<Tab title="Headers"> <Tab title="Headers">
@ -435,7 +457,7 @@
toggle toggle
name="header" name="header"
headings headings
bindings={mergedAndCompleteBindings} bindings={mergedBindings}
/> />
</Tab> </Tab>
<Tab title="Body"> <Tab title="Body">