2020-09-29 05:04:08 +13:00
|
|
|
const fetch = require("node-fetch")
|
2021-03-20 08:07:47 +13:00
|
|
|
const { downloadTemplate } = require("../../utilities/fileSystem")
|
2020-09-29 05:04:08 +13:00
|
|
|
|
2020-11-07 01:30:30 +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"
|
|
|
|
|
2021-05-03 19:31:09 +12:00
|
|
|
exports.fetch = async function (ctx) {
|
2020-09-29 05:04:08 +13:00
|
|
|
const { type = "app" } = ctx.query
|
2021-10-15 02:18:41 +13:00
|
|
|
let response,
|
|
|
|
error = false
|
|
|
|
try {
|
|
|
|
response = await fetch(`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
|
|
|
}
|
|
|
|
|
2021-03-10 05:07:44 +13:00
|
|
|
// can't currently test this, have to ignore from coverage
|
|
|
|
/* istanbul ignore next */
|
2021-05-03 19:31:09 +12:00
|
|
|
exports.downloadTemplate = async function (ctx) {
|
2020-09-29 05:04:08 +13:00
|
|
|
const { type, name } = ctx.params
|
|
|
|
|
2021-03-20 08:07:47 +13:00
|
|
|
await downloadTemplate(type, name)
|
2020-09-29 05:04:08 +13:00
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
message: `template ${type}:${name} downloaded successfully.`,
|
|
|
|
}
|
|
|
|
}
|