From c655557313730575f427279f513b2e824f593fa4 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Thu, 1 Feb 2024 02:29:24 -0800 Subject: [PATCH] Refactor commands and DB --- src-tauri/grpc/src/lib.rs | 5 +- src-tauri/src/analytics.rs | 18 +- src-tauri/src/http.rs | 16 +- src-tauri/src/main.rs | 600 +++++++++--------- src-tauri/src/models.rs | 260 ++++---- src-web/components/GrpcConnectionLayout.tsx | 71 ++- src-web/components/SettingsDropdown.tsx | 2 +- .../components/WorkspaceActionsDropdown.tsx | 2 +- src-web/components/core/Stacks.tsx | 1 + src-web/hooks/useCookieJars.ts | 2 +- src-web/hooks/useCreateCookieJar.ts | 2 +- src-web/hooks/useCreateEnvironment.ts | 2 +- src-web/hooks/useCreateFolder.ts | 2 +- src-web/hooks/useCreateRequest.ts | 2 +- src-web/hooks/useCreateWorkspace.ts | 2 +- src-web/hooks/useDeleteAnyRequest.tsx | 2 +- src-web/hooks/useDeleteCookieJar.tsx | 2 +- src-web/hooks/useDeleteEnvironment.tsx | 2 +- src-web/hooks/useDeleteFolder.tsx | 2 +- src-web/hooks/useDeleteResponse.ts | 2 +- src-web/hooks/useDeleteResponses.ts | 2 +- src-web/hooks/useDeleteWorkspace.tsx | 2 +- src-web/hooks/useDuplicateRequest.ts | 2 +- src-web/hooks/useEnvironments.ts | 2 +- src-web/hooks/useExportData.tsx | 2 +- src-web/hooks/useFilterResponse.ts | 2 +- src-web/hooks/useFolders.ts | 2 +- src-web/hooks/useGrpc.ts | 6 +- src-web/hooks/useImportData.tsx | 2 +- src-web/hooks/useRequests.ts | 2 +- src-web/hooks/useResponses.ts | 2 +- src-web/hooks/useSendAnyRequest.ts | 2 +- src-web/hooks/useSettings.ts | 2 +- src-web/hooks/useUpdateAnyFolder.ts | 2 +- src-web/hooks/useUpdateAnyRequest.ts | 2 +- src-web/hooks/useUpdateCookieJar.ts | 2 +- src-web/hooks/useUpdateEnvironment.ts | 2 +- src-web/hooks/useUpdateSettings.ts | 2 +- src-web/hooks/useUpdateWorkspace.ts | 2 +- src-web/hooks/useVariables.ts | 2 +- src-web/hooks/useWorkspaces.ts | 2 +- src-web/lib/analytics.ts | 2 +- src-web/lib/keyValueStore.ts | 4 +- src-web/lib/sendEphemeralRequest.ts | 2 +- src-web/lib/store.ts | 12 +- src-web/main.tsx | 11 +- 46 files changed, 534 insertions(+), 540 deletions(-) diff --git a/src-tauri/grpc/src/lib.rs b/src-tauri/grpc/src/lib.rs index 56dcf85a..9de2b164 100644 --- a/src-tauri/grpc/src/lib.rs +++ b/src-tauri/grpc/src/lib.rs @@ -1,8 +1,7 @@ -use prost::Message; use prost_reflect::{DynamicMessage, SerializeOptions}; use serde::{Deserialize, Serialize}; use serde_json::Deserializer; -use tokio_stream::{Stream, StreamExt}; +use tokio_stream::Stream; use tonic::transport::Uri; use tonic::{IntoRequest, Response, Streaming}; @@ -77,7 +76,7 @@ impl Stream for ClientStream { fn poll_next( self: std::pin::Pin<&mut Self>, - cx: &mut std::task::Context<'_>, + _cx: &mut std::task::Context<'_>, ) -> std::task::Poll> { println!("poll_next"); todo!() diff --git a/src-tauri/src/analytics.rs b/src-tauri/src/analytics.rs index b00da023..53a71afb 100644 --- a/src-tauri/src/analytics.rs +++ b/src-tauri/src/analytics.rs @@ -3,8 +3,7 @@ use serde::{Deserialize, Serialize}; use serde_json::json; use sqlx::{Pool, Sqlite}; use sqlx::types::JsonValue; -use tauri::{AppHandle, Manager, State}; -use tokio::sync::Mutex; +use tauri::{AppHandle, Manager}; use crate::{is_dev, models}; @@ -126,17 +125,15 @@ pub struct LaunchEventInfo { pub num_launches: i32, } -pub async fn track_launch_event(app_handle: &AppHandle) -> LaunchEventInfo { +pub async fn track_launch_event(app_handle: &AppHandle, db: &Pool) -> LaunchEventInfo { let namespace = "analytics"; let last_tracked_version_key = "last_tracked_version"; - let db_instance: State<'_, Mutex>> = app_handle.state(); - let pool = &*db_instance.lock().await; let mut info = LaunchEventInfo::default(); - info.num_launches = models::get_key_value_int(namespace, "num_launches", 0, pool).await + 1; + info.num_launches = models::get_key_value_int(db, namespace, "num_launches", 0).await + 1; info.previous_version = - models::get_key_value_string(namespace, last_tracked_version_key, "", pool).await; + models::get_key_value_string(db, namespace, last_tracked_version_key, "").await; info.current_version = app_handle.package_info().version.to_string(); if info.previous_version.is_empty() { @@ -167,19 +164,18 @@ pub async fn track_launch_event(app_handle: &AppHandle) -> LaunchEventInfo { AnalyticsAction::Launch, Some(json!({ "num_launches": info.num_launches })), ) - .await; - + .await; // Update key values models::set_key_value_string( + db, namespace, last_tracked_version_key, info.current_version.as_str(), - pool, ) .await; - models::set_key_value_int(namespace, "num_launches", info.num_launches, pool).await; + models::set_key_value_int(db, namespace, "num_launches", info.num_launches).await; info } diff --git a/src-tauri/src/http.rs b/src-tauri/src/http.rs index 77561d2b..d9b677e7 100644 --- a/src-tauri/src/http.rs +++ b/src-tauri/src/http.rs @@ -19,16 +19,16 @@ use tauri::{AppHandle, Wry}; use crate::{emit_side_effect, models, render, response_err}; pub async fn send_http_request( + app_handle: &AppHandle, + db: &Pool, request: models::HttpRequest, response: &models::HttpResponse, environment: Option, cookie_jar: Option, - app_handle: &AppHandle, - pool: &Pool, download_path: Option, ) -> Result { let environment_ref = environment.as_ref(); - let workspace = models::get_workspace(&request.workspace_id, pool) + let workspace = models::get_workspace(db, &request.workspace_id) .await .expect("Failed to get Workspace"); @@ -88,7 +88,7 @@ pub async fn send_http_request( let url = match Url::from_str(url_string.as_str()) { Ok(u) => u, Err(e) => { - return response_err(response, e.to_string(), app_handle, pool).await; + return response_err(response, e.to_string(), app_handle, db).await; } }; @@ -293,7 +293,7 @@ pub async fn send_http_request( let sendable_req = match request_builder.build() { Ok(r) => r, Err(e) => { - return response_err(response, e.to_string(), app_handle, pool).await; + return response_err(response, e.to_string(), app_handle, db).await; } }; @@ -362,7 +362,7 @@ pub async fn send_http_request( ); } - response = models::update_response_if_id(&response, pool) + response = models::update_response_if_id(db, &response) .await .expect("Failed to update response"); if !request.id.is_empty() { @@ -397,7 +397,7 @@ pub async fn send_http_request( .collect::>(), ); cookie_jar.cookies = json_cookies; - match models::upsert_cookie_jar(pool, &cookie_jar).await { + match models::upsert_cookie_jar(db, &cookie_jar).await { Ok(updated_jar) => { emit_side_effect(app_handle, "updated_model", &updated_jar); } @@ -409,6 +409,6 @@ pub async fn send_http_request( Ok(response) } - Err(e) => response_err(response, e.to_string(), app_handle, pool).await, + Err(e) => response_err(response, e.to_string(), app_handle, db).await, } } diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index fb1e285c..17664d81 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -39,6 +39,7 @@ use window_ext::TrafficLightWindowExt; use crate::analytics::{AnalyticsAction, AnalyticsResource}; use crate::http::send_http_request; +use crate::models::{cancel_pending_responses, CookieJar, create_response, delete_all_responses, delete_cookie_jar, delete_environment, delete_folder, delete_request, delete_response, delete_workspace, duplicate_request, Environment, EnvironmentVariable, find_cookie_jars, find_environments, find_folders, find_requests, find_responses, find_workspaces, Folder, get_cookie_jar, get_environment, get_folder, get_key_value_raw, get_or_create_settings, get_request, get_response, get_workspace, get_workspace_export_resources, HttpRequest, HttpResponse, KeyValue, set_key_value_raw, Settings, update_response_if_id, update_settings, upsert_cookie_jar, upsert_environment, upsert_folder, upsert_request, upsert_workspace, Workspace}; use crate::plugin::{ImportResources, ImportResult}; use crate::updates::{update_mode_from_str, UpdateMode, YaakUpdater}; @@ -63,11 +64,8 @@ pub struct CustomResponse { pub status_reason: Option<&'static str>, } -async fn migrate_db( - app_handle: AppHandle, - db_instance: &Mutex>, -) -> Result<(), String> { - let pool = &*db_instance.lock().await; +async fn migrate_db(app_handle: AppHandle, db: &Mutex>) -> Result<(), String> { + let pool = &*db.lock().await; let p = app_handle .path_resolver() .resolve_resource("migrations") @@ -80,10 +78,10 @@ async fn migrate_db( } #[tauri::command] -async fn grpc_reflect( +async fn cmd_grpc_reflect( endpoint: &str, // app_handle: AppHandle, - // db_instance: State<'_, Mutex>>, + // db_state: State<'_, Mutex>>, ) -> Result, String> { let uri = if endpoint.starts_with("http://") || endpoint.starts_with("https://") { Uri::from_str(endpoint).map_err(|e| e.to_string())? @@ -94,13 +92,13 @@ async fn grpc_reflect( } #[tauri::command] -async fn grpc_call_unary( +async fn cmd_grpc_call_unary( endpoint: &str, service: &str, method: &str, message: &str, // app_handle: AppHandle, - // db_instance: State<'_, Mutex>>, + // db_state: State<'_, Mutex>>, ) -> Result { let uri = if endpoint.starts_with("http://") || endpoint.starts_with("https://") { Uri::from_str(endpoint).map_err(|e| e.to_string())? @@ -111,13 +109,13 @@ async fn grpc_call_unary( } #[tauri::command] -async fn grpc_client_streaming( +async fn cmd_grpc_client_streaming( endpoint: &str, service: &str, method: &str, message: &str, // app_handle: AppHandle, - // db_instance: State<'_, Mutex>>, + // db_state: State<'_, Mutex>>, ) -> Result { let uri = if endpoint.starts_with("http://") || endpoint.starts_with("https://") { Uri::from_str(endpoint).map_err(|e| e.to_string())? @@ -128,13 +126,12 @@ async fn grpc_client_streaming( } #[tauri::command] -async fn grpc_server_streaming( +async fn cmd_grpc_server_streaming( endpoint: &str, service: &str, method: &str, message: &str, app_handle: AppHandle, - // db_instance: State<'_, Mutex>>, ) -> Result { let uri = if endpoint.starts_with("http://") || endpoint.starts_with("https://") { Uri::from_str(endpoint).map_err(|e| e.to_string())? @@ -163,19 +160,19 @@ async fn grpc_server_streaming( } #[tauri::command] -async fn send_ephemeral_request( - mut request: models::HttpRequest, +async fn cmd_send_ephemeral_request( + mut request: HttpRequest, environment_id: Option<&str>, cookie_jar_id: Option<&str>, app_handle: AppHandle, - db_instance: State<'_, Mutex>>, -) -> Result { - let pool = &*db_instance.lock().await; - let response = models::HttpResponse::new(); + db_state: State<'_, Mutex>>, +) -> Result { + let db = &*db_state.lock().await; + let response = HttpResponse::new(); request.id = "".to_string(); let environment = match environment_id { Some(id) => Some( - models::get_environment(id, pool) + get_environment(db, id) .await .expect("Failed to get environment"), ), @@ -183,7 +180,7 @@ async fn send_ephemeral_request( }; let cookie_jar = match cookie_jar_id { Some(id) => Some( - models::get_cookie_jar(id, pool) + get_cookie_jar(db, id) .await .expect("Failed to get cookie jar"), ), @@ -192,26 +189,26 @@ async fn send_ephemeral_request( // let cookie_jar_id2 = cookie_jar_id.unwrap_or("").to_string(); send_http_request( + &app_handle, + db, request, &response, environment, cookie_jar, - &app_handle, - pool, None, ) .await } #[tauri::command] -async fn filter_response( +async fn cmd_filter_response( window: Window, - db_instance: State<'_, Mutex>>, + db_state: State<'_, Mutex>>, response_id: &str, filter: &str, ) -> Result { - let pool = &*db_instance.lock().await; - let response = models::get_response(response_id, pool) + let db = &*db_state.lock().await; + let response = get_response(db, response_id) .await .expect("Failed to get response"); @@ -242,12 +239,12 @@ async fn filter_response( } #[tauri::command] -async fn import_data( +async fn cmd_import_data( window: Window, - db_instance: State<'_, Mutex>>, + db_state: State<'_, Mutex>>, file_paths: Vec<&str>, ) -> Result { - let pool = &*db_instance.lock().await; + let db = &*db_state.lock().await; let mut result: Option = None; let plugins = vec!["importer-yaak", "importer-insomnia", "importer-postman"]; for plugin_name in plugins { @@ -277,7 +274,7 @@ async fn import_data( info!("Importing resources"); for w in r.resources.workspaces { - let x = models::upsert_workspace(pool, w) + let x = upsert_workspace(db, w) .await .expect("Failed to create workspace"); imported_resources.workspaces.push(x.clone()); @@ -285,7 +282,7 @@ async fn import_data( } for e in r.resources.environments { - let x = models::upsert_environment(pool, e) + let x = upsert_environment(db, e) .await .expect("Failed to create environment"); imported_resources.environments.push(x.clone()); @@ -293,7 +290,7 @@ async fn import_data( } for f in r.resources.folders { - let x = models::upsert_folder(pool, f) + let x = upsert_folder(db, f) .await .expect("Failed to create folder"); imported_resources.folders.push(x.clone()); @@ -301,7 +298,7 @@ async fn import_data( } for r in r.resources.requests { - let x = models::upsert_request(pool, r) + let x = upsert_request(db, r) .await .expect("Failed to create request"); imported_resources.requests.push(x.clone()); @@ -314,14 +311,15 @@ async fn import_data( } #[tauri::command] -async fn export_data( +async fn cmd_export_data( app_handle: AppHandle, - db_instance: State<'_, Mutex>>, + db_state: State<'_, Mutex>>, export_path: &str, workspace_id: &str, ) -> Result<(), String> { - let pool = &*db_instance.lock().await; - let export_data = models::get_workspace_export_resources(&app_handle, pool, workspace_id).await; + let db = &*db_state.lock().await; + let export_data = + get_workspace_export_resources(&app_handle, db, workspace_id).await; let f = File::options() .create(true) .truncate(true) @@ -346,24 +344,24 @@ async fn export_data( } #[tauri::command] -async fn send_request( +async fn cmd_send_request( window: Window, - db_instance: State<'_, Mutex>>, + db_state: State<'_, Mutex>>, request_id: &str, environment_id: Option<&str>, cookie_jar_id: Option<&str>, download_dir: Option<&str>, -) -> Result { - let pool = &*db_instance.lock().await; +) -> Result { + let db = &*db_state.lock().await; let app_handle = window.app_handle(); - let request = models::get_request(request_id, pool) + let request = get_request(db, request_id) .await .expect("Failed to get request"); let environment = match environment_id { Some(id) => Some( - models::get_environment(id, pool) + get_environment(db, id) .await .expect("Failed to get environment"), ), @@ -372,14 +370,15 @@ async fn send_request( let cookie_jar = match cookie_jar_id { Some(id) => Some( - models::get_cookie_jar(id, pool) + get_cookie_jar(db, id) .await .expect("Failed to get cookie jar"), ), None => None, }; - let response = models::create_response( + let response = create_response( + db, &request.id, 0, 0, @@ -391,7 +390,6 @@ async fn send_request( vec![], None, None, - pool, ) .await .expect("Failed to create response"); @@ -405,27 +403,27 @@ async fn send_request( emit_side_effect(&app_handle, "created_model", response.clone()); send_http_request( + &app_handle, + db, request.clone(), &response, environment, cookie_jar, - &app_handle, - &pool, download_path, ) .await } async fn response_err( - response: &models::HttpResponse, + response: &HttpResponse, error: String, app_handle: &AppHandle, - pool: &Pool, -) -> Result { + db: &Pool, +) -> Result { let mut response = response.clone(); response.elapsed = -1; response.error = Some(error.clone()); - response = models::update_response_if_id(&response, pool) + response = update_response_if_id(db, &response) .await .expect("Failed to update response"); emit_side_effect(app_handle, "updated_model", &response); @@ -433,7 +431,7 @@ async fn response_err( } #[tauri::command] -async fn track_event( +async fn cmd_track_event( window: Window, resource: &str, action: &str, @@ -455,35 +453,35 @@ async fn track_event( } #[tauri::command] -async fn set_update_mode( +async fn cmd_set_update_mode( update_mode: &str, window: Window, - db_instance: State<'_, Mutex>>, -) -> Result { - set_key_value("app", "update_mode", update_mode, window, db_instance).await + db_state: State<'_, Mutex>>, +) -> Result { + cmd_set_key_value("app", "update_mode", update_mode, window, db_state).await } #[tauri::command] -async fn get_key_value( +async fn cmd_get_key_value( namespace: &str, key: &str, - db_instance: State<'_, Mutex>>, -) -> Result, ()> { - let pool = &*db_instance.lock().await; - let result = models::get_key_value_raw(namespace, key, pool).await; + db_state: State<'_, Mutex>>, +) -> Result, ()> { + let db = &*db_state.lock().await; + let result = get_key_value_raw(db, namespace, key).await; Ok(result) } #[tauri::command] -async fn set_key_value( +async fn cmd_set_key_value( namespace: &str, key: &str, value: &str, window: Window, - db_instance: State<'_, Mutex>>, -) -> Result { - let pool = &*db_instance.lock().await; - let (key_value, created) = models::set_key_value_raw(namespace, key, value, pool).await; + db_state: State<'_, Mutex>>, +) -> Result { + let db = &*db_state.lock().await; + let (key_value, created) = set_key_value_raw(db, namespace, key, value).await; if created { emit_and_return(&window, "created_model", key_value) @@ -493,14 +491,14 @@ async fn set_key_value( } #[tauri::command] -async fn create_workspace( +async fn cmd_create_workspace( name: &str, window: Window, - db_instance: State<'_, Mutex>>, -) -> Result { - let pool = &*db_instance.lock().await; + db_state: State<'_, Mutex>>, +) -> Result { + let db = &*db_state.lock().await; let created_workspace = - models::upsert_workspace(pool, models::Workspace::new(name.to_string())) + upsert_workspace(db, Workspace::new(name.to_string())) .await .expect("Failed to create Workspace"); @@ -508,15 +506,15 @@ async fn create_workspace( } #[tauri::command] -async fn update_cookie_jar( - cookie_jar: models::CookieJar, +async fn cmd_update_cookie_jar( + cookie_jar: CookieJar, window: Window, - db_instance: State<'_, Mutex>>, -) -> Result { - let pool = &*db_instance.lock().await; + db_state: State<'_, Mutex>>, +) -> Result { + let db = &*db_state.lock().await; println!("Updating cookie jar {}", cookie_jar.cookies.len()); - let updated = models::upsert_cookie_jar(pool, &cookie_jar) + let updated = upsert_cookie_jar(db, &cookie_jar) .await .expect("Failed to update cookie jar"); @@ -524,29 +522,29 @@ async fn update_cookie_jar( } #[tauri::command] -async fn delete_cookie_jar( +async fn cmd_delete_cookie_jar( window: Window, - db_instance: State<'_, Mutex>>, + db_state: State<'_, Mutex>>, cookie_jar_id: &str, -) -> Result { - let pool = &*db_instance.lock().await; - let req = models::delete_cookie_jar(cookie_jar_id, pool) +) -> Result { + let db = &*db_state.lock().await; + let req = delete_cookie_jar(db, cookie_jar_id) .await .expect("Failed to delete cookie jar"); emit_and_return(&window, "deleted_model", req) } #[tauri::command] -async fn create_cookie_jar( +async fn cmd_create_cookie_jar( workspace_id: &str, name: &str, window: Window, - db_instance: State<'_, Mutex>>, -) -> Result { - let pool = &*db_instance.lock().await; - let created_cookie_jar = models::upsert_cookie_jar( - pool, - &models::CookieJar { + db_state: State<'_, Mutex>>, +) -> Result { + let db = &*db_state.lock().await; + let created_cookie_jar = upsert_cookie_jar( + db, + &CookieJar { name: name.to_string(), workspace_id: workspace_id.to_string(), ..Default::default() @@ -559,17 +557,17 @@ async fn create_cookie_jar( } #[tauri::command] -async fn create_environment( +async fn cmd_create_environment( workspace_id: &str, name: &str, - variables: Vec, + variables: Vec, window: Window, - db_instance: State<'_, Mutex>>, -) -> Result { - let pool = &*db_instance.lock().await; - let created_environment = models::upsert_environment( - pool, - models::Environment { + db_state: State<'_, Mutex>>, +) -> Result { + let db = &*db_state.lock().await; + let created_environment = upsert_environment( + db, + Environment { workspace_id: workspace_id.to_string(), name: name.to_string(), variables: Json(variables), @@ -583,18 +581,18 @@ async fn create_environment( } #[tauri::command] -async fn create_request( +async fn cmd_create_request( workspace_id: &str, name: &str, sort_priority: f64, folder_id: Option<&str>, window: Window, - db_instance: State<'_, Mutex>>, -) -> Result { - let pool = &*db_instance.lock().await; - let created_request = models::upsert_request( - pool, - models::HttpRequest { + db_state: State<'_, Mutex>>, +) -> Result { + let db = &*db_state.lock().await; + let created_request = upsert_request( + db, + HttpRequest { workspace_id: workspace_id.to_string(), name: name.to_string(), method: "GET".to_string(), @@ -610,27 +608,26 @@ async fn create_request( } #[tauri::command] -async fn duplicate_request( +async fn cmd_duplicate_request( id: &str, window: Window, - db_instance: State<'_, Mutex>>, -) -> Result { - let pool = &*db_instance.lock().await; - let request = models::duplicate_request(id, pool) + db_state: State<'_, Mutex>>, +) -> Result { + let db = &*db_state.lock().await; + let request = duplicate_request(db, id) .await .expect("Failed to duplicate request"); emit_and_return(&window, "updated_model", request) } #[tauri::command] -async fn update_workspace( - workspace: models::Workspace, +async fn cmd_update_workspace( + workspace: Workspace, window: Window, - db_instance: State<'_, Mutex>>, -) -> Result { - let pool = &*db_instance.lock().await; - - let updated_workspace = models::upsert_workspace(pool, workspace) + db_state: State<'_, Mutex>>, +) -> Result { + let db = &*db_state.lock().await; + let updated_workspace = upsert_workspace(db, workspace) .await .expect("Failed to update request"); @@ -638,14 +635,13 @@ async fn update_workspace( } #[tauri::command] -async fn update_environment( - environment: models::Environment, +async fn cmd_update_environment( + environment: Environment, window: Window, - db_instance: State<'_, Mutex>>, -) -> Result { - let pool = &*db_instance.lock().await; - - let updated_environment = models::upsert_environment(pool, environment) + db_state: State<'_, Mutex>>, +) -> Result { + let db = &*db_state.lock().await; + let updated_environment = upsert_environment(db, environment) .await .expect("Failed to update environment"); @@ -653,55 +649,55 @@ async fn update_environment( } #[tauri::command] -async fn update_request( - request: models::HttpRequest, +async fn cmd_update_request( + request: HttpRequest, window: Window, - db_instance: State<'_, Mutex>>, -) -> Result { - let pool = &*db_instance.lock().await; - let updated_request = models::upsert_request(pool, request) + db_state: State<'_, Mutex>>, +) -> Result { + let db = &*db_state.lock().await; + let updated_request = upsert_request(db, request) .await .expect("Failed to update request"); emit_and_return(&window, "updated_model", updated_request) } #[tauri::command] -async fn delete_request( +async fn cmd_delete_request( window: Window, - db_instance: State<'_, Mutex>>, + db_state: State<'_, Mutex>>, request_id: &str, -) -> Result { - let pool = &*db_instance.lock().await; - let req = models::delete_request(request_id, pool) +) -> Result { + let db = &*db_state.lock().await; + let req = delete_request(db, request_id) .await .expect("Failed to delete request"); emit_and_return(&window, "deleted_model", req) } #[tauri::command] -async fn list_folders( +async fn cmd_list_folders( workspace_id: &str, - db_instance: State<'_, Mutex>>, -) -> Result, String> { - let pool = &*db_instance.lock().await; - models::find_folders(workspace_id, pool) + db_state: State<'_, Mutex>>, +) -> Result, String> { + let db = &*db_state.lock().await; + find_folders(db, workspace_id) .await .map_err(|e| e.to_string()) } #[tauri::command] -async fn create_folder( +async fn cmd_create_folder( workspace_id: &str, name: &str, sort_priority: f64, folder_id: Option<&str>, window: Window, - db_instance: State<'_, Mutex>>, -) -> Result { - let pool = &*db_instance.lock().await; - let created_request = models::upsert_folder( - pool, - models::Folder { + db_state: State<'_, Mutex>>, +) -> Result { + let db = &*db_state.lock().await; + let created_request = upsert_folder( + db, + Folder { workspace_id: workspace_id.to_string(), name: name.to_string(), folder_id: folder_id.map(|s| s.to_string()), @@ -716,51 +712,51 @@ async fn create_folder( } #[tauri::command] -async fn update_folder( - folder: models::Folder, +async fn cmd_update_folder( + folder: Folder, window: Window, - db_instance: State<'_, Mutex>>, -) -> Result { - let pool = &*db_instance.lock().await; - let updated_folder = models::upsert_folder(pool, folder) + db_state: State<'_, Mutex>>, +) -> Result { + let db = &*db_state.lock().await; + let updated_folder = upsert_folder(db, folder) .await .expect("Failed to update request"); emit_and_return(&window, "updated_model", updated_folder) } #[tauri::command] -async fn delete_folder( +async fn cmd_delete_folder( window: Window, - db_instance: State<'_, Mutex>>, + db_state: State<'_, Mutex>>, folder_id: &str, -) -> Result { - let pool = &*db_instance.lock().await; - let req = models::delete_folder(folder_id, pool) +) -> Result { + let db = &*db_state.lock().await; + let req = delete_folder(db, folder_id) .await .expect("Failed to delete folder"); emit_and_return(&window, "deleted_model", req) } #[tauri::command] -async fn delete_environment( +async fn cmd_delete_environment( window: Window, - db_instance: State<'_, Mutex>>, + db_state: State<'_, Mutex>>, environment_id: &str, -) -> Result { - let pool = &*db_instance.lock().await; - let req = models::delete_environment(environment_id, pool) +) -> Result { + let db = &*db_state.lock().await; + let req = delete_environment(db, environment_id) .await .expect("Failed to delete environment"); emit_and_return(&window, "deleted_model", req) } #[tauri::command] -async fn list_requests( +async fn cmd_list_requests( workspace_id: &str, - db_instance: State<'_, Mutex>>, -) -> Result, String> { - let pool = &*db_instance.lock().await; - let requests = models::find_requests(workspace_id, pool) + db_state: State<'_, Mutex>>, +) -> Result, String> { + let db = &*db_state.lock().await; + let requests = find_requests(db, workspace_id) .await .expect("Failed to find requests"); // .map_err(|e| e.to_string()) @@ -768,12 +764,12 @@ async fn list_requests( } #[tauri::command] -async fn list_environments( +async fn cmd_list_environments( workspace_id: &str, - db_instance: State<'_, Mutex>>, -) -> Result, String> { - let pool = &*db_instance.lock().await; - let environments = models::find_environments(workspace_id, pool) + db_state: State<'_, Mutex>>, +) -> Result, String> { + let db = &*db_state.lock().await; + let environments = find_environments(db, workspace_id) .await .expect("Failed to find environments"); @@ -781,20 +777,19 @@ async fn list_environments( } #[tauri::command] -async fn get_settings(db_instance: State<'_, Mutex>>) -> Result { - let pool = &*db_instance.lock().await; - Ok(models::get_or_create_settings(pool).await) +async fn cmd_get_settings(db_state: State<'_, Mutex>>) -> Result { + let db = &*db_state.lock().await; + Ok(get_or_create_settings(db).await) } #[tauri::command] -async fn update_settings( - settings: models::Settings, +async fn cmd_update_settings( + settings: Settings, window: Window, - db_instance: State<'_, Mutex>>, -) -> Result { - let pool = &*db_instance.lock().await; - - let updated_settings = models::update_settings(pool, settings) + db_state: State<'_, Mutex>>, +) -> Result { + let db = &*db_state.lock().await; + let updated_settings = update_settings(db, settings) .await .expect("Failed to update settings"); @@ -802,52 +797,49 @@ async fn update_settings( } #[tauri::command] -async fn get_folder( - id: &str, - db_instance: State<'_, Mutex>>, -) -> Result { - let pool = &*db_instance.lock().await; - models::get_folder(id, pool) +async fn cmd_get_folder(id: &str, db_state: State<'_, Mutex>>) -> Result { + let db = &*db_state.lock().await; + get_folder(db, id) .await .map_err(|e| e.to_string()) } #[tauri::command] -async fn get_request( +async fn cmd_get_request( id: &str, - db_instance: State<'_, Mutex>>, -) -> Result { - let pool = &*db_instance.lock().await; - models::get_request(id, pool) + db_state: State<'_, Mutex>>, +) -> Result { + let db = &*db_state.lock().await; + get_request(db, id) .await .map_err(|e| e.to_string()) } #[tauri::command] -async fn get_cookie_jar( +async fn cmd_get_cookie_jar( id: &str, - db_instance: State<'_, Mutex>>, -) -> Result { - let pool = &*db_instance.lock().await; - models::get_cookie_jar(id, pool) + db_state: State<'_, Mutex>>, +) -> Result { + let db = &*db_state.lock().await; + get_cookie_jar(db, id) .await .map_err(|e| e.to_string()) } #[tauri::command] -async fn list_cookie_jars( +async fn cmd_list_cookie_jars( workspace_id: &str, - db_instance: State<'_, Mutex>>, -) -> Result, String> { - let pool = &*db_instance.lock().await; - let cookie_jars = models::find_cookie_jars(workspace_id, pool) + db_state: State<'_, Mutex>>, +) -> Result, String> { + let db = &*db_state.lock().await; + let cookie_jars = find_cookie_jars(db, workspace_id) .await .expect("Failed to find cookie jars"); if cookie_jars.is_empty() { - let cookie_jar = models::upsert_cookie_jar( - pool, - &models::CookieJar { + let cookie_jar = upsert_cookie_jar( + db, + &CookieJar { name: "Default".to_string(), workspace_id: workspace_id.to_string(), ..Default::default() @@ -862,75 +854,75 @@ async fn list_cookie_jars( } #[tauri::command] -async fn get_environment( +async fn cmd_get_environment( id: &str, - db_instance: State<'_, Mutex>>, -) -> Result { - let pool = &*db_instance.lock().await; - models::get_environment(id, pool) + db_state: State<'_, Mutex>>, +) -> Result { + let db = &*db_state.lock().await; + get_environment(db, id) .await .map_err(|e| e.to_string()) } #[tauri::command] -async fn get_workspace( +async fn cmd_get_workspace( id: &str, - db_instance: State<'_, Mutex>>, -) -> Result { - let pool = &*db_instance.lock().await; - models::get_workspace(id, pool) + db_state: State<'_, Mutex>>, +) -> Result { + let db = &*db_state.lock().await; + get_workspace(db, id) .await .map_err(|e| e.to_string()) } #[tauri::command] -async fn list_responses( +async fn cmd_list_responses( request_id: &str, limit: Option, - db_instance: State<'_, Mutex>>, -) -> Result, String> { - let pool = &*db_instance.lock().await; - models::find_responses(request_id, limit, pool) + db_state: State<'_, Mutex>>, +) -> Result, String> { + let db = &*db_state.lock().await; + find_responses(db, request_id, limit) .await .map_err(|e| e.to_string()) } #[tauri::command] -async fn delete_response( +async fn cmd_delete_response( id: &str, window: Window, - db_instance: State<'_, Mutex>>, -) -> Result { - let pool = &*db_instance.lock().await; - let response = models::delete_response(id, pool) + db_state: State<'_, Mutex>>, +) -> Result { + let db = &*db_state.lock().await; + let response = delete_response(db, id) .await .expect("Failed to delete response"); emit_and_return(&window, "deleted_model", response) } #[tauri::command] -async fn delete_all_responses( +async fn cmd_delete_all_responses( request_id: &str, - db_instance: State<'_, Mutex>>, + db_state: State<'_, Mutex>>, ) -> Result<(), String> { - let pool = &*db_instance.lock().await; - models::delete_all_responses(request_id, pool) + let db = &*db_state.lock().await; + delete_all_responses(db, request_id) .await .map_err(|e| e.to_string()) } #[tauri::command] -async fn list_workspaces( - db_instance: State<'_, Mutex>>, -) -> Result, String> { - let pool = &*db_instance.lock().await; - let workspaces = models::find_workspaces(pool) +async fn cmd_list_workspaces( + db_state: State<'_, Mutex>>, +) -> Result, String> { + let db = &*db_state.lock().await; + let workspaces = find_workspaces(db) .await .expect("Failed to find workspaces"); if workspaces.is_empty() { - let workspace = models::upsert_workspace( - pool, - models::Workspace { + let workspace = upsert_workspace( + db, + Workspace { name: "Yaak".to_string(), ..Default::default() }, @@ -944,32 +936,32 @@ async fn list_workspaces( } #[tauri::command] -async fn new_window(window: Window, url: &str) -> Result<(), String> { +async fn cmd_new_window(window: Window, url: &str) -> Result<(), String> { create_window(&window.app_handle(), Some(url)); Ok(()) } #[tauri::command] -async fn delete_workspace( +async fn cmd_delete_workspace( window: Window, - db_instance: State<'_, Mutex>>, + db_state: State<'_, Mutex>>, workspace_id: &str, -) -> Result { - let pool = &*db_instance.lock().await; - let workspace = models::delete_workspace(workspace_id, pool) +) -> Result { + let db = &*db_state.lock().await; + let workspace = delete_workspace(db, workspace_id) .await .expect("Failed to delete Workspace"); emit_and_return(&window, "deleted_model", workspace) } #[tauri::command] -async fn check_for_updates( +async fn cmd_check_for_updates( app_handle: AppHandle, - db_instance: State<'_, Mutex>>, + db_state: State<'_, Mutex>>, yaak_updater: State<'_, Mutex>, ) -> Result { - let pool = &*db_instance.lock().await; - let update_mode = get_update_mode(pool).await; + let db = &*db_state.lock().await; + let update_mode = get_update_mode(db).await; yaak_updater .lock() .await @@ -1038,58 +1030,58 @@ fn main() { let yaak_updater = YaakUpdater::new(); app.manage(Mutex::new(yaak_updater)); - let _ = models::cancel_pending_responses(&pool).await; + let _ = cancel_pending_responses(&pool).await; Ok(()) }) }) .invoke_handler(tauri::generate_handler![ - check_for_updates, - create_cookie_jar, - create_environment, - create_folder, - create_request, - create_workspace, - delete_all_responses, - delete_cookie_jar, - delete_environment, - delete_folder, - delete_request, - delete_response, - delete_workspace, - duplicate_request, - export_data, - filter_response, - get_cookie_jar, - get_environment, - get_folder, - get_key_value, - get_request, - get_settings, - get_workspace, - grpc_call_unary, - grpc_client_streaming, - grpc_server_streaming, - grpc_reflect, - import_data, - list_cookie_jars, - list_environments, - list_folders, - list_requests, - list_responses, - list_workspaces, - new_window, - send_ephemeral_request, - send_request, - set_key_value, - set_update_mode, - track_event, - update_cookie_jar, - update_environment, - update_folder, - update_request, - update_settings, - update_workspace, + cmd_check_for_updates, + cmd_create_cookie_jar, + cmd_create_environment, + cmd_create_folder, + cmd_create_request, + cmd_create_workspace, + cmd_delete_all_responses, + cmd_delete_cookie_jar, + cmd_delete_environment, + cmd_delete_folder, + cmd_delete_request, + cmd_delete_response, + cmd_delete_workspace, + cmd_duplicate_request, + cmd_export_data, + cmd_filter_response, + cmd_get_cookie_jar, + cmd_get_environment, + cmd_get_folder, + cmd_get_key_value, + cmd_get_request, + cmd_get_settings, + cmd_get_workspace, + cmd_grpc_call_unary, + cmd_grpc_client_streaming, + cmd_grpc_server_streaming, + cmd_grpc_reflect, + cmd_import_data, + cmd_list_cookie_jars, + cmd_list_environments, + cmd_list_folders, + cmd_list_requests, + cmd_list_responses, + cmd_list_workspaces, + cmd_new_window, + cmd_send_ephemeral_request, + cmd_send_request, + cmd_set_key_value, + cmd_set_update_mode, + cmd_track_event, + cmd_update_cookie_jar, + cmd_update_environment, + cmd_update_folder, + cmd_update_request, + cmd_update_settings, + cmd_update_workspace, ]) .build(tauri::generate_context!()) .expect("error while running tauri application") @@ -1125,7 +1117,9 @@ fn main() { let h = app_handle.clone(); tauri::async_runtime::spawn(async move { - let info = analytics::track_launch_event(&h).await; + let db_state: State<'_, Mutex>> = h.state(); + let db = &*db_state.lock().await; + let info = analytics::track_launch_event(&h, db).await; info!("Launched Yaak {:?}", info); // Wait for window render and give a chance for the user to notice @@ -1144,9 +1138,9 @@ fn main() { // Run update check whenever window is focused tauri::async_runtime::spawn(async move { let val: State<'_, Mutex> = h.state(); - let db_instance: State<'_, Mutex>> = h.state(); - let pool = &*db_instance.lock().await; - let update_mode = get_update_mode(pool).await; + let db_state: State<'_, Mutex>> = h.state(); + let db = &*db_state.lock().await; + let update_mode = get_update_mode(&db).await; _ = val.lock().await.check(&h, update_mode).await; }); } @@ -1271,7 +1265,7 @@ fn emit_side_effect(app_handle: &AppHandle, event: &s app_handle.emit_all(event, &payload).unwrap(); } -async fn get_update_mode(pool: &Pool) -> UpdateMode { - let settings = models::get_or_create_settings(pool).await; +async fn get_update_mode(db: &Pool) -> UpdateMode { + let settings = get_or_create_settings(db).await; update_mode_from_str(settings.update_channel.as_str()) } diff --git a/src-tauri/src/models.rs b/src-tauri/src/models.rs index cac82da4..5621c7b2 100644 --- a/src-tauri/src/models.rs +++ b/src-tauri/src/models.rs @@ -58,9 +58,7 @@ impl Workspace { } #[derive(sqlx::FromRow, Debug, Clone, Serialize, Deserialize, Default)] -pub struct CookieX { - -} +pub struct CookieX {} #[derive(sqlx::FromRow, Debug, Clone, Serialize, Deserialize, Default)] #[serde(default, rename_all = "camelCase")] @@ -260,32 +258,32 @@ pub struct KeyValue { } pub async fn set_key_value_string( + db: &Pool, namespace: &str, key: &str, value: &str, - pool: &Pool, ) -> (KeyValue, bool) { let encoded = serde_json::to_string(value); - set_key_value_raw(namespace, key, &encoded.unwrap(), pool).await + set_key_value_raw(db, namespace, key, &encoded.unwrap()).await } pub async fn set_key_value_int( + db: &Pool, namespace: &str, key: &str, value: i32, - pool: &Pool, ) -> (KeyValue, bool) { let encoded = serde_json::to_string(&value); - set_key_value_raw(namespace, key, &encoded.unwrap(), pool).await + set_key_value_raw(db, namespace, key, &encoded.unwrap()).await } pub async fn get_key_value_string( + db: &Pool, namespace: &str, key: &str, default: &str, - pool: &Pool, ) -> String { - match get_key_value_raw(namespace, key, pool).await { + match get_key_value_raw(db, namespace, key).await { None => default.to_string(), Some(v) => { let result = serde_json::from_str(&v.value); @@ -296,17 +294,12 @@ pub async fn get_key_value_string( default.to_string() } } - }, + } } } -pub async fn get_key_value_int( - namespace: &str, - key: &str, - default: i32, - pool: &Pool, -) -> i32 { - match get_key_value_raw(namespace, key, pool).await { +pub async fn get_key_value_int(db: &Pool, namespace: &str, key: &str, default: i32) -> i32 { + match get_key_value_raw(db, namespace, key).await { None => default.clone(), Some(v) => { let result = serde_json::from_str(&v.value); @@ -317,17 +310,17 @@ pub async fn get_key_value_int( default.clone() } } - }, + } } } pub async fn set_key_value_raw( + db: &Pool, namespace: &str, key: &str, value: &str, - pool: &Pool, ) -> (KeyValue, bool) { - let existing = get_key_value_raw(namespace, key, pool).await; + let existing = get_key_value_raw(db, namespace, key).await; sqlx::query!( r#" INSERT INTO key_values (namespace, key, value) @@ -339,17 +332,17 @@ pub async fn set_key_value_raw( key, value, ) - .execute(pool) + .execute(db) .await .expect("Failed to insert key value"); - let kv = get_key_value_raw(namespace, key, pool) + let kv = get_key_value_raw(db, namespace, key) .await .expect("Failed to get key value"); (kv, existing.is_none()) } -pub async fn get_key_value_raw(namespace: &str, key: &str, pool: &Pool) -> Option { +pub async fn get_key_value_raw(db: &Pool, namespace: &str, key: &str) -> Option { sqlx::query_as!( KeyValue, r#" @@ -360,12 +353,12 @@ pub async fn get_key_value_raw(namespace: &str, key: &str, pool: &Pool) namespace, key, ) - .fetch_one(pool) + .fetch_one(db) .await .ok() } -pub async fn find_workspaces(pool: &Pool) -> Result, sqlx::Error> { +pub async fn find_workspaces(db: &Pool) -> Result, sqlx::Error> { sqlx::query_as!( Workspace, r#" @@ -383,11 +376,11 @@ pub async fn find_workspaces(pool: &Pool) -> Result, sqlx FROM workspaces "#, ) - .fetch_all(pool) + .fetch_all(db) .await } -pub async fn get_workspace(id: &str, pool: &Pool) -> Result { +pub async fn get_workspace(db: &Pool, id: &str) -> Result { sqlx::query_as!( Workspace, r#" @@ -406,12 +399,12 @@ pub async fn get_workspace(id: &str, pool: &Pool) -> Result) -> Result { - let workspace = get_workspace(id, pool).await?; +pub async fn delete_workspace(db: &Pool, id: &str) -> Result { + let workspace = get_workspace(db, id).await?; let _ = sqlx::query!( r#" DELETE FROM workspaces @@ -419,17 +412,17 @@ pub async fn delete_workspace(id: &str, pool: &Pool) -> Result) -> Result { +pub async fn get_cookie_jar(db: &Pool, id: &str) -> Result { sqlx::query_as!( CookieJar, r#" @@ -445,11 +438,14 @@ pub async fn get_cookie_jar(id: &str, pool: &Pool) -> Result) -> Result, sqlx::Error> { +pub async fn find_cookie_jars( + db: &Pool, + workspace_id: &str, +) -> Result, sqlx::Error> { sqlx::query_as!( CookieJar, r#" @@ -465,12 +461,12 @@ pub async fn find_cookie_jars(workspace_id: &str, pool: &Pool) -> Result "#, workspace_id, ) - .fetch_all(pool) - .await + .fetch_all(db) + .await } -pub async fn delete_cookie_jar(id: &str, pool: &Pool) -> Result { - let cookie_jar = get_cookie_jar(id, pool).await?; +pub async fn delete_cookie_jar(db: &Pool, id: &str) -> Result { + let cookie_jar = get_cookie_jar(db, id).await?; let _ = sqlx::query!( r#" @@ -479,14 +475,14 @@ pub async fn delete_cookie_jar(id: &str, pool: &Pool) -> Result, + db: &Pool, cookie_jar: &CookieJar, ) -> Result { let id = match cookie_jar.id.as_str() { @@ -513,15 +509,15 @@ pub async fn upsert_cookie_jar( trimmed_name, cookie_jar.cookies, ) - .execute(pool) - .await?; + .execute(db) + .await?; - get_cookie_jar(&id, pool).await + get_cookie_jar(db, &id).await } pub async fn find_environments( + db: &Pool, workspace_id: &str, - pool: &Pool, ) -> Result, sqlx::Error> { sqlx::query_as!( Environment, @@ -533,12 +529,12 @@ pub async fn find_environments( "#, workspace_id, ) - .fetch_all(pool) + .fetch_all(db) .await } -pub async fn delete_environment(id: &str, pool: &Pool) -> Result { - let env = get_environment(id, pool).await?; +pub async fn delete_environment(db: &Pool, id: &str) -> Result { + let env = get_environment(db, id).await?; let _ = sqlx::query!( r#" DELETE FROM environments @@ -546,13 +542,13 @@ pub async fn delete_environment(id: &str, pool: &Pool) -> Result) -> Result { +async fn get_settings(db: &Pool) -> Result { sqlx::query_as!( Settings, r#" @@ -568,28 +564,30 @@ async fn get_settings(pool: &Pool) -> Result { WHERE id = 'default' "#, ) - .fetch_one(pool) + .fetch_one(db) .await } -pub async fn get_or_create_settings(pool: &Pool) -> Settings { - if let Ok(settings) = get_settings(pool).await { - settings - } else { - sqlx::query!( - r#" +pub async fn get_or_create_settings(db: &Pool) -> Settings { + if let Ok(settings) = get_settings(db).await { + return settings; + } + + sqlx::query!( + r#" INSERT INTO settings (id) VALUES ('default') "#, - ) - .execute(pool) - .await.expect("Failed to insert settings"); - get_settings(pool).await.expect("Failed to get settings") - } + ) + .execute(db) + .await + .expect("Failed to insert settings"); + + get_settings(db).await.expect("Failed to get settings") } pub async fn update_settings( - pool: &Pool, + db: &Pool, settings: Settings, ) -> Result { sqlx::query!( @@ -604,13 +602,13 @@ pub async fn update_settings( settings.appearance, settings.update_channel ) - .execute(pool) + .execute(db) .await?; - get_settings(pool).await + get_settings(db).await } pub async fn upsert_environment( - pool: &Pool, + db: &Pool, environment: Environment, ) -> Result { let id = match environment.id.as_str() { @@ -637,12 +635,12 @@ pub async fn upsert_environment( trimmed_name, environment.variables, ) - .execute(pool) + .execute(db) .await?; - get_environment(&id, pool).await + get_environment(db, &id).await } -pub async fn get_environment(id: &str, pool: &Pool) -> Result { +pub async fn get_environment(db: &Pool, id: &str) -> Result { sqlx::query_as!( Environment, r#" @@ -659,11 +657,11 @@ pub async fn get_environment(id: &str, pool: &Pool) -> Result) -> Result { +pub async fn get_folder(db: &Pool, id: &str) -> Result { sqlx::query_as!( Folder, r#" @@ -681,13 +679,13 @@ pub async fn get_folder(id: &str, pool: &Pool) -> Result, workspace_id: &str, - pool: &Pool, ) -> Result, sqlx::Error> { sqlx::query_as!( Folder, @@ -706,12 +704,12 @@ pub async fn find_folders( "#, workspace_id, ) - .fetch_all(pool) + .fetch_all(db) .await } -pub async fn delete_folder(id: &str, pool: &Pool) -> Result { - let env = get_folder(id, pool).await?; +pub async fn delete_folder(db: &Pool, id: &str) -> Result { + let env = get_folder(db, id).await?; let _ = sqlx::query!( r#" DELETE FROM folders @@ -719,13 +717,13 @@ pub async fn delete_folder(id: &str, pool: &Pool) -> Result, r: Folder) -> Result { +pub async fn upsert_folder(db: &Pool, r: Folder) -> Result { let id = match r.id.as_str() { "" => generate_id(Some("fl")), _ => r.id.to_string(), @@ -754,22 +752,19 @@ pub async fn upsert_folder(pool: &Pool, r: Folder) -> Result) -> Result { - let mut request = get_request(id, pool).await?.clone(); +pub async fn duplicate_request(db: &Pool, id: &str) -> Result { + let mut request = get_request(db, id).await?.clone(); request.id = "".to_string(); - upsert_request(pool, request).await + upsert_request(db, request).await } -pub async fn upsert_request( - pool: &Pool, - r: HttpRequest, -) -> Result { +pub async fn upsert_request(db: &Pool, r: HttpRequest) -> Result { let id = match r.id.as_str() { "" => generate_id(Some("rq")), _ => r.id.to_string(), @@ -824,15 +819,15 @@ pub async fn upsert_request( headers_json, r.sort_priority, ) - .execute(pool) + .execute(db) .await?; - get_request(&id, pool).await + get_request(db, &id).await } pub async fn find_requests( + db: &Pool, workspace_id: &str, - pool: &Pool, ) -> Result, sqlx::Error> { sqlx::query_as!( HttpRequest, @@ -859,11 +854,11 @@ pub async fn find_requests( "#, workspace_id, ) - .fetch_all(pool) + .fetch_all(db) .await } -pub async fn get_request(id: &str, pool: &Pool) -> Result { +pub async fn get_request(db: &Pool, id: &str) -> Result { sqlx::query_as!( HttpRequest, r#" @@ -889,15 +884,15 @@ pub async fn get_request(id: &str, pool: &Pool) -> Result) -> Result { - let req = get_request(id, pool).await?; +pub async fn delete_request(db: &Pool, id: &str) -> Result { + let req = get_request(db, id).await?; // DB deletes will cascade but this will delete the files - delete_all_responses(id, pool).await?; + delete_all_responses(db, id).await?; let _ = sqlx::query!( r#" @@ -906,7 +901,7 @@ pub async fn delete_request(id: &str, pool: &Pool) -> Result) -> Result, request_id: &str, elapsed: i64, elapsed_headers: i64, @@ -925,9 +921,8 @@ pub async fn create_response( headers: Vec, version: Option<&str>, remote_addr: Option<&str>, - pool: &Pool, ) -> Result { - let req = get_request(request_id, pool).await?; + let req = get_request(db, request_id).await?; let id = generate_id(Some("rp")); let headers_json = Json(headers); sqlx::query!( @@ -963,13 +958,13 @@ pub async fn create_response( version, remote_addr, ) - .execute(pool) + .execute(db) .await?; - get_response(&id, pool).await + get_response(db, &id).await } -pub async fn cancel_pending_responses(pool: &Pool) -> Result<(), sqlx::Error> { +pub async fn cancel_pending_responses(db: &Pool) -> Result<(), sqlx::Error> { sqlx::query!( r#" UPDATE http_responses @@ -977,24 +972,24 @@ pub async fn cancel_pending_responses(pool: &Pool) -> Result<(), sqlx::E WHERE elapsed = 0; "#, ) - .execute(pool) + .execute(db) .await?; Ok(()) } pub async fn update_response_if_id( + db: &Pool, response: &HttpResponse, - pool: &Pool, ) -> Result { if response.id.is_empty() { Ok(response.clone()) } else { - update_response(response, pool).await + update_response(db, response).await } } pub async fn upsert_workspace( - pool: &Pool, + db: &Pool, workspace: Workspace, ) -> Result { let id = match workspace.id.as_str() { @@ -1031,15 +1026,15 @@ pub async fn upsert_workspace( workspace.setting_follow_redirects, workspace.setting_validate_certificates, ) - .execute(pool) + .execute(db) .await?; - get_workspace(&id, pool).await + get_workspace(db, &id).await } pub async fn update_response( + db: &Pool, response: &HttpResponse, - pool: &Pool, ) -> Result { let headers_json = Json(&response.headers); sqlx::query!( @@ -1072,12 +1067,12 @@ pub async fn update_response( response.remote_addr, response.id, ) - .execute(pool) + .execute(db) .await?; - get_response(&response.id, pool).await + get_response(db, &response.id).await } -pub async fn get_response(id: &str, pool: &Pool) -> Result { +pub async fn get_response(db: &Pool, id: &str) -> Result { sqlx::query_as!( HttpResponse, r#" @@ -1091,14 +1086,14 @@ pub async fn get_response(id: &str, pool: &Pool) -> Result, request_id: &str, limit: Option, - pool: &Pool, ) -> Result, sqlx::Error> { let limit_unwrapped = limit.unwrap_or_else(|| i64::MAX); sqlx::query_as!( @@ -1117,13 +1112,13 @@ pub async fn find_responses( request_id, limit_unwrapped, ) - .fetch_all(pool) + .fetch_all(db) .await } pub async fn find_responses_by_workspace_id( + db: &Pool, workspace_id: &str, - pool: &Pool, ) -> Result, sqlx::Error> { sqlx::query_as!( HttpResponse, @@ -1139,12 +1134,12 @@ pub async fn find_responses_by_workspace_id( "#, workspace_id, ) - .fetch_all(pool) + .fetch_all(db) .await } -pub async fn delete_response(id: &str, pool: &Pool) -> Result { - let resp = get_response(id, pool).await?; +pub async fn delete_response(db: &Pool, id: &str) -> Result { + let resp = get_response(db, id).await?; // Delete the body file if it exists if let Some(p) = resp.body_path.clone() { @@ -1160,18 +1155,15 @@ pub async fn delete_response(id: &str, pool: &Pool) -> Result, -) -> Result<(), sqlx::Error> { - for r in find_responses(request_id, None, pool).await? { - delete_response(&r.id, pool).await?; +pub async fn delete_all_responses(db: &Pool, request_id: &str) -> Result<(), sqlx::Error> { + for r in find_responses(db, request_id, None).await? { + delete_response(db, &r.id).await?; } Ok(()) } @@ -1204,10 +1196,10 @@ pub struct WorkspaceExportResources { pub async fn get_workspace_export_resources( app_handle: &AppHandle, - pool: &Pool, + db: &Pool, workspace_id: &str, ) -> WorkspaceExport { - let workspace = get_workspace(workspace_id, pool) + let workspace = get_workspace(db, workspace_id) .await .expect("Failed to get workspace"); return WorkspaceExport { @@ -1216,13 +1208,13 @@ pub async fn get_workspace_export_resources( timestamp: chrono::Utc::now().naive_utc(), resources: WorkspaceExportResources { workspaces: vec![workspace], - environments: find_environments(workspace_id, pool) + environments: find_environments(db, workspace_id) .await .expect("Failed to get environments"), - folders: find_folders(workspace_id, pool) + folders: find_folders(db, workspace_id) .await .expect("Failed to get folders"), - requests: find_requests(workspace_id, pool) + requests: find_requests(db, workspace_id) .await .expect("Failed to get requests"), }, diff --git a/src-web/components/GrpcConnectionLayout.tsx b/src-web/components/GrpcConnectionLayout.tsx index e8c1c188..febed260 100644 --- a/src-web/components/GrpcConnectionLayout.tsx +++ b/src-web/components/GrpcConnectionLayout.tsx @@ -1,7 +1,8 @@ +import useResizeObserver from '@react-hook/resize-observer'; import classNames from 'classnames'; import { format } from 'date-fns'; import type { CSSProperties, FormEvent } from 'react'; -import React, { useCallback, useEffect, useMemo, useState } from 'react'; +import React, { useRef, useCallback, useEffect, useMemo, useState } from 'react'; import { useAlert } from '../hooks/useAlert'; import type { GrpcMessage } from '../hooks/useGrpc'; import { useGrpc } from '../hooks/useGrpc'; @@ -129,6 +130,12 @@ export function GrpcConnectionLayout({ style }: Props) { return { value, options }; }, [grpc.schema, method.value, service.value]); + const [paneSize, setPaneSize] = useState(99999); + const urlContainerEl = useRef(null); + useResizeObserver(urlContainerEl.current, (entry) => { + setPaneSize(entry.contentRect.width); + }); + if (url.isLoading || url.value == null) { return null; } @@ -138,7 +145,13 @@ export function GrpcConnectionLayout({ style }: Props) { style={style} leftSlot={() => ( -
+
- + +
, onSelect: async () => { - const hasUpdate: boolean = await invoke('check_for_updates'); + const hasUpdate: boolean = await invoke('cmd_check_for_updates'); if (!hasUpdate) { alert({ id: 'no-updates', diff --git a/src-web/components/WorkspaceActionsDropdown.tsx b/src-web/components/WorkspaceActionsDropdown.tsx index 2f168440..3c33d433 100644 --- a/src-web/components/WorkspaceActionsDropdown.tsx +++ b/src-web/components/WorkspaceActionsDropdown.tsx @@ -59,7 +59,7 @@ export const WorkspaceActionsDropdown = memo(function WorkspaceActionsDropdown({ onClick={async () => { hide(); const environmentId = (await getRecentEnvironments(w.id))[0]; - await invoke('new_window', { + await invoke('cmd_new_window', { url: routes.paths.workspace({ workspaceId: w.id, environmentId }), }); }} diff --git a/src-web/components/core/Stacks.tsx b/src-web/components/core/Stacks.tsx index 94530cc5..00b3cb14 100644 --- a/src-web/components/core/Stacks.tsx +++ b/src-web/components/core/Stacks.tsx @@ -6,6 +6,7 @@ const gapClasses = { 0: 'gap-0', 0.5: 'gap-0.5', 1: 'gap-1', + 1.5: 'gap-1.5', 2: 'gap-2', 3: 'gap-3', 4: 'gap-4', diff --git a/src-web/hooks/useCookieJars.ts b/src-web/hooks/useCookieJars.ts index d87fec2d..ec4481c7 100644 --- a/src-web/hooks/useCookieJars.ts +++ b/src-web/hooks/useCookieJars.ts @@ -15,7 +15,7 @@ export function useCookieJars() { queryKey: cookieJarsQueryKey({ workspaceId: workspaceId ?? 'n/a' }), queryFn: async () => { if (workspaceId == null) return []; - return (await invoke('list_cookie_jars', { workspaceId })) as CookieJar[]; + return (await invoke('cmd_list_cookie_jars', { workspaceId })) as CookieJar[]; }, }).data ?? [] ); diff --git a/src-web/hooks/useCreateCookieJar.ts b/src-web/hooks/useCreateCookieJar.ts index 8edb32d2..0e1bf02d 100644 --- a/src-web/hooks/useCreateCookieJar.ts +++ b/src-web/hooks/useCreateCookieJar.ts @@ -23,7 +23,7 @@ export function useCreateCookieJar() { label: 'Name', defaultValue: 'My Jar', }); - return invoke('create_cookie_jar', { workspaceId, name }); + return invoke('cmd_create_cookie_jar', { workspaceId, name }); }, onSettled: () => trackEvent('CookieJar', 'Create'), onSuccess: async (cookieJar) => { diff --git a/src-web/hooks/useCreateEnvironment.ts b/src-web/hooks/useCreateEnvironment.ts index 23f0d92f..871be224 100644 --- a/src-web/hooks/useCreateEnvironment.ts +++ b/src-web/hooks/useCreateEnvironment.ts @@ -22,7 +22,7 @@ export function useCreateEnvironment() { label: 'Name', defaultValue: 'My Environment', }); - return invoke('create_environment', { name, variables: [], workspaceId }); + return invoke('cmd_create_environment', { name, variables: [], workspaceId }); }, onSettled: () => trackEvent('Environment', 'Create'), onSuccess: async (environment) => { diff --git a/src-web/hooks/useCreateFolder.ts b/src-web/hooks/useCreateFolder.ts index 844c77d8..abe6eabb 100644 --- a/src-web/hooks/useCreateFolder.ts +++ b/src-web/hooks/useCreateFolder.ts @@ -16,7 +16,7 @@ export function useCreateFolder() { } patch.name = patch.name || 'New Folder'; patch.sortPriority = patch.sortPriority || -Date.now(); - return invoke('create_folder', { workspaceId, ...patch }); + return invoke('cmd_create_folder', { workspaceId, ...patch }); }, onSettled: () => trackEvent('Folder', 'Create'), onSuccess: async (request) => { diff --git a/src-web/hooks/useCreateRequest.ts b/src-web/hooks/useCreateRequest.ts index ba5b2f10..cb367c01 100644 --- a/src-web/hooks/useCreateRequest.ts +++ b/src-web/hooks/useCreateRequest.ts @@ -34,7 +34,7 @@ export function useCreateRequest() { } } patch.folderId = patch.folderId || activeRequest?.folderId; - return invoke('create_request', { workspaceId, name: '', ...patch }); + return invoke('cmd_create_request', { workspaceId, name: '', ...patch }); }, onSettled: () => trackEvent('HttpRequest', 'Create'), onSuccess: async (request) => { diff --git a/src-web/hooks/useCreateWorkspace.ts b/src-web/hooks/useCreateWorkspace.ts index 39c510c3..58422cfa 100644 --- a/src-web/hooks/useCreateWorkspace.ts +++ b/src-web/hooks/useCreateWorkspace.ts @@ -10,7 +10,7 @@ export function useCreateWorkspace({ navigateAfter }: { navigateAfter: boolean } const queryClient = useQueryClient(); return useMutation>({ mutationFn: (patch) => { - return invoke('create_workspace', patch); + return invoke('cmd_create_workspace', patch); }, onSettled: () => trackEvent('Workspace', 'Create'), onSuccess: async (workspace) => { diff --git a/src-web/hooks/useDeleteAnyRequest.tsx b/src-web/hooks/useDeleteAnyRequest.tsx index eb1f76bd..e1373419 100644 --- a/src-web/hooks/useDeleteAnyRequest.tsx +++ b/src-web/hooks/useDeleteAnyRequest.tsx @@ -27,7 +27,7 @@ export function useDeleteAnyRequest() { ), }); if (!confirmed) return null; - return invoke('delete_request', { requestId: id }); + return invoke('cmd_delete_request', { requestId: id }); }, onSettled: () => trackEvent('HttpRequest', 'Delete'), onSuccess: async (request) => { diff --git a/src-web/hooks/useDeleteCookieJar.tsx b/src-web/hooks/useDeleteCookieJar.tsx index 0bcff2db..fc3bed00 100644 --- a/src-web/hooks/useDeleteCookieJar.tsx +++ b/src-web/hooks/useDeleteCookieJar.tsx @@ -23,7 +23,7 @@ export function useDeleteCookieJar(cookieJar: CookieJar | null) { ), }); if (!confirmed) return null; - return invoke('delete_cookie_jar', { cookieJarId: cookieJar?.id }); + return invoke('cmd_delete_cookie_jar', { cookieJarId: cookieJar?.id }); }, onSettled: () => trackEvent('CookieJar', 'Delete'), onSuccess: async (cookieJar) => { diff --git a/src-web/hooks/useDeleteEnvironment.tsx b/src-web/hooks/useDeleteEnvironment.tsx index b36f4910..982ed54b 100644 --- a/src-web/hooks/useDeleteEnvironment.tsx +++ b/src-web/hooks/useDeleteEnvironment.tsx @@ -23,7 +23,7 @@ export function useDeleteEnvironment(environment: Environment | null) { ), }); if (!confirmed) return null; - return invoke('delete_environment', { environmentId: environment?.id }); + return invoke('cmd_delete_environment', { environmentId: environment?.id }); }, onSettled: () => trackEvent('Environment', 'Delete'), onSuccess: async (environment) => { diff --git a/src-web/hooks/useDeleteFolder.tsx b/src-web/hooks/useDeleteFolder.tsx index 57219c34..70e6c125 100644 --- a/src-web/hooks/useDeleteFolder.tsx +++ b/src-web/hooks/useDeleteFolder.tsx @@ -26,7 +26,7 @@ export function useDeleteFolder(id: string | null) { ), }); if (!confirmed) return null; - return invoke('delete_folder', { folderId: id }); + return invoke('cmd_delete_folder', { folderId: id }); }, onSettled: () => trackEvent('Folder', 'Delete'), onSuccess: async (folder) => { diff --git a/src-web/hooks/useDeleteResponse.ts b/src-web/hooks/useDeleteResponse.ts index e3aa6572..f763587c 100644 --- a/src-web/hooks/useDeleteResponse.ts +++ b/src-web/hooks/useDeleteResponse.ts @@ -8,7 +8,7 @@ export function useDeleteResponse(id: string | null) { const queryClient = useQueryClient(); return useMutation({ mutationFn: async () => { - return await invoke('delete_response', { id: id }); + return await invoke('cmd_delete_response', { id: id }); }, onSettled: () => trackEvent('HttpResponse', 'Delete'), onSuccess: ({ requestId, id: responseId }) => { diff --git a/src-web/hooks/useDeleteResponses.ts b/src-web/hooks/useDeleteResponses.ts index 933cf1b3..d3f896f0 100644 --- a/src-web/hooks/useDeleteResponses.ts +++ b/src-web/hooks/useDeleteResponses.ts @@ -8,7 +8,7 @@ export function useDeleteResponses(requestId?: string) { return useMutation({ mutationFn: async () => { if (requestId === undefined) return; - await invoke('delete_all_responses', { requestId }); + await invoke('cmd_delete_all_responses', { requestId }); }, onSettled: () => trackEvent('HttpResponse', 'DeleteMany'), onSuccess: async () => { diff --git a/src-web/hooks/useDeleteWorkspace.tsx b/src-web/hooks/useDeleteWorkspace.tsx index 03e0a24d..88796bbb 100644 --- a/src-web/hooks/useDeleteWorkspace.tsx +++ b/src-web/hooks/useDeleteWorkspace.tsx @@ -28,7 +28,7 @@ export function useDeleteWorkspace(workspace: Workspace | null) { ), }); if (!confirmed) return null; - return invoke('delete_workspace', { workspaceId: workspace?.id }); + return invoke('cmd_delete_workspace', { workspaceId: workspace?.id }); }, onSettled: () => trackEvent('Workspace', 'Delete'), onSuccess: async (workspace) => { diff --git a/src-web/hooks/useDuplicateRequest.ts b/src-web/hooks/useDuplicateRequest.ts index 16c32731..a73b6c19 100644 --- a/src-web/hooks/useDuplicateRequest.ts +++ b/src-web/hooks/useDuplicateRequest.ts @@ -21,7 +21,7 @@ export function useDuplicateRequest({ return useMutation({ mutationFn: async () => { if (id === null) throw new Error("Can't duplicate a null request"); - return invoke('duplicate_request', { id }); + return invoke('cmd_duplicate_request', { id }); }, onSettled: () => trackEvent('HttpRequest', 'Duplicate'), onSuccess: async (request) => { diff --git a/src-web/hooks/useEnvironments.ts b/src-web/hooks/useEnvironments.ts index c3dba4c1..a7e709f9 100644 --- a/src-web/hooks/useEnvironments.ts +++ b/src-web/hooks/useEnvironments.ts @@ -15,7 +15,7 @@ export function useEnvironments() { queryKey: environmentsQueryKey({ workspaceId: workspaceId ?? 'n/a' }), queryFn: async () => { if (workspaceId == null) return []; - return (await invoke('list_environments', { workspaceId })) as Environment[]; + return (await invoke('cmd_list_environments', { workspaceId })) as Environment[]; }, }).data ?? [] ); diff --git a/src-web/hooks/useExportData.tsx b/src-web/hooks/useExportData.tsx index b6b7bb77..7dde2ab8 100644 --- a/src-web/hooks/useExportData.tsx +++ b/src-web/hooks/useExportData.tsx @@ -25,7 +25,7 @@ export function useExportData() { return; } - await invoke('export_data', { workspaceId: workspace.id, exportPath }); + await invoke('cmd_export_data', { workspaceId: workspace.id, exportPath }); }, }); } diff --git a/src-web/hooks/useFilterResponse.ts b/src-web/hooks/useFilterResponse.ts index 4ed9bf19..f9b1845b 100644 --- a/src-web/hooks/useFilterResponse.ts +++ b/src-web/hooks/useFilterResponse.ts @@ -16,7 +16,7 @@ export function useFilterResponse({ return null; } - return (await invoke('filter_response', { responseId, filter })) as string | null; + return (await invoke('cmd_filter_response', { responseId, filter })) as string | null; }, }).data ?? null ); diff --git a/src-web/hooks/useFolders.ts b/src-web/hooks/useFolders.ts index a1ace628..8bfb97c3 100644 --- a/src-web/hooks/useFolders.ts +++ b/src-web/hooks/useFolders.ts @@ -15,7 +15,7 @@ export function useFolders() { queryKey: foldersQueryKey({ workspaceId: workspaceId ?? 'n/a' }), queryFn: async () => { if (workspaceId == null) return []; - return (await invoke('list_folders', { workspaceId })) as Folder[]; + return (await invoke('cmd_list_folders', { workspaceId })) as Folder[]; }, }).data ?? [] ); diff --git a/src-web/hooks/useGrpc.ts b/src-web/hooks/useGrpc.ts index 6890a55f..8d001cee 100644 --- a/src-web/hooks/useGrpc.ts +++ b/src-web/hooks/useGrpc.ts @@ -30,7 +30,7 @@ export function useGrpc(url: string | null) { mutationKey: ['grpc_unary', url], mutationFn: async ({ service, method, message }) => { if (url === null) throw new Error('No URL provided'); - return (await invoke('grpc_call_unary', { + return (await invoke('cmd_grpc_call_unary', { endpoint: url, service, method, @@ -50,7 +50,7 @@ export function useGrpc(url: string | null) { setMessages([ { isServer: false, message: JSON.stringify(JSON.parse(message)), time: new Date() }, ]); - return (await invoke('grpc_server_streaming', { + return (await invoke('cmd_grpc_server_streaming', { endpoint: url, service, method, @@ -64,7 +64,7 @@ export function useGrpc(url: string | null) { queryFn: async () => { if (url === null) return []; console.log('GETTING SCHEMA', url); - return (await invoke('grpc_reflect', { endpoint: url })) as ReflectResponseService[]; + return (await invoke('cmd_grpc_reflect', { endpoint: url })) as ReflectResponseService[]; }, }); diff --git a/src-web/hooks/useImportData.tsx b/src-web/hooks/useImportData.tsx index 24b5397a..ce29afcd 100644 --- a/src-web/hooks/useImportData.tsx +++ b/src-web/hooks/useImportData.tsx @@ -35,7 +35,7 @@ export function useImportData() { environments: Environment[]; folders: Folder[]; requests: HttpRequest[]; - } = await invoke('import_data', { + } = await invoke('cmd_import_data', { filePaths: Array.isArray(selected) ? selected : [selected], }); const importedWorkspace = imported.workspaces[0]; diff --git a/src-web/hooks/useRequests.ts b/src-web/hooks/useRequests.ts index c5344ee0..c0743e72 100644 --- a/src-web/hooks/useRequests.ts +++ b/src-web/hooks/useRequests.ts @@ -15,7 +15,7 @@ export function useRequests() { queryKey: requestsQueryKey({ workspaceId: workspaceId ?? 'n/a' }), queryFn: async () => { if (workspaceId == null) return []; - return (await invoke('list_requests', { workspaceId })) as HttpRequest[]; + return (await invoke('cmd_list_requests', { workspaceId })) as HttpRequest[]; }, }).data ?? [] ); diff --git a/src-web/hooks/useResponses.ts b/src-web/hooks/useResponses.ts index cd914147..ac9cc2f3 100644 --- a/src-web/hooks/useResponses.ts +++ b/src-web/hooks/useResponses.ts @@ -13,7 +13,7 @@ export function useResponses(requestId: string | null) { initialData: [], queryKey: responsesQueryKey({ requestId: requestId ?? 'n/a' }), queryFn: async () => { - return (await invoke('list_responses', { requestId, limit: 200 })) as HttpResponse[]; + return (await invoke('cmd_list_responses', { requestId, limit: 200 })) as HttpResponse[]; }, }).data ?? [] ); diff --git a/src-web/hooks/useSendAnyRequest.ts b/src-web/hooks/useSendAnyRequest.ts index 58230bee..9ff23bc8 100644 --- a/src-web/hooks/useSendAnyRequest.ts +++ b/src-web/hooks/useSendAnyRequest.ts @@ -31,7 +31,7 @@ export function useSendAnyRequest(options: { download?: boolean } = {}) { } } - return invoke('send_request', { + return invoke('cmd_send_request', { requestId: id, environmentId, downloadDir: downloadDir, diff --git a/src-web/hooks/useSettings.ts b/src-web/hooks/useSettings.ts index de58ad10..ca521694 100644 --- a/src-web/hooks/useSettings.ts +++ b/src-web/hooks/useSettings.ts @@ -11,7 +11,7 @@ export function useSettings() { useQuery({ queryKey: settingsQueryKey(), queryFn: async () => { - return (await invoke('get_settings')) as Settings; + return (await invoke('cmd_get_settings')) as Settings; }, }).data ?? undefined ); diff --git a/src-web/hooks/useUpdateAnyFolder.ts b/src-web/hooks/useUpdateAnyFolder.ts index b093f2b6..7aad7048 100644 --- a/src-web/hooks/useUpdateAnyFolder.ts +++ b/src-web/hooks/useUpdateAnyFolder.ts @@ -14,7 +14,7 @@ export function useUpdateAnyFolder() { throw new Error("Can't update a null folder"); } - await invoke('update_folder', { folder: update(folder) }); + await invoke('cmd_update_folder', { folder: update(folder) }); }, onMutate: async ({ id, update }) => { const folder = await getFolder(id); diff --git a/src-web/hooks/useUpdateAnyRequest.ts b/src-web/hooks/useUpdateAnyRequest.ts index 7a4f447f..85e89d0f 100644 --- a/src-web/hooks/useUpdateAnyRequest.ts +++ b/src-web/hooks/useUpdateAnyRequest.ts @@ -20,7 +20,7 @@ export function useUpdateAnyRequest() { const patchedRequest = typeof update === 'function' ? update(request) : { ...request, ...update }; - await invoke('update_request', { request: patchedRequest }); + await invoke('cmd_update_request', { request: patchedRequest }); }, onMutate: async ({ id, update }) => { const request = await getRequest(id); diff --git a/src-web/hooks/useUpdateCookieJar.ts b/src-web/hooks/useUpdateCookieJar.ts index f5406ec8..0a899363 100644 --- a/src-web/hooks/useUpdateCookieJar.ts +++ b/src-web/hooks/useUpdateCookieJar.ts @@ -15,7 +15,7 @@ export function useUpdateCookieJar(id: string | null) { const newCookieJar = typeof v === 'function' ? v(cookieJar) : { ...cookieJar, ...v }; console.log('NEW COOKIE JAR', newCookieJar.cookies.length); - await invoke('update_cookie_jar', { cookieJar: newCookieJar }); + await invoke('cmd_update_cookie_jar', { cookieJar: newCookieJar }); }, onMutate: async (v) => { const cookieJar = await getCookieJar(id); diff --git a/src-web/hooks/useUpdateEnvironment.ts b/src-web/hooks/useUpdateEnvironment.ts index 0c95a11a..6c984e83 100644 --- a/src-web/hooks/useUpdateEnvironment.ts +++ b/src-web/hooks/useUpdateEnvironment.ts @@ -14,7 +14,7 @@ export function useUpdateEnvironment(id: string | null) { } const newEnvironment = typeof v === 'function' ? v(environment) : { ...environment, ...v }; - await invoke('update_environment', { environment: newEnvironment }); + await invoke('cmd_update_environment', { environment: newEnvironment }); }, onMutate: async (v) => { const environment = await getEnvironment(id); diff --git a/src-web/hooks/useUpdateSettings.ts b/src-web/hooks/useUpdateSettings.ts index 8431a84a..a3ee7bd6 100644 --- a/src-web/hooks/useUpdateSettings.ts +++ b/src-web/hooks/useUpdateSettings.ts @@ -8,7 +8,7 @@ export function useUpdateSettings() { return useMutation({ mutationFn: async (settings) => { - await invoke('update_settings', { settings }); + await invoke('cmd_update_settings', { settings }); }, onMutate: async (settings) => { queryClient.setQueryData(settingsQueryKey(), settings); diff --git a/src-web/hooks/useUpdateWorkspace.ts b/src-web/hooks/useUpdateWorkspace.ts index 3b71ee00..92d86b22 100644 --- a/src-web/hooks/useUpdateWorkspace.ts +++ b/src-web/hooks/useUpdateWorkspace.ts @@ -14,7 +14,7 @@ export function useUpdateWorkspace(id: string | null) { } const newWorkspace = typeof v === 'function' ? v(workspace) : { ...workspace, ...v }; - await invoke('update_workspace', { workspace: newWorkspace }); + await invoke('cmd_update_workspace', { workspace: newWorkspace }); }, onMutate: async (v) => { const workspace = await getWorkspace(id); diff --git a/src-web/hooks/useVariables.ts b/src-web/hooks/useVariables.ts index 0a9cfaa8..4b0d8504 100644 --- a/src-web/hooks/useVariables.ts +++ b/src-web/hooks/useVariables.ts @@ -11,7 +11,7 @@ export function useVariables({ environmentId }: { environmentId: string }) { useQuery({ queryKey: variablesQueryKey({ environmentId }), queryFn: async () => { - return (await invoke('list_variables', { environmentId })) as EnvironmentVariable[]; + return (await invoke('cmd_list_variables', { environmentId })) as EnvironmentVariable[]; }, }).data ?? [] ); diff --git a/src-web/hooks/useWorkspaces.ts b/src-web/hooks/useWorkspaces.ts index 4e09cbac..e8d05df9 100644 --- a/src-web/hooks/useWorkspaces.ts +++ b/src-web/hooks/useWorkspaces.ts @@ -10,7 +10,7 @@ export function workspacesQueryKey(_?: {}) { export function useWorkspaces() { return ( useQuery(workspacesQueryKey(), async () => { - return (await invoke('list_workspaces')) as Workspace[]; + return (await invoke('cmd_list_workspaces')) as Workspace[]; }).data ?? [] ); } diff --git a/src-web/lib/analytics.ts b/src-web/lib/analytics.ts index 2c1504b6..b72f86a5 100644 --- a/src-web/lib/analytics.ts +++ b/src-web/lib/analytics.ts @@ -25,7 +25,7 @@ export function trackEvent( | 'Duplicate', attributes: Record = {}, ) { - invoke('track_event', { + invoke('cmd_track_event', { resource: resource, action, attributes, diff --git a/src-web/lib/keyValueStore.ts b/src-web/lib/keyValueStore.ts index 69235d91..76269c62 100644 --- a/src-web/lib/keyValueStore.ts +++ b/src-web/lib/keyValueStore.ts @@ -14,7 +14,7 @@ export async function setKeyValue({ key: string | string[]; value: T; }): Promise { - await invoke('set_key_value', { + await invoke('cmd_set_key_value', { namespace, key: buildKeyValueKey(key), value: JSON.stringify(value), @@ -30,7 +30,7 @@ export async function getKeyValue({ key: string | string[]; fallback: T; }) { - const kv = (await invoke('get_key_value', { + const kv = (await invoke('cmd_get_key_value', { namespace, key: buildKeyValueKey(key), })) as KeyValue | null; diff --git a/src-web/lib/sendEphemeralRequest.ts b/src-web/lib/sendEphemeralRequest.ts index daa4a909..878c6818 100644 --- a/src-web/lib/sendEphemeralRequest.ts +++ b/src-web/lib/sendEphemeralRequest.ts @@ -7,5 +7,5 @@ export async function sendEphemeralRequest( ): Promise { // Remove some things that we don't want to associate const newRequest = { ...request }; - return invoke('send_ephemeral_request', { request: newRequest, environmentId }); + return invoke('cmd_send_ephemeral_request', { request: newRequest, environmentId }); } diff --git a/src-web/lib/store.ts b/src-web/lib/store.ts index dd4f29df..3c1896a2 100644 --- a/src-web/lib/store.ts +++ b/src-web/lib/store.ts @@ -2,12 +2,12 @@ import { invoke } from '@tauri-apps/api'; import type { CookieJar, Environment, Folder, HttpRequest, Settings, Workspace } from './models'; export async function getSettings(): Promise { - return invoke('get_settings', {}); + return invoke('cmd_get_settings', {}); } export async function getRequest(id: string | null): Promise { if (id === null) return null; - const request: HttpRequest = (await invoke('get_request', { id })) ?? null; + const request: HttpRequest = (await invoke('cmd_get_request', { id })) ?? null; if (request == null) { return null; } @@ -16,7 +16,7 @@ export async function getRequest(id: string | null): Promise export async function getEnvironment(id: string | null): Promise { if (id === null) return null; - const environment: Environment = (await invoke('get_environment', { id })) ?? null; + const environment: Environment = (await invoke('cmd_get_environment', { id })) ?? null; if (environment == null) { return null; } @@ -25,7 +25,7 @@ export async function getEnvironment(id: string | null): Promise { if (id === null) return null; - const folder: Folder = (await invoke('get_folder', { id })) ?? null; + const folder: Folder = (await invoke('cmd_get_folder', { id })) ?? null; if (folder == null) { return null; } @@ -34,7 +34,7 @@ export async function getFolder(id: string | null): Promise { export async function getWorkspace(id: string | null): Promise { if (id === null) return null; - const workspace: Workspace = (await invoke('get_workspace', { id })) ?? null; + const workspace: Workspace = (await invoke('cmd_get_workspace', { id })) ?? null; if (workspace == null) { return null; } @@ -43,7 +43,7 @@ export async function getWorkspace(id: string | null): Promise export async function getCookieJar(id: string | null): Promise { if (id === null) return null; - const cookieJar: CookieJar = (await invoke('get_cookie_jar', { id })) ?? null; + const cookieJar: CookieJar = (await invoke('cmd_get_cookie_jar', { id })) ?? null; if (cookieJar == null) { return null; } diff --git a/src-web/main.tsx b/src-web/main.tsx index b53972f7..899910d7 100644 --- a/src-web/main.tsx +++ b/src-web/main.tsx @@ -1,15 +1,12 @@ -import { invoke } from '@tauri-apps/api'; +import { type } from '@tauri-apps/api/os'; +import { appWindow } from '@tauri-apps/api/window'; import { StrictMode } from 'react'; import { createRoot } from 'react-dom/client'; -import { attachConsole } from 'tauri-plugin-log-api'; import { App } from './components/App'; -import { maybeRestorePathname } from './lib/persistPathname'; import './main.css'; import { getSettings } from './lib/store'; import type { Appearance } from './lib/theme/window'; import { setAppearanceOnDocument } from './lib/theme/window'; -import { appWindow } from '@tauri-apps/api/window'; -import { type } from '@tauri-apps/api/os'; // Hide decorations here because it doesn't work in Rust for some reason (bug?) const osType = await type(); @@ -17,8 +14,8 @@ if (osType !== 'Darwin') { await appWindow.setDecorations(false); } -await attachConsole(); -await maybeRestorePathname(); +// await attachConsole(); +// await maybeRestorePathname(); const settings = await getSettings(); setAppearanceOnDocument(settings.appearance as Appearance);