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

Commit

Permalink
feat: allowed javascript object as input and output
Browse files Browse the repository at this point in the history
  • Loading branch information
codeasashu committed Jan 28, 2022
1 parent 17af052 commit 1f1976c
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'

const { promises: { writeFile, readFile } } = require('fs')
const fs = require('fs');
const { dump } = require('js-yaml')
const { parseMdTable } = require('./md-utils')
const { version } = require('../package.json')
Expand All @@ -12,11 +12,11 @@ async function postmanToOpenApi (input, output, {
responseHeaders = true, replaceVars = false, additionalVars = {}
} = {}) {
// TODO validate?
let collectionFile = await resolveInput(input)
let collectionFile = typeof input === "string" ? await resolveInput(input) : input;
if (replaceVars) {
collectionFile = replacePostmanVariables(collectionFile, additionalVars)
}
const _postmanJson = JSON.parse(collectionFile)
const _postmanJson = typeof collectionFile === "string" ? JSON.parse(collectionFile) : collectionFile;
const postmanJson = _postmanJson.collection || _postmanJson
const { item: items, variable = [] } = postmanJson
const paths = {}
Expand Down Expand Up @@ -65,9 +65,12 @@ async function postmanToOpenApi (input, output, {
...parseTags(tags),
paths
}
if (output === 'raw') {
return openApi;
}
const openApiYml = dump(openApi, { skipInvalid: true })
if (output != null) {
await writeFile(output, openApiYml, 'utf8')
await fs.writeFileSync(output, openApiYml, 'utf8')
}
return openApiYml
}
Expand Down Expand Up @@ -562,7 +565,7 @@ async function resolveInput (input) {
if (input.trim().startsWith('{')) {
return input
} else {
return readFile(input, 'utf8')
return fs.readFileSync(input, 'utf8')
}
}

Expand Down

0 comments on commit 1f1976c

Please sign in to comment.