Routing API typing.
This commit is contained in:
parent
23d9808cb6
commit
efcdd360e3
|
@ -9,7 +9,7 @@ import { getUserMetadataParams, InternalTables } from "../../db/utils"
|
||||||
import {
|
import {
|
||||||
AccessibleRolesResponse,
|
AccessibleRolesResponse,
|
||||||
Database,
|
Database,
|
||||||
DestroyRoleResponse,
|
DeleteRoleResponse,
|
||||||
FetchRolesResponse,
|
FetchRolesResponse,
|
||||||
FindRoleResponse,
|
FindRoleResponse,
|
||||||
Role,
|
Role,
|
||||||
|
@ -199,7 +199,7 @@ export async function save(ctx: UserCtx<SaveRoleRequest, SaveRoleResponse>) {
|
||||||
builderSocket?.emitRoleUpdate(ctx, role)
|
builderSocket?.emitRoleUpdate(ctx, role)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function destroy(ctx: UserCtx<void, DestroyRoleResponse>) {
|
export async function destroy(ctx: UserCtx<void, DeleteRoleResponse>) {
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
let roleId = ctx.params.roleId as string
|
let roleId = ctx.params.roleId as string
|
||||||
if (roles.isBuiltin(roleId)) {
|
if (roles.isBuiltin(roleId)) {
|
||||||
|
|
|
@ -1,11 +1,17 @@
|
||||||
import { getRoutingInfo } from "../../utilities/routing"
|
import { getRoutingInfo } from "../../utilities/routing"
|
||||||
import { roles } from "@budibase/backend-core"
|
import { roles } from "@budibase/backend-core"
|
||||||
import { UserCtx } from "@budibase/types"
|
import {
|
||||||
|
FetchClientScreenRoutingResponse,
|
||||||
|
FetchScreenRoutingResponse,
|
||||||
|
ScreenRoutingJson,
|
||||||
|
UserCtx,
|
||||||
|
} from "@budibase/types"
|
||||||
|
|
||||||
const URL_SEPARATOR = "/"
|
const URL_SEPARATOR = "/"
|
||||||
|
|
||||||
class Routing {
|
class Routing {
|
||||||
json: any
|
json: ScreenRoutingJson
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.json = {}
|
this.json = {}
|
||||||
}
|
}
|
||||||
|
@ -43,7 +49,7 @@ class Routing {
|
||||||
* @returns The routing structure, this is the full structure designed for use in the builder,
|
* @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.
|
* 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 screenRoutes = await getRoutingInfo()
|
||||||
const routing = new Routing()
|
const routing = new Routing()
|
||||||
|
|
||||||
|
@ -56,11 +62,13 @@ async function getRoutingStructure() {
|
||||||
return { routes: routing.json }
|
return { routes: routing.json }
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetch(ctx: UserCtx) {
|
export async function fetch(ctx: UserCtx<void, FetchScreenRoutingResponse>) {
|
||||||
ctx.body = await getRoutingStructure()
|
ctx.body = await getRoutingStructure()
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function clientFetch(ctx: UserCtx) {
|
export async function clientFetch(
|
||||||
|
ctx: UserCtx<void, FetchClientScreenRoutingResponse>
|
||||||
|
) {
|
||||||
const routing = await getRoutingStructure()
|
const routing = await getRoutingStructure()
|
||||||
let roleId = ctx.user?.role?._id
|
let roleId = ctx.user?.role?._id
|
||||||
const roleIds = roleId ? await roles.getUserRoleIdHierarchy(roleId) : []
|
const roleIds = roleId ? await roles.getUserRoleIdHierarchy(roleId) : []
|
||||||
|
|
|
@ -1,24 +1,19 @@
|
||||||
import { createRoutingView } from "../../db/views/staticViews"
|
import { createRoutingView } from "../../db/views/staticViews"
|
||||||
import { ViewName, getQueryIndex, UNICODE_MAX } from "../../db/utils"
|
import { ViewName, getQueryIndex, UNICODE_MAX } from "../../db/utils"
|
||||||
import { context } from "@budibase/backend-core"
|
import { context } from "@budibase/backend-core"
|
||||||
import { ScreenRouting, Document } from "@budibase/types"
|
import { ScreenRoutesViewOutput } from "@budibase/types"
|
||||||
|
|
||||||
interface ScreenRoutesView extends Document {
|
export async function getRoutingInfo(): Promise<ScreenRoutesViewOutput[]> {
|
||||||
id: string
|
|
||||||
routing: ScreenRouting
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getRoutingInfo(): Promise<ScreenRoutesView[]> {
|
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
try {
|
try {
|
||||||
const allRouting = await db.query<ScreenRoutesView>(
|
const allRouting = await db.query<ScreenRoutesViewOutput>(
|
||||||
getQueryIndex(ViewName.ROUTING),
|
getQueryIndex(ViewName.ROUTING),
|
||||||
{
|
{
|
||||||
startkey: "",
|
startkey: "",
|
||||||
endkey: UNICODE_MAX,
|
endkey: UNICODE_MAX,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
return allRouting.rows.map(row => row.value as ScreenRoutesView)
|
return allRouting.rows.map(row => row.value)
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
// check if the view doesn't exist, it should for all new instances
|
// check if the view doesn't exist, it should for all new instances
|
||||||
/* istanbul ignore next */
|
/* istanbul ignore next */
|
||||||
|
|
|
@ -13,3 +13,4 @@ export * from "./component"
|
||||||
export * from "./integration"
|
export * from "./integration"
|
||||||
export * from "./metadata"
|
export * from "./metadata"
|
||||||
export * from "./query"
|
export * from "./query"
|
||||||
|
export * from "./screen"
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
import { ScreenRoutingJson } from "../../../documents"
|
||||||
|
|
||||||
|
export interface FetchScreenRoutingResponse {
|
||||||
|
routes: ScreenRoutingJson
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface FetchClientScreenRoutingResponse
|
||||||
|
extends FetchScreenRoutingResponse {}
|
|
@ -18,7 +18,7 @@ export interface FindRoleResponse extends Role {}
|
||||||
|
|
||||||
export type FetchRolesResponse = Role[]
|
export type FetchRolesResponse = Role[]
|
||||||
|
|
||||||
export interface DestroyRoleResponse {
|
export interface DeleteRoleResponse {
|
||||||
message: string
|
message: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,3 +24,20 @@ export interface Screen extends Document {
|
||||||
name?: string
|
name?: string
|
||||||
pluginAdded?: boolean
|
pluginAdded?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ScreenRoutesViewOutput extends Document {
|
||||||
|
id: string
|
||||||
|
routing: ScreenRouting
|
||||||
|
}
|
||||||
|
|
||||||
|
export type ScreenRoutingJson = Record<
|
||||||
|
string,
|
||||||
|
{
|
||||||
|
subpaths: Record<
|
||||||
|
string,
|
||||||
|
{
|
||||||
|
screens: Record<string, string>
|
||||||
|
}
|
||||||
|
>
|
||||||
|
}
|
||||||
|
>
|
||||||
|
|
Loading…
Reference in New Issue