Plugin API typing.

This commit is contained in:
mike12345567 2024-12-03 17:02:30 +00:00
parent d0179ed815
commit 3446b1c678
5 changed files with 43 additions and 16 deletions

View File

@ -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.")
}

View File

@ -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<UploadPluginRequest, UploadPluginResponse>
) {
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<CreatePluginRequest, CreatePluginResponse>
ctx: UserCtx<CreatePluginRequest, CreatePluginResponse>
) {
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<void, FetchPluginResponse>) {
ctx.body = await sdk.plugins.fetch()
}
export async function destroy(ctx: any) {
export async function destroy(ctx: UserCtx<void, DeletePluginResponse>) {
const { pluginId } = ctx.params
try {

View File

@ -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<Plugin[]> {
const db = tenancy.getGlobalDB()
const response = await db.allDocs(
dbCore.getPluginParams(null, {

View File

@ -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
}

View File

@ -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 {