Skip to content

Commit

Permalink
feat: add JSON format support
Browse files Browse the repository at this point in the history
  • Loading branch information
joolfe committed Aug 29, 2022
1 parent 09a3f26 commit c16a09f
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const jsonc = require('jsonc-parser')
async function postmanToOpenApi (input, output, {
info = {}, defaultTag = 'default', pathDepth = 0,
auth: optsAuth, servers, externalDocs = {}, folders = {},
responseHeaders = true, replaceVars = false, additionalVars = {}
responseHeaders = true, replaceVars = false, additionalVars = {}, outputFormat = 'yaml'
} = {}) {
// TODO validate?
let collectionFile = await resolveInput(input)
Expand Down Expand Up @@ -66,11 +66,11 @@ async function postmanToOpenApi (input, output, {
...parseTags(tags),
paths
}
const openApiYml = dump(openApi, { skipInvalid: true })
const openApiDoc = outputFormat === 'json' ? JSON.stringify(openApi, null, 4) : dump(openApi, { skipInvalid: true })
if (output != null) {
await writeFile(output, openApiYml, 'utf8')
await writeFile(output, openApiDoc, 'utf8')
}
return openApiYml
return openApiDoc
}

/* Calculate the tags for folders items based on the options */
Expand Down
6 changes: 6 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const COLLECTION_NO_OPTIONS = './test/resources/input/NoOptionsInBody.json'
const COLLECTION_NULL_HEADERS = './test/resources/input/NullHeaders.json'

const EXPECTED_BASIC = readFileSync('./test/resources/output/Basic.yml', 'utf8')
const EXPECTED_BASIC_JSON = readFileSync('./test/resources/output/Basic.json', 'utf8')
const EXPECTED_BASIC_NO_OPTS = readFileSync('./test/resources/output/BasicNoOptions.yml', 'utf8')
const EXPECTED_INFO_OPTS = readFileSync('./test/resources/output/InfoOpts.yml', 'utf8')
const EXPECTED_NO_VERSION = readFileSync('./test/resources/output/NoVersion.yml', 'utf8')
Expand Down Expand Up @@ -491,6 +492,11 @@ describe('Library specs', function () {
const result = await postmanToOpenApi(COLLECTION_JSON_COMMENTS, OUTPUT_PATH, { pathDepth: 2 })
equal(result, EXPECTED_COLLECTION_JSON_COMMENTS)
})

it('should return "json" format is requested', async function () {
const result = await postmanToOpenApi(COLLECTION_BASIC, OUTPUT_PATH, { outputFormat: 'json' })
equal(result, EXPECTED_BASIC_JSON)
})
})
})

Expand Down
96 changes: 96 additions & 0 deletions test/resources/output/Basic.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
"openapi": "3.0.0",
"info": {
"title": "Postman to OpenAPI",
"description": "Mi super test collection from postman",
"version": "1.1.0"
},
"servers": [
{
"url": "https://api.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": {}
}
}
}
}
},
"/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": {
"text/plain": {
"schema": {
"type": "string",
"example": "This is an example Note"
}
}
}
},
"responses": {
"200": {
"description": "Successful response",
"content": {
"application/json": {}
}
}
}
}
}
}
}

0 comments on commit c16a09f

Please sign in to comment.