This repository has been archived by the owner on Dec 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
152 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,69 @@ | ||
'use strict' | ||
|
||
function postmanToOpenApi (input, output, opts) { | ||
console.log(input + output + opts) | ||
const { promises: { writeFile, readFile } } = require('fs') | ||
const { safeDump } = require('js-yaml') | ||
|
||
async function postmanToOpenApi (input, output, { save = true }) { | ||
// TODO validate? | ||
const collectionFile = await readFile(input) | ||
const postmanJson = JSON.parse(collectionFile) | ||
const { item: items, info: { name: title, description } } = postmanJson | ||
const paths = {} | ||
|
||
for (const item of items) { | ||
const { request: { url: { path }, method, body }, name: summary } = item | ||
const compiledPath = '/' + path.join('/') | ||
if (!paths[compiledPath]) paths[compiledPath] = {} | ||
paths[compiledPath][method.toLowerCase()] = { | ||
summary, | ||
requestBody: parseBody(body), | ||
responses: { | ||
200: { | ||
description: 'Successful response', | ||
content: { | ||
'application/json': {} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
const openApi = { | ||
openapi: '3.0.0', | ||
info: { | ||
title, | ||
description, | ||
version: '1.0.0' | ||
}, | ||
paths | ||
} | ||
|
||
if (save) { | ||
await writeFile(output, safeDump(openApi), 'utf8') | ||
} | ||
|
||
return openApi | ||
} | ||
|
||
function parseBody (body) { | ||
if (body.mode === 'raw') { | ||
return { | ||
content: { | ||
'application/json': { | ||
schema: { | ||
type: 'object', | ||
example: JSON.parse(body.raw) | ||
} | ||
} | ||
} | ||
} | ||
} else { | ||
return { | ||
content: { | ||
'text/plain': {} | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = postmanToOpenApi |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,5 +65,8 @@ | |
"functions": 90, | ||
"branches": 90, | ||
"check-coverage": true | ||
}, | ||
"dependencies": { | ||
"js-yaml": "^3.14.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
openapi: 3.0.0 | ||
info: | ||
title: Postman to OpenAPI | ||
description: 'Mi super test collection from postman ' | ||
version: 1.0.0 | ||
paths: | ||
/hola: | ||
post: | ||
summary: Post example 2 | ||
requestBody: | ||
content: | ||
text/plain: {} | ||
responses: | ||
'200': | ||
description: Successful response | ||
content: | ||
application/json: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters