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

Commit

Permalink
feat: support path parameter & replace the value and description
Browse files Browse the repository at this point in the history
  • Loading branch information
Ericcy Hung(洪嘉育) committed Aug 4, 2021
1 parent 9c26105 commit faa7cbc
Show file tree
Hide file tree
Showing 4 changed files with 305 additions and 83 deletions.
42 changes: 37 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ async function postmanToOpenApi (input, output, {
request: { url, method, body, description: rawDesc, header, auth },
name: summary, tag = defaultTag, event: events, response
} = element
const { path, query, protocol, host, port, valid } = scrapeURL(url)
const { path, query, protocol, host, port, valid, pathVar } = scrapeURL(url)
if (valid) {
domains.add(calculateDomains(protocol, host, port))
const joinedPath = calculatePath(path, pathDepth)
if (!paths[joinedPath]) paths[joinedPath] = {}
const { description, paramsMeta } = descriptionParse(rawDesc)
let { description, paramsMeta } = descriptionParse(rawDesc)
paramsMeta = formatPathVarToParamsMeta(paramsMeta, pathVar)
paths[joinedPath][method.toLowerCase()] = {
tags: [tag],
summary,
Expand Down Expand Up @@ -333,8 +334,12 @@ function parseOptsAuth (optAuth) {
function calculatePath (paths, pathDepth) {
paths = paths.slice(pathDepth) // path depth
// replace repeated '{' and '}' chars
return '/' + paths.map(path => path.replace(/([{}])\1+/g, '$1'))
.join('/')
// replace `:` chars at first
return '/' + paths.map(path => {
path = path.replace(/([{}])\1+/g, '$1')
path = path.replace(/^:(.*)/g, '{$1}')
return path
}).join('/')
}

function calculateDomains (protocol, hosts, port) {
Expand Down Expand Up @@ -362,7 +367,8 @@ function scrapeURL (url) {
protocol: objUrl.protocol.slice(0, -1),
host: decodeURIComponent(objUrl.hostname).split('.'),
port: objUrl.port,
valid: true
valid: true,
pathVar: url.variable
}
}

Expand Down Expand Up @@ -529,6 +535,32 @@ function parseResponseHeaders (headerArray, responseHeaders) {
return (Object.keys(headers).length > 0) ? { headers } : {}
}

/**
* This function will parse path variables, format data and merged to paramsMeta
* @param {object} paramsMeta
* @param {array} variables
* @returns {object}
*/
function formatPathVarToParamsMeta (paramsMeta = {}, variables = []) {
variables.map((variable) => {
// if `# postman-to-openapi` is exist, skip replace
if (!paramsMeta[variable.key]) {
const { key, description, value: example } = variable
paramsMeta[key] = {
object: 'path',
name: key,
required: 'true',
type: isNaN(variable.value) ? 'string' : 'number',
...(variable.description ? { description } : {}),
...(example ? { example } : {})
}
}

return paramsMeta
})
return paramsMeta
}

postmanToOpenApi.version = version

module.exports = postmanToOpenApi
71 changes: 70 additions & 1 deletion test/resources/input/v2/PathParams.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,75 @@
"description": "Obtain a list of users descriptions\n\n# postman-to-openapi\n\n| object | name | description | required | type | example |\n|--------|----------|--------------------------------|----------|--------|-----------|\n| path | user_id | This is just a user identifier | true | number | 476587598 |\n| path | group_id | Group of the user | true | string | RETAIL |"
},
"response": []
},
{
"name": "Get one users with path params with type",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "https://api.io/desc/:user_id/:type",
"protocol": "https",
"host": [
"api",
"io"
],
"path": [
"desc",
":user_id",
":type"
],
"variable": [
{
"key": "user_id",
"value": "476587598",
"description": "This is just a user identifier"
},
{
"key": "type",
"value": "user",
"description": "This is just a user type"
}
]
},
"description": "Obtain a list of users descriptions"
},
"response": []
},
{
"name": "Get all users with description and params with type",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "https://api.io/desc/:user_id/:type/all",
"protocol": "https",
"host": [
"api",
"io"
],
"path": [
"desc",
":user_id",
":type",
"all"
],
"variable": [
{
"key": "user_id",
"value": "{{user_id}}",
"description": "This description will be replaced"
},
{
"key": "type",
"value": "user",
"description": "This is just a user type"
}
]
},
"description": "Obtain a list of users descriptions\n\n# postman-to-openapi\n\n| object | name | description | required | type | example |\n|--------|----------|--------------------------------|----------|--------|-----------|\n| path | user_id | This is just a user identifier | true | number | 476587598 |\n| path | group_id | Group of the user | true | string | RETAIL |"
},
"response": []
}
],
"event": [
Expand All @@ -60,4 +129,4 @@
}
],
"protocolProfileBehavior": {}
}
}
Loading

0 comments on commit faa7cbc

Please sign in to comment.