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

Commit 952a977

Browse files
committed
feat: Support Get with query params #17
Basic support in this moment
1 parent a128a5c commit 952a977

5 files changed

Lines changed: 147 additions & 14 deletions

File tree

lib/index.js

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ async function postmanToOpenApi (input, output, { save = true, info = {}, defaul
1717
items.splice(i, 1, ...tagged)
1818
element = tagged.shift()
1919
}
20-
const { request: { url: { path }, method, body, description }, name: summary, tag = defaultTag } = element
20+
const { request: { url: { path, query }, method, body, description }, name: summary, tag = defaultTag } = element
2121
const compiledPath = '/' + path.join('/')
2222
if (!paths[compiledPath]) paths[compiledPath] = {}
2323
paths[compiledPath][method.toLowerCase()] = {
2424
tags: [tag],
2525
summary,
2626
...(description ? { description } : {}),
27-
requestBody: parseBody(body),
27+
...parseBody(body, method),
28+
...parseParams(query),
2829
responses: {
2930
200: {
3031
description: 'Successful response',
@@ -50,7 +51,7 @@ async function postmanToOpenApi (input, output, { save = true, info = {}, defaul
5051
}
5152

5253
function compileInfo (postmanJson, optsInfo) {
53-
const { info: { name, description: desc }, variable } = postmanJson
54+
const { info: { name, description: desc }, variable = [] } = postmanJson
5455
const ver = getVarValue(variable, 'version', '1.0.0')
5556
const { title = name, description = desc, version = ver, termsOfService } = optsInfo
5657
return {
@@ -61,25 +62,44 @@ function compileInfo (postmanJson, optsInfo) {
6162
}
6263
}
6364

64-
function parseBody (body) {
65-
if (body.mode === 'raw') {
66-
return {
67-
content: {
65+
function parseBody (body = {}, method) {
66+
// Swagger validation return an error if GET has body
67+
if (['GET'].includes(method)) return {}
68+
const { mode, raw } = body
69+
let content = {}
70+
switch (mode) {
71+
case 'raw':
72+
content = {
6873
'application/json': {
6974
schema: {
7075
type: 'object',
71-
example: JSON.parse(body.raw)
76+
example: JSON.parse(raw)
7277
}
7378
}
7479
}
75-
}
76-
} else {
77-
return {
78-
content: {
80+
break
81+
case 'file':
82+
content = {
7983
'text/plain': {}
8084
}
81-
}
85+
break
86+
default:
87+
content = {}
88+
break
8289
}
90+
return { requestBody: { content } }
91+
}
92+
93+
function parseParams (query = []) {
94+
const parameters = query.map(({ key, description }) => ({
95+
name: key,
96+
in: 'query',
97+
schema: {
98+
type: 'string'
99+
},
100+
...(description ? { description } : {})
101+
}))
102+
return (parameters.length) ? { parameters } : {}
83103
}
84104

85105
function getVarValue (variables, name, def) {

test/index.spec.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ const { readFileSync, existsSync, unlinkSync } = require('fs')
88

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

11-
const COLLECTION_BASIC = './test/resources/input/PostmantoOpenAPI.postman_collection.json'
11+
const COLLECTION_BASIC = './test/resources/input/PostmantoOpenAPI.json'
1212
const COLLECTION_SIMPLE = './test/resources/input/SimplePost.json'
1313
const COLLECTION_NO_VERSION = './test/resources/input/NoVersion.json'
1414
const COLLECTION_FOLDERS = './test/resources/input/FolderCollection.json'
15+
const COLLECTION_GET = './test/resources/input/GetMethods.json'
1516

1617
const EXPECTED_BASIC = readFileSync('./test/resources/output/Basic.yml', 'utf8')
1718
const EXPECTED_INFO_OPTS = readFileSync('./test/resources/output/InfoOpts.yml', 'utf8')
1819
const EXPECTED_NO_VERSION = readFileSync('./test/resources/output/NoVersion.yml', 'utf8')
1920
const EXPECTED_CUSTOM_TAG = readFileSync('./test/resources/output/CustomTag.yml', 'utf8')
2021
const EXPECTED_FOLDERS = readFileSync('./test/resources/output/Folders.yml', 'utf8')
22+
const EXPECTED_GET_METHODS = readFileSync('./test/resources/output/GetMethods.yml', 'utf8')
2123

2224
describe('Library specs', function () {
2325
afterEach('remove file', function () {
@@ -62,4 +64,12 @@ describe('Library specs', function () {
6264
const result = await postmanToOpenApi(COLLECTION_FOLDERS, OUTPUT_PATH)
6365
equal(EXPECTED_FOLDERS, result)
6466
})
67+
68+
it('should parse GET methods with query string', async function () {
69+
const result = await postmanToOpenApi(COLLECTION_GET, OUTPUT_PATH)
70+
equal(EXPECTED_GET_METHODS, result)
71+
})
72+
73+
// other types of params
74+
// do something about mandatory params?
6575
})
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"info": {
3+
"_postman_id": "2c1cccde-7a4d-4dea-bcd8-ce39544b0432",
4+
"name": "Get Methods",
5+
"description": "API to manage GET methods",
6+
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
7+
},
8+
"item": [
9+
{
10+
"name": "Get list of users",
11+
"request": {
12+
"method": "GET",
13+
"header": [],
14+
"url": {
15+
"raw": "https://api.io/users?age=45&name=Jhon&review=true",
16+
"protocol": "https",
17+
"host": [
18+
"api",
19+
"io"
20+
],
21+
"path": [
22+
"users"
23+
],
24+
"query": [
25+
{
26+
"key": "age",
27+
"value": "45",
28+
"description": "Filter by age"
29+
},
30+
{
31+
"key": "name",
32+
"value": "Jhon",
33+
"description": "Filter by name"
34+
},
35+
{
36+
"key": "review",
37+
"value": "true",
38+
"description": "Indicate if should be reviewed or not"
39+
}
40+
]
41+
},
42+
"description": "Obtain a list of users that fullfill the conditions of the filters"
43+
},
44+
"response": []
45+
}
46+
],
47+
"event": [
48+
{
49+
"listen": "prerequest",
50+
"script": {
51+
"id": "631b8a30-dcaf-449a-9f2e-83a9f13044ae",
52+
"type": "text/javascript",
53+
"exec": [
54+
""
55+
]
56+
}
57+
},
58+
{
59+
"listen": "test",
60+
"script": {
61+
"id": "b43e5a36-2b9e-47a0-aab9-edc036b968a6",
62+
"type": "text/javascript",
63+
"exec": [
64+
""
65+
]
66+
}
67+
}
68+
],
69+
"protocolProfileBehavior": {}
70+
}
File renamed without changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Get Methods
4+
description: API to manage GET methods
5+
version: 1.0.0
6+
paths:
7+
/users:
8+
get:
9+
tags:
10+
- default
11+
summary: Get list of users
12+
description: Obtain a list of users that fullfill the conditions of the filters
13+
parameters:
14+
- name: age
15+
in: query
16+
schema:
17+
type: string
18+
description: Filter by age
19+
- name: name
20+
in: query
21+
schema:
22+
type: string
23+
description: Filter by name
24+
- name: review
25+
in: query
26+
schema:
27+
type: string
28+
description: Indicate if should be reviewed or not
29+
responses:
30+
'200':
31+
description: Successful response
32+
content:
33+
application/json: {}

0 commit comments

Comments
 (0)