Handle postman subfolders when importing json (#1150)

This commit is contained in:
Florin Mirosnicencu
2020-09-08 16:14:40 +01:00
committed by GitHub
parent c36239772e
commit ab736cf975

View File

@@ -141,6 +141,7 @@ export default {
} else if (collections.info && collections.info.schema.includes("v2.1.0")) {
//replace the variables, postman uses {{var}}, Hoppscotch uses <<var>>
collections = JSON.parse(content.replaceAll(/{{([a-z]+)}}/gi, '<<$1>>'))
collections.item = this.flattenPostmanFolders(collections)
collections = this.parsePostmanCollection(collections)
} else {
return this.failedImport()
@@ -204,6 +205,10 @@ export default {
name: "",
requests: [],
}
if (folders) {
//pick up collection name even when all children are folders
postwomanCollection[0].name = collection.info ? collection.info.name : ""
}
for (let collectionItem of collection.item) {
if (collectionItem.request) {
if (postwomanCollection[0]) {
@@ -295,6 +300,44 @@ export default {
}
return pwRequest
},
flattenPostmanFolders(collection) {
let items = []
for (let collectionItem of collection.item) {
if (this.hasFolder(collectionItem)) {
let newFolderItems = []
for (let folderItem of collectionItem.item) {
if (this.isSubFolder(folderItem)) {
newFolderItems = newFolderItems.concat(this.flattenPostmanItem(folderItem))
} else {
newFolderItems.push(folderItem)
}
}
collectionItem.item = newFolderItems
}
items.push(collectionItem)
}
return items;
},
hasFolder(item) {
return item.hasOwnProperty('item')
},
isSubFolder(item) {
return item.hasOwnProperty('_postman_isSubFolder') && item._postman_isSubFolder
},
flattenPostmanItem(subFolder, subFolderGlue = ' -- ') {
delete subFolder._postman_isSubFolder
let flattenedItems = []
for (let subFolderItem of subFolder.item) {
subFolderItem.name = subFolder.name + subFolderGlue + subFolderItem.name
if (this.isSubFolder(subFolderItem)) {
flattenedItems = flattenedItems.concat(this.flattenPostmanItem(subFolderItem))
} else {
flattenedItems.push(subFolderItem)
}
}
return flattenedItems
},
},
}
</script>