2021-08-05 10:59:08 +02:00
|
|
|
const { StaticDatabases } = require("@budibase/auth/db")
|
|
|
|
const { getGlobalDB } = require("@budibase/auth/tenancy")
|
|
|
|
|
|
|
|
const KEYS_DOC = StaticDatabases.GLOBAL.docs.apiKeys
|
|
|
|
|
|
|
|
async function getBuilderMainDoc() {
|
|
|
|
const db = getGlobalDB()
|
|
|
|
try {
|
|
|
|
return await db.get(KEYS_DOC)
|
|
|
|
} catch (err) {
|
|
|
|
// doesn't exist yet, nothing to get
|
|
|
|
return {
|
|
|
|
_id: KEYS_DOC,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function setBuilderMainDoc(doc) {
|
|
|
|
// make sure to override the ID
|
|
|
|
doc._id = KEYS_DOC
|
|
|
|
const db = getGlobalDB()
|
|
|
|
return db.put(doc)
|
|
|
|
}
|
2021-07-14 17:29:32 +02:00
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.fetch = async function (ctx) {
|
2021-03-22 17:39:11 +01:00
|
|
|
try {
|
2021-08-05 10:59:08 +02:00
|
|
|
const mainDoc = await getBuilderMainDoc()
|
2021-03-22 17:39:11 +01:00
|
|
|
ctx.body = mainDoc.apiKeys ? mainDoc.apiKeys : {}
|
|
|
|
} catch (err) {
|
|
|
|
/* istanbul ignore next */
|
|
|
|
ctx.throw(400, err)
|
2020-07-06 09:06:59 +02:00
|
|
|
}
|
2020-07-02 17:53:09 +02:00
|
|
|
}
|
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.update = async function (ctx) {
|
2021-03-22 17:39:11 +01:00
|
|
|
const key = ctx.params.key
|
2020-07-06 09:06:59 +02:00
|
|
|
const value = ctx.request.body.value
|
2020-10-28 21:35:06 +01:00
|
|
|
|
2021-03-22 17:39:11 +01:00
|
|
|
try {
|
2021-08-05 10:59:08 +02:00
|
|
|
const mainDoc = await getBuilderMainDoc()
|
2021-03-22 17:39:11 +01:00
|
|
|
if (mainDoc.apiKeys == null) {
|
|
|
|
mainDoc.apiKeys = {}
|
2020-07-06 09:06:59 +02:00
|
|
|
}
|
2021-03-22 17:39:11 +01:00
|
|
|
mainDoc.apiKeys[key] = value
|
2021-08-05 10:59:08 +02:00
|
|
|
const resp = await setBuilderMainDoc(mainDoc)
|
2021-03-22 17:39:11 +01:00
|
|
|
ctx.body = {
|
|
|
|
_id: resp.id,
|
|
|
|
_rev: resp.rev,
|
2020-07-06 09:06:59 +02:00
|
|
|
}
|
2021-03-22 17:39:11 +01:00
|
|
|
} catch (err) {
|
|
|
|
/* istanbul ignore next */
|
|
|
|
ctx.throw(400, err)
|
|
|
|
}
|
2020-07-02 17:53:09 +02:00
|
|
|
}
|