Browse Source

refactor: inherit properties when saving

nivedin 1 year ago
parent
commit
b893607ad1

+ 34 - 0
packages/hoppscotch-common/src/components/collections/SaveRequest.vue

@@ -74,6 +74,7 @@ import { Picked } from "~/helpers/types/HoppPicked"
 import { useI18n } from "@composables/i18n"
 import { useToast } from "@composables/toast"
 import {
+  cascaseParentCollectionForHeaderAuth,
   editGraphqlRequest,
   editRESTRequest,
   saveGraphqlRequestAs,
@@ -239,6 +240,17 @@ const saveRequestAs = async () => {
       },
     }
 
+    const { auth, headers, name } = cascaseParentCollectionForHeaderAuth(
+      `${picked.value.collectionIndex}`
+    )
+
+    RESTTabs.currentActiveTab.value.document.inheritedProperties = {
+      auth,
+      headers,
+      parentId: `${picked.value.collectionIndex}`,
+      parentName: name,
+    }
+
     platform.analytics?.logEvent({
       type: "HOPP_SAVE_REQUEST",
       createdNow: true,
@@ -266,6 +278,17 @@ const saveRequestAs = async () => {
       },
     }
 
+    const { auth, headers, name } = cascaseParentCollectionForHeaderAuth(
+      picked.value.folderPath
+    )
+
+    RESTTabs.currentActiveTab.value.document.inheritedProperties = {
+      auth,
+      headers,
+      parentId: picked.value.folderPath,
+      parentName: name,
+    }
+
     platform.analytics?.logEvent({
       type: "HOPP_SAVE_REQUEST",
       createdNow: true,
@@ -294,6 +317,17 @@ const saveRequestAs = async () => {
       },
     }
 
+    const { auth, headers, name } = cascaseParentCollectionForHeaderAuth(
+      picked.value.folderPath
+    )
+
+    RESTTabs.currentActiveTab.value.document.inheritedProperties = {
+      auth,
+      headers,
+      parentId: picked.value.folderPath,
+      parentName: name,
+    }
+
     platform.analytics?.logEvent({
       type: "HOPP_SAVE_REQUEST",
       createdNow: false,

+ 1 - 57
packages/hoppscotch-common/src/components/collections/index.vue

@@ -189,6 +189,7 @@ import {
   moveRESTFolder,
   navigateToFolderWithIndexPath,
   restCollectionStore,
+  cascaseParentCollectionForHeaderAuth,
 } from "~/newstore/collections"
 import TeamCollectionAdapter from "~/helpers/teams/TeamCollectionAdapter"
 import {
@@ -1314,63 +1315,6 @@ const selectPicked = (payload: Picked | null) => {
   emit("select", payload)
 }
 
-const cascaseParentCollectionForHeaderAuth = (
-  folderPath: string | undefined
-) => {
-  let auth: HoppRESTRequest["auth"] = {
-    authType: "none",
-    authActive: false,
-  }
-  const headers: HoppRESTRequest["headers"] = []
-  let name = ""
-  if (!folderPath) return { auth, headers, name: "" }
-  const path = folderPath.split("/").map((i) => parseInt(i))
-
-  // Check if the path is empty or invalid
-  if (!path || path.length === 0) {
-    console.error("Invalid path:", folderPath)
-    return { auth, headers, name: "" }
-  }
-
-  // Loop through the path and get the last parent folder with authType other than 'inherit'
-  for (let i = 0; i < path.length; i++) {
-    const parentFolder = navigateToFolderWithIndexPath(
-      restCollectionStore.value.state,
-      [...path.slice(0, i + 1)] // Create a copy of the path array
-    )
-
-    // Check if parentFolder is undefined or null
-    if (!parentFolder) {
-      console.error("Parent folder not found for path:", path)
-      return { auth, headers, name: "" }
-    }
-
-    const parentFolderAuth = parentFolder.auth
-    const parentFolderHeaders = parentFolder.headers
-
-    // Check if authType is not 'inherit'
-    if (parentFolderAuth?.authType !== "inherit") {
-      auth = parentFolderAuth || auth
-      name = parentFolder.name
-    }
-    // Update headers, overwriting duplicates by key
-    if (parentFolderHeaders) {
-      const activeHeaders = parentFolderHeaders.filter((h) => h.active)
-      activeHeaders.forEach((header) => {
-        const index = headers.findIndex((h) => h.key === header.key)
-        if (index !== -1) {
-          // Replace the existing header with the same key
-          headers[index] = header
-        } else {
-          headers.push(header)
-        }
-      })
-    }
-  }
-
-  return { auth, headers, name }
-}
-
 /**
  * This function is called when the user clicks on a request
  * @param selectedRequest The request that the user clicked on emited from the collection tree

+ 57 - 0
packages/hoppscotch-common/src/newstore/collections.ts

@@ -62,6 +62,63 @@ export function navigateToFolderWithIndexPath(
   return target !== undefined ? target : null
 }
 
+export function cascaseParentCollectionForHeaderAuth(
+  folderPath: string | undefined
+) {
+  let auth: HoppRESTRequest["auth"] = {
+    authType: "none",
+    authActive: false,
+  }
+  const headers: HoppRESTRequest["headers"] = []
+  let name = ""
+  if (!folderPath) return { auth, headers, name: "" }
+  const path = folderPath.split("/").map((i) => parseInt(i))
+
+  // Check if the path is empty or invalid
+  if (!path || path.length === 0) {
+    console.error("Invalid path:", folderPath)
+    return { auth, headers, name: "" }
+  }
+
+  // Loop through the path and get the last parent folder with authType other than 'inherit'
+  for (let i = 0; i < path.length; i++) {
+    const parentFolder = navigateToFolderWithIndexPath(
+      restCollectionStore.value.state,
+      [...path.slice(0, i + 1)] // Create a copy of the path array
+    )
+
+    // Check if parentFolder is undefined or null
+    if (!parentFolder) {
+      console.error("Parent folder not found for path:", path)
+      return { auth, headers, name: "" }
+    }
+
+    const parentFolderAuth = parentFolder.auth
+    const parentFolderHeaders = parentFolder.headers
+
+    // Check if authType is not 'inherit'
+    if (parentFolderAuth?.authType !== "inherit") {
+      auth = parentFolderAuth || auth
+      name = parentFolder.name
+    }
+    // Update headers, overwriting duplicates by key
+    if (parentFolderHeaders) {
+      const activeHeaders = parentFolderHeaders.filter((h) => h.active)
+      activeHeaders.forEach((header) => {
+        const index = headers.findIndex((h) => h.key === header.key)
+        if (index !== -1) {
+          // Replace the existing header with the same key
+          headers[index] = header
+        } else {
+          headers.push(header)
+        }
+      })
+    }
+  }
+
+  return { auth, headers, name }
+}
+
 function reorderItems(array: unknown[], from: number, to: number) {
   const item = array.splice(from, 1)[0]
   if (from < to) {