1
0
Fork 0
mirror of synced 2024-06-30 03:50:37 +12:00
budibase/packages/server/src/api/controllers/templates.js

55 lines
1.3 KiB
JavaScript
Raw Normal View History

2020-09-29 05:04:08 +13:00
const fetch = require("node-fetch")
const {
downloadTemplate,
exportTemplateFromApp,
getLocalTemplates,
2020-09-29 05:04:08 +13:00
} = require("../../utilities/templates")
const env = require("../../environment")
2020-09-29 05:04:08 +13:00
// development flag, can be used to test against templates exported locally
2020-09-29 05:04:08 +13:00
const DEFAULT_TEMPLATES_BUCKET =
"prod-budi-templates.s3-eu-west-1.amazonaws.com"
exports.fetch = async function(ctx) {
const { type = "app" } = ctx.query
if (env.LOCAL_TEMPLATES) {
ctx.body = Object.values(getLocalTemplates()[type])
} else {
const response = await fetch(
`https://${DEFAULT_TEMPLATES_BUCKET}/manifest.json`
)
const json = await response.json()
ctx.body = Object.values(json.templates[type])
}
2020-09-29 05:04:08 +13:00
}
// can't currently test this, have to ignore from coverage
/* istanbul ignore next */
2020-09-29 05:04:08 +13:00
exports.downloadTemplate = async function(ctx) {
const { type, name } = ctx.params
if (!env.LOCAL_TEMPLATES) {
await downloadTemplate(type, name)
}
2020-09-29 05:04:08 +13:00
ctx.body = {
message: `template ${type}:${name} downloaded successfully.`,
}
}
exports.exportTemplateFromApp = async function(ctx) {
const { appId } = ctx.user
2020-09-29 05:04:08 +13:00
const { templateName } = ctx.request.body
await exportTemplateFromApp({
appId,
templateName,
})
ctx.status = 200
ctx.body = {
message: `Created template: ${templateName}`,
}
}