1
0
Fork 0
mirror of synced 2024-06-02 10:34:40 +12:00
budibase/packages/worker/src/api/routes/global/templates.ts

38 lines
1.3 KiB
TypeScript

import Router from "@koa/router"
import * as controller from "../../controllers/global/templates"
import { TemplatePurpose, TemplateTypes } from "../../../constants"
import { auth as authCore } from "@budibase/backend-core"
import Joi from "joi"
const { adminOnly, joiValidator } = authCore
const router = new Router()
function buildTemplateSaveValidation() {
// prettier-ignore
return joiValidator.body(Joi.object({
_id: Joi.string().allow(null, ""),
_rev: Joi.string().allow(null, ""),
ownerId: Joi.string().allow(null, ""),
name: Joi.string().allow(null, ""),
contents: Joi.string().required(),
purpose: Joi.string().required().valid(...Object.values(TemplatePurpose)),
type: Joi.string().required().valid(...Object.values(TemplateTypes)),
}).required().unknown(true).optional())
}
router
.get("/api/global/template/definitions", controller.definitions)
.post(
"/api/global/template",
adminOnly,
buildTemplateSaveValidation(),
controller.save
)
.get("/api/global/template", controller.fetch)
.get("/api/global/template/:type", controller.fetchByType)
.get("/api/global/template/:ownerId", controller.fetchByOwner)
.get("/api/global/template/:id", controller.find)
.delete("/api/global/template/:id/:rev", adminOnly, controller.destroy)
export default router