Improve sanity checking of valid datasources to prevent erroneous API calls

This commit is contained in:
Andrew Kingston 2023-08-04 15:26:06 +01:00
parent dd4b304736
commit 9652356ee0
4 changed files with 20 additions and 1 deletions

View File

@ -98,10 +98,16 @@ export const createActions = context => {
return await getAPI()?.actions.deleteRows(rows)
}
// Gets a single row from a datasource
const getRow = async id => {
return await getAPI()?.actions.getRow(id)
}
// Checks if a certain datasource config is valid
const isDatasourceValid = datasource => {
return getAPI()?.actions.isDatasourceValid(datasource)
}
return {
datasource: {
...datasource,
@ -112,6 +118,7 @@ export const createActions = context => {
updateRow,
deleteRows,
getRow,
isDatasourceValid,
},
},
}

View File

@ -98,7 +98,7 @@ export const createActions = context => {
loading.set(true)
// Abandon if we don't have a valid datasource
if (!$datasource) {
if (!datasource.actions.isDatasourceValid($datasource)) {
return
}

View File

@ -25,6 +25,10 @@ export const createActions = context => {
})
}
const isDatasourceValid = datasource => {
return datasource?.type === "table" && datasource?.tableId
}
const getRow = async id => {
const res = await API.searchTable({
tableId: get(datasource).tableId,
@ -48,6 +52,7 @@ export const createActions = context => {
updateRow: saveRow,
deleteRows,
getRow,
isDatasourceValid,
},
},
}

View File

@ -60,6 +60,12 @@ export const createActions = context => {
return res?.rows?.[0]
}
const isDatasourceValid = datasource => {
return (
datasource?.type === "viewV2" && datasource?.id && datasource?.tableId
)
}
return {
viewV2: {
actions: {
@ -69,6 +75,7 @@ export const createActions = context => {
updateRow,
deleteRows,
getRow,
isDatasourceValid,
},
},
}