Misc system APIs typing.

This commit is contained in:
mike12345567 2024-12-05 17:02:36 +00:00
parent 95b7b14ff6
commit 165ff4e768
16 changed files with 61 additions and 109 deletions

View File

@ -0,0 +1,4 @@
import { Account, AccountMetadata } from "../../../documents"
export interface SaveAccountRequest extends Account {}
export interface SaveAccountResponse extends AccountMetadata {}

View File

@ -1,8 +1,11 @@
export interface GetEnvironmentResponse {
multiTenancy: boolean
offlineMode: boolean
cloud: boolean
accountPortalUrl: string
baseUrl: string
accountPortalUrl?: string
disableAccountPortal: boolean
baseUrl?: string
isDev: boolean
maintenance: { type: string }[]
passwordMinLength?: string
}

View File

@ -1,3 +1,8 @@
export * from "./environment"
export * from "./status"
export * from "./ops"
export * from "./account"
export * from "./log"
export * from "./migration"
export * from "./restore"
export * from "./tenant"

View File

@ -0,0 +1 @@
export type GetLogResponse = Buffer

View File

@ -0,0 +1,5 @@
import { MigrationDefinition, MigrationOptions } from "../../../sdk"
export interface RunGlobalMigrationRequest extends MigrationOptions {}
export type FetchMigrationDefinitionsResponse = MigrationDefinition[]

View File

@ -0,0 +1,3 @@
export interface SystemRestoreResponse {
message: string
}

View File

@ -0,0 +1,3 @@
export interface GetTenantInfoResponse {
exists: boolean
}

View File

@ -1,53 +0,0 @@
import { tenancy, db as dbCore } from "@budibase/backend-core"
import { BBContext } from "@budibase/types"
export async function save(ctx: BBContext) {
const db = tenancy.getGlobalDB()
const workspaceDoc = ctx.request.body
// workspace does not exist yet
if (!workspaceDoc._id) {
workspaceDoc._id = dbCore.generateWorkspaceID()
}
try {
const response = await db.put(workspaceDoc)
ctx.body = {
_id: response.id,
_rev: response.rev,
}
} catch (err: any) {
ctx.throw(err.status, err)
}
}
export async function fetch(ctx: BBContext) {
const db = tenancy.getGlobalDB()
const response = await db.allDocs(
dbCore.getWorkspaceParams(undefined, {
include_docs: true,
})
)
ctx.body = response.rows.map(row => row.doc)
}
export async function find(ctx: BBContext) {
const db = tenancy.getGlobalDB()
try {
ctx.body = await db.get(ctx.params.id)
} catch (err: any) {
ctx.throw(err.status, err)
}
}
export async function destroy(ctx: BBContext) {
const db = tenancy.getGlobalDB()
const { id, rev } = ctx.params
try {
await db.remove(id, rev)
ctx.body = { message: "Workspace deleted successfully" }
} catch (err: any) {
ctx.throw(err.status, err)
}
}

View File

@ -1,7 +1,15 @@
import { Account, AccountMetadata, Ctx } from "@budibase/types"
import {
Account,
AccountMetadata,
Ctx,
SaveAccountRequest,
SaveAccountResponse,
} from "@budibase/types"
import * as accounts from "../../../sdk/accounts"
export const save = async (ctx: Ctx<Account, AccountMetadata>) => {
export const save = async (
ctx: Ctx<SaveAccountRequest, SaveAccountResponse>
) => {
const account = ctx.request.body as Account
let metadata: AccountMetadata = {
_id: accounts.metadata.formatAccountMetadataId(account.accountId),
@ -14,7 +22,7 @@ export const save = async (ctx: Ctx<Account, AccountMetadata>) => {
ctx.status = 200
}
export const destroy = async (ctx: any) => {
export const destroy = async (ctx: Ctx<void, void>) => {
const accountId = accounts.metadata.formatAccountMetadataId(
ctx.params.accountId
)

View File

@ -1,4 +1,4 @@
import { Ctx, MaintenanceType } from "@budibase/types"
import { Ctx, GetEnvironmentResponse, MaintenanceType } from "@budibase/types"
import env from "../../../environment"
import { env as coreEnv, db as dbCore } from "@budibase/backend-core"
import nodeFetch from "node-fetch"
@ -38,13 +38,13 @@ async function isSqsMissing() {
return !(await isSqsAvailable())
}
export const fetch = async (ctx: Ctx) => {
export const fetch = async (ctx: Ctx<void, GetEnvironmentResponse>) => {
ctx.body = {
multiTenancy: !!env.MULTI_TENANCY,
offlineMode: !!coreEnv.OFFLINE_MODE,
cloud: !env.SELF_HOSTED,
accountPortalUrl: env.ACCOUNT_PORTAL_URL,
disableAccountPortal: env.DISABLE_ACCOUNT_PORTAL,
disableAccountPortal: !!env.DISABLE_ACCOUNT_PORTAL,
baseUrl: env.PLATFORM_URL,
isDev: env.isDev() && !env.isTest(),
maintenance: [],

View File

@ -1,7 +1,7 @@
import { UserCtx } from "@budibase/types"
import { GetLogResponse, UserCtx } from "@budibase/types"
import { installation, logging } from "@budibase/backend-core"
export async function getLogs(ctx: UserCtx) {
export async function getLogs(ctx: UserCtx<void, GetLogResponse>) {
const logReadStream = logging.system.getLogReadStream()
const { installId } = await installation.getInstall()

View File

@ -1,13 +1,23 @@
import {
FetchMigrationDefinitionsResponse,
RunGlobalMigrationRequest,
UserCtx,
} from "@budibase/types"
const { migrate, MIGRATIONS } = require("../../../migrations")
export const runMigrations = async (ctx: any) => {
export const runMigrations = async (
ctx: UserCtx<RunGlobalMigrationRequest, void>
) => {
const options = ctx.request.body
// don't await as can take a while, just return
migrate(options)
ctx.status = 200
}
export const fetchDefinitions = async (ctx: any) => {
export const fetchDefinitions = async (
ctx: UserCtx<void, FetchMigrationDefinitionsResponse>
) => {
ctx.body = MIGRATIONS
ctx.status = 200
}

View File

@ -1,8 +1,10 @@
import env from "../../../environment"
import { BBContext } from "@budibase/types"
import { SystemRestoreResponse, UserCtx } from "@budibase/types"
import { cache } from "@budibase/backend-core"
export async function systemRestored(ctx: BBContext) {
export async function systemRestored(
ctx: UserCtx<void, SystemRestoreResponse>
) {
if (!env.SELF_HOSTED) {
ctx.throw(405, "This operation is not allowed in cloud.")
}

View File

@ -1,7 +1,7 @@
import { UserCtx } from "@budibase/types"
import { GetTenantInfoResponse, UserCtx } from "@budibase/types"
import * as tenantSdk from "../../../sdk/tenants"
export async function destroy(ctx: UserCtx) {
export async function destroy(ctx: UserCtx<void, void>) {
const user = ctx.user!
const tenantId = ctx.params.tenantId
@ -18,6 +18,6 @@ export async function destroy(ctx: UserCtx) {
}
}
export async function info(ctx: UserCtx) {
export async function info(ctx: UserCtx<void, GetTenantInfoResponse>) {
ctx.body = await tenantSdk.tenantInfo(ctx.params.tenantId)
}

View File

@ -1,37 +0,0 @@
import Router from "@koa/router"
import * as controller from "../../controllers/global/workspaces"
import { auth } from "@budibase/backend-core"
import Joi from "joi"
const router: Router = new Router()
function buildWorkspaceSaveValidation() {
// prettier-ignore
return auth.joiValidator.body(Joi.object({
_id: Joi.string().optional(),
_rev: Joi.string().optional(),
name: Joi.string().required(),
users: Joi.array().required(),
managers: Joi.array().required(),
roles: Joi.object({
default: Joi.string().optional(),
app: Joi.object()
.pattern(/.*/, Joi.string())
.required()
.unknown(true),
}).unknown(true).optional(),
}).required().unknown(true))
}
router
.post(
"/api/global/workspaces",
auth.adminOnly,
buildWorkspaceSaveValidation(),
controller.save
)
.delete("/api/global/workspaces/:id", auth.adminOnly, controller.destroy)
.get("/api/global/workspaces", controller.fetch)
.get("/api/global/workspaces/:id", controller.find)
export default router

View File

@ -2,7 +2,6 @@ import Router from "@koa/router"
import { api as pro } from "@budibase/pro"
import userRoutes from "./global/users"
import configRoutes from "./global/configs"
import workspaceRoutes from "./global/workspaces"
import templateRoutes from "./global/templates"
import emailRoutes from "./global/email"
import authRoutes from "./global/auth"
@ -24,7 +23,6 @@ export const routes: Router[] = [
configRoutes,
userRoutes,
pro.users,
workspaceRoutes,
authRoutes,
templateRoutes,
tenantsRoutes,