|
|
|
@ -1,22 +1,22 @@ |
|
|
|
<script> |
|
|
|
import { params } from "@roxi/routify" |
|
|
|
import { datasources, integrations, queries, flags } from "stores/backend" |
|
|
|
import { datasources, flags, integrations, queries } from "stores/backend" |
|
|
|
import { |
|
|
|
Layout, |
|
|
|
Input, |
|
|
|
Select, |
|
|
|
Tabs, |
|
|
|
Tab, |
|
|
|
Banner, |
|
|
|
Divider, |
|
|
|
Body, |
|
|
|
Button, |
|
|
|
Divider, |
|
|
|
Heading, |
|
|
|
RadioGroup, |
|
|
|
Input, |
|
|
|
Label, |
|
|
|
Body, |
|
|
|
TextArea, |
|
|
|
Table, |
|
|
|
Layout, |
|
|
|
notifications, |
|
|
|
RadioGroup, |
|
|
|
Select, |
|
|
|
Tab, |
|
|
|
Table, |
|
|
|
Tabs, |
|
|
|
TextArea, |
|
|
|
} from "@budibase/bbui" |
|
|
|
import KeyValueBuilder from "components/integration/KeyValueBuilder.svelte" |
|
|
|
import EditableLabel from "components/common/inputs/EditableLabel.svelte" |
|
|
|
@ -26,33 +26,24 @@ |
|
|
|
import RestBodyInput from "../../_components/RestBodyInput.svelte" |
|
|
|
import { capitalise } from "helpers" |
|
|
|
import { onMount } from "svelte" |
|
|
|
import { |
|
|
|
fieldsToSchema, |
|
|
|
schemaToFields, |
|
|
|
breakQueryString, |
|
|
|
buildQueryString, |
|
|
|
keyValueToQueryParameters, |
|
|
|
queryParametersToKeyValue, |
|
|
|
flipHeaderState, |
|
|
|
} from "helpers/data/utils" |
|
|
|
import restUtils from "helpers/data/utils" |
|
|
|
import { |
|
|
|
RestBodyTypes as bodyTypes, |
|
|
|
SchemaTypeOptions, |
|
|
|
} from "constants/backend" |
|
|
|
import JSONPreview from "components/integration/JSONPreview.svelte" |
|
|
|
import AccessLevelSelect from "components/integration/AccessLevelSelect.svelte" |
|
|
|
import DynamicVariableModal from "../../_components/DynamicVariableModal.svelte" |
|
|
|
import Placeholder from "assets/bb-spaceship.svg" |
|
|
|
import { cloneDeep } from "lodash/fp" |
|
|
|
|
|
|
|
let query, datasource |
|
|
|
let breakQs = {}, |
|
|
|
bindings = {} |
|
|
|
let url = "" |
|
|
|
let saveId, isGet |
|
|
|
let saveId, url |
|
|
|
let response, schema, enabledHeaders |
|
|
|
let datasourceType, integrationInfo, queryConfig, responseSuccess |
|
|
|
let authConfigId |
|
|
|
let dynamicVariables |
|
|
|
let dynamicVariables, addVariableModal, varBinding |
|
|
|
|
|
|
|
$: datasourceType = datasource?.source |
|
|
|
$: integrationInfo = $integrations[datasourceType] |
|
|
|
@ -61,10 +52,13 @@ |
|
|
|
$: checkQueryName(url) |
|
|
|
$: responseSuccess = response?.info?.code >= 200 && response?.info?.code < 400 |
|
|
|
$: isGet = query?.queryVerb === "read" |
|
|
|
$: authConfigs = buildAuthConfigs(datasource) |
|
|
|
$: authConfigs = restUtils.buildAuthConfigs(datasource) |
|
|
|
$: schemaReadOnly = !responseSuccess |
|
|
|
$: variablesReadOnly = !responseSuccess |
|
|
|
$: showVariablesTab = shouldShowVariables(dynamicVariables, variablesReadOnly) |
|
|
|
$: showVariablesTab = restUtils.shouldShowVariables( |
|
|
|
dynamicVariables, |
|
|
|
variablesReadOnly |
|
|
|
) |
|
|
|
|
|
|
|
function getSelectedQuery() { |
|
|
|
return cloneDeep( |
|
|
|
@ -92,7 +86,7 @@ |
|
|
|
if (!base) { |
|
|
|
return base |
|
|
|
} |
|
|
|
const qs = buildQueryString(qsObj) |
|
|
|
const qs = restUtils.buildQueryString(qsObj) |
|
|
|
let newUrl = base |
|
|
|
if (base.includes("?")) { |
|
|
|
newUrl = base.split("?")[0] |
|
|
|
@ -100,29 +94,15 @@ |
|
|
|
return qs.length > 0 ? `${newUrl}?${qs}` : newUrl |
|
|
|
} |
|
|
|
|
|
|
|
const buildAuthConfigs = datasource => { |
|
|
|
if (datasource?.config?.authConfigs) { |
|
|
|
return datasource.config.authConfigs.map(c => ({ |
|
|
|
label: c.name, |
|
|
|
value: c._id, |
|
|
|
})) |
|
|
|
} |
|
|
|
return [] |
|
|
|
} |
|
|
|
|
|
|
|
function learnMoreBanner() { |
|
|
|
window.open("https://docs.budibase.com/building-apps/data/transformers") |
|
|
|
} |
|
|
|
|
|
|
|
function buildQuery() { |
|
|
|
const newQuery = { ...query } |
|
|
|
const queryString = buildQueryString(breakQs) |
|
|
|
const queryString = restUtils.buildQueryString(breakQs) |
|
|
|
newQuery.fields.path = url.split("?")[0] |
|
|
|
newQuery.fields.queryString = queryString |
|
|
|
newQuery.fields.authConfigId = authConfigId |
|
|
|
newQuery.fields.disabledHeaders = flipHeaderState(enabledHeaders) |
|
|
|
newQuery.schema = fieldsToSchema(schema) |
|
|
|
newQuery.parameters = keyValueToQueryParameters(bindings) |
|
|
|
newQuery.fields.disabledHeaders = restUtils.flipHeaderState(enabledHeaders) |
|
|
|
newQuery.schema = restUtils.fieldsToSchema(schema) |
|
|
|
newQuery.parameters = restUtils.keyValueToQueryParameters(bindings) |
|
|
|
return newQuery |
|
|
|
} |
|
|
|
|
|
|
|
@ -135,8 +115,10 @@ |
|
|
|
notifications.success(`Request saved successfully.`) |
|
|
|
|
|
|
|
if (dynamicVariables) { |
|
|
|
const dynamicVars = rebuildVariables(saveId) |
|
|
|
datasource.config.dynamicVariables = dynamicVars |
|
|
|
datasource.config.dynamicVariables = restUtils.rebuildVariables( |
|
|
|
saveId, |
|
|
|
dynamicVariables |
|
|
|
) |
|
|
|
await datasources.save(datasource) |
|
|
|
} |
|
|
|
} catch (err) { |
|
|
|
@ -175,57 +157,21 @@ |
|
|
|
return id |
|
|
|
} |
|
|
|
|
|
|
|
// convert dynamic variables list to simple key/val object |
|
|
|
const variablesToObject = datasource => { |
|
|
|
const variablesList = datasource?.config?.dynamicVariables |
|
|
|
if (variablesList && variablesList.length > 0) { |
|
|
|
return variablesList.reduce( |
|
|
|
(acc, next) => ({ ...acc, [next.name]: next.value }), |
|
|
|
{} |
|
|
|
) |
|
|
|
} |
|
|
|
return {} |
|
|
|
} |
|
|
|
|
|
|
|
// convert dynamic variables object back to a list, enrich with query id |
|
|
|
const rebuildVariables = queryId => { |
|
|
|
let variables = [] |
|
|
|
if (dynamicVariables) { |
|
|
|
variables = Object.entries(dynamicVariables).map(entry => { |
|
|
|
return { |
|
|
|
name: entry[0], |
|
|
|
value: entry[1], |
|
|
|
queryId, |
|
|
|
} |
|
|
|
}) |
|
|
|
} |
|
|
|
return variables |
|
|
|
} |
|
|
|
|
|
|
|
const shouldShowVariables = (dynamicVariables, variablesReadOnly) => { |
|
|
|
if ( |
|
|
|
dynamicVariables && |
|
|
|
// show when editable or when read only and not empty |
|
|
|
(!variablesReadOnly || Object.keys(dynamicVariables).length > 0) |
|
|
|
) { |
|
|
|
return true |
|
|
|
} |
|
|
|
return false |
|
|
|
} |
|
|
|
|
|
|
|
const schemaMenuItems = [ |
|
|
|
{ |
|
|
|
text: "Create dynamic variable", |
|
|
|
onClick: () => { |
|
|
|
console.log("create variable") |
|
|
|
onClick: input => { |
|
|
|
varBinding = `{{ data.0.[${input.name}] }}` |
|
|
|
addVariableModal.show() |
|
|
|
}, |
|
|
|
}, |
|
|
|
] |
|
|
|
const responseHeadersMenuItems = [ |
|
|
|
{ |
|
|
|
text: "Create dynamic variable", |
|
|
|
onClick: () => { |
|
|
|
console.log("create variable") |
|
|
|
onClick: input => { |
|
|
|
varBinding = `{{ info.headers.[${input.name}] }}` |
|
|
|
addVariableModal.show() |
|
|
|
}, |
|
|
|
}, |
|
|
|
] |
|
|
|
@ -237,14 +183,14 @@ |
|
|
|
datasource = $datasources.list.find(ds => ds._id === query?.datasourceId) |
|
|
|
const datasourceUrl = datasource?.config.url |
|
|
|
const qs = query?.fields.queryString |
|
|
|
breakQs = breakQueryString(qs) |
|
|
|
breakQs = restUtils.breakQueryString(qs) |
|
|
|
if (datasourceUrl && !query.fields.path?.startsWith(datasourceUrl)) { |
|
|
|
const path = query.fields.path |
|
|
|
query.fields.path = `${datasource.config.url}/${path ? path : ""}` |
|
|
|
} |
|
|
|
url = buildUrl(query.fields.path, breakQs) |
|
|
|
schema = schemaToFields(query.schema) |
|
|
|
bindings = queryParametersToKeyValue(query.parameters) |
|
|
|
schema = restUtils.schemaToFields(query.schema) |
|
|
|
bindings = restUtils.queryParametersToKeyValue(query.parameters) |
|
|
|
authConfigId = getAuthConfigId() |
|
|
|
if (!query.fields.disabledHeaders) { |
|
|
|
query.fields.disabledHeaders = {} |
|
|
|
@ -255,7 +201,7 @@ |
|
|
|
query.fields.disabledHeaders[header] = false |
|
|
|
} |
|
|
|
} |
|
|
|
enabledHeaders = flipHeaderState(query.fields.disabledHeaders) |
|
|
|
enabledHeaders = restUtils.flipHeaderState(query.fields.disabledHeaders) |
|
|
|
if (query && !query.transformer) { |
|
|
|
query.transformer = "return data" |
|
|
|
} |
|
|
|
@ -267,10 +213,16 @@ |
|
|
|
if (query && !query.fields.bodyType) { |
|
|
|
query.fields.bodyType = "none" |
|
|
|
} |
|
|
|
dynamicVariables = variablesToObject(datasource) |
|
|
|
dynamicVariables = restUtils.variablesToObject(datasource) |
|
|
|
}) |
|
|
|
</script> |
|
|
|
|
|
|
|
<DynamicVariableModal |
|
|
|
{datasource} |
|
|
|
{dynamicVariables} |
|
|
|
bind:binding={varBinding} |
|
|
|
bind:this={addVariableModal} |
|
|
|
/> |
|
|
|
{#if query && queryConfig} |
|
|
|
<div class="inner"> |
|
|
|
<div class="top"> |
|
|
|
@ -340,7 +292,10 @@ |
|
|
|
{#if !$flags.queryTransformerBanner} |
|
|
|
<Banner |
|
|
|
extraButtonText="Learn more" |
|
|
|
extraButtonAction={learnMoreBanner} |
|
|
|
extraButtonAction={() => |
|
|
|
window.open( |
|
|
|
"https://docs.budibase.com/building-apps/data/transformers" |
|
|
|
)} |
|
|
|
on:change={() => |
|
|
|
flags.updateFlag("queryTransformerBanner", true)} |
|
|
|
> |
|
|
|
@ -449,9 +404,9 @@ |
|
|
|
name="Variable" |
|
|
|
headings |
|
|
|
keyHeading="Name" |
|
|
|
keyPlaceholder="e.g. cookie" |
|
|
|
keyPlaceholder="Variable name" |
|
|
|
valueHeading={`Value`} |
|
|
|
valuePlaceholder={`e.g. {{ headers.set-cookie }}`} |
|
|
|
valuePlaceholder={`{{ value }}`} |
|
|
|
readOnly={variablesReadOnly} |
|
|
|
/> |
|
|
|
</Layout> |
|
|
|
|