mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-01-11 22:40:26 +01:00
collection plugin actions
This commit is contained in:
@@ -57,6 +57,10 @@ export interface Context {
|
||||
send(args: SendHttpRequestRequest): Promise<SendHttpRequestResponse['httpResponse']>;
|
||||
getById(args: GetHttpRequestByIdRequest): Promise<GetHttpRequestByIdResponse['httpRequest']>;
|
||||
render(args: RenderHttpRequestRequest): Promise<RenderHttpRequestResponse['httpRequest']>;
|
||||
list(args: { workspaceId?: string; folderId?: string }): Promise<Array<any>>;
|
||||
};
|
||||
folder: {
|
||||
list(args: { workspaceId?: string }): Promise<Array<any>>;
|
||||
};
|
||||
httpResponse: {
|
||||
find(args: FindHttpResponsesRequest): Promise<FindHttpResponsesResponse['httpResponses']>;
|
||||
@@ -64,6 +68,10 @@ export interface Context {
|
||||
templates: {
|
||||
render<T extends JsonValue>(args: TemplateRenderRequest & { data: T }): Promise<T>;
|
||||
};
|
||||
file: {
|
||||
writeText(filePath: string, content: string): Promise<void>;
|
||||
readText(filePath: string): Promise<string>;
|
||||
};
|
||||
plugin: {
|
||||
reload(): void;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
import type { Context } from './Context';
|
||||
import type { Folder, Workspace } from '../bindings/gen_models';
|
||||
import type { Icon } from '../bindings/gen_events';
|
||||
|
||||
export type HttpCollectionAction = { label: string; icon?: Icon };
|
||||
|
||||
export type CallHttpCollectionActionArgs = { folder?: Folder; workspace?: Workspace };
|
||||
|
||||
export type HttpCollectionActionPlugin = HttpCollectionAction & {
|
||||
onSelect(ctx: Context, args: CallHttpCollectionActionArgs): Promise<void> | void;
|
||||
};
|
||||
@@ -4,6 +4,7 @@ import type { Context } from './Context';
|
||||
import type { FilterPlugin } from './FilterPlugin';
|
||||
import { GrpcRequestActionPlugin } from './GrpcRequestActionPlugin';
|
||||
import type { HttpRequestActionPlugin } from './HttpRequestActionPlugin';
|
||||
import type { HttpCollectionActionPlugin } from './HttpCollectionActionPlugin';
|
||||
import type { ImporterPlugin } from './ImporterPlugin';
|
||||
import type { TemplateFunctionPlugin } from './TemplateFunctionPlugin';
|
||||
import type { ThemePlugin } from './ThemePlugin';
|
||||
@@ -12,6 +13,7 @@ export type { Context };
|
||||
export type { DynamicTemplateFunctionArg } from './TemplateFunctionPlugin';
|
||||
export type { DynamicAuthenticationArg } from './AuthenticationPlugin';
|
||||
export type { TemplateFunctionPlugin };
|
||||
export type { HttpCollectionActionPlugin } from './HttpCollectionActionPlugin';
|
||||
|
||||
/**
|
||||
* The global structure of a Yaak plugin
|
||||
@@ -24,6 +26,7 @@ export type PluginDefinition = {
|
||||
filter?: FilterPlugin;
|
||||
authentication?: AuthenticationPlugin;
|
||||
httpRequestActions?: HttpRequestActionPlugin[];
|
||||
httpCollectionActions?: HttpCollectionActionPlugin[];
|
||||
grpcRequestActions?: GrpcRequestActionPlugin[];
|
||||
templateFunctions?: TemplateFunctionPlugin[];
|
||||
};
|
||||
|
||||
@@ -10,6 +10,7 @@ import {
|
||||
GrpcRequestAction,
|
||||
HttpAuthenticationAction,
|
||||
HttpRequestAction,
|
||||
HttpCollectionAction,
|
||||
InternalEvent,
|
||||
InternalEventPayload,
|
||||
ListCookieNamesResponse,
|
||||
@@ -172,6 +173,24 @@ export class PluginInstance {
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
payload.type === 'get_http_collection_actions_request' &&
|
||||
Array.isArray(this.#mod?.httpCollectionActions)
|
||||
) {
|
||||
const reply: HttpRequestAction[] = this.#mod.httpCollectionActions.map((a) => ({
|
||||
...a,
|
||||
// Add everything except onSelect
|
||||
onSelect: undefined,
|
||||
}));
|
||||
const replyPayload: InternalEventPayload = {
|
||||
type: 'get_http_collection_actions_response',
|
||||
pluginRefId: this.#workerData.pluginRefId,
|
||||
actions: reply,
|
||||
};
|
||||
this.#sendPayload(context, replyPayload, replyId);
|
||||
return;
|
||||
}
|
||||
|
||||
if (payload.type === 'get_themes_request' && Array.isArray(this.#mod?.themes)) {
|
||||
const replyPayload: InternalEventPayload = {
|
||||
type: 'get_themes_response',
|
||||
@@ -302,6 +321,18 @@ export class PluginInstance {
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
payload.type === 'call_http_collection_action_request' &&
|
||||
Array.isArray(this.#mod.httpCollectionActions)
|
||||
) {
|
||||
const action = this.#mod.httpCollectionActions[payload.index];
|
||||
if (typeof action?.onSelect === 'function') {
|
||||
await action.onSelect(ctx, payload.args);
|
||||
this.#sendEmpty(context, replyId);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
payload.type === 'call_grpc_request_action_request' &&
|
||||
Array.isArray(this.#mod.grpcRequestActions)
|
||||
@@ -611,6 +642,26 @@ export class PluginInstance {
|
||||
);
|
||||
return httpRequest;
|
||||
},
|
||||
list: async (args: { workspaceId?: string; folderId?: string }) => {
|
||||
const payload = {
|
||||
type: 'list_http_requests_request',
|
||||
// plugin events use camelCase field names in Rust -> snake_case mapping
|
||||
folderId: args.folderId,
|
||||
workspaceId: args.workspaceId,
|
||||
} as any;
|
||||
const { httpRequests } = await this.#sendForReply<any>(context, payload);
|
||||
return httpRequests as any[];
|
||||
},
|
||||
},
|
||||
folder: {
|
||||
list: async (args: { workspaceId?: string }) => {
|
||||
const payload = {
|
||||
type: 'list_folders_request',
|
||||
workspaceId: args.workspaceId,
|
||||
} as any;
|
||||
const { folders } = await this.#sendForReply<any>(context, payload);
|
||||
return folders as any[];
|
||||
},
|
||||
},
|
||||
cookies: {
|
||||
getValue: async (args: GetCookieValueRequest) => {
|
||||
@@ -638,6 +689,24 @@ export class PluginInstance {
|
||||
return result.data as any;
|
||||
},
|
||||
},
|
||||
file: {
|
||||
writeText: async (filePath: string, content: string) => {
|
||||
const payload: InternalEventPayload = {
|
||||
type: 'write_text_file_request',
|
||||
filePath,
|
||||
content,
|
||||
} as any;
|
||||
await this.#sendForReply(context, payload);
|
||||
},
|
||||
readText: async (filePath: string) => {
|
||||
const payload: InternalEventPayload = {
|
||||
type: 'read_text_file_request',
|
||||
filePath,
|
||||
} as any;
|
||||
const result = await this.#sendForReply<any>(context, payload);
|
||||
return result.content;
|
||||
},
|
||||
},
|
||||
store: {
|
||||
get: async <T>(key: string) => {
|
||||
const payload = { type: 'get_key_value_request', key } as const;
|
||||
|
||||
Reference in New Issue
Block a user