diff --git a/lib/index.js b/lib/index.js index 49ede1d..c720562 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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) } @@ -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 diff --git a/test/index.spec.js b/test/index.spec.js index 80eac4a..d563a6e 100644 --- a/test/index.spec.js +++ b/test/index.spec.js @@ -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') @@ -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) + }) })