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

Commit

Permalink
feat: Servers cfg #10 AUTO
Browse files Browse the repository at this point in the history
Automatic generate servers array
  • Loading branch information
joolfe committed Aug 1, 2020
1 parent 8c7da53 commit 07e68df
Show file tree
Hide file tree
Showing 15 changed files with 281 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ async function postmanToOpenApi (input, output, { save = true, info = {}, defaul
const postmanJson = JSON.parse(collectionFile)
const { item: items } = postmanJson
const paths = {}
const domains = new Set()

for (let [i, element] of items.entries()) {
const { item, name } = element
Expand All @@ -18,9 +19,10 @@ async function postmanToOpenApi (input, output, { save = true, info = {}, defaul
element = tagged.shift()
}
const {
request: { url: { path, query }, method, body, description, header },
request: { url: { path, query, protocol, host }, method, body, description, header },
name: summary, tag = defaultTag
} = element
domains.add(calculateDomains(protocol, host))
const joinedPath = calculatePath(path)
if (!paths[joinedPath]) paths[joinedPath] = {}
paths[joinedPath][method.toLowerCase()] = {
Expand All @@ -43,6 +45,7 @@ async function postmanToOpenApi (input, output, { save = true, info = {}, defaul
const openApi = {
openapi: '3.0.0',
info: compileInfo(postmanJson, info),
...parseServers(domains),
...parseAuth(postmanJson, auth),
paths
}
Expand Down Expand Up @@ -209,4 +212,14 @@ function calculatePath (paths) {
.join('/')
}

function calculateDomains (protocol, hosts) {
return protocol + '://' + hosts.join('.')
}

/* Parse domains from operations for servers root array */
function parseServers (domains) {
const servers = Array.from(domains).map(domain => ({ url: domain }))
return { servers }
}

module.exports = postmanToOpenApi
7 changes: 7 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const COLLECTION_HEADERS = './test/resources/input/Headers.json'
const COLLECTION_AUTH_BEARER = './test/resources/input/AuthBearer.json'
const COLLECTION_AUTH_BASIC = './test/resources/input/AuthBasic.json'
const COLLECTION_PATH_PARAMS = './test/resources/input/PathParams.json'
const COLLECTION_MULTIPLE_SERVERS = './test/resources/input/MultipleServers.json'

const EXPECTED_BASIC = readFileSync('./test/resources/output/Basic.yml', 'utf8')
const EXPECTED_INFO_OPTS = readFileSync('./test/resources/output/InfoOpts.yml', 'utf8')
Expand All @@ -29,6 +30,7 @@ const EXPECTED_AUTH_BEARER = readFileSync('./test/resources/output/AuthBearer.ym
const EXPECTED_AUTH_BASIC = readFileSync('./test/resources/output/AuthBasic.yml', 'utf8')
const EXPECTED_BASIC_WITH_AUTH = readFileSync('./test/resources/output/BasicWithAuth.yml', 'utf8')
const EXPECTED_PATH_PARAMS = readFileSync('./test/resources/output/PathParams.yml', 'utf8')
const EXPECTED_MULTIPLE_SERVERS = readFileSync('./test/resources/output/MultipleServers.yml', 'utf8')

describe('Library specs', function () {
afterEach('remove file', function () {
Expand Down Expand Up @@ -116,4 +118,9 @@ describe('Library specs', function () {
const result = await postmanToOpenApi(COLLECTION_PATH_PARAMS, OUTPUT_PATH)
equal(EXPECTED_PATH_PARAMS, result)
})

it('should parse servers from existing host in postman collection', async function () {
const result = await postmanToOpenApi(COLLECTION_MULTIPLE_SERVERS, OUTPUT_PATH)
equal(EXPECTED_MULTIPLE_SERVERS, result)
})
})
157 changes: 157 additions & 0 deletions test/resources/input/MultipleServers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
{
"info": {
"_postman_id": "bff596d1-5880-4c4a-9f53-e49dee178afc",
"name": "MultipleServers",
"description": "Collection to test multiples server parsing.",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Create new User",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"example\": \"field\",\n \"other\": {\n \"data1\": \"yes\",\n \"data2\": \"no\"\n }\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "https://api.io/users",
"protocol": "https",
"host": [
"api",
"io"
],
"path": [
"users"
]
},
"description": "Create a new user into your amazing API"
},
"response": []
},
{
"name": "Create a post",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "file",
"file": {},
"options": {
"raw": {
"language": "text"
}
}
},
"url": {
"raw": "https://api.dev.io/posts",
"protocol": "https",
"host": [
"api",
"dev",
"io"
],
"path": [
"posts"
]
}
},
"response": []
},
{
"name": "Create a note",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "This is an example Note",
"options": {
"raw": {
"language": "text"
}
}
},
"url": {
"raw": "https://api.pre.io/note",
"protocol": "https",
"host": [
"api",
"pre",
"io"
],
"path": [
"note"
]
},
"description": "Just an example of text raw body"
},
"response": []
},
{
"name": "Update User",
"request": {
"method": "PUT",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"example\": \"field\",\n \"other\": {\n \"data1\": \"yes\",\n \"data2\": \"no\"\n }\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "https://api.io/users",
"protocol": "https",
"host": [
"api",
"io"
],
"path": [
"users"
]
},
"description": "Update an existing user"
},
"response": []
}
],
"event": [
{
"listen": "prerequest",
"script": {
"id": "15118c41-9c90-4fad-85f3-81a87bbdf5a0",
"type": "text/javascript",
"exec": [
""
]
}
},
{
"listen": "test",
"script": {
"id": "0ef1db97-1c19-46a8-9fca-a6843a1df616",
"type": "text/javascript",
"exec": [
""
]
}
}
],
"variable": [
{
"id": "600f8632-5dfe-418b-83cd-f00302a437c0",
"key": "version",
"value": "1.1.0"
}
],
"protocolProfileBehavior": {}
}
2 changes: 2 additions & 0 deletions test/resources/output/AuthBasic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ info:
title: AuthBasic
description: Collection to test authorization global
version: 1.0.0
servers:
- url: 'https://api.io'
components:
securitySchemes:
basicAuth:
Expand Down
2 changes: 2 additions & 0 deletions test/resources/output/AuthBearer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ info:
title: AuthBearer
description: Collection to test authorization global
version: 1.0.0
servers:
- url: 'https://api.io'
components:
securitySchemes:
bearerAuth:
Expand Down
2 changes: 2 additions & 0 deletions test/resources/output/Basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ info:
title: Postman to OpenAPI
description: Mi super test collection from postman
version: 1.1.0
servers:
- url: 'https://api.io'
paths:
/users:
post:
Expand Down
2 changes: 2 additions & 0 deletions test/resources/output/BasicWithAuth.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ info:
title: Postman to OpenAPI
description: Mi super test collection from postman
version: 1.1.0
servers:
- url: 'https://api.io'
components:
securitySchemes:
myCustomAuth:
Expand Down
2 changes: 2 additions & 0 deletions test/resources/output/CustomTag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ info:
title: Simple Post
description: Just a simple collection for test
version: 2.3.0
servers:
- url: 'https://api.io'
paths:
/users:
post:
Expand Down
2 changes: 2 additions & 0 deletions test/resources/output/Folders.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ info:
title: Folder Collection
description: Just a simple collection for test
version: 2.3.0
servers:
- url: 'https://api.io'
paths:
/users:
post:
Expand Down
2 changes: 2 additions & 0 deletions test/resources/output/GetMethods.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ info:
title: Get Methods
description: API to manage GET methods
version: 1.0.0
servers:
- url: 'https://api.io'
paths:
/users:
get:
Expand Down
2 changes: 2 additions & 0 deletions test/resources/output/Headers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ info:
title: Headers
description: API with headers
version: 1.0.0
servers:
- url: 'https://api.io'
paths:
/users:
get:
Expand Down
2 changes: 2 additions & 0 deletions test/resources/output/InfoOpts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ info:
description: Description from options
version: 6.0.7-beta
termsOfService: 'http://tos.myweb.com'
servers:
- url: 'https://api.io'
paths:
/users:
post:
Expand Down
81 changes: 81 additions & 0 deletions test/resources/output/MultipleServers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
openapi: 3.0.0
info:
title: MultipleServers
description: Collection to test multiples server parsing.
version: 1.1.0
servers:
- url: 'https://api.io'
- url: 'https://api.dev.io'
- url: 'https://api.pre.io'
paths:
/users:
post:
tags:
- default
summary: Create new User
description: Create a new user into your amazing API
requestBody:
content:
application/json:
schema:
type: object
example:
example: field
other:
data1: 'yes'
data2: 'no'
responses:
'200':
description: Successful response
content:
application/json: {}
put:
tags:
- default
summary: Update User
description: Update an existing user
requestBody:
content:
application/json:
schema:
type: object
example:
example: field
other:
data1: 'yes'
data2: 'no'
responses:
'200':
description: Successful response
content:
application/json: {}
/posts:
post:
tags:
- default
summary: Create a post
requestBody:
content:
text/plain: {}
responses:
'200':
description: Successful response
content:
application/json: {}
/note:
post:
tags:
- default
summary: Create a note
description: Just an example of text raw body
requestBody:
content:
application/json:
schema:
type: string
example: This is an example Note
responses:
'200':
description: Successful response
content:
application/json: {}
2 changes: 2 additions & 0 deletions test/resources/output/NoVersion.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ info:
title: No Version Collection
description: This collection don't have a version as variable
version: 1.0.0
servers:
- url: 'https://api.io'
paths:
/users:
post:
Expand Down
Loading

0 comments on commit 07e68df

Please sign in to comment.