Generalized frontend model store (#193)

This commit is contained in:
Gregory Schier
2025-03-31 11:56:17 -07:00
committed by GitHub
parent ce885c3551
commit f1757ae427
201 changed files with 2185 additions and 2865 deletions

View File

@@ -24,11 +24,11 @@ use tokio::fs::{create_dir_all, File};
use tokio::io::AsyncWriteExt;
use tokio::sync::watch::Receiver;
use tokio::sync::{oneshot, Mutex};
use yaak_models::query_manager::QueryManagerExt;
use yaak_models::models::{
Cookie, CookieJar, Environment, HttpRequest, HttpResponse, HttpResponseHeader,
HttpResponseState, ProxySetting, ProxySettingAuth,
};
use yaak_models::query_manager::QueryManagerExt;
use yaak_models::util::UpdateSource;
use yaak_plugins::events::{
CallHttpAuthenticationRequest, HttpHeader, RenderPurpose, WindowContext,
@@ -46,10 +46,9 @@ pub async fn send_http_request<R: Runtime>(
) -> Result<HttpResponse> {
let app_handle = window.app_handle().clone();
let plugin_manager = app_handle.state::<PluginManager>();
let update_source = &UpdateSource::from_window(&window);
let (settings, workspace) = {
let db = window.db();
let settings = db.get_or_create_settings(update_source);
let settings = db.get_settings();
let workspace = db.get_workspace(&unrendered_request.workspace_id)?;
(settings, workspace)
};

View File

@@ -13,12 +13,12 @@ use error::Result as YaakResult;
use eventsource_client::{EventParser, SSE};
use log::{debug, error, warn};
use std::collections::{BTreeMap, HashMap};
use std::fs::{create_dir_all, File};
use std::fs::{File, create_dir_all};
use std::path::PathBuf;
use std::str::FromStr;
use std::time::Duration;
use std::{fs, panic};
use tauri::{is_dev, AppHandle, Emitter, RunEvent, State, WebviewWindow};
use tauri::{AppHandle, Emitter, RunEvent, State, WebviewWindow, is_dev};
use tauri::{Listener, Runtime};
use tauri::{Manager, WindowEvent};
use tauri_plugin_log::fern::colors::ColoredLevelConfig;
@@ -29,15 +29,15 @@ use tokio::sync::Mutex;
use tokio::task::block_in_place;
use yaak_common::window::WorkspaceWindowTrait;
use yaak_grpc::manager::{DynamicMessage, GrpcHandle};
use yaak_grpc::{deserialize_message, serialize_message, Code, ServiceDefinition};
use yaak_grpc::{Code, ServiceDefinition, deserialize_message, serialize_message};
use yaak_models::models::{
CookieJar, Environment, EnvironmentVariable, Folder, GrpcConnection, GrpcConnectionState,
GrpcEvent, GrpcEventType, GrpcRequest, HttpRequest, HttpResponse, HttpResponseState, KeyValue,
Plugin, Settings, WebsocketRequest, Workspace, WorkspaceMeta,
CookieJar, Environment, Folder, GrpcConnection, GrpcConnectionState, GrpcEvent, GrpcEventType,
GrpcRequest, HttpRequest, HttpResponse, HttpResponseState, Plugin, WebsocketRequest, Workspace,
WorkspaceMeta,
};
use yaak_models::query_manager::QueryManagerExt;
use yaak_models::util::{
get_workspace_export_resources, maybe_gen_id, maybe_gen_id_opt, BatchUpsertResult, UpdateSource,
BatchUpsertResult, UpdateSource, get_workspace_export_resources, maybe_gen_id, maybe_gen_id_opt,
};
use yaak_plugins::events::{
BootResponse, CallHttpAuthenticationRequest, CallHttpRequestActionRequest, FilterResponse,
@@ -1064,47 +1064,6 @@ fn response_err<R: Runtime>(
response
}
#[tauri::command]
async fn cmd_set_update_mode<R: Runtime>(
update_mode: &str,
app_handle: AppHandle<R>,
window: WebviewWindow<R>,
) -> YaakResult<KeyValue> {
let (key_value, _created) = app_handle.db().set_key_value_raw(
"app",
"update_mode",
update_mode,
&UpdateSource::from_window(&window),
);
Ok(key_value)
}
#[tauri::command]
async fn cmd_get_key_value<R: Runtime>(
namespace: &str,
key: &str,
app_handle: AppHandle<R>,
) -> YaakResult<Option<KeyValue>> {
Ok(app_handle.db().get_key_value_raw(namespace, key))
}
#[tauri::command]
async fn cmd_set_key_value<R: Runtime>(
app_handle: AppHandle<R>,
window: WebviewWindow<R>,
namespace: &str,
key: &str,
value: &str,
) -> YaakResult<KeyValue> {
let (key_value, _created) = app_handle.db().set_key_value_raw(
namespace,
key,
value,
&UpdateSource::from_window(&window),
);
Ok(key_value)
}
#[tauri::command]
async fn cmd_install_plugin<R: Runtime>(
directory: &str,
@@ -1144,64 +1103,6 @@ async fn cmd_uninstall_plugin<R: Runtime>(
Ok(plugin)
}
#[tauri::command]
async fn cmd_update_cookie_jar<R: Runtime>(
cookie_jar: CookieJar,
app_handle: AppHandle<R>,
window: WebviewWindow<R>,
) -> YaakResult<CookieJar> {
Ok(app_handle.db().upsert_cookie_jar(&cookie_jar, &UpdateSource::from_window(&window))?)
}
#[tauri::command]
async fn cmd_delete_cookie_jar<R: Runtime>(
app_handle: AppHandle<R>,
window: WebviewWindow<R>,
cookie_jar_id: &str,
) -> YaakResult<CookieJar> {
Ok(app_handle
.db()
.delete_cookie_jar_by_id(cookie_jar_id, &UpdateSource::from_window(&window))?)
}
#[tauri::command]
async fn cmd_create_cookie_jar<R: Runtime>(
workspace_id: &str,
name: &str,
app_handle: AppHandle<R>,
window: WebviewWindow<R>,
) -> YaakResult<CookieJar> {
Ok(app_handle.db().upsert_cookie_jar(
&CookieJar {
name: name.to_string(),
workspace_id: workspace_id.to_string(),
..Default::default()
},
&UpdateSource::from_window(&window),
)?)
}
#[tauri::command]
async fn cmd_create_environment<R: Runtime>(
workspace_id: &str,
environment_id: Option<&str>,
name: &str,
variables: Vec<EnvironmentVariable>,
app_handle: AppHandle<R>,
window: WebviewWindow<R>,
) -> YaakResult<Environment> {
Ok(app_handle.db().upsert_environment(
&Environment {
workspace_id: workspace_id.to_string(),
environment_id: environment_id.map(|s| s.to_string()),
name: name.to_string(),
variables,
..Default::default()
},
&UpdateSource::from_window(&window),
)?)
}
#[tauri::command]
async fn cmd_create_grpc_request<R: Runtime>(
workspace_id: &str,
@@ -1223,193 +1124,6 @@ async fn cmd_create_grpc_request<R: Runtime>(
)?)
}
#[tauri::command]
async fn cmd_duplicate_grpc_request<R: Runtime>(
id: &str,
app_handle: AppHandle<R>,
window: WebviewWindow<R>,
) -> YaakResult<GrpcRequest> {
let db = app_handle.db();
let request = db.get_grpc_request(id)?;
Ok(db.duplicate_grpc_request(&request, &UpdateSource::from_window(&window))?)
}
#[tauri::command]
async fn cmd_duplicate_folder<R: Runtime>(
app_handle: AppHandle<R>,
window: WebviewWindow<R>,
id: &str,
) -> YaakResult<Folder> {
let db = app_handle.db();
let folder = db.get_folder(id)?;
Ok(db.duplicate_folder(&folder, &UpdateSource::from_window(&window))?)
}
#[tauri::command]
async fn cmd_duplicate_http_request<R: Runtime>(
id: &str,
app_handle: AppHandle<R>,
window: WebviewWindow<R>,
) -> YaakResult<HttpRequest> {
let db = app_handle.db();
let request = db.get_http_request(id)?;
Ok(db.duplicate_http_request(&request, &UpdateSource::from_window(&window))?)
}
#[tauri::command]
async fn cmd_update_workspace<R: Runtime>(
workspace: Workspace,
app_handle: AppHandle<R>,
window: WebviewWindow<R>,
) -> YaakResult<Workspace> {
Ok(app_handle.db().upsert_workspace(&workspace, &UpdateSource::from_window(&window))?)
}
#[tauri::command]
async fn cmd_update_workspace_meta<R: Runtime>(
workspace_meta: WorkspaceMeta,
app_handle: AppHandle<R>,
window: WebviewWindow<R>,
) -> YaakResult<WorkspaceMeta> {
Ok(app_handle
.db()
.upsert_workspace_meta(&workspace_meta, &UpdateSource::from_window(&window))?)
}
#[tauri::command]
async fn cmd_update_environment<R: Runtime>(
environment: Environment,
app_handle: AppHandle<R>,
window: WebviewWindow<R>,
) -> YaakResult<Environment> {
Ok(app_handle.db().upsert_environment(&environment, &UpdateSource::from_window(&window))?)
}
#[tauri::command]
async fn cmd_update_grpc_request<R: Runtime>(
request: GrpcRequest,
app_handle: AppHandle<R>,
window: WebviewWindow<R>,
) -> YaakResult<GrpcRequest> {
Ok(app_handle.db().upsert_grpc_request(&request, &UpdateSource::from_window(&window))?)
}
#[tauri::command]
async fn cmd_upsert_http_request<R: Runtime>(
request: HttpRequest,
window: WebviewWindow<R>,
app_handle: AppHandle<R>,
) -> YaakResult<HttpRequest> {
Ok(app_handle.db().upsert(&request, &UpdateSource::from_window(&window))?)
}
#[tauri::command]
async fn cmd_delete_grpc_request<R: Runtime>(
app_handle: AppHandle<R>,
request_id: &str,
window: WebviewWindow<R>,
) -> YaakResult<GrpcRequest> {
Ok(app_handle
.db()
.delete_grpc_request_by_id(request_id, &UpdateSource::from_window(&window))?)
}
#[tauri::command]
async fn cmd_delete_http_request<R: Runtime>(
app_handle: AppHandle<R>,
request_id: &str,
window: WebviewWindow<R>,
) -> YaakResult<HttpRequest> {
Ok(app_handle
.db()
.delete_http_request_by_id(request_id, &UpdateSource::from_window(&window))?)
}
#[tauri::command]
async fn cmd_list_folders<R: Runtime>(
workspace_id: &str,
app_handle: AppHandle<R>,
) -> YaakResult<Vec<Folder>> {
Ok(app_handle.db().list_folders(workspace_id)?)
}
#[tauri::command]
async fn cmd_update_folder<R: Runtime>(
folder: Folder,
app_handle: AppHandle<R>,
window: WebviewWindow<R>,
) -> YaakResult<Folder> {
Ok(app_handle.db().upsert_folder(&folder, &UpdateSource::from_window(&window))?)
}
#[tauri::command]
async fn cmd_delete_folder<R: Runtime>(
app_handle: AppHandle<R>,
window: WebviewWindow<R>,
folder_id: &str,
) -> YaakResult<Folder> {
Ok(app_handle.db().delete_folder_by_id(folder_id, &UpdateSource::from_window(&window))?)
}
#[tauri::command]
async fn cmd_delete_environment<R: Runtime>(
app_handle: AppHandle<R>,
window: WebviewWindow<R>,
environment_id: &str,
) -> YaakResult<Environment> {
Ok(app_handle
.db()
.delete_environment_by_id(environment_id, &UpdateSource::from_window(&window))?)
}
#[tauri::command]
async fn cmd_list_grpc_connections<R: Runtime>(
workspace_id: &str,
app_handle: AppHandle<R>,
) -> YaakResult<Vec<GrpcConnection>> {
Ok(app_handle.db().list_grpc_connections_for_workspace(workspace_id, None)?)
}
#[tauri::command]
async fn cmd_list_grpc_events<R: Runtime>(
connection_id: &str,
app_handle: AppHandle<R>,
) -> YaakResult<Vec<GrpcEvent>> {
Ok(app_handle.db().list_grpc_events(connection_id)?)
}
#[tauri::command]
async fn cmd_list_grpc_requests<R: Runtime>(
workspace_id: &str,
app_handle: AppHandle<R>,
) -> YaakResult<Vec<GrpcRequest>> {
Ok(app_handle.db().list_grpc_requests(workspace_id)?)
}
#[tauri::command]
async fn cmd_list_http_requests<R: Runtime>(
workspace_id: &str,
app_handle: AppHandle<R>,
) -> YaakResult<Vec<HttpRequest>> {
Ok(app_handle.db().list_http_requests(workspace_id)?)
}
#[tauri::command]
async fn cmd_list_environments<R: Runtime>(
workspace_id: &str,
app_handle: AppHandle<R>,
) -> YaakResult<Vec<Environment>> {
// Not sure of a better place to put this...
let db = app_handle.db();
db.ensure_base_environment(workspace_id)?;
Ok(db.list_environments(workspace_id)?)
}
#[tauri::command]
async fn cmd_list_plugins<R: Runtime>(app_handle: AppHandle<R>) -> YaakResult<Vec<Plugin>> {
Ok(app_handle.db().list_plugins()?)
}
#[tauri::command]
async fn cmd_reload_plugins<R: Runtime>(
app_handle: AppHandle<R>,
@@ -1438,99 +1152,6 @@ async fn cmd_plugin_info<R: Runtime>(
.await)
}
#[tauri::command]
async fn cmd_get_settings<R: Runtime>(window: WebviewWindow<R>) -> YaakResult<Settings> {
Ok(window.db().get_or_create_settings(&UpdateSource::from_window(&window)))
}
#[tauri::command]
async fn cmd_update_settings<R: Runtime>(
settings: Settings,
app_handle: AppHandle<R>,
window: WebviewWindow<R>,
) -> YaakResult<Settings> {
Ok(app_handle.db().upsert_settings(&settings, &UpdateSource::from_window(&window))?)
}
#[tauri::command]
async fn cmd_get_folder<R: Runtime>(id: &str, app_handle: AppHandle<R>) -> YaakResult<Folder> {
Ok(app_handle.db().get_folder(id)?)
}
#[tauri::command]
async fn cmd_list_cookie_jars<R: Runtime>(
workspace_id: &str,
app_handle: AppHandle<R>,
window: WebviewWindow<R>,
) -> YaakResult<Vec<CookieJar>> {
let db = app_handle.db();
let cookie_jars = db.list_cookie_jars(workspace_id)?;
if cookie_jars.is_empty() {
let cookie_jar = db.upsert_cookie_jar(
&CookieJar {
name: "Default".to_string(),
workspace_id: workspace_id.to_string(),
..Default::default()
},
&UpdateSource::from_window(&window),
)?;
Ok(vec![cookie_jar])
} else {
Ok(cookie_jars)
}
}
#[tauri::command]
async fn cmd_list_key_values<R: Runtime>(app_handle: AppHandle<R>) -> YaakResult<Vec<KeyValue>> {
Ok(app_handle.db().list_key_values_raw()?)
}
#[tauri::command]
async fn cmd_get_environment<R: Runtime>(
id: &str,
app_handle: AppHandle<R>,
) -> YaakResult<Environment> {
Ok(app_handle.db().get_environment(id)?)
}
#[tauri::command]
async fn cmd_get_workspace<R: Runtime>(
id: &str,
app_handle: AppHandle<R>,
) -> YaakResult<Workspace> {
Ok(app_handle.db().get_workspace(id)?)
}
#[tauri::command]
async fn cmd_list_http_responses<R: Runtime>(
workspace_id: &str,
limit: Option<i64>,
app_handle: AppHandle<R>,
) -> YaakResult<Vec<HttpResponse>> {
Ok(app_handle.db().list_http_responses_for_workspace(workspace_id, limit.map(|l| l as u64))?)
}
#[tauri::command]
async fn cmd_delete_http_response<R: Runtime>(
id: &str,
app_handle: AppHandle<R>,
window: WebviewWindow<R>,
) -> YaakResult<HttpResponse> {
let db = app_handle.db();
let http_response = db.get_http_response(id)?;
Ok(db.delete_http_response(&http_response, &UpdateSource::from_window(&window))?)
}
#[tauri::command]
async fn cmd_delete_grpc_connection<R: Runtime>(
id: &str,
app_handle: AppHandle<R>,
window: WebviewWindow<R>,
) -> YaakResult<GrpcConnection> {
Ok(app_handle.db().delete_grpc_connection_by_id(id, &UpdateSource::from_window(&window))?)
}
#[tauri::command]
async fn cmd_delete_all_grpc_connections<R: Runtime>(
request_id: &str,
@@ -1568,29 +1189,6 @@ async fn cmd_delete_all_http_responses<R: Runtime>(
.delete_all_http_responses_for_request(request_id, &UpdateSource::from_window(&window))?)
}
#[tauri::command]
async fn cmd_list_workspaces<R: Runtime>(
app_handle: AppHandle<R>,
window: WebviewWindow<R>,
) -> YaakResult<Vec<Workspace>> {
let db = app_handle.db();
let mut workspaces = db.find_all::<Workspace>()?;
if workspaces.is_empty() {
workspaces.push(db.upsert_workspace(
&Workspace {
name: "Yaak".to_string(),
setting_follow_redirects: true,
setting_validate_certificates: true,
..Default::default()
},
&UpdateSource::from_window(&window),
)?);
}
Ok(workspaces)
}
#[tauri::command]
async fn cmd_get_workspace_meta<R: Runtime>(
app_handle: AppHandle<R>,
@@ -1599,7 +1197,7 @@ async fn cmd_get_workspace_meta<R: Runtime>(
) -> YaakResult<WorkspaceMeta> {
let db = app_handle.db();
let workspace = db.get_workspace(workspace_id)?;
Ok(db.get_or_create_workspace_meta(&workspace, &UpdateSource::from_window(&window))?)
Ok(db.get_or_create_workspace_meta(&workspace.id, &UpdateSource::from_window(&window))?)
}
#[tauri::command]
@@ -1620,15 +1218,6 @@ async fn cmd_new_main_window(app_handle: AppHandle, url: &str) -> Result<(), Str
Ok(())
}
#[tauri::command]
async fn cmd_delete_workspace<R: Runtime>(
app_handle: AppHandle<R>,
window: WebviewWindow<R>,
workspace_id: &str,
) -> YaakResult<Workspace> {
Ok(app_handle.db().delete_workspace_by_id(workspace_id, &UpdateSource::from_window(&window))?)
}
#[tauri::command]
async fn cmd_check_for_updates<R: Runtime>(
window: WebviewWindow<R>,
@@ -1722,53 +1311,24 @@ pub fn run() {
cmd_call_http_authentication_action,
cmd_call_http_request_action,
cmd_check_for_updates,
cmd_create_cookie_jar,
cmd_create_environment,
cmd_create_grpc_request,
cmd_curl_to_request,
cmd_delete_all_grpc_connections,
cmd_delete_all_http_responses,
cmd_delete_cookie_jar,
cmd_delete_environment,
cmd_delete_folder,
cmd_delete_grpc_connection,
cmd_delete_grpc_request,
cmd_delete_http_request,
cmd_delete_http_response,
cmd_delete_send_history,
cmd_delete_workspace,
cmd_dismiss_notification,
cmd_duplicate_folder,
cmd_duplicate_grpc_request,
cmd_duplicate_http_request,
cmd_export_data,
cmd_filter_response,
cmd_format_json,
cmd_get_environment,
cmd_get_folder,
cmd_get_http_authentication_summaries,
cmd_get_http_authentication_config,
cmd_get_key_value,
cmd_get_settings,
cmd_get_sse_events,
cmd_get_workspace,
cmd_get_workspace_meta,
cmd_grpc_go,
cmd_grpc_reflect,
cmd_http_request_actions,
cmd_import_data,
cmd_install_plugin,
cmd_list_cookie_jars,
cmd_list_environments,
cmd_list_folders,
cmd_list_grpc_connections,
cmd_list_grpc_events,
cmd_list_grpc_requests,
cmd_list_key_values,
cmd_list_http_requests,
cmd_list_http_responses,
cmd_list_plugins,
cmd_list_workspaces,
cmd_metadata,
cmd_new_child_window,
cmd_new_main_window,
@@ -1779,19 +1339,9 @@ pub fn run() {
cmd_save_response,
cmd_send_ephemeral_request,
cmd_send_http_request,
cmd_set_key_value,
cmd_set_update_mode,
cmd_template_functions,
cmd_template_tokens_to_string,
cmd_uninstall_plugin,
cmd_update_cookie_jar,
cmd_update_environment,
cmd_update_folder,
cmd_update_grpc_request,
cmd_upsert_http_request,
cmd_update_settings,
cmd_update_workspace,
cmd_update_workspace_meta,
])
.register_uri_scheme_protocol("yaak", handle_uri_scheme)
.build(tauri::generate_context!())
@@ -1859,7 +1409,7 @@ pub fn run() {
}
async fn get_update_mode<R: Runtime>(window: &WebviewWindow<R>) -> YaakResult<UpdateMode> {
let settings = window.db().get_or_create_settings(&UpdateSource::from_window(window));
let settings = window.db().get_settings();
Ok(UpdateMode::new(settings.update_channel.as_str()))
}
@@ -1934,13 +1484,10 @@ fn get_window_from_window_context<R: Runtime>(
}
};
let window = app_handle.webview_windows().iter().find_map(|(_, w)| {
if w.label() == label {
Some(w.to_owned())
} else {
None
}
});
let window = app_handle
.webview_windows()
.iter()
.find_map(|(_, w)| if w.label() == label { Some(w.to_owned()) } else { None });
if window.is_none() {
error!("Failed to find window by {window_context:?}");

View File

@@ -8,7 +8,6 @@ use tauri_plugin_dialog::{DialogExt, MessageDialogButtons};
use tauri_plugin_updater::UpdaterExt;
use tokio::task::block_in_place;
use yaak_models::query_manager::QueryManagerExt;
use yaak_models::util::UpdateSource;
use yaak_plugins::manager::PluginManager;
use crate::is_dev;
@@ -67,7 +66,7 @@ impl YaakUpdater {
mode: UpdateMode,
update_trigger: UpdateTrigger,
) -> Result<bool> {
let settings = window.db().get_or_create_settings(&UpdateSource::from_window(window));
let settings = window.db().get_settings();
let update_key = format!("{:x}", md5::compute(settings.id));
self.last_update_check = SystemTime::now();