diff --git a/packages/server/src/api/controllers/plugin/file.ts b/packages/server/src/api/controllers/plugin/file.ts index 3b21f08387..0f40998617 100644 --- a/packages/server/src/api/controllers/plugin/file.ts +++ b/packages/server/src/api/controllers/plugin/file.ts @@ -3,8 +3,12 @@ import { getPluginMetadata, extractTarball, } from "../../../utilities/fileSystem" +import { FileType } from "@budibase/types" -export async function fileUpload(file: { name: string; path: string }) { +export async function fileUpload(file: FileType) { + if (!file.name || !file.path) { + throw new Error("File is not valid - cannot upload.") + } if (!file.name.endsWith(".tar.gz")) { throw new Error("Plugin must be compressed into a gzipped tarball.") } diff --git a/packages/server/src/api/controllers/plugin/index.ts b/packages/server/src/api/controllers/plugin/index.ts index e1c51f0219..2a4d69b671 100644 --- a/packages/server/src/api/controllers/plugin/index.ts +++ b/packages/server/src/api/controllers/plugin/index.ts @@ -2,26 +2,37 @@ import { npmUpload, urlUpload, githubUpload } from "./uploaders" import { plugins as pluginCore } from "@budibase/backend-core" import { PluginType, - FileType, PluginSource, - Ctx, CreatePluginRequest, CreatePluginResponse, + UserCtx, + UploadPluginRequest, + Plugin, + UploadPluginResponse, + FetchPluginResponse, + DeletePluginResponse, } from "@budibase/types" import env from "../../../environment" import { clientAppSocket } from "../../../websockets" import sdk from "../../../sdk" import { sdk as pro } from "@budibase/pro" -export async function upload(ctx: any) { - const plugins: FileType[] = - ctx.request.files.file.length > 1 - ? Array.from(ctx.request.files.file) - : [ctx.request.files.file] +export async function upload( + ctx: UserCtx +) { + const files = ctx.request.files + const plugins = + files && Array.isArray(files.file) && files.file.length > 1 + ? Array.from(files.file) + : [files?.file] + try { - let docs = [] + let docs: Plugin[] = [] // can do single or multiple plugins for (let plugin of plugins) { + if (!plugin || Array.isArray(plugin)) { + continue + } const doc = await sdk.plugins.processUploaded(plugin, PluginSource.FILE) docs.push(doc) } @@ -37,7 +48,7 @@ export async function upload(ctx: any) { } export async function create( - ctx: Ctx + ctx: UserCtx ) { const { source, url, headers, githubToken } = ctx.request.body @@ -91,11 +102,11 @@ export async function create( } } -export async function fetch(ctx: any) { +export async function fetch(ctx: UserCtx) { ctx.body = await sdk.plugins.fetch() } -export async function destroy(ctx: any) { +export async function destroy(ctx: UserCtx) { const { pluginId } = ctx.params try { diff --git a/packages/server/src/sdk/plugins/plugins.ts b/packages/server/src/sdk/plugins/plugins.ts index f6c26ca339..650065b40c 100644 --- a/packages/server/src/sdk/plugins/plugins.ts +++ b/packages/server/src/sdk/plugins/plugins.ts @@ -10,7 +10,7 @@ import env from "../../environment" import { clientAppSocket } from "../../websockets" import { sdk as pro } from "@budibase/pro" -export async function fetch(type?: PluginType) { +export async function fetch(type?: PluginType): Promise { const db = tenancy.getGlobalDB() const response = await db.allDocs( dbCore.getPluginParams(null, { diff --git a/packages/types/src/api/web/plugins.ts b/packages/types/src/api/web/plugins.ts index 458ad3f6ce..4dd47550e5 100644 --- a/packages/types/src/api/web/plugins.ts +++ b/packages/types/src/api/web/plugins.ts @@ -1,4 +1,10 @@ -import { PluginSource } from "../../documents" +import { PluginSource, Plugin } from "../../documents" + +export interface UploadPluginRequest {} +export interface UploadPluginResponse { + message: string + plugins: Plugin[] +} export interface CreatePluginRequest { source: PluginSource @@ -10,3 +16,9 @@ export interface CreatePluginRequest { export interface CreatePluginResponse { plugin: any } + +export type FetchPluginResponse = Plugin[] + +export interface DeletePluginResponse { + message: string +} diff --git a/packages/types/src/documents/global/plugin.ts b/packages/types/src/documents/global/plugin.ts index dd96bc20db..5a7c701ae5 100644 --- a/packages/types/src/documents/global/plugin.ts +++ b/packages/types/src/documents/global/plugin.ts @@ -13,8 +13,8 @@ export enum PluginSource { FILE = "File Upload", } export interface FileType { - path: string - name: string + path: string | null + name: string | null } export interface Plugin extends Document {