Skip to content

Commit

Permalink
Merge pull request #15096 from Budibase/frontend-core-ts-2
Browse files Browse the repository at this point in the history
Convert frontend-core API client to Typescript
  • Loading branch information
aptkingston authored Dec 17, 2024
2 parents fc38a46 + 287f8ce commit b7d7946
Show file tree
Hide file tree
Showing 112 changed files with 2,307 additions and 1,725 deletions.
6 changes: 3 additions & 3 deletions packages/builder/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { get } from "svelte/store"
import { auth, navigation } from "./stores/portal"

export const API = createAPIClient({
attachHeaders: (headers: Record<string, string>) => {
attachHeaders: headers => {
// Attach app ID header from store
let appId = get(appStore).appId
if (appId) {
Expand All @@ -22,7 +22,7 @@ export const API = createAPIClient({
}
},

onError: (error: any) => {
onError: error => {
const { url, message, status, method, handled } = error || {}

// Log any errors that we haven't manually handled
Expand All @@ -45,7 +45,7 @@ export const API = createAPIClient({
}
}
},
onMigrationDetected: (appId: string) => {
onMigrationDetected: appId => {
const updatingUrl = `/builder/app/updating/${appId}`

if (window.location.pathname === updatingUrl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@
async function generateAICronExpression() {
loadingAICronExpression = true
try {
const response = await API.generateCronExpression({
prompt: aiCronPrompt,
})
const response = await API.generateCronExpression(aiCronPrompt)
cronExpression = response.message
dispatch("change", response.message)
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,19 @@
}
const exportAllData = async () => {
return await API.exportView({
viewName: view,
format: exportFormat,
})
return await API.exportView(view, exportFormat)
}
const exportFilteredData = async () => {
let payload = {
tableId: view,
format: exportFormat,
search: {
paginate: false,
},
}
let payload = {}
if (selectedRows?.length) {
payload.rows = selectedRows.map(row => row._id)
}
if (sorting) {
payload.search.sort = sorting.sortColumn
payload.search.sortOrder = sorting.sortOrder
payload.sort = sorting.sortColumn
payload.sortOrder = sorting.sortOrder
}
return await API.exportRows(payload)
return await API.exportRows(view, exportFormat, payload)
}
const exportData = async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@
const importData = async () => {
try {
loading = true
await API.importTableData({
tableId,
rows,
identifierFields,
})
await API.importTableData(tableId, rows, identifierFields)
notifications.success("Rows successfully imported")
popover.hide()
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
const toggleAction = async (action, enabled) => {
if (enabled) {
await rowActions.enableView(tableId, viewId, action.id)
await rowActions.enableView(tableId, action.id, viewId)
} else {
await rowActions.disableView(tableId, viewId, action.id)
await rowActions.disableView(tableId, action.id, viewId)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,7 @@
allValid = false
if (rows.length > 0) {
const response = await API.validateExistingTableImport({
rows,
tableId,
})
const response = await API.validateExistingTableImport(rows, tableId)
validation = response.schemaValidation
invalidColumns = response.invalidColumns
allValid = response.allValid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
loading = true
try {
if (rows.length > 0) {
const response = await API.validateNewTableImport({ rows, schema })
const response = await API.validateNewTableImport(rows, schema)
validation = response.schemaValidation
allValid = response.allValid
errors = response.errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
}
try {
const app = await API.duplicateApp(data, appId)
const app = await API.duplicateApp(appId, data)
appsStore.load()
if (!sdk.users.isBuilder($auth.user, app?.duplicateAppId)) {
// Refresh for access to created applications
Expand Down
8 changes: 0 additions & 8 deletions packages/builder/src/index.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@
if (!hasSynced && application) {
try {
await API.syncApp(application)
// check if user has beta access
// const betaResponse = await API.checkBetaAccess($auth?.user?.email)
// betaAccess = betaResponse.access
} catch (error) {
notifications.error("Failed to sync with production database")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@
return
}
try {
data = await API.fetchViewData({
name,
data = await API.fetchViewData(name, {
calculation,
field,
groupBy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,18 @@
}
async function fetchBackups(filters, page, dateRange = []) {
const body = {
appId: $appStore.appId,
const opts = {
...filters,
page,
}
const [startDate, endDate] = dateRange
if (startDate) {
body.startDate = startDate
opts.startDate = startDate
}
if (endDate) {
body.endDate = endDate
opts.endDate = endDate
}
const response = await backups.searchBackups(body)
const response = await backups.searchBackups($appStore.appId, opts)
pageInfo.fetched(response.hasNextPage, response.nextPage)
// flatten so we have an easier structure to use for the table schema
Expand All @@ -123,9 +120,7 @@
async function createManualBackup() {
try {
loading = true
let response = await backups.createManualBackup({
appId: $appStore.appId,
})
let response = await backups.createManualBackup($appStore.appId)
await fetchBackups(filterOpt, page)
notifications.success(response.message)
} catch (err) {
Expand All @@ -149,24 +144,14 @@
async function handleButtonClick({ detail }) {
if (detail.type === "backupDelete") {
await backups.deleteBackup({
appId: $appStore.appId,
backupId: detail.backupId,
})
await backups.deleteBackup($appStore.appId, detail.backupId)
await fetchBackups(filterOpt, page)
} else if (detail.type === "backupRestore") {
await backups.restoreBackup({
appId: $appStore.appId,
backupId: detail.backupId,
name: detail.restoreBackupName,
})
await fetchBackups(filterOpt, page)
} else if (detail.type === "backupUpdate") {
await backups.updateBackup({
appId: $appStore.appId,
backupId: detail.backupId,
name: detail.name,
})
await backups.restoreBackup(
$appStore.appId,
detail.backupId,
detail.restoreBackupName
)
await fetchBackups(filterOpt, page)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@
logsPageInfo.loading()
await auditLogs.search({
bookmark: logsPage,
startDate: dateRange[0],
endDate: dateRange[1],
startDate: dateRange[0] || undefined,
endDate: dateRange[1] || undefined,
fullSearch: logSearchTerm,
userIds: selectedUsers,
appIds: selectedApps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
const activateLicenseKey = async () => {
try {
await API.activateLicenseKey({ licenseKey })
await API.activateLicenseKey(licenseKey)
await auth.getSelf()
await getLicenseKey()
notifications.success("Successfully activated")
Expand Down Expand Up @@ -119,7 +119,7 @@
async function activateOfflineLicense(offlineLicenseToken) {
try {
await API.activateOfflineLicense({ offlineLicenseToken })
await API.activateOfflineLicense(offlineLicenseToken)
await auth.getSelf()
await getOfflineLicense()
notifications.success("Successfully activated")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,7 @@
if (image) {
let data = new FormData()
data.append("file", image)
await API.uploadOIDCLogo({
name: image.name,
data,
})
await API.uploadOIDCLogo(image.name, data)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,7 @@
async function deleteSmtp() {
// Delete the SMTP config
try {
await API.deleteConfig({
id: smtpConfig._id,
rev: smtpConfig._rev,
})
await API.deleteConfig(smtpConfig._id, smtpConfig._rev)
smtpConfig = {
type: ConfigTypes.SMTP,
config: {
Expand Down
15 changes: 3 additions & 12 deletions packages/builder/src/stores/builder/automations.js
Original file line number Diff line number Diff line change
Expand Up @@ -751,10 +751,7 @@ const automationActions = store => ({
automation.definition.trigger.inputs.rowActionId
)
} else {
await API.deleteAutomation({
automationId: automation?._id,
automationRev: automation?._rev,
})
await API.deleteAutomation(automation?._id, automation?._rev)
}

store.update(state => {
Expand Down Expand Up @@ -836,10 +833,7 @@ const automationActions = store => ({
test: async (automation, testData) => {
let result
try {
result = await API.testAutomation({
automationId: automation?._id,
testData,
})
result = await API.testAutomation(automation?._id, testData)
} catch (err) {
const message = err.message || err.status || JSON.stringify(err)
throw `Automation test failed - ${message}`
Expand Down Expand Up @@ -893,10 +887,7 @@ const automationActions = store => ({
})
},
clearLogErrors: async ({ automationId, appId } = {}) => {
return await API.clearAutomationLogErrors({
automationId,
appId,
})
return await API.clearAutomationLogErrors(automationId, appId)
},
addTestDataToAutomation: data => {
let newAutomation = cloneDeep(get(selectedAutomation).data)
Expand Down
7 changes: 2 additions & 5 deletions packages/builder/src/stores/builder/flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ export function createFlagsStore() {
set(flags)
},
updateFlag: async (flag, value) => {
await API.updateFlag({
flag,
value,
})
await API.updateFlag(flag, value)
await actions.fetch()
},
toggleUiFeature: async feature => {
await API.toggleUiFeature({ value: feature })
await API.toggleUiFeature(feature)
},
}

Expand Down
5 changes: 1 addition & 4 deletions packages/builder/src/stores/builder/layouts.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ export class LayoutStore extends BudiStore {
if (!layout?._id) {
return
}
await API.deleteLayout({
layoutId: layout._id,
layoutRev: layout._rev,
})
await API.deleteLayout(layout._id, layout._rev)
this.update(state => {
state.layouts = state.layouts.filter(x => x._id !== layout._id)
return state
Expand Down
5 changes: 1 addition & 4 deletions packages/builder/src/stores/builder/navigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ export class NavigationStore extends BudiStore {

async save(navigation) {
const appId = get(appStore).appId
const app = await API.saveAppMetadata({
appId,
metadata: { navigation },
})
const app = await API.saveAppMetadata(appId, { navigation })
this.syncAppNavigation(app.navigation)
}

Expand Down
12 changes: 2 additions & 10 deletions packages/builder/src/stores/builder/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,10 @@ export function createPermissionStore() {
return {
subscribe,
save: async ({ level, role, resource }) => {
return await API.updatePermissionForResource({
resourceId: resource,
roleId: role,
level,
})
return await API.updatePermissionForResource(resource, role, level)
},
remove: async ({ level, role, resource }) => {
return await API.removePermissionFromResource({
resourceId: resource,
roleId: role,
level,
})
return await API.removePermissionFromResource(resource, role, level)
},
forResource: async resourceId => {
return (await API.getPermissionForResource(resourceId)).permissions
Expand Down
10 changes: 2 additions & 8 deletions packages/builder/src/stores/builder/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ export function createQueriesStore() {
}

const importQueries = async ({ data, datasourceId }) => {
return await API.importQueries({
datasourceId,
data,
})
return await API.importQueries(datasourceId, data)
}

const select = id => {
Expand All @@ -87,10 +84,7 @@ export function createQueriesStore() {
}

const deleteQuery = async query => {
await API.deleteQuery({
queryId: query?._id,
queryRev: query?._rev,
})
await API.deleteQuery(query._id, query._rev)
store.update(state => {
state.list = state.list.filter(existing => existing._id !== query._id)
return state
Expand Down
Loading

0 comments on commit b7d7946

Please sign in to comment.