From ab736cf9752ac0e83076b72e31fc6cd084fb532f Mon Sep 17 00:00:00 2001 From: Florin Mirosnicencu Date: Tue, 8 Sep 2020 16:14:40 +0100 Subject: [PATCH] Handle postman subfolders when importing json (#1150) --- .../collections/import-export-collections.vue | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/components/collections/import-export-collections.vue b/components/collections/import-export-collections.vue index 3a89735c..f8c16d90 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 <> 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 + }, }, }