Prechádzať zdrojové kódy

add query param deletion

biblius 2 týždňov pred
rodič
commit
ee7797b205

+ 5 - 5
src-tauri/seed/init.sql

@@ -13,14 +13,14 @@ INSERT INTO
   VALUES(0, 0, 0, 'BASE_URL', 'https://jsonplaceholder.typicode.com', false);
 
 INSERT INTO 
-  workspace_entries(id, workspace_id, parent_id, name, type)
-  VALUES (0, 0, NULL, 'My collection', 1);
+  workspace_entries(id, workspace_id, parent_id, name, type, auth_inherit)
+  VALUES (0, 0, NULL, 'My collection', 1, FALSE);
 
 INSERT INTO 
-  workspace_entries(id, workspace_id, parent_id, name, type)
+  workspace_entries(id, workspace_id, parent_id, name, type, auth_inherit)
   VALUES 
-    (1, 0, 0, 'My request in col', 0),
-    (2, 0, NULL, 'My request', 0);
+    (1, 0, 0, 'My request in col', 0, TRUE),
+    (2, 0, NULL, 'My request', 0, FALSE);
 
 INSERT INTO 
   request_params(workspace_id, request_id, method, url)

+ 51 - 0
src-tauri/src/cmd.rs

@@ -177,6 +177,57 @@ pub async fn expand_url(
     }
 }
 
+/// Used for directly deleting query params.
+#[tauri::command]
+pub async fn delete_query_param(
+    state: tauri::State<'_, AppState>,
+    req_id: i64,
+    id: i64,
+    url: String,
+) -> Result<String, String> {
+    let qp = db::get_query_param(&state.db, id)
+        .await
+        .map_err(|e| e.to_string())?;
+
+    let Some(position) = qp.position else {
+        db::delete_query_param(&state.db, id)
+            .await
+            .map_err(|e| e.to_string())?;
+        return Ok(url);
+    };
+
+    match RequestUrl::parse(&url) {
+        Ok(mut url) => {
+            let Some((removed, offset)) = url.remove_query_param(position as usize) else {
+                return Err(format!("no query param at position {position}"));
+            };
+
+            db::delete_query_param(&state.db, id)
+                .await
+                .map_err(|e| e.to_string())?;
+
+            db::update_query_param_position(
+                &state.db,
+                req_id,
+                offset as i64,
+                removed.position as i64,
+            )
+            .await
+            .map_err(|e| e.to_string())?;
+
+            db::update_request_url(&state.db, req_id, &url.to_string(), None, None)
+                .await
+                .map_err(|e| e.to_string())?;
+
+            Ok(url.to_string())
+        }
+        Err(e) => {
+            log::error!("{e:?}");
+            Err(e.to_string())
+        }
+    }
+}
+
 /// Used for updating disabled query params.
 #[tauri::command]
 pub async fn update_query_param_values(

+ 8 - 0
src-tauri/src/db.rs

@@ -242,6 +242,14 @@ pub async fn get_query_param(db: &SqlitePool, qp_id: i64) -> AppResult<QueryPara
     .await?)
 }
 
+pub async fn delete_query_param(db: &SqlitePool, id: i64) -> AppResult<()> {
+    sqlx::query!("DELETE FROM request_query_params  WHERE id = ?", id)
+        .execute(db)
+        .await?;
+
+    Ok(())
+}
+
 pub async fn update_query_param_values(
     db: &SqlitePool,
     id: i64,

+ 2 - 1
src-tauri/src/lib.rs

@@ -62,7 +62,8 @@ pub fn run() {
             cmd::rename_auth,
             cmd::delete_auth,
             cmd::update_query_param_enabled,
-            cmd::update_query_param_values
+            cmd::update_query_param_values,
+            cmd::delete_query_param
         ])
         .run(tauri::generate_context!())
         .expect("error while running tauri application");

+ 60 - 46
src/lib/components/WorkspaceEntry.svelte

@@ -6,6 +6,7 @@
     cancelRequest,
     deleteBody,
     deleteHeader,
+    deleteQueryParam,
     insertHeader,
     sendRequest,
     setEntryAuth,
@@ -258,13 +259,14 @@
           <Tabs.List class="shrink-0">
             <Tabs.Trigger value="params">Parameters</Tabs.Trigger>
             <Tabs.Trigger value="headers">Headers</Tabs.Trigger>
-            <Tabs.Trigger value="body">Body</Tabs.Trigger>
             <Tabs.Trigger value="auth">Auth</Tabs.Trigger>
           </Tabs.List>
 
           <div class="flex-1 overflow-auto p-2">
             <!-- ================= PARAMETERS ================= -->
 
+            <!-- ================= PATH ================= -->
+
             <Tabs.Content value="params" class="space-y-4">
               {#if _state.entry?.path?.length > 0}
                 <div>
@@ -296,11 +298,13 @@
                 </div>
               {/if}
 
+              <!-- ================= QUERY ================= -->
+
               {#if _state.entry?.query?.length > 0}
                 <div>
                   <h3 class="mb-2 text-sm font-medium">Query</h3>
                   <div
-                    class="grid grid-cols-[5%_1fr_1fr] items-center justify-center gap-2 text-sm"
+                    class="grid grid-cols-[2%_1fr_1fr_2%] items-center justify-center gap-2 text-sm"
                   >
                     {#each _state.entry.query as param}
                       <div class="flex justify-center">
@@ -335,10 +339,64 @@
                             param,
                           })}
                       />
+                      <div class="flex justify-center">
+                        <Trash
+                          class="h-4 w-4 cursor-pointer text-muted-foreground hover:text-destructive"
+                          onclick={() => deleteQueryParam(param)}
+                        />
+                      </div>
                     {/each}
                   </div>
                 </div>
               {/if}
+
+              <!-- ================= BODY ================= -->
+
+              <div class="space-y-4">
+                <h3 class="mb-2 text-sm font-medium">Body</h3>
+                <Tabs.Root value={_state.entry.body === null ? "none" : "json"}>
+                  <Tabs.List>
+                    <Tabs.Trigger value="none" onclick={deleteBody}
+                      >None</Tabs.Trigger
+                    >
+                    <Tabs.Trigger value="json">JSON</Tabs.Trigger>
+                    <Tabs.Trigger value="form">Form</Tabs.Trigger>
+                    <Tabs.Trigger value="text">Text</Tabs.Trigger>
+                  </Tabs.List>
+
+                  <Tabs.Content value="json">
+                    <BodyEditor
+                      input={_state.entry.body?.content}
+                      type={_state.entry.body?.ty}
+                      onStateChange={(update) => {
+                        if (
+                          update.docChanged &&
+                          _state.entry!!.body?.content !==
+                            update.state.doc.toString()
+                        ) {
+                          updateBodyContent(
+                            update.state.doc.toString(),
+                            "Json",
+                          );
+                        }
+                      }}
+                    />
+                  </Tabs.Content>
+
+                  <Tabs.Content value="form">
+                    <p class="text-sm text-muted-foreground">
+                      Form body editor coming soon.
+                    </p>
+                  </Tabs.Content>
+
+                  <Tabs.Content value="text">
+                    <textarea
+                      class="w-full min-h-[200px] rounded-md border bg-background p-2 font-mono text-sm"
+                      placeholder="Raw text body"
+                    ></textarea>
+                  </Tabs.Content>
+                </Tabs.Root>
+              </div>
             </Tabs.Content>
 
             <!-- ================= HEADERS ================= -->
@@ -377,50 +435,6 @@
               </div>
             </Tabs.Content>
 
-            <!-- ================= BODY ================= -->
-
-            <Tabs.Content value="body" class="space-y-4">
-              <Tabs.Root value={_state.entry.body === null ? "none" : "json"}>
-                <Tabs.List>
-                  <Tabs.Trigger value="none" onclick={deleteBody}
-                    >None</Tabs.Trigger
-                  >
-                  <Tabs.Trigger value="json">JSON</Tabs.Trigger>
-                  <Tabs.Trigger value="form">Form</Tabs.Trigger>
-                  <Tabs.Trigger value="text">Text</Tabs.Trigger>
-                </Tabs.List>
-
-                <Tabs.Content value="json">
-                  <BodyEditor
-                    input={_state.entry.body?.content}
-                    type={_state.entry.body?.ty}
-                    onStateChange={(update) => {
-                      if (
-                        update.docChanged &&
-                        _state.entry!!.body?.content !==
-                          update.state.doc.toString()
-                      ) {
-                        updateBodyContent(update.state.doc.toString(), "Json");
-                      }
-                    }}
-                  />
-                </Tabs.Content>
-
-                <Tabs.Content value="form">
-                  <p class="text-sm text-muted-foreground">
-                    Form body editor coming soon.
-                  </p>
-                </Tabs.Content>
-
-                <Tabs.Content value="text">
-                  <textarea
-                    class="w-full min-h-[200px] rounded-md border bg-background p-2 font-mono text-sm"
-                    placeholder="Raw text body"
-                  ></textarea>
-                </Tabs.Content>
-              </Tabs.Root>
-            </Tabs.Content>
-
             <!-- ================= AUTH ================= -->
 
             <Tabs.Content value="auth" class="space-y-4">

+ 27 - 0
src/lib/state.svelte.ts

@@ -517,6 +517,31 @@ export async function updateUrl(up: UrlUpdate) {
   console.debug("updated", $state.snapshot(state.entry));
 }
 
+export async function deleteQueryParam(param: QueryParam) {
+  console.log($state.snapshot(state.entry.query));
+
+  const url = await invoke("delete_query_param", {
+    id: param.id,
+    url: state.entry.url,
+    reqId: state.entry.id,
+  });
+
+  for (const q of state.entry.query) {
+    if (q.position < param.position!!) {
+      continue;
+    }
+    // +1 for the &, +1 for the =
+    q.position -= param.key.length + param.value.length + 2;
+  }
+
+  state.entry.query = state.entry.query.filter((p) => p.id !== param.id);
+  state.entry.url = url;
+
+  console.log($state.snapshot(state.entry.query));
+
+  expandUrl();
+}
+
 export async function updateQueryParamEnabled(param: QueryParam) {
   console.log(param);
   console.log($state.snapshot(state.entry.query));
@@ -550,6 +575,8 @@ export async function updateQueryParamEnabled(param: QueryParam) {
   param.position = newQp.position;
 
   console.log($state.snapshot(state.entry.query));
+
+  expandUrl();
 }
 
 export async function expandUrl() {