Integrate query with datasource auth

This commit is contained in:
Rory Powell 2021-12-11 23:34:30 +00:00
parent 9fa4fe6c76
commit 077c9ef434
4 changed files with 78 additions and 18 deletions

View File

@ -34,7 +34,7 @@
} }
} }
const onDelete = () => { const onRemove = () => {
configs = configs.filter(c => { configs = configs.filter(c => {
return c._id !== currentConfig._id return c._id !== currentConfig._id
}) })
@ -42,7 +42,7 @@
</script> </script>
<Modal bind:this={modal}> <Modal bind:this={modal}>
<RestAuthenticationModal {configs} {currentConfig} {onConfirm} {onDelete} /> <RestAuthenticationModal {configs} {currentConfig} {onConfirm} {onRemove} />
</Modal> </Modal>
<Layout gap="S" noPadding> <Layout gap="S" noPadding>

View File

@ -6,7 +6,7 @@
export let configs export let configs
export let currentConfig export let currentConfig
export let onConfirm export let onConfirm
export let onDelete export let onRemove
let form = { let form = {
basic: {}, basic: {},
@ -59,6 +59,10 @@
type: form.type, type: form.type,
} }
if (currentConfig) {
newConfig._id = currentConfig._id
}
if (form.type === AUTH_TYPES.BASIC) { if (form.type === AUTH_TYPES.BASIC) {
newConfig.config = { newConfig.config = {
...form.basic, ...form.basic,
@ -158,8 +162,8 @@
cancelText={"Cancel"} cancelText={"Cancel"}
size="M" size="M"
showSecondaryButton={!!currentConfig} showSecondaryButton={!!currentConfig}
secondaryButtonText={"Delete"} secondaryButtonText={"Remove"}
secondaryAction={onDelete} secondaryAction={onRemove}
secondaryButtonWarning={true} secondaryButtonWarning={true}
> >
<Layout gap="S"> <Layout gap="S">

View File

@ -50,6 +50,7 @@
let saveId let saveId
let response, schema, isGet let response, schema, isGet
let datasourceType, integrationInfo, queryConfig, responseSuccess let datasourceType, integrationInfo, queryConfig, responseSuccess
let authConfigId
$: datasource = $datasources.list.find(ds => ds._id === query?.datasourceId) $: datasource = $datasources.list.find(ds => ds._id === query?.datasourceId)
$: datasourceType = datasource?.source $: datasourceType = datasource?.source
@ -60,6 +61,7 @@
$: isGet = query?.queryVerb === "read" $: isGet = query?.queryVerb === "read"
$: responseSuccess = $: responseSuccess =
response?.info?.code >= 200 && response?.info?.code <= 206 response?.info?.code >= 200 && response?.info?.code <= 206
$: authConfigs = buildAuthConfigs(datasource)
function getSelectedQuery() { function getSelectedQuery() {
return cloneDeep( return cloneDeep(
@ -91,6 +93,16 @@
return qs.length > 0 ? `${newUrl}?${qs}` : newUrl 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() { function learnMoreBanner() {
window.open("https://docs.budibase.com/building-apps/data/transformers") window.open("https://docs.budibase.com/building-apps/data/transformers")
} }
@ -100,6 +112,7 @@
const queryString = buildQueryString(breakQs) const queryString = buildQueryString(breakQs)
newQuery.fields.path = url.split("?")[0] newQuery.fields.path = url.split("?")[0]
newQuery.fields.queryString = queryString newQuery.fields.queryString = queryString
newQuery.fields.authConfigId = authConfigId
newQuery.schema = fieldsToSchema(schema) newQuery.schema = fieldsToSchema(schema)
newQuery.parameters = keyValueToQueryParameters(bindings) newQuery.parameters = keyValueToQueryParameters(bindings)
return newQuery return newQuery
@ -132,6 +145,22 @@
} }
} }
const getAuthConfigId = () => {
let id = query.fields.authConfigId
if (id) {
// find the matching config on the datasource
const matchedConfig = datasource?.config?.authConfigs?.filter(
c => c._id === authConfigId
)[0]
// clear the id if the config is not found (deleted)
// i.e. just show 'None' in the dropdown
if (!matchedConfig) {
id = undefined
}
}
return id
}
onMount(() => { onMount(() => {
query = getSelectedQuery() query = getSelectedQuery()
const qs = query?.fields.queryString const qs = query?.fields.queryString
@ -139,6 +168,7 @@
url = buildUrl(query.fields.path, breakQs) url = buildUrl(query.fields.path, breakQs)
schema = schemaToFields(query.schema) schema = schemaToFields(query.schema)
bindings = queryParametersToKeyValue(query.parameters) bindings = queryParametersToKeyValue(query.parameters)
authConfigId = getAuthConfigId()
if (query && !query.transformer) { if (query && !query.transformer) {
query.transformer = "return data" query.transformer = "return data"
} }
@ -238,6 +268,19 @@
/> />
</Layout> </Layout>
</Tab> </Tab>
<div class="auth-container">
<div />
<!-- spacer -->
<div class="auth-select">
<Select
label="Auth"
labelPosition="left"
placeholder="None"
bind:value={authConfigId}
options={authConfigs}
/>
</div>
</div>
</Tabs> </Tabs>
</Layout> </Layout>
</div> </div>
@ -384,4 +427,12 @@
margin-top: var(--spacing-xl); margin-top: var(--spacing-xl);
justify-content: center; justify-content: center;
} }
.auth-container {
width: 100%;
display: flex;
justify-content: space-between;
}
.auth-select {
width: 200px;
}
</style> </style>

View File

@ -190,18 +190,22 @@ module RestModule {
const authConfig = this.config.authConfigs.filter( const authConfig = this.config.authConfigs.filter(
c => c._id === authConfigId c => c._id === authConfigId
)[0] )[0]
let config // check the config still exists before proceeding
switch (authConfig.type) { // if not - do nothing
case AuthType.BASIC: if (authConfig) {
config = authConfig.config as BasicAuthConfig let config
headers.Authorization = `Basic ${Buffer.from( switch (authConfig.type) {
`${config.username}:${config.password}` case AuthType.BASIC:
).toString("base64")}` config = authConfig.config as BasicAuthConfig
break headers.Authorization = `Basic ${Buffer.from(
case AuthType.BEARER: `${config.username}:${config.password}`
config = authConfig.config as BearerAuthConfig ).toString("base64")}`
headers.Authorization = `Bearer ${config.token}` break
break case AuthType.BEARER:
config = authConfig.config as BearerAuthConfig
headers.Authorization = `Bearer ${config.token}`
break
}
} }
} }
@ -242,7 +246,8 @@ module RestModule {
} }
this.startTimeMs = performance.now() this.startTimeMs = performance.now()
const response = await fetch(this.getUrl(path, queryString), input) const url = this.getUrl(path, queryString)
const response = await fetch(url, input)
return await this.parseResponse(response) return await this.parseResponse(response)
} }