|
|
|
@ -38,6 +38,7 @@ |
|
|
|
import DynamicVariableModal from "../../_components/DynamicVariableModal.svelte" |
|
|
|
import Placeholder from "assets/bb-spaceship.svg" |
|
|
|
import { cloneDeep } from "lodash/fp" |
|
|
|
import { RawRestBodyTypes } from "constants/backend" |
|
|
|
|
|
|
|
let query, datasource |
|
|
|
let breakQs = {}, |
|
|
|
@ -54,13 +55,10 @@ |
|
|
|
$: checkQueryName(url) |
|
|
|
$: responseSuccess = response?.info?.code >= 200 && response?.info?.code < 400 |
|
|
|
$: isGet = query?.queryVerb === "read" |
|
|
|
$: authConfigs = restUtils.buildAuthConfigs(datasource) |
|
|
|
$: authConfigs = buildAuthConfigs(datasource) |
|
|
|
$: schemaReadOnly = !responseSuccess |
|
|
|
$: variablesReadOnly = !responseSuccess |
|
|
|
$: showVariablesTab = restUtils.shouldShowVariables( |
|
|
|
dynamicVariables, |
|
|
|
variablesReadOnly |
|
|
|
) |
|
|
|
$: showVariablesTab = shouldShowVariables(dynamicVariables, variablesReadOnly) |
|
|
|
|
|
|
|
function getSelectedQuery() { |
|
|
|
return cloneDeep( |
|
|
|
@ -117,11 +115,7 @@ |
|
|
|
notifications.success(`Request saved successfully.`) |
|
|
|
|
|
|
|
if (dynamicVariables) { |
|
|
|
datasource.config.dynamicVariables = restUtils.rebuildVariables( |
|
|
|
datasource, |
|
|
|
saveId, |
|
|
|
dynamicVariables |
|
|
|
) |
|
|
|
datasource.config.dynamicVariables = rebuildVariables(saveId) |
|
|
|
datasource = await datasources.save(datasource) |
|
|
|
} |
|
|
|
} catch (err) { |
|
|
|
@ -160,6 +154,16 @@ |
|
|
|
return id |
|
|
|
} |
|
|
|
|
|
|
|
const buildAuthConfigs = datasource => { |
|
|
|
if (datasource?.config?.authConfigs) { |
|
|
|
return datasource.config.authConfigs.map(c => ({ |
|
|
|
label: c.name, |
|
|
|
value: c._id, |
|
|
|
})) |
|
|
|
} |
|
|
|
return [] |
|
|
|
} |
|
|
|
|
|
|
|
const schemaMenuItems = [ |
|
|
|
{ |
|
|
|
text: "Create dynamic variable", |
|
|
|
@ -179,6 +183,49 @@ |
|
|
|
}, |
|
|
|
] |
|
|
|
|
|
|
|
// convert dynamic variables list to simple key/val object |
|
|
|
const getDynamicVariables = (datasource, queryId) => { |
|
|
|
const variablesList = datasource?.config?.dynamicVariables |
|
|
|
if (variablesList && variablesList.length > 0) { |
|
|
|
const filtered = queryId |
|
|
|
? variablesList.filter(variable => variable.queryId === queryId) |
|
|
|
: variablesList |
|
|
|
return filtered.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, |
|
|
|
} |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
let existing = datasource?.config?.dynamicVariables || [] |
|
|
|
// remove existing query variables (for changes and deletions) |
|
|
|
existing = existing.filter(variable => variable.queryId !== queryId) |
|
|
|
// re-add the new query variables |
|
|
|
return [...existing, ...variables] |
|
|
|
} |
|
|
|
|
|
|
|
const shouldShowVariables = (dynamicVariables, variablesReadOnly) => { |
|
|
|
return !!( |
|
|
|
dynamicVariables && |
|
|
|
// show when editable or when read only and not empty |
|
|
|
(!variablesReadOnly || Object.keys(dynamicVariables).length > 0) |
|
|
|
) |
|
|
|
} |
|
|
|
|
|
|
|
onMount(async () => { |
|
|
|
query = getSelectedQuery() |
|
|
|
// clear any unsaved changes to the datasource |
|
|
|
@ -214,12 +261,16 @@ |
|
|
|
} |
|
|
|
} |
|
|
|
if (query && !query.fields.bodyType) { |
|
|
|
query.fields.bodyType = "none" |
|
|
|
if (query.fields.requestBody) { |
|
|
|
query.fields.bodyType = RawRestBodyTypes.JSON |
|
|
|
} else { |
|
|
|
query.fields.bodyType = RawRestBodyTypes.NONE |
|
|
|
} |
|
|
|
} |
|
|
|
if (query && !query.fields.pagination) { |
|
|
|
query.fields.pagination = {} |
|
|
|
} |
|
|
|
dynamicVariables = restUtils.getDynamicVariables(datasource, query._id) |
|
|
|
dynamicVariables = getDynamicVariables(datasource, query._id) |
|
|
|
}) |
|
|
|
</script> |
|
|
|
|
|
|
|
|