From c16a09ff3acdfebdef44bffff4aef6f57afde447 Mon Sep 17 00:00:00 2001 From: Jorge Date: Mon, 29 Aug 2022 13:25:12 +0200 Subject: [PATCH 1/5] feat: add JSON format support --- lib/index.js | 8 +-- test/index.spec.js | 6 ++ test/resources/output/Basic.json | 96 ++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 4 deletions(-) create mode 100644 test/resources/output/Basic.json diff --git a/lib/index.js b/lib/index.js index 864811d..e858597 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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) @@ -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 */ diff --git a/test/index.spec.js b/test/index.spec.js index 3948929..2925182 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -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') @@ -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) + }) }) }) diff --git a/test/resources/output/Basic.json b/test/resources/output/Basic.json new file mode 100644 index 0000000..dcc5915 --- /dev/null +++ b/test/resources/output/Basic.json @@ -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": {} + } + } + } + } + } + } +} \ No newline at end of file From c0a37aef1c74fa9e0db78e540c38c3cffa544aed Mon Sep 17 00:00:00 2001 From: Jorge Date: Mon, 29 Aug 2022 13:32:45 +0200 Subject: [PATCH 2/5] docs: document new option --- docs/index.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docs/index.md b/docs/index.md index 5fea780..ff5ae2f 100644 --- a/docs/index.md +++ b/docs/index.md @@ -132,6 +132,7 @@ The third parameter used in the library method is an `options` object containing | [responseHeaders](#responseheaders-boolean) | Indicate if should parse the response headers from the collection examples. | | [replaceVars](#replacevars-boolean) | Boolean value to indicate if postman variables should be replaced.| | [additionalVars](#additionalvars-object) | Object to provide additional values for variables replacement.| +| [outputFormat](#outputformat-string) | Indicate the format of the output document. | ### info (Object) @@ -346,6 +347,12 @@ This parameter is a json Object that contain as key the variable name and as val Take into account that variable values provided in the `additionalVars` Object supersede those defined at Postman collection level. +### outputFormat (string) + +Indicates the resulting format of the OpenAPI document between `json` and `yaml`, the resulting file will be writte using this format and also the result value fo the method `postmanToOpenApi(...)` will use this format. + +Default value is `yaml`, if you use a unknown value `yaml` will be used. + # Features ## Basic conversion From 7aea5453d1bc938d45d10331e487674b92514432 Mon Sep 17 00:00:00 2001 From: Jorge Date: Mon, 29 Aug 2022 13:34:44 +0200 Subject: [PATCH 3/5] build: update version and ts devs --- package.json | 2 +- types/index.d.ts | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 778f972..ae4c952 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "postman-to-openapi", - "version": "2.5.0", + "version": "2.6.0", "description": "Convert postman collection to OpenAPI spec", "main": "lib/index.js", "types": "types/index.d.ts", diff --git a/types/index.d.ts b/types/index.d.ts index ac98d62..f4af157 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -82,7 +82,9 @@ export interface Options { responseHeaders?: boolean, // Default value false replaceVars?: boolean, - additionalVars?: { [key: string]: string } + additionalVars?: { [key: string]: string }, + // Default value 'yaml' + outputFormat?: 'json' | 'yaml' } export default function postmanToOpenApi (input: string, output?: string, options?: Options) : Promise From 2813792f90b60001a97bba76e1f8337fcaa4419d Mon Sep 17 00:00:00 2001 From: Jorge Date: Mon, 29 Aug 2022 13:35:01 +0200 Subject: [PATCH 4/5] build: lock file --- package-lock.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 829eaad..9a1f085 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "postman-to-openapi", - "version": "2.4.2", + "version": "2.6.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "postman-to-openapi", - "version": "2.4.2", + "version": "2.6.0", "license": "MIT", "dependencies": { "commander": "^8.3.0", From 99c8d6aaede6cd0a2d217d3d2a50af1de16b98a5 Mon Sep 17 00:00:00 2001 From: Jorge Date: Mon, 29 Aug 2022 13:35:34 +0200 Subject: [PATCH 5/5] docs: changelog --- CHANGELOG.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91b4c79..e8b8691 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,21 @@ +## [2.6.0](https://github.com/joolfe/postman-to-openapi/compare/2.5.0...2.6.0) (2022-08-29) + + +### Features + +* add JSON format support ([c16a09f](https://github.com/joolfe/postman-to-openapi/commit/c16a09ff3acdfebdef44bffff4aef6f57afde447)) + + +### Documentation + +* document new option ([c0a37ae](https://github.com/joolfe/postman-to-openapi/commit/c0a37aef1c74fa9e0db78e540c38c3cffa544aed)) + + +### Build System + +* lock file ([2813792](https://github.com/joolfe/postman-to-openapi/commit/2813792f90b60001a97bba76e1f8337fcaa4419d)) +* update version and ts devs ([7aea545](https://github.com/joolfe/postman-to-openapi/commit/7aea5453d1bc938d45d10331e487674b92514432)) + ## [2.5.0](https://github.com/joolfe/postman-to-openapi/compare/2.4.2...2.5.0) (2022-08-28)