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

Commit

Permalink
feat: basic structure conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
joolfe committed Jun 30, 2020
1 parent b6665e2 commit 05080d9
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 11 deletions.
66 changes: 64 additions & 2 deletions lib/index.js
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
8 changes: 2 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,8 @@
"functions": 90,
"branches": 90,
"check-coverage": true
},
"dependencies": {
"js-yaml": "^3.14.0"
}
}
12 changes: 10 additions & 2 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@

const { describe, it } = require('mocha')
const postmanToOpenApi = require('../lib')
const path = require('path')

describe('First test', function () {
it('test', function () {
postmanToOpenApi('', '', {})
it('should work with a basic transform', async function () {
const input = path.join(__dirname, '/resources/PostmantoOpenAPI.postman_collection.json')
const output = path.join(__dirname, '/openAPIRes.yml')
await postmanToOpenApi(input, output, {})
})

it('should work when no save', async function () {
const input = path.join(__dirname, '/resources/PostmantoOpenAPI.postman_collection.json')
const result = await postmanToOpenApi(input, '', { save: false })
})
})
17 changes: 17 additions & 0 deletions test/openAPIRes.yml
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: {}
57 changes: 56 additions & 1 deletion test/resources/PostmantoOpenAPI.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"info": {
"_postman_id": "2a45baa5-352f-4831-8483-1a0fcbc7da37",
"name": "Postman to OpenAPI",
"description": "Mi super test collection from postman ",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
Expand All @@ -20,16 +21,70 @@
}
},
"url": {
"raw": "https://www.google.es",
"raw": "https://www.google.es/hola",
"protocol": "https",
"host": [
"www",
"google",
"es"
],
"path": [
"hola"
]
}
},
"response": []
},
{
"name": "Post example 2",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "file",
"file": {},
"options": {
"raw": {
"language": "text"
}
}
},
"url": {
"raw": "https://www.google.es/hola",
"protocol": "https",
"host": [
"www",
"google",
"es"
],
"path": [
"hola"
]
}
},
"response": []
}
],
"event": [
{
"listen": "prerequest",
"script": {
"id": "ef248fcc-2944-4cba-837f-680b10a45b30",
"type": "text/javascript",
"exec": [
""
]
}
},
{
"listen": "test",
"script": {
"id": "8e7453aa-5712-4a90-bc32-965c7d47906a",
"type": "text/javascript",
"exec": [
""
]
}
}
],
"protocolProfileBehavior": {}
Expand Down

0 comments on commit 05080d9

Please sign in to comment.