Skip to content

Commit

Permalink
Merge pull request #15182 from Budibase/chore/typing-updates
Browse files Browse the repository at this point in the history
API typing fixes
  • Loading branch information
mike12345567 authored Dec 13, 2024
2 parents 640008d + d20592e commit d1de3f1
Show file tree
Hide file tree
Showing 37 changed files with 165 additions and 82 deletions.
19 changes: 14 additions & 5 deletions packages/server/src/api/controllers/analytics.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { events, context } from "@budibase/backend-core"
import { context, events } from "@budibase/backend-core"
import {
AnalyticsEnabledResponse,
AnalyticsPingRequest,
AnalyticsPingResponse,
App,
PingSource,
Ctx,
AnalyticsEnabledResponse,
PingSource,
} from "@budibase/types"
import { DocumentType, isDevAppID } from "../../db/utils"

Expand All @@ -15,9 +16,12 @@ export const isEnabled = async (ctx: Ctx<void, AnalyticsEnabledResponse>) => {
}
}

export const ping = async (ctx: Ctx<AnalyticsPingRequest, void>) => {
export const ping = async (
ctx: Ctx<AnalyticsPingRequest, AnalyticsPingResponse>
) => {
const body = ctx.request.body

let pingType: PingSource | undefined
switch (body.source) {
case PingSource.APP: {
const db = context.getAppDB({ skip_setup: true })
Expand All @@ -29,13 +33,18 @@ export const ping = async (ctx: Ctx<AnalyticsPingRequest, void>) => {
} else {
await events.serve.servedApp(appInfo, body.timezone, body.embedded)
}
pingType = PingSource.APP
break
}
case PingSource.BUILDER: {
await events.serve.servedBuilder(body.timezone)
pingType = PingSource.BUILDER
break
}
}

ctx.status = 200
ctx.body = {
message: "pong",
source: pingType,
}
}
24 changes: 11 additions & 13 deletions packages/server/src/api/controllers/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ import {
ImportToUpdateAppRequest,
ImportToUpdateAppResponse,
SetRevertableAppVersionRequest,
AddAppSampleDataResponse,
UnpublishAppResponse,
SetRevertableAppVersionResponse,
} from "@budibase/types"
import { BASE_LAYOUT_PROP_IDS } from "../../constants/layouts"
import sdk from "../../sdk"
Expand Down Expand Up @@ -175,7 +178,9 @@ async function createInstance(appId: string, template: AppTemplate) {
return { _id: appId }
}

export const addSampleData = async (ctx: UserCtx<void, void>) => {
export const addSampleData = async (
ctx: UserCtx<void, AddAppSampleDataResponse>
) => {
const db = context.getAppDB()

try {
Expand All @@ -188,7 +193,7 @@ export const addSampleData = async (ctx: UserCtx<void, void>) => {
await db.bulkDocs([...defaultDbDocs])
}

ctx.status = 200
ctx.body = { message: "Sample tables added." }
}

export async function fetch(ctx: UserCtx<void, FetchAppsResponse>) {
Expand Down Expand Up @@ -528,7 +533,6 @@ export async function create(
await appPostCreate(ctx, newApplication)
await cache.bustCache(cache.CacheKey.CHECKLIST)
ctx.body = newApplication
ctx.status = 200
}

// This endpoint currently operates as a PATCH rather than a PUT
Expand All @@ -551,7 +555,6 @@ export async function update(

const app = await updateAppPackage(ctx.request.body, ctx.params.appId)
await events.app.updated(app)
ctx.status = 200
ctx.body = app
builderSocket?.emitAppMetadataUpdate(ctx, {
theme: app.theme,
Expand Down Expand Up @@ -592,7 +595,6 @@ export async function updateClient(
}
const app = await updateAppPackage(appPackageUpdates, ctx.params.appId)
await events.app.versionUpdated(app, currentVersion, updatedToVersion)
ctx.status = 200
ctx.body = app
}

Expand Down Expand Up @@ -624,7 +626,6 @@ export async function revertClient(
}
const app = await updateAppPackage(appPackageUpdates, ctx.params.appId)
await events.app.versionReverted(app, currentVersion, revertedToVersion)
ctx.status = 200
ctx.body = app
}

Expand Down Expand Up @@ -689,11 +690,10 @@ export async function destroy(ctx: UserCtx<void, DeleteAppResponse>) {
await preDestroyApp(ctx)
const result = await destroyApp(ctx)
await postDestroyApp(ctx)
ctx.status = 200
ctx.body = result
}

export async function unpublish(ctx: UserCtx<void, void>) {
export async function unpublish(ctx: UserCtx<void, UnpublishAppResponse>) {
const prodAppId = dbCore.getProdAppID(ctx.params.appId)
const dbExists = await dbCore.dbExists(prodAppId)

Expand All @@ -705,8 +705,8 @@ export async function unpublish(ctx: UserCtx<void, void>) {
await preDestroyApp(ctx)
await unpublishApp(ctx)
await postDestroyApp(ctx)
ctx.status = 204
builderSocket?.emitAppUnpublish(ctx)
ctx.body = { message: "App unpublished." }
}

export async function sync(ctx: UserCtx<void, SyncAppResponse>) {
Expand Down Expand Up @@ -802,7 +802,6 @@ export async function duplicateApp(
duplicateAppId: newApplication?.appId,
sourceAppId,
}
ctx.status = 200
}

export async function updateAppPackage(
Expand Down Expand Up @@ -830,7 +829,7 @@ export async function updateAppPackage(
}

export async function setRevertableVersion(
ctx: UserCtx<SetRevertableAppVersionRequest, void>
ctx: UserCtx<SetRevertableAppVersionRequest, SetRevertableAppVersionResponse>
) {
if (!env.isDev()) {
ctx.status = 403
Expand All @@ -840,8 +839,7 @@ export async function setRevertableVersion(
const app = await sdk.applications.metadata.get()
app.revertableVersion = ctx.request.body.revertableVersion
await db.put(app)

ctx.status = 200
ctx.body = { message: "Revertable version updated." }
}

async function migrateAppNavigation() {
Expand Down
2 changes: 0 additions & 2 deletions packages/server/src/api/controllers/automation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export async function create(

const createdAutomation = await sdk.automations.create(automation)

ctx.status = 200
ctx.body = {
message: "Automation created successfully",
automation: createdAutomation,
Expand All @@ -84,7 +83,6 @@ export async function update(

const updatedAutomation = await sdk.automations.update(automation)

ctx.status = 200
ctx.body = {
message: `Automation ${automation._id} updated successfully.`,
automation: updatedAutomation,
Expand Down
4 changes: 1 addition & 3 deletions packages/server/src/api/controllers/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ export async function update(
}
}

ctx.status = 200
ctx.message = "Datasource saved successfully."
ctx.body = {
datasource: await sdk.datasources.removeSecretSingle(datasource),
Expand Down Expand Up @@ -290,8 +289,7 @@ export async function destroy(ctx: UserCtx<void, DeleteDatasourceResponse>) {
await db.remove(datasourceId, ctx.params.revId)
await events.datasource.deleted(datasource)

ctx.message = `Datasource deleted.`
ctx.status = 200
ctx.body = { message: `Datasource deleted.` }
builderSocket?.emitDatasourceDeletion(ctx, datasourceId)
}

Expand Down
2 changes: 0 additions & 2 deletions packages/server/src/api/controllers/layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export async function save(
layout._rev = response.rev

ctx.body = layout
ctx.status = 200
}

export async function destroy(ctx: UserCtx<void, DeleteLayoutResponse>) {
Expand All @@ -51,5 +50,4 @@ export async function destroy(ctx: UserCtx<void, DeleteLayoutResponse>) {
await db.remove(layoutId, layoutRev)
await events.layout.deleted(layoutId)
ctx.body = { message: "Layout deleted successfully" }
ctx.status = 200
}
9 changes: 5 additions & 4 deletions packages/server/src/api/controllers/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@ import {
Ctx,
FetchOldMigrationResponse,
GetOldMigrationStatus,
RuneOldMigrationResponse,
RunOldMigrationRequest,
} from "@budibase/types"
import {
getAppMigrationVersion,
getLatestEnabledMigrationId,
} from "../../appMigrations"

export async function migrate(ctx: Ctx<RunOldMigrationRequest, void>) {
export async function migrate(
ctx: Ctx<RunOldMigrationRequest, RuneOldMigrationResponse>
) {
const options = ctx.request.body
// don't await as can take a while, just return
migrationImpl(options)
ctx.status = 200
ctx.body = { message: "Migration started." }
}

export async function fetchDefinitions(
ctx: Ctx<void, FetchOldMigrationResponse>
) {
ctx.body = MIGRATIONS
ctx.status = 200
}

export async function getMigrationStatus(
Expand All @@ -41,5 +43,4 @@ export async function getMigrationStatus(
!latestMigrationId || latestAppliedMigration >= latestMigrationId

ctx.body = { migrated }
ctx.status = 200
}
4 changes: 2 additions & 2 deletions packages/server/src/api/controllers/permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export async function getDependantResources(
export async function addPermission(ctx: UserCtx<void, AddPermissionResponse>) {
const params: AddPermissionRequest = ctx.params
await sdk.permissions.updatePermissionOnRole(params, PermissionUpdateType.ADD)
ctx.status = 200
ctx.body = { message: "Permission added." }
}

export async function removePermission(
Expand All @@ -110,5 +110,5 @@ export async function removePermission(
params,
PermissionUpdateType.REMOVE
)
ctx.status = 200
ctx.body = { message: "Permission removed." }
}
2 changes: 0 additions & 2 deletions packages/server/src/api/controllers/query/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ const _import = async (
...importResult,
datasourceId,
}
ctx.status = 200
}
export { _import as import }

Expand Down Expand Up @@ -455,6 +454,5 @@ export async function destroy(ctx: UserCtx<void, DeleteQueryResponse>) {
const datasource = await sdk.datasources.get(query.datasourceId)
await db.remove(ctx.params.queryId, ctx.params.revId)
ctx.body = { message: `Query deleted.` }
ctx.status = 200
await events.query.deleted(datasource, query)
}
3 changes: 1 addition & 2 deletions packages/server/src/api/controllers/role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,7 @@ export async function destroy(ctx: UserCtx<void, DeleteRoleResponse>) {
// clean up inherits
await removeRoleFromOthers(roleId)

ctx.message = `Role ${ctx.params.roleId} deleted successfully`
ctx.status = 200
ctx.body = { message: `Role ${ctx.params.roleId} deleted successfully` }
builderSocket?.emitRoleDeletion(ctx, role)
}

Expand Down
4 changes: 0 additions & 4 deletions packages/server/src/api/controllers/row/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ export async function patch(
if (!row) {
ctx.throw(404, "Row not found")
}
ctx.status = 200

ctx.eventEmitter?.emitRow({
eventName: EventType.ROW_UPDATE,
Expand Down Expand Up @@ -110,7 +109,6 @@ export const save = async (ctx: UserCtx<SaveRowRequest, SaveRowResponse>) => {
: await quotas.addRow(() =>
sdk.rows.save(sourceId, ctx.request.body, ctx.user?._id)
)
ctx.status = 200

ctx.eventEmitter?.emitRow({
eventName: EventType.ROW_SAVE,
Expand Down Expand Up @@ -223,7 +221,6 @@ async function deleteRow(ctx: UserCtx<DeleteRowRequest>) {

export async function destroy(ctx: UserCtx<DeleteRowRequest>) {
let response, row
ctx.status = 200

if (isDeleteRows(ctx.request.body)) {
response = await deleteRows(ctx)
Expand Down Expand Up @@ -275,7 +272,6 @@ export async function search(ctx: Ctx<SearchRowRequest, SearchRowResponse>) {
rows: undefined,
}

ctx.status = 200
ctx.body = await sdk.rows.search(searchParams)
}

Expand Down
12 changes: 9 additions & 3 deletions packages/server/src/api/controllers/rowAction/run.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { RowActionTriggerRequest, Ctx } from "@budibase/types"
import {
RowActionTriggerRequest,
Ctx,
RowActionTriggerResponse,
} from "@budibase/types"
import sdk from "../../../sdk"

export async function run(ctx: Ctx<RowActionTriggerRequest, void>) {
export async function run(
ctx: Ctx<RowActionTriggerRequest, RowActionTriggerResponse>
) {
const { tableId, actionId } = ctx.params
const { rowId } = ctx.request.body

await sdk.rowActions.run(tableId, actionId, rowId, ctx.user)
ctx.status = 200
ctx.body = { message: "Row action triggered." }
}
1 change: 0 additions & 1 deletion packages/server/src/api/controllers/screen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ export async function destroy(ctx: UserCtx<void, DeleteScreenResponse>) {
ctx.body = {
message: "Screen deleted successfully",
}
ctx.status = 200
builderSocket?.emitScreenDeletion(ctx, id)
}

Expand Down
Loading

0 comments on commit d1de3f1

Please sign in to comment.