refactor: inherit properties when saving
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user