1
0
Fork 0
mirror of synced 2024-09-21 20:01:32 +12:00
budibase/packages/server/src/api/controllers/templates.ts

44 lines
1.2 KiB
TypeScript
Raw Normal View History

import nodeFetch from "node-fetch"
import { downloadTemplate as dlTemplate } from "../../utilities/fileSystem"
import env from "../../environment"
import { BBContext } from "@budibase/types"
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"
export async function fetch(ctx: BBContext) {
2022-05-19 21:56:51 +12:00
let type = env.TEMPLATE_REPOSITORY
let response,
error = false
try {
response = await nodeFetch(
`https://${DEFAULT_TEMPLATES_BUCKET}/manifest.json`
)
if (response.status !== 200) {
error = true
}
} catch (err) {
error = true
}
// if there is an error, simply return no templates
if (!error && response) {
const json = await response.json()
ctx.body = Object.values(json.templates[type])
} else {
ctx.body = []
}
2020-09-29 05:04:08 +13:00
}
// can't currently test this, have to ignore from coverage
/* istanbul ignore next */
export async function downloadTemplate(ctx: BBContext) {
2020-09-29 05:04:08 +13:00
const { type, name } = ctx.params
await dlTemplate(type, name)
2020-09-29 05:04:08 +13:00
ctx.body = {
message: `template ${type}:${name} downloaded successfully.`,
}
}