Handle postman subfolders when importing json (#1150)
This commit is contained in:
committed by
GitHub
parent
c36239772e
commit
ab736cf975
@@ -141,6 +141,7 @@ export default {
|
|||||||
} else if (collections.info && collections.info.schema.includes("v2.1.0")) {
|
} else if (collections.info && collections.info.schema.includes("v2.1.0")) {
|
||||||
//replace the variables, postman uses {{var}}, Hoppscotch uses <<var>>
|
//replace the variables, postman uses {{var}}, Hoppscotch uses <<var>>
|
||||||
collections = JSON.parse(content.replaceAll(/{{([a-z]+)}}/gi, '<<$1>>'))
|
collections = JSON.parse(content.replaceAll(/{{([a-z]+)}}/gi, '<<$1>>'))
|
||||||
|
collections.item = this.flattenPostmanFolders(collections)
|
||||||
collections = this.parsePostmanCollection(collections)
|
collections = this.parsePostmanCollection(collections)
|
||||||
} else {
|
} else {
|
||||||
return this.failedImport()
|
return this.failedImport()
|
||||||
@@ -204,6 +205,10 @@ export default {
|
|||||||
name: "",
|
name: "",
|
||||||
requests: [],
|
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) {
|
for (let collectionItem of collection.item) {
|
||||||
if (collectionItem.request) {
|
if (collectionItem.request) {
|
||||||
if (postwomanCollection[0]) {
|
if (postwomanCollection[0]) {
|
||||||
@@ -295,6 +300,44 @@ export default {
|
|||||||
}
|
}
|
||||||
return pwRequest
|
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>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user