Updated plugin APIs

This commit is contained in:
Gregory Schier
2024-08-01 07:01:57 -07:00
parent a643713139
commit 76dd4380ce
7 changed files with 42 additions and 15 deletions

View File

@@ -7,7 +7,8 @@
"lib"
],
"scripts": {
"prepublish": "tsc"
"build": "tsc",
"prepublish": "npm run build"
},
"dependencies": {
"@types/node": "^22.0.0"

View File

@@ -1,2 +1,2 @@
export type AtLeast<T, K extends keyof T> = Partial<T> & Pick<T, K>;
export type SingleOrArray<T> = T[] | T;
export type OneOrMany<T> = T[] | T;

View File

@@ -0,0 +1,13 @@
import { YaakContext } from './context';
export type FilterPluginResponse = string[];
export type FilterPlugin = {
name: string;
description?: string;
canFilter(ctx: YaakContext, args: { mimeType: string }): Promise<boolean>;
onFilter(
ctx: YaakContext,
args: { payload: string; mimeType: string },
): Promise<FilterPluginResponse>;
};

View File

@@ -0,0 +1,8 @@
import { HttpRequest } from '../models';
import { YaakContext } from './context';
export type HttpRequestActionPlugin = {
key: string;
label: string;
onSelect(ctx: YaakContext, args: { httpRequest: HttpRequest }): void;
};

View File

@@ -3,14 +3,14 @@ import { Environment, Folder, HttpRequest, Workspace } from '../models';
import { YaakContext } from './context';
export type ImportPluginResponse = null | {
resources: Partial<{
workspaces: AtLeast<Workspace, 'name' | 'id' | 'model'>[];
environments: AtLeast<Environment, 'name' | 'id' | 'model' | 'workspaceId'>[];
httpRequests: AtLeast<HttpRequest, 'name' | 'id' | 'model' | 'workspaceId'>[];
folders: AtLeast<Folder, 'name' | 'id' | 'model' | 'workspaceId'>[];
}>;
workspaces: AtLeast<Workspace, 'name' | 'id' | 'model'>[];
environments: AtLeast<Environment, 'name' | 'id' | 'model' | 'workspaceId'>[];
httpRequests: AtLeast<HttpRequest, 'name' | 'id' | 'model' | 'workspaceId'>[];
folders: AtLeast<Folder, 'name' | 'id' | 'model' | 'workspaceId'>[];
};
export type ImporterPlugin = {
onImport(ctx: YaakContext, fileContents: string): Promise<ImportPluginResponse>;
name: string;
description?: string;
onImport(ctx: YaakContext, args: { text: string }): Promise<ImportPluginResponse>;
};

View File

@@ -1,4 +1,6 @@
import { SingleOrArray } from '../helpers';
import { OneOrMany } from '../helpers';
import { FilterPlugin } from './filter';
import { HttpRequestActionPlugin } from './httpRequestAction';
import { ImporterPlugin } from './import';
import { ThemePlugin } from './theme';
@@ -6,8 +8,8 @@ import { ThemePlugin } from './theme';
* The global structure of a Yaak plugin
*/
export type YaakPlugin = {
/** One or many plugins to import data into Yaak */
importers?: SingleOrArray<ImporterPlugin>;
/** One or many themes to customize the Yaak UI */
themes?: SingleOrArray<ThemePlugin>;
importer?: OneOrMany<ImporterPlugin>;
theme?: OneOrMany<ThemePlugin>;
filter?: OneOrMany<FilterPlugin>;
httpRequestAction?: OneOrMany<HttpRequestActionPlugin>;
};

View File

@@ -1,5 +1,8 @@
import { Theme } from '../themes';
import { YaakContext } from './context';
export type ThemePlugin = {
theme: Theme;
name: string;
description?: string;
getTheme(ctx: YaakContext, fileContents: string): Promise<Theme>;
};