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

Commit

Permalink
feat: allow input as string instead of file path
Browse files Browse the repository at this point in the history
  • Loading branch information
joolfe committed Jan 15, 2022
1 parent a64c003 commit 5dc1a18
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async function postmanToOpenApi (input, output, {
responseHeaders = true, replaceVars = false, additionalVars = {}
} = {}) {
// TODO validate?
let collectionFile = await readFile(input, 'utf8')
let collectionFile = await resolveInput(input)
if (replaceVars) {
collectionFile = replacePostmanVariables(collectionFile, additionalVars)
}
Expand Down Expand Up @@ -554,6 +554,18 @@ function parseResponseHeaders (headerArray, responseHeaders) {
return (Object.keys(headers).length > 0) ? { headers } : {}
}

/**
* Just check if is a string collection or a path.
* moved to method for allow easy changes in the future like check if it is a collection, validations...
*/
async function resolveInput (input) {
if (input.trim().startsWith('{')) {
return input
} else {
return readFile(input, 'utf8')
}
}

postmanToOpenApi.version = version

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 @@ -6,6 +6,7 @@ const path = require('path')
const { equal, ok } = require('assert').strict
const { readFileSync, existsSync, unlinkSync } = require('fs')
const { version } = require('../package.json')
const { promises: { readFile } } = require('fs')

const OUTPUT_PATH = path.join(__dirname, '/openAPIRes.yml')

Expand Down Expand Up @@ -483,4 +484,10 @@ describe('Library specs', function () {
const result = await postmanToOpenApi(COLLECTION_NULL_HEADERS, OUTPUT_PATH, {})
equal(result, EXPECTED_NULL_HEADER)
})

it('should work with string as input (instead of a file path)', async function () {
const collectionString = await readFile(COLLECTION_NO_OPTIONS, 'utf8')
const result = await postmanToOpenApi(collectionString, OUTPUT_PATH, {})
equal(result, EXPECTED_BASIC)
})
})

0 comments on commit 5dc1a18

Please sign in to comment.