Add plugin upload to core API client, add response to plugin upload endpoint

This commit is contained in:
Andrew Kingston 2022-08-11 10:37:57 +01:00
parent b7116ccc18
commit f2ee01d58f
3 changed files with 26 additions and 1 deletions

View File

@ -24,6 +24,7 @@ import { buildSelfEndpoints } from "./self"
import { buildViewEndpoints } from "./views"
import { buildLicensingEndpoints } from "./licensing"
import { buildGroupsEndpoints } from "./groups"
import { buildPluginEndpoints } from "./plugins"
const defaultAPIClientConfig = {
/**
@ -243,5 +244,6 @@ export const createAPIClient = config => {
...buildSelfEndpoints(API),
...buildLicensingEndpoints(API),
...buildGroupsEndpoints(API),
...buildPluginEndpoints(API),
}
}

View File

@ -0,0 +1,13 @@
export const buildPluginEndpoints = API => ({
/**
* Uploads a plugin tarball bundle
* @param data the plugin tarball bundle to upload
*/
uploadPlugin: async data => {
return await API.post({
url: "/api/plugin/upload",
body: data,
json: false,
})
},
})

View File

@ -11,6 +11,7 @@ export async function upload(ctx: any) {
: [ctx.request.files.file]
const db = getGlobalDB()
try {
let docs = []
// can do single or multiple plugins
for (let plugin of plugins) {
const { metadata, directory } = await extractPluginTarball(plugin)
@ -42,15 +43,24 @@ export async function upload(ctx: any) {
`Plugin already exists: name: ${name}, version: ${version}`
)
}
await db.put({
const doc = {
_id: pluginId,
name,
version,
description,
...metadata,
jsUrl: `${bucketPath}${jsFileName}`,
}
const response = await db.put(doc)
docs.push({
...doc,
_rev: response.rev,
})
}
ctx.body = {
message: "Plugin(s) uploaded successfully",
plugins: docs,
}
} catch (err: any) {
const errMsg = err?.message ? err?.message : err
ctx.throw(400, `Failed to import plugin: ${errMsg}`)