Fix plugins

This commit is contained in:
Gregory Schier
2024-06-13 07:03:50 -07:00
parent 641fe86cf7
commit 33763b6d2f
26 changed files with 793 additions and 72 deletions

View File

@@ -735,7 +735,7 @@ async fn cmd_filter_response(
};
let body = read_to_string(response.body_path.unwrap()).unwrap();
let filter_result = plugin::run_plugin_filter(plugin_name, filter, &body)
let filter_result = plugin::run_plugin_filter(&w.app_handle(), plugin_name, filter, &body)
.await
.expect("Failed to run filter");
Ok(filter_result.filtered)
@@ -758,7 +758,7 @@ async fn cmd_import_data(
read_to_string(file_path).unwrap_or_else(|_| panic!("Unable to read file {}", file_path));
let file_contents = file.as_str();
for plugin_name in plugins {
let v = run_plugin_import(plugin_name, file_contents)
let v = run_plugin_import(&w.app_handle(), plugin_name, file_contents)
.await
.map_err(|e| e.to_string())?;
if let Some(r) = v {
@@ -903,12 +903,12 @@ async fn cmd_request_to_curl(
.await
.map_err(|e| e.to_string())?;
let rendered = render_request(&request, &workspace, environment.as_ref());
Ok(run_plugin_export_curl(&rendered)?)
Ok(run_plugin_export_curl(&app, &rendered)?)
}
#[tauri::command]
async fn cmd_curl_to_request(command: &str, workspace_id: &str) -> Result<HttpRequest, String> {
let v = run_plugin_import("importer-curl", command)
async fn cmd_curl_to_request(app_handle: AppHandle, command: &str, workspace_id: &str) -> Result<HttpRequest, String> {
let v = run_plugin_import(&app_handle, "importer-curl", command)
.await
.map_err(|e| e.to_string());
match v {

View File

@@ -2,6 +2,8 @@ use std::path;
use log::error;
use serde::{Deserialize, Serialize};
use tauri::path::BaseDirectory;
use tauri::{AppHandle, Manager};
use crate::deno::run_plugin_deno_block;
use crate::models::{HttpRequest, WorkspaceExportResources};
@@ -17,12 +19,17 @@ pub struct ImportResult {
}
pub async fn run_plugin_filter(
app_handle: &AppHandle,
plugin_name: &str,
response_body: &str,
filter: &str,
) -> Option<FilterResult> {
let plugin_dir = path::Path::new("/Users/gschier/Workspace/yaak/plugins");
let plugin_index_file = plugin_dir.join(plugin_name).join("build/index.mjs");
let plugin_dir = app_handle
.path()
.resolve("plugins", BaseDirectory::Resource)
.expect("failed to resolve plugin directory resource")
.join(plugin_name);
let plugin_index_file = plugin_dir.join("index.mjs");
let result = run_plugin_deno_block(
plugin_index_file.to_str().unwrap(),
@@ -45,9 +52,16 @@ pub async fn run_plugin_filter(
Some(resources)
}
pub fn run_plugin_export_curl(request: &HttpRequest) -> Result<String, String> {
let plugin_dir = path::Path::new("/Users/gschier/Workspace/yaak/plugins");
let plugin_index_file = plugin_dir.join("exporter-curl").join("build/index.mjs");
pub fn run_plugin_export_curl(
app_handle: &AppHandle,
request: &HttpRequest,
) -> Result<String, String> {
let plugin_dir = app_handle
.path()
.resolve("plugins", BaseDirectory::Resource)
.expect("failed to resolve plugin directory resource")
.join("exporter-curl");
let plugin_index_file = plugin_dir.join("index.mjs");
let request_json = serde_json::to_value(request).map_err(|e| e.to_string())?;
let result = run_plugin_deno_block(
@@ -62,11 +76,16 @@ pub fn run_plugin_export_curl(request: &HttpRequest) -> Result<String, String> {
}
pub async fn run_plugin_import(
app_handle: &AppHandle,
plugin_name: &str,
file_contents: &str,
) -> Result<Option<ImportResult>, String> {
let plugin_dir = path::Path::new("/Users/gschier/Workspace/yaak/plugins");
let plugin_index_file = plugin_dir.join(plugin_name).join("build/index.mjs");
let plugin_dir = app_handle
.path()
.resolve("plugins", BaseDirectory::Resource)
.expect("failed to resolve plugin directory resource")
.join(plugin_name);
let plugin_index_file = plugin_dir.join("index.mjs");
let result = run_plugin_deno_block(
plugin_index_file.to_str().unwrap(),