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

Commit

Permalink
feat: Support Headers definition #19
Browse files Browse the repository at this point in the history
  • Loading branch information
joolfe committed Jul 26, 2020
1 parent 952a977 commit c05919e
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 11 deletions.
35 changes: 24 additions & 11 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ async function postmanToOpenApi (input, output, { save = true, info = {}, defaul
items.splice(i, 1, ...tagged)
element = tagged.shift()
}
const { request: { url: { path, query }, method, body, description }, name: summary, tag = defaultTag } = element
const {
request: { url: { path, query }, method, body, description, header },
name: summary, tag = defaultTag
} = element
const compiledPath = '/' + path.join('/')
if (!paths[compiledPath]) paths[compiledPath] = {}
paths[compiledPath][method.toLowerCase()] = {
tags: [tag],
summary,
...(description ? { description } : {}),
...parseBody(body, method),
...parseParams(query),
...parseParameters(query, header),
responses: {
200: {
description: 'Successful response',
Expand Down Expand Up @@ -90,18 +93,28 @@ function parseBody (body = {}, method) {
return { requestBody: { content } }
}

function parseParams (query = []) {
const parameters = query.map(({ key, description }) => ({
name: key,
in: 'query',
schema: {
type: 'string'
},
...(description ? { description } : {})
}))
/* Parse the Postman query and header and transform into OpenApi parameters */
function parseParameters (query = [], header = []) {
// parse Headers
let parameters = header.reduce(mapParameters('header'), [])
// parse Query
parameters = query.reduce(mapParameters('query'), parameters)
return (parameters.length) ? { parameters } : {}
}

/* Accumulator function for different types of parameters */
function mapParameters (type) {
return (parameters, { key, description }) => {
parameters.push({
name: key,
in: type,
schema: { type: 'string' },
...(description ? { description } : {})
})
return parameters
}
}

function getVarValue (variables, name, def) {
const variable = variables.find(({ key }) => key === name)
return variable ? variable.value : def
Expand Down
7 changes: 7 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ const COLLECTION_SIMPLE = './test/resources/input/SimplePost.json'
const COLLECTION_NO_VERSION = './test/resources/input/NoVersion.json'
const COLLECTION_FOLDERS = './test/resources/input/FolderCollection.json'
const COLLECTION_GET = './test/resources/input/GetMethods.json'
const COLLECTION_HEADERS = './test/resources/input/Headers.json'

const EXPECTED_BASIC = readFileSync('./test/resources/output/Basic.yml', 'utf8')
const EXPECTED_INFO_OPTS = readFileSync('./test/resources/output/InfoOpts.yml', 'utf8')
const EXPECTED_NO_VERSION = readFileSync('./test/resources/output/NoVersion.yml', 'utf8')
const EXPECTED_CUSTOM_TAG = readFileSync('./test/resources/output/CustomTag.yml', 'utf8')
const EXPECTED_FOLDERS = readFileSync('./test/resources/output/Folders.yml', 'utf8')
const EXPECTED_GET_METHODS = readFileSync('./test/resources/output/GetMethods.yml', 'utf8')
const EXPECTED_HEADERS = readFileSync('./test/resources/output/Headers.yml', 'utf8')

describe('Library specs', function () {
afterEach('remove file', function () {
Expand Down Expand Up @@ -70,6 +72,11 @@ describe('Library specs', function () {
equal(EXPECTED_GET_METHODS, result)
})

it('should parse HEADERS parameters', async function () {
const result = await postmanToOpenApi(COLLECTION_HEADERS, OUTPUT_PATH)
equal(EXPECTED_HEADERS, result)
})

// other types of params
// do something about mandatory params?
})
126 changes: 126 additions & 0 deletions test/resources/input/Headers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
{
"info": {
"_postman_id": "e73be420-bb04-4dd6-bc29-296a80feec7c",
"name": "Headers",
"description": "API with headers",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Get list of users",
"request": {
"method": "GET",
"header": [
{
"key": "X-My-Header",
"value": "hudjilksns78jsijns090",
"description": "Custom header",
"type": "text"
},
{
"key": "X-Other",
"value": "other",
"description": "Another header",
"type": "text"
}
],
"url": {
"raw": "https://api.io/users?name=Jhon&review=true",
"protocol": "https",
"host": [
"api",
"io"
],
"path": [
"users"
],
"query": [
{
"key": "age",
"value": "45",
"description": "Filter by age",
"disabled": true
},
{
"key": "name",
"value": "Jhon",
"description": "Filter by name"
},
{
"key": "review",
"value": "true",
"description": "Indicate if should be reviewed or not"
}
]
},
"description": "Obtain a list of users that fullfill the conditions of the filters"
},
"response": []
},
{
"name": "Create new User",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"value": "application/json",
"description": "Indicate the type of body sent by client",
"type": "text"
},
{
"key": "X-My-Header",
"value": "hudjilksns78jsijns090",
"description": "Custom header",
"type": "text"
}
],
"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": []
}
],
"event": [
{
"listen": "prerequest",
"script": {
"id": "442a1ec6-72b9-416f-abb3-d2477ca14c0e",
"type": "text/javascript",
"exec": [
""
]
}
},
{
"listen": "test",
"script": {
"id": "127cb01a-be4f-4a62-97f9-d779d318a9a1",
"type": "text/javascript",
"exec": [
""
]
}
}
],
"protocolProfileBehavior": {}
}
74 changes: 74 additions & 0 deletions test/resources/output/Headers.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
openapi: 3.0.0
info:
title: Headers
description: API with headers
version: 1.0.0
paths:
/users:
get:
tags:
- default
summary: Get list of users
description: Obtain a list of users that fullfill the conditions of the filters
parameters:
- name: X-My-Header
in: header
schema:
type: string
description: Custom header
- name: X-Other
in: header
schema:
type: string
description: Another header
- name: age
in: query
schema:
type: string
description: Filter by age
- name: name
in: query
schema:
type: string
description: Filter by name
- name: review
in: query
schema:
type: string
description: Indicate if should be reviewed or not
responses:
'200':
description: Successful response
content:
application/json: {}
post:
tags:
- default
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'
parameters:
- name: Content-Type
in: header
schema:
type: string
description: Indicate the type of body sent by client
- name: X-My-Header
in: header
schema:
type: string
description: Custom header
responses:
'200':
description: Successful response
content:
application/json: {}

0 comments on commit c05919e

Please sign in to comment.