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

Commit

Permalink
feat: General API info #12
Browse files Browse the repository at this point in the history
  • Loading branch information
joolfe committed Jul 25, 2020
1 parent 9e459f1 commit b82441f
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 12 deletions.
23 changes: 15 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
const { promises: { writeFile, readFile } } = require('fs')
const { safeDump } = require('js-yaml')

async function postmanToOpenApi (input, output, { save = true }) {
async function postmanToOpenApi (input, output, { save = true, info = {} }) {
// TODO validate?
const collectionFile = await readFile(input)
const postmanJson = JSON.parse(collectionFile)
const { item: items, info: { name: title, description }, variable } = postmanJson
const version = getVarValue(variable, 'version', '1.0.0')
const { item: items } = postmanJson
const paths = {}

for (const item of items) {
Expand All @@ -32,11 +31,7 @@ async function postmanToOpenApi (input, output, { save = true }) {

const openApi = {
openapi: '3.0.0',
info: {
title,
description,
version
},
info: compileInfo(postmanJson, info),
paths
}

Expand All @@ -47,6 +42,18 @@ async function postmanToOpenApi (input, output, { save = true }) {
return openApiYml
}

function compileInfo (postmanJson, optsInfo) {
const { info: { name, description: desc }, variable } = postmanJson
const ver = getVarValue(variable, 'version', '1.0.0')
const { title = name, description = desc, version = ver, termsOfService } = optsInfo
return {
title,
description,
version,
...(termsOfService ? { termsOfService } : {})
}
}

function parseBody (body) {
if (body.mode === 'raw') {
return {
Expand Down
20 changes: 17 additions & 3 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ const { readFileSync, existsSync, unlinkSync } = require('fs')

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

const COLLECTION_BASIC = path.join(__dirname, '/resources/input/PostmantoOpenAPI.postman_collection.json')
const EXPECTED_BASIC = readFileSync(path.join(__dirname, './resources/output/Basic.yml'), 'utf8')
const COLLECTION_BASIC = './test/resources/input/PostmantoOpenAPI.postman_collection.json'
const EXPECTED_BASIC = readFileSync('./test/resources/output/Basic.yml', 'utf8')
const COLLECTION_SIMPLE = './test/resources/input/SimplePost.json'
const EXPECTED_INFO_OPTS = readFileSync('./test/resources/output/InfoOpts.yml', 'utf8')

describe('Library specs', function () {
afterEach('remove file', function () {
Expand All @@ -20,11 +22,23 @@ describe('Library specs', function () {

it('should work with a basic transform', async function () {
const result = await postmanToOpenApi(COLLECTION_BASIC, OUTPUT_PATH, {})
equal(EXPECTED_BASIC, result.slice(0, -1))
equal(EXPECTED_BASIC, result)
ok(existsSync(OUTPUT_PATH))
})

it('should work when no save', async function () {
await postmanToOpenApi(COLLECTION_BASIC, '', { save: false })
})

it('should work if info is passed as parameter', async function () {
const result = await postmanToOpenApi(COLLECTION_SIMPLE, OUTPUT_PATH, {
info: {
title: 'Options title',
version: '6.0.7-beta',
description: 'Description from options',
termsOfService: 'http://tos.myweb.com'
}
})
equal(EXPECTED_INFO_OPTS, result)
})
})
47 changes: 47 additions & 0 deletions test/resources/input/SimplePost.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"info": {
"_postman_id": "3ad7e1b4-f3a1-4a8f-8bd0-56ff18f53339",
"name": "Simple Post",
"description": "Just a simple collection for test",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Create new User",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"example\": \"field\",\n \"other\": {\n \"data1\": \"yes\",\n \"data2\": \"no\"\n }\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "https://api.io/users",
"protocol": "https",
"host": [
"api",
"io"
],
"path": [
"users"
]
},
"description": "Create a new user into your amazing API"
},
"response": []
}
],
"variable": [
{
"id": "a1b68638-d65e-4ee8-9a91-4d5749e9c2f2",
"key": "version",
"value": "2.3.0"
}
],
"protocolProfileBehavior": {}
}
2 changes: 1 addition & 1 deletion test/resources/output/Basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ paths:
'200':
description: Successful response
content:
application/json: {}
application/json: {}
26 changes: 26 additions & 0 deletions test/resources/output/InfoOpts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
openapi: 3.0.0
info:
title: Options title
description: Description from options
version: 6.0.7-beta
termsOfService: 'http://tos.myweb.com'
paths:
/users:
post:
summary: Create new User
description: Create a new user into your amazing API
requestBody:
content:
application/json:
schema:
type: object
example:
example: field
other:
data1: 'yes'
data2: 'no'
responses:
'200':
description: Successful response
content:
application/json: {}

0 comments on commit b82441f

Please sign in to comment.