From f2ee01d58fbe1f260c7a171cea0b316f244be319 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Thu, 11 Aug 2022 10:37:57 +0100 Subject: [PATCH] Add plugin upload to core API client, add response to plugin upload endpoint --- packages/frontend-core/src/api/index.js | 2 ++ packages/frontend-core/src/api/plugins.js | 13 +++++++++++++ packages/server/src/api/controllers/plugin.ts | 12 +++++++++++- 3 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 packages/frontend-core/src/api/plugins.js diff --git a/packages/frontend-core/src/api/index.js b/packages/frontend-core/src/api/index.js index 1db3b0b2f8..3b9bb5b57e 100644 --- a/packages/frontend-core/src/api/index.js +++ b/packages/frontend-core/src/api/index.js @@ -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), } } diff --git a/packages/frontend-core/src/api/plugins.js b/packages/frontend-core/src/api/plugins.js new file mode 100644 index 0000000000..14b764d453 --- /dev/null +++ b/packages/frontend-core/src/api/plugins.js @@ -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, + }) + }, +}) diff --git a/packages/server/src/api/controllers/plugin.ts b/packages/server/src/api/controllers/plugin.ts index d2e2cf1507..f742c7f10c 100644 --- a/packages/server/src/api/controllers/plugin.ts +++ b/packages/server/src/api/controllers/plugin.ts @@ -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}`)