2022-03-22 01:23:22 +01:00
|
|
|
import Router from "@koa/router"
|
2022-03-20 02:13:54 +01:00
|
|
|
import * as controller from "../controllers/static"
|
|
|
|
import { budibaseTempDir } from "../../utilities/budibaseDir"
|
|
|
|
import authorized from "../../middleware/authorized"
|
2022-11-22 19:49:19 +01:00
|
|
|
import { permissions } from "@budibase/backend-core"
|
2022-03-20 02:13:54 +01:00
|
|
|
import * as env from "../../environment"
|
|
|
|
import { paramResource } from "../../middleware/resourceId"
|
2022-11-22 19:49:19 +01:00
|
|
|
const { BUILDER, PermissionType, PermissionLevel } = permissions
|
2020-04-14 16:14:57 +02:00
|
|
|
|
2022-11-21 19:33:34 +01:00
|
|
|
const router: Router = new Router()
|
2020-04-14 16:14:57 +02:00
|
|
|
|
2021-03-10 18:55:42 +01:00
|
|
|
/* istanbul ignore next */
|
2022-03-22 01:23:22 +01:00
|
|
|
router.param("file", async (file: any, ctx: any, next: any) => {
|
2020-05-18 07:40:29 +02:00
|
|
|
ctx.file = file && file.includes(".") ? file : "index.html"
|
2021-03-25 17:08:09 +01:00
|
|
|
if (!ctx.file.startsWith("budibase-client")) {
|
|
|
|
return next()
|
|
|
|
}
|
|
|
|
// test serves from require
|
|
|
|
if (env.isTest()) {
|
|
|
|
ctx.devPath = require.resolve("@budibase/client").split(ctx.file)[0]
|
|
|
|
} else if (env.isDev()) {
|
|
|
|
// Serving the client library from your local dir in dev
|
2020-05-18 07:40:29 +02:00
|
|
|
ctx.devPath = budibaseTempDir()
|
|
|
|
}
|
2021-03-25 17:08:09 +01:00
|
|
|
return next()
|
2020-05-18 07:40:29 +02:00
|
|
|
})
|
2020-05-06 13:17:15 +02:00
|
|
|
|
2021-04-01 15:11:58 +02:00
|
|
|
// only used in development for retrieving the client library,
|
|
|
|
// in production the client lib is always stored in the object store.
|
2021-04-01 13:48:38 +02:00
|
|
|
if (env.isDev()) {
|
2021-04-01 15:11:58 +02:00
|
|
|
router.get("/api/assets/client", controller.serveClientLibrary)
|
2021-04-01 13:48:38 +02:00
|
|
|
}
|
|
|
|
|
2020-05-18 07:40:29 +02:00
|
|
|
router
|
2021-03-31 20:55:55 +02:00
|
|
|
// TODO: for now this builder endpoint is not authorized/secured, will need to be
|
|
|
|
.get("/builder/:file*", controller.serveBuilder)
|
2021-03-15 13:10:21 +01:00
|
|
|
.post("/api/attachments/process", authorized(BUILDER), controller.uploadFile)
|
2022-08-15 16:46:55 +02:00
|
|
|
.post(
|
|
|
|
"/api/attachments/delete",
|
|
|
|
authorized(BUILDER),
|
|
|
|
controller.deleteObjects
|
|
|
|
)
|
2022-06-07 00:30:36 +02:00
|
|
|
.post("/api/beta/:feature", controller.toggleBetaUiFeature)
|
2021-04-01 15:38:31 +02:00
|
|
|
.post(
|
2021-06-08 13:50:58 +02:00
|
|
|
"/api/attachments/:tableId/upload",
|
|
|
|
paramResource("tableId"),
|
2022-11-17 15:59:18 +01:00
|
|
|
authorized(PermissionType.TABLE, PermissionLevel.WRITE),
|
2021-04-01 15:38:31 +02:00
|
|
|
controller.uploadFile
|
|
|
|
)
|
2022-08-12 12:29:57 +02:00
|
|
|
.post(
|
|
|
|
"/api/attachments/:tableId/delete",
|
|
|
|
paramResource("tableId"),
|
2022-11-17 15:59:18 +01:00
|
|
|
authorized(PermissionType.TABLE, PermissionLevel.WRITE),
|
2022-08-12 12:29:57 +02:00
|
|
|
controller.deleteObjects
|
|
|
|
)
|
2022-09-07 11:40:00 +02:00
|
|
|
.get("/app/preview", authorized(BUILDER), controller.serveBuilderPreview)
|
2020-06-12 21:42:55 +02:00
|
|
|
.get("/:appId/:path*", controller.serveApp)
|
2022-03-23 17:45:06 +01:00
|
|
|
.get("/app/:appUrl/:path*", controller.serveApp)
|
2022-01-13 18:18:24 +01:00
|
|
|
.post(
|
|
|
|
"/api/attachments/:datasourceId/url",
|
2022-11-17 15:59:18 +01:00
|
|
|
authorized(PermissionType.TABLE, PermissionLevel.READ),
|
2022-01-13 18:18:24 +01:00
|
|
|
controller.getSignedUploadURL
|
|
|
|
)
|
2020-04-14 16:14:57 +02:00
|
|
|
|
2022-11-22 19:49:19 +01:00
|
|
|
export = router
|