Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

Handle postman subfolders when importing json (#1150)

Florin Mirosnicencuab736cf9752a
diff --git a/components/collections/import-export-collections.vue b/components/collections/import-export-collections.vue
index 3a89735c4..f8c16d907 100644
--- a/components/collections/import-export-collections.vue
+++ b/components/collections/import-export-collections.vue
@@ -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>