refactor: inherit properties when saving

This commit is contained in:
nivedin
2023-11-30 14:16:09 +05:30
committed by Andrew Bastin
parent 7c3a84246d
commit b893607ad1
3 changed files with 92 additions and 57 deletions

View File

@@ -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) {