Skip to content
This repository has been archived by the owner on Dec 27, 2024. It is now read-only.

Commit

Permalink
feat: add support for nested folders
Browse files Browse the repository at this point in the history
  • Loading branch information
paymand committed Mar 23, 2021
1 parent 3eab561 commit 4f5ee97
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 30 deletions.
25 changes: 19 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,30 @@ async function postmanToOpenApi (input, output, { info = {}, defaultTag = 'defau
const tags = {}

for (let [i, element] of items.entries()) {
const { item, name, description: tagDesc } = element
if (item != null) { // is a folder
const tagged = item.map(e => ({ ...e, tag: name }))
tags[name] = tagDesc
items.splice(i, 1, ...tagged)
element = tagged.shift()
while (element['item'] != null) { // is a folder
const { item, name, description: tagDesc } = element
const tagged = item.map(e => ({ ...e, tag: element.tag ? element.tag + ' > ' + name : name }))
tags[element.tag ? element.tag + ' > ' + name : name] = tagDesc
if (tagged.length){
items.splice(i, 1, ...tagged)
element = tagged.shift()
} else {
element = null;
break;
}
}
if (!element) {
continue;
}
const {
request: { url, method, body, description: rawDesc, header },
name: summary, tag = defaultTag, event: events
} = element

if (url.raw == "") {
console.log(`Empty url for request '${summary}'. Ignoring this request`)
continue;
}
const { path, query, protocol, host, port } = scrapeURL(url)
domains.add(calculateDomains(protocol, host, port))
const joinedPath = calculatePath(path, pathDepth)
Expand Down
112 changes: 100 additions & 12 deletions test/resources/input/v2/FolderCollection.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"info": {
"_postman_id": "d56d78d2-cf20-4779-ad84-d9e578ab9d5a",
"_postman_id": "4adefe15-8b43-49c5-9336-5a5296a26592",
"name": "Folder Collection",
"description": "Just a simple collection for test",
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
Expand All @@ -9,6 +9,54 @@
{
"name": "Users",
"item": [
{
"name": "Admin",
"item": [
{
"name": "Get user roles",
"protocolProfileBehavior": {
"disableBodyPruning": true
},
"request": {
"method": "GET",
"header": [],
"body": {
"mode": "raw",
"raw": "",
"options": {
"raw": {
"language": "json"
}
}
},
"url": "https://api.io/users/admin/roles",
"description": "This is a get request in a nested folder"
},
"response": []
}
],
"description": "Admin operations at User level",
"event": [
{
"listen": "prerequest",
"script": {
"type": "text/javascript",
"exec": [
""
]
}
},
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
""
]
}
}
]
},
{
"name": "Create a user",
"request": {
Expand Down Expand Up @@ -53,7 +101,6 @@
{
"listen": "prerequest",
"script": {
"id": "686448f3-5f27-4ab0-be72-f5751d40d587",
"type": "text/javascript",
"exec": [
""
Expand All @@ -63,19 +110,65 @@
{
"listen": "test",
"script": {
"id": "be665e36-6d0b-402d-9577-03aa4601d721",
"type": "text/javascript",
"exec": [
""
]
}
}
],
"protocolProfileBehavior": {}
]
},
{
"name": "Posts",
"item": [
{
"name": "Admin",
"item": [
{
"name": "Get posts settings",
"protocolProfileBehavior": {
"disableBodyPruning": true
},
"request": {
"method": "GET",
"header": [],
"body": {
"mode": "raw",
"raw": "",
"options": {
"raw": {
"language": "json"
}
}
},
"url": "https://api.io/posts/admin/settings",
"description": "This is a get request in a nested folder"
},
"response": []
}
],
"description": "Admin operations for Post items",
"event": [
{
"listen": "prerequest",
"script": {
"type": "text/javascript",
"exec": [
""
]
}
},
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
""
]
}
}
]
},
{
"name": "Create a post",
"request": {
Expand All @@ -100,7 +193,6 @@
{
"listen": "prerequest",
"script": {
"id": "090c5815-9bf7-42ad-aee7-4e0b6949064e",
"type": "text/javascript",
"exec": [
""
Expand All @@ -110,15 +202,13 @@
{
"listen": "test",
"script": {
"id": "b57c9340-3e13-4e3a-8a20-38cd0ee3c4b4",
"type": "text/javascript",
"exec": [
""
]
}
}
],
"protocolProfileBehavior": {}
]
},
{
"name": "Create a info",
Expand All @@ -141,10 +231,8 @@
],
"variable": [
{
"id": "e4619cfa-c046-413a-9e69-f40a20248a4d",
"key": "version",
"value": "2.3.0"
}
],
"protocolProfileBehavior": {}
]
}
Loading

0 comments on commit 4f5ee97

Please sign in to comment.