Template API typing.

This commit is contained in:
mike12345567 2024-12-05 16:26:57 +00:00
parent cdabe324ad
commit 30717dae6c
4 changed files with 81 additions and 27 deletions

View File

@ -8,3 +8,4 @@ export * from "./oldMigration"
export * from "./email"
export * from "./role"
export * from "./self"
export * from "./template"

View File

@ -0,0 +1,30 @@
import { Document, Template } from "../../../documents"
export interface TemplateDefinition {
name: string
description: string
category: string
}
export interface TemplateBinding {
name: string
description: string
}
export interface FetchTemplateDefinitionResponse {
info: Record<string, TemplateDefinition>
bindings: Record<string, TemplateBinding[]>
}
export interface SaveTemplateRequest extends Template {}
export interface SaveTemplateResponse extends Template {}
export type FetchTemplateResponse = Template[]
export type FetchTemplateByTypeResponse = Template[]
export type FetchTemplateByOwnerIDResponse = Template[]
export interface FindTemplateResponse extends Template {}
export interface DeleteTemplateResponse {
message: string
}

View File

@ -3,10 +3,25 @@ import {
TemplateBindings,
GLOBAL_OWNER,
} from "../../../constants"
import { getTemplates } from "../../../constants/templates"
import { getTemplateByID, getTemplates } from "../../../constants/templates"
import { tenancy, db as dbCore } from "@budibase/backend-core"
import {
DeleteTemplateResponse,
FetchTemplateByOwnerIDResponse,
FetchTemplateByTypeResponse,
FetchTemplateDefinitionResponse,
FetchTemplateResponse,
FindTemplateResponse,
SaveTemplateRequest,
SaveTemplateResponse,
TemplateBinding,
TemplateDefinition,
UserCtx,
} from "@budibase/types"
export async function save(ctx: any) {
export async function save(
ctx: UserCtx<SaveTemplateRequest, SaveTemplateResponse>
) {
const db = tenancy.getGlobalDB()
let template = ctx.request.body
if (!template.ownerId) {
@ -23,9 +38,11 @@ export async function save(ctx: any) {
}
}
export async function definitions(ctx: any) {
const bindings: any = {}
const info: any = {}
export async function definitions(
ctx: UserCtx<void, FetchTemplateDefinitionResponse>
) {
const bindings: Record<string, TemplateBinding[]> = {}
const info: Record<string, TemplateDefinition> = {}
for (let template of TemplateMetadata.email) {
bindings[template.purpose] = template.bindings
info[template.purpose] = {
@ -44,34 +61,33 @@ export async function definitions(ctx: any) {
}
}
export async function fetch(ctx: any) {
export async function fetch(ctx: UserCtx<void, FetchTemplateResponse>) {
ctx.body = await getTemplates()
}
export async function fetchByType(ctx: any) {
// @ts-ignore
export async function fetchByType(
ctx: UserCtx<void, FetchTemplateByTypeResponse>
) {
ctx.body = await getTemplates({
type: ctx.params.type,
})
}
export async function fetchByOwner(ctx: any) {
export async function fetchByOwner(
ctx: UserCtx<void, FetchTemplateByOwnerIDResponse>
) {
// @ts-ignore
ctx.body = await getTemplates({
ownerId: ctx.params.ownerId,
})
}
export async function find(ctx: any) {
// @ts-ignore
ctx.body = await getTemplates({
id: ctx.params.id,
})
export async function find(ctx: UserCtx<void, FindTemplateResponse>) {
ctx.body = await getTemplateByID(ctx.params.id)
}
export async function destroy(ctx: any) {
export async function destroy(ctx: UserCtx<void, DeleteTemplateResponse>) {
const db = tenancy.getGlobalDB()
await db.remove(ctx.params.id, ctx.params.rev)
ctx.message = `Template ${ctx.params.id} deleted.`
ctx.status = 200
ctx.body = { message: `Template ${ctx.params.id} deleted.` }
}

View File

@ -48,8 +48,21 @@ export function addBaseTemplates(templates: Template[], type?: string) {
export async function getTemplates({
ownerId,
type,
id,
}: { ownerId?: string; type?: string; id?: string } = {}) {
}: { ownerId?: string; type?: string } = {}) {
const db = tenancy.getGlobalDB()
const response = await db.allDocs<Template>(
dbCore.getTemplateParams(ownerId || GLOBAL_OWNER, undefined, {
include_docs: true,
})
)
let templates = response.rows.map(row => row.doc!)
if (type) {
templates = templates.filter(template => template.type === type)
}
return addBaseTemplates(templates, type)
}
export async function getTemplateByID(id: string, ownerId?: string) {
const db = tenancy.getGlobalDB()
const response = await db.allDocs<Template>(
dbCore.getTemplateParams(ownerId || GLOBAL_OWNER, id, {
@ -58,16 +71,10 @@ export async function getTemplates({
)
let templates = response.rows.map(row => row.doc!)
// should only be one template with ID
if (id) {
return templates[0]
}
if (type) {
templates = templates.filter(template => template.type === type)
}
return addBaseTemplates(templates, type)
return templates[0]
}
export async function getTemplateByPurpose(type: string, purpose: string) {
const templates = (await getTemplates({ type })) as Template[]
const templates = await getTemplates({ type })
return templates.find((template: Template) => template.purpose === purpose)
}