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

Commit

Permalink
feat: added concat and separator option to folder tags calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
joolfe committed Mar 28, 2021
1 parent 6397d7c commit 4513095
Show file tree
Hide file tree
Showing 4 changed files with 433 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ const { dump } = require('js-yaml')
const { parseMdTable } = require('./md-utils')
const { version } = require('../package.json')

async function postmanToOpenApi (input, output, { info = {}, defaultTag = 'default', pathDepth = 0, auth, servers, externalDocs = {} } = {}) {
async function postmanToOpenApi (input, output, {
info = {}, defaultTag = 'default', pathDepth = 0,
auth, servers, externalDocs = {}, folders = {}
} = {}) {
// TODO validate?
const collectionFile = await readFile(input)
const postmanJson = JSON.parse(collectionFile)
Expand All @@ -16,8 +19,8 @@ async function postmanToOpenApi (input, output, { info = {}, defaultTag = 'defau

for (let [i, element] of items.entries()) {
while (element.item != null) { // is a folder
const { item, name, description: tagDesc } = element
const tag = element.tag ? element.tag + ' > ' + name : name
const { item, description: tagDesc } = element
const tag = calculateFolderTag(element, folders)
const tagged = item.map(e => ({ ...e, tag }))
tags[tag] = tagDesc
items.splice(i, 1, ...tagged)
Expand Down Expand Up @@ -60,6 +63,11 @@ async function postmanToOpenApi (input, output, { info = {}, defaultTag = 'defau
return openApiYml
}

/* Calculate the tags for folders items based on the options */
function calculateFolderTag ({ tag, name }, { separator = ' > ', concat = true }) {
return (tag && concat) ? `${tag}${separator}${name}` : name
}

function compileInfo (postmanJson, optsInfo) {
const { info: { name, description: desc }, variable = [] } = postmanJson
const ver = getVarValue(variable, 'version', '1.0.0')
Expand Down
12 changes: 12 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ const EXPECTED_INFO_OPTS = readFileSync('./test/resources/output/InfoOpts.yml',
const EXPECTED_NO_VERSION = readFileSync('./test/resources/output/NoVersion.yml', 'utf8')
const EXPECTED_CUSTOM_TAG = readFileSync('./test/resources/output/CustomTag.yml', 'utf8')
const EXPECTED_FOLDERS = readFileSync('./test/resources/output/Folders.yml', 'utf8')
const EXPECTED_FOLDERS_NO_CONCAT = readFileSync('./test/resources/output/FoldersNoConcat.yml', 'utf8')
const EXPECTED_FOLDERS_SEPARATOR = readFileSync('./test/resources/output/FoldersSeparator.yml', 'utf8')
const EXPECTED_GET_METHODS = readFileSync('./test/resources/output/GetMethods.yml', 'utf8')
const EXPECTED_HEADERS = readFileSync('./test/resources/output/Headers.yml', 'utf8')
const EXPECTED_AUTH_BEARER = readFileSync('./test/resources/output/AuthBearer.yml', 'utf8')
Expand Down Expand Up @@ -104,6 +106,16 @@ describe('Library specs', function () {
equal(result, EXPECTED_FOLDERS)
})

it('should use "folders.separator" options for customize tags separators ', async function () {
const result = await postmanToOpenApi(COLLECTION_FOLDERS, OUTPUT_PATH, { folders: { separator: '-' } })
equal(result, EXPECTED_FOLDERS_SEPARATOR)
})

it('should use "folders.concat" options for not concatenate folder names as tags ', async function () {
const result = await postmanToOpenApi(COLLECTION_FOLDERS, OUTPUT_PATH, { folders: { concat: false } })
equal(result, EXPECTED_FOLDERS_NO_CONCAT)
})

it('should parse GET methods with query string', async function () {
const result = await postmanToOpenApi(COLLECTION_GET, OUTPUT_PATH)
equal(result, EXPECTED_GET_METHODS)
Expand Down
204 changes: 204 additions & 0 deletions test/resources/output/FoldersNoConcat.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
openapi: 3.0.0
info:
title: Folder Collection
description: Just a simple collection for test
version: 2.3.0
servers:
- url: https://api.io
tags:
- name: Users
description: Operations at User level
- name: Admin
description: Admin operations for Post items
- name: Posts
description: Operations for Post items
- name: Folder 1
description: Folder 1 description
- name: Folder 2
description: Folder 2 description
- name: Folder 3
description: Folder 3 description
- name: Folder 4
description: Folder 4 description
- name: Folder 5
description: Folder 5 description
- name: Empty Folder
paths:
/users/admin/roles:
get:
tags:
- Admin
summary: Get user roles
description: This is a get request in a nested folder
responses:
'200':
description: Successful response
content:
application/json: {}
/users:
post:
tags:
- Users
summary: Create a user
description: This is a post request with json body
requestBody:
content:
application/json:
schema:
type: object
example:
example: field
other:
data1: 'yes'
data2: 'no'
responses:
'200':
description: Successful response
content:
application/json: {}
/customer:
post:
tags:
- Users
summary: Create a customer
description: This is a post request with json body for create a customer
requestBody:
content:
application/json:
schema:
type: object
example:
example: field
other:
data1: 'yes'
data2: 'no'
responses:
'200':
description: Successful response
content:
application/json: {}
/posts/admin/settings:
get:
tags:
- Admin
summary: Get posts settings
description: This is a get request in a nested folder
responses:
'200':
description: Successful response
content:
application/json: {}
/posts:
post:
tags:
- Posts
summary: Create a post
requestBody:
content:
text/plain: {}
responses:
'200':
description: Successful response
content:
application/json: {}
/req4:
post:
tags:
- Folder 5
summary: Request 4
description: This is a post request with json body
requestBody:
content:
application/json:
schema:
type: object
example:
example: field
other:
data1: 'yes'
data2: 'no'
responses:
'200':
description: Successful response
content:
application/json: {}
/req3:
post:
tags:
- Folder 3
summary: Request 3
description: This is a post request with json body
requestBody:
content:
application/json:
schema:
type: object
example:
example: field
other:
data1: 'yes'
data2: 'no'
responses:
'200':
description: Successful response
content:
application/json: {}
/req2:
post:
tags:
- Folder 2
summary: Request 2
description: This is a post request with json body
requestBody:
content:
application/json:
schema:
type: object
example:
example: field
other:
data1: 'yes'
data2: 'no'
responses:
'200':
description: Successful response
content:
application/json: {}
/req1:
post:
tags:
- Folder 1
summary: Request 1
description: This is a post request with json body
requestBody:
content:
application/json:
schema:
type: object
example:
example: field
other:
data1: 'yes'
data2: 'no'
responses:
'200':
description: Successful response
content:
application/json: {}
/info:
post:
tags:
- default
summary: Create a info
requestBody:
content:
application/json:
schema:
type: object
example:
test: here
responses:
'200':
description: Successful response
content:
application/json: {}
Loading

0 comments on commit 4513095

Please sign in to comment.