From efcdd360e3d02b3992ae796fa8d16ecab73d4426 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Tue, 3 Dec 2024 17:42:31 +0000 Subject: [PATCH] Routing API typing. --- packages/server/src/api/controllers/role.ts | 4 ++-- packages/server/src/api/controllers/routing.ts | 18 +++++++++++++----- packages/server/src/utilities/routing/index.ts | 13 ++++--------- packages/types/src/api/web/app/index.ts | 1 + packages/types/src/api/web/app/screen.ts | 8 ++++++++ packages/types/src/api/web/role.ts | 2 +- packages/types/src/documents/app/screen.ts | 17 +++++++++++++++++ 7 files changed, 46 insertions(+), 17 deletions(-) create mode 100644 packages/types/src/api/web/app/screen.ts diff --git a/packages/server/src/api/controllers/role.ts b/packages/server/src/api/controllers/role.ts index 7f5ac6b92f..30ca70b505 100644 --- a/packages/server/src/api/controllers/role.ts +++ b/packages/server/src/api/controllers/role.ts @@ -9,7 +9,7 @@ import { getUserMetadataParams, InternalTables } from "../../db/utils" import { AccessibleRolesResponse, Database, - DestroyRoleResponse, + DeleteRoleResponse, FetchRolesResponse, FindRoleResponse, Role, @@ -199,7 +199,7 @@ export async function save(ctx: UserCtx) { builderSocket?.emitRoleUpdate(ctx, role) } -export async function destroy(ctx: UserCtx) { +export async function destroy(ctx: UserCtx) { const db = context.getAppDB() let roleId = ctx.params.roleId as string if (roles.isBuiltin(roleId)) { diff --git a/packages/server/src/api/controllers/routing.ts b/packages/server/src/api/controllers/routing.ts index 040cda4dd0..fc9e58def5 100644 --- a/packages/server/src/api/controllers/routing.ts +++ b/packages/server/src/api/controllers/routing.ts @@ -1,11 +1,17 @@ import { getRoutingInfo } from "../../utilities/routing" import { roles } from "@budibase/backend-core" -import { UserCtx } from "@budibase/types" +import { + FetchClientScreenRoutingResponse, + FetchScreenRoutingResponse, + ScreenRoutingJson, + UserCtx, +} from "@budibase/types" const URL_SEPARATOR = "/" class Routing { - json: any + json: ScreenRoutingJson + constructor() { this.json = {} } @@ -43,7 +49,7 @@ class Routing { * @returns The routing structure, this is the full structure designed for use in the builder, * if the client routing is required then the updateRoutingStructureForUserRole should be used. */ -async function getRoutingStructure() { +async function getRoutingStructure(): Promise<{ routes: ScreenRoutingJson }> { const screenRoutes = await getRoutingInfo() const routing = new Routing() @@ -56,11 +62,13 @@ async function getRoutingStructure() { return { routes: routing.json } } -export async function fetch(ctx: UserCtx) { +export async function fetch(ctx: UserCtx) { ctx.body = await getRoutingStructure() } -export async function clientFetch(ctx: UserCtx) { +export async function clientFetch( + ctx: UserCtx +) { const routing = await getRoutingStructure() let roleId = ctx.user?.role?._id const roleIds = roleId ? await roles.getUserRoleIdHierarchy(roleId) : [] diff --git a/packages/server/src/utilities/routing/index.ts b/packages/server/src/utilities/routing/index.ts index 82d45743ce..f0086daa72 100644 --- a/packages/server/src/utilities/routing/index.ts +++ b/packages/server/src/utilities/routing/index.ts @@ -1,24 +1,19 @@ import { createRoutingView } from "../../db/views/staticViews" import { ViewName, getQueryIndex, UNICODE_MAX } from "../../db/utils" import { context } from "@budibase/backend-core" -import { ScreenRouting, Document } from "@budibase/types" +import { ScreenRoutesViewOutput } from "@budibase/types" -interface ScreenRoutesView extends Document { - id: string - routing: ScreenRouting -} - -export async function getRoutingInfo(): Promise { +export async function getRoutingInfo(): Promise { const db = context.getAppDB() try { - const allRouting = await db.query( + const allRouting = await db.query( getQueryIndex(ViewName.ROUTING), { startkey: "", endkey: UNICODE_MAX, } ) - return allRouting.rows.map(row => row.value as ScreenRoutesView) + return allRouting.rows.map(row => row.value) } catch (err: any) { // check if the view doesn't exist, it should for all new instances /* istanbul ignore next */ diff --git a/packages/types/src/api/web/app/index.ts b/packages/types/src/api/web/app/index.ts index 82770543df..15c9c8929d 100644 --- a/packages/types/src/api/web/app/index.ts +++ b/packages/types/src/api/web/app/index.ts @@ -13,3 +13,4 @@ export * from "./component" export * from "./integration" export * from "./metadata" export * from "./query" +export * from "./screen" diff --git a/packages/types/src/api/web/app/screen.ts b/packages/types/src/api/web/app/screen.ts new file mode 100644 index 0000000000..fc9eb63975 --- /dev/null +++ b/packages/types/src/api/web/app/screen.ts @@ -0,0 +1,8 @@ +import { ScreenRoutingJson } from "../../../documents" + +export interface FetchScreenRoutingResponse { + routes: ScreenRoutingJson +} + +export interface FetchClientScreenRoutingResponse + extends FetchScreenRoutingResponse {} diff --git a/packages/types/src/api/web/role.ts b/packages/types/src/api/web/role.ts index f1c573c849..eeedaea163 100644 --- a/packages/types/src/api/web/role.ts +++ b/packages/types/src/api/web/role.ts @@ -18,7 +18,7 @@ export interface FindRoleResponse extends Role {} export type FetchRolesResponse = Role[] -export interface DestroyRoleResponse { +export interface DeleteRoleResponse { message: string } diff --git a/packages/types/src/documents/app/screen.ts b/packages/types/src/documents/app/screen.ts index 4977c79b0b..b2cedf31a9 100644 --- a/packages/types/src/documents/app/screen.ts +++ b/packages/types/src/documents/app/screen.ts @@ -24,3 +24,20 @@ export interface Screen extends Document { name?: string pluginAdded?: boolean } + +export interface ScreenRoutesViewOutput extends Document { + id: string + routing: ScreenRouting +} + +export type ScreenRoutingJson = Record< + string, + { + subpaths: Record< + string, + { + screens: Record + } + > + } +>