Template API typing.
This commit is contained in:
parent
cdabe324ad
commit
30717dae6c
|
@ -8,3 +8,4 @@ export * from "./oldMigration"
|
||||||
export * from "./email"
|
export * from "./email"
|
||||||
export * from "./role"
|
export * from "./role"
|
||||||
export * from "./self"
|
export * from "./self"
|
||||||
|
export * from "./template"
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -3,10 +3,25 @@ import {
|
||||||
TemplateBindings,
|
TemplateBindings,
|
||||||
GLOBAL_OWNER,
|
GLOBAL_OWNER,
|
||||||
} from "../../../constants"
|
} from "../../../constants"
|
||||||
import { getTemplates } from "../../../constants/templates"
|
import { getTemplateByID, getTemplates } from "../../../constants/templates"
|
||||||
import { tenancy, db as dbCore } from "@budibase/backend-core"
|
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()
|
const db = tenancy.getGlobalDB()
|
||||||
let template = ctx.request.body
|
let template = ctx.request.body
|
||||||
if (!template.ownerId) {
|
if (!template.ownerId) {
|
||||||
|
@ -23,9 +38,11 @@ export async function save(ctx: any) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function definitions(ctx: any) {
|
export async function definitions(
|
||||||
const bindings: any = {}
|
ctx: UserCtx<void, FetchTemplateDefinitionResponse>
|
||||||
const info: any = {}
|
) {
|
||||||
|
const bindings: Record<string, TemplateBinding[]> = {}
|
||||||
|
const info: Record<string, TemplateDefinition> = {}
|
||||||
for (let template of TemplateMetadata.email) {
|
for (let template of TemplateMetadata.email) {
|
||||||
bindings[template.purpose] = template.bindings
|
bindings[template.purpose] = template.bindings
|
||||||
info[template.purpose] = {
|
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()
|
ctx.body = await getTemplates()
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchByType(ctx: any) {
|
export async function fetchByType(
|
||||||
// @ts-ignore
|
ctx: UserCtx<void, FetchTemplateByTypeResponse>
|
||||||
|
) {
|
||||||
ctx.body = await getTemplates({
|
ctx.body = await getTemplates({
|
||||||
type: ctx.params.type,
|
type: ctx.params.type,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchByOwner(ctx: any) {
|
export async function fetchByOwner(
|
||||||
|
ctx: UserCtx<void, FetchTemplateByOwnerIDResponse>
|
||||||
|
) {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
ctx.body = await getTemplates({
|
ctx.body = await getTemplates({
|
||||||
ownerId: ctx.params.ownerId,
|
ownerId: ctx.params.ownerId,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function find(ctx: any) {
|
export async function find(ctx: UserCtx<void, FindTemplateResponse>) {
|
||||||
// @ts-ignore
|
ctx.body = await getTemplateByID(ctx.params.id)
|
||||||
ctx.body = await getTemplates({
|
|
||||||
id: ctx.params.id,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function destroy(ctx: any) {
|
export async function destroy(ctx: UserCtx<void, DeleteTemplateResponse>) {
|
||||||
const db = tenancy.getGlobalDB()
|
const db = tenancy.getGlobalDB()
|
||||||
await db.remove(ctx.params.id, ctx.params.rev)
|
await db.remove(ctx.params.id, ctx.params.rev)
|
||||||
ctx.message = `Template ${ctx.params.id} deleted.`
|
ctx.body = { message: `Template ${ctx.params.id} deleted.` }
|
||||||
ctx.status = 200
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,8 +48,21 @@ export function addBaseTemplates(templates: Template[], type?: string) {
|
||||||
export async function getTemplates({
|
export async function getTemplates({
|
||||||
ownerId,
|
ownerId,
|
||||||
type,
|
type,
|
||||||
id,
|
}: { ownerId?: string; type?: string } = {}) {
|
||||||
}: { ownerId?: string; type?: string; id?: 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 db = tenancy.getGlobalDB()
|
||||||
const response = await db.allDocs<Template>(
|
const response = await db.allDocs<Template>(
|
||||||
dbCore.getTemplateParams(ownerId || GLOBAL_OWNER, id, {
|
dbCore.getTemplateParams(ownerId || GLOBAL_OWNER, id, {
|
||||||
|
@ -58,16 +71,10 @@ export async function getTemplates({
|
||||||
)
|
)
|
||||||
let templates = response.rows.map(row => row.doc!)
|
let templates = response.rows.map(row => row.doc!)
|
||||||
// should only be one template with ID
|
// should only be one template with ID
|
||||||
if (id) {
|
return templates[0]
|
||||||
return templates[0]
|
|
||||||
}
|
|
||||||
if (type) {
|
|
||||||
templates = templates.filter(template => template.type === type)
|
|
||||||
}
|
|
||||||
return addBaseTemplates(templates, type)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getTemplateByPurpose(type: string, purpose: string) {
|
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)
|
return templates.find((template: Template) => template.purpose === purpose)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue