Remove unnecessary ctx.file APIs - plugins can use node:fs directly

This commit is contained in:
Gregory Schier
2025-12-28 14:06:35 -08:00
parent 218fdf3715
commit 6d5ba685f1
4 changed files with 7 additions and 76 deletions

View File

@@ -68,10 +68,6 @@ export interface Context {
templates: { templates: {
render<T extends JsonValue>(args: TemplateRenderRequest & { data: T }): Promise<T>; render<T extends JsonValue>(args: TemplateRenderRequest & { data: T }): Promise<T>;
}; };
file: {
writeText(filePath: string, content: string): Promise<void>;
readText(filePath: string): Promise<string>;
};
plugin: { plugin: {
reload(): void; reload(): void;
}; };

View File

@@ -689,24 +689,6 @@ export class PluginInstance {
return result.data as any; 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: { store: {
get: async <T>(key: string) => { get: async <T>(key: string) => {
const payload = { type: 'get_key_value_request', key } as const; const payload = { type: 'get_key_value_request', key } as const;

View File

@@ -21,10 +21,10 @@ use yaak_plugins::error::Error::PluginErr;
use yaak_plugins::events::{ use yaak_plugins::events::{
Color, DeleteKeyValueResponse, EmptyPayload, ErrorResponse, FindHttpResponsesResponse, Color, DeleteKeyValueResponse, EmptyPayload, ErrorResponse, FindHttpResponsesResponse,
GetCookieValueResponse, GetHttpRequestByIdResponse, GetKeyValueResponse, Icon, InternalEvent, GetCookieValueResponse, GetHttpRequestByIdResponse, GetKeyValueResponse, Icon, InternalEvent,
ListHttpRequestsResponse, InternalEventPayload, ListCookieNamesResponse, ListHttpRequestsResponse,
InternalEventPayload, ListCookieNamesResponse, RenderGrpcRequestResponse, RenderGrpcRequestResponse, RenderHttpRequestResponse, SendHttpRequestResponse,
RenderHttpRequestResponse, SendHttpRequestResponse, SetKeyValueResponse, ShowToastRequest, SetKeyValueResponse, ShowToastRequest, TemplateRenderResponse, WindowInfoResponse,
TemplateRenderResponse, WindowInfoResponse, WindowNavigateEvent, WindowNavigateEvent,
}; };
use yaak_plugins::plugin_handle::PluginHandle; use yaak_plugins::plugin_handle::PluginHandle;
use yaak_plugins::template_callback::PluginTemplateCallback; use yaak_plugins::template_callback::PluginTemplateCallback;
@@ -65,9 +65,8 @@ pub(crate) async fn handle_plugin_event<R: Runtime>(
InternalEventPayload::ListHttpRequestsRequest(req) => { InternalEventPayload::ListHttpRequestsRequest(req) => {
let mut http_requests = Vec::new(); let mut http_requests = Vec::new();
if let Some(folder_id) = req.folder_id { if let Some(folder_id) = req.folder_id {
http_requests = app_handle http_requests =
.db() app_handle.db().list_http_requests_for_folder_recursive(&folder_id)?;
.list_http_requests_for_folder_recursive(&folder_id)?;
} else if let Some(workspace_id) = req.workspace_id { } else if let Some(workspace_id) = req.workspace_id {
http_requests = app_handle.db().list_http_requests(&workspace_id)?; http_requests = app_handle.db().list_http_requests(&workspace_id)?;
} }
@@ -380,26 +379,7 @@ pub(crate) async fn handle_plugin_event<R: Runtime>(
environment_id, environment_id,
}))) })))
} }
InternalEventPayload::WriteTextFileRequest(req) => {
use std::fs;
use std::path::Path;
// Ensure the directory exists
if let Some(parent) = Path::new(&req.file_path).parent() {
fs::create_dir_all(parent)?;
}
fs::write(&req.file_path, &req.content)?;
Ok(Some(InternalEventPayload::WriteTextFileResponse(EmptyPayload {})))
}
InternalEventPayload::ReadTextFileRequest(req) => {
use std::fs;
let content = fs::read_to_string(&req.file_path)?;
Ok(Some(InternalEventPayload::ReadTextFileResponse(
yaak_plugins::events::ReadTextFileResponse { content },
)))
}
_ => Ok(None), _ => Ok(None),
} }
} }

View File

@@ -163,11 +163,6 @@ pub enum InternalEventPayload {
GetThemesRequest(GetThemesRequest), GetThemesRequest(GetThemesRequest),
GetThemesResponse(GetThemesResponse), GetThemesResponse(GetThemesResponse),
WriteTextFileRequest(WriteTextFileRequest),
WriteTextFileResponse(EmptyPayload),
ReadTextFileRequest(ReadTextFileRequest),
ReadTextFileResponse(ReadTextFileResponse),
/// Returned when a plugin doesn't get run, just so the server /// Returned when a plugin doesn't get run, just so the server
/// has something to listen for /// has something to listen for
EmptyResponse(EmptyPayload), EmptyResponse(EmptyPayload),
@@ -1319,25 +1314,3 @@ pub struct DeleteKeyValueRequest {
pub struct DeleteKeyValueResponse { pub struct DeleteKeyValueResponse {
pub deleted: bool, pub deleted: bool,
} }
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "gen_events.ts")]
pub struct WriteTextFileRequest {
pub file_path: String,
pub content: String,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "gen_events.ts")]
pub struct ReadTextFileRequest {
pub file_path: String,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize, TS)]
#[serde(default, rename_all = "camelCase")]
#[ts(export, export_to = "gen_events.ts")]
pub struct ReadTextFileResponse {
pub content: String,
}