budibase/packages/server/middleware/routes/components.js

48 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-04-03 18:35:20 +02:00
const Router = require("@koa/router");
2020-04-03 17:15:53 +02:00
const send = require("koa-send")
2020-04-06 15:05:57 +02:00
const StatusCodes = require("../../utilities/statusCodes")
2020-04-03 17:15:53 +02:00
const {
getComponentDefinitions,
componentLibraryInfo,
} = require("../../utilities/builder")
const router = Router();
2020-04-06 15:05:57 +02:00
router.get("/_builder/:appname/componentlibrary", async ctx => {
const info = await componentLibraryInfo(
ctx.config,
ctx.params.appname,
ctx.query.lib
)
await send(ctx, info.components._lib || "index.js", { root: info.libDir })
})
2020-04-03 17:15:53 +02:00
router.get("/_builder/api/:appname/components", async ctx => {
try {
ctx.body = getComponentDefinitions(
2020-04-06 10:09:48 +02:00
ctx.config,
2020-04-03 17:15:53 +02:00
ctx.params.appname,
ctx.query.lib
)
ctx.response.status = StatusCodes.OK
} catch (e) {
if (e.status) {
ctx.response.status = e.status
} else {
throw e
}
}
})
router.get("/_builder/api/:appname/componentlibrary", async ctx => {
const info = await componentLibraryInfo(
2020-04-06 10:09:48 +02:00
ctx.config,
2020-04-03 17:15:53 +02:00
ctx.params.appname,
ctx.query.lib ? decodeURI(ctx.query.lib) : ""
)
ctx.body = info.components
ctx.response.status = StatusCodes.OK
})
module.exports = router