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

Commit

Permalink
feat: Infer the type of parameters #37
Browse files Browse the repository at this point in the history
  • Loading branch information
joolfe committed Jul 26, 2020
1 parent f391960 commit 05fd1e8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Or in other words, transform [this specification](https://schema.getpostman.com/

- POST request with JSON body.
- Allow extract the api version from a collection general `variable`.
- Customize general API information.
- Transform query and headers parameters.
- Automatic infer types from query and headers parameters.

## Development

Expand Down
10 changes: 9 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function mapParameters (type) {
parameters.push({
name: key,
in: type,
schema: { type: 'string' },
schema: { type: inferType(value) },
...(description ? { description } : {}),
...(value ? { example: value } : {})
})
Expand All @@ -121,4 +121,12 @@ function getVarValue (variables, name, def) {
return variable ? variable.value : def
}

/* calculate the type of a variable based on OPenApi types */
function inferType (value) {
if (/^\d+$/.test(value)) return 'integer'
if (/-?\d+\.\d+/.test(value)) return 'number'
if (/^(true|false)$/.test(value)) return 'boolean'
return 'string'
}

module.exports = postmanToOpenApi
7 changes: 6 additions & 1 deletion test/resources/input/GetMethods.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"method": "GET",
"header": [],
"url": {
"raw": "https://api.io/users?age=45&name=Jhon&review=true",
"raw": "https://api.io/users?age=45&name=Jhon&review=true&number=23.56",
"protocol": "https",
"host": [
"api",
Expand All @@ -36,6 +36,11 @@
"key": "review",
"value": "true",
"description": "Indicate if should be reviewed or not"
},
{
"key": "number",
"value": "23.56",
"description": "This is a number"
}
]
},
Expand Down
10 changes: 8 additions & 2 deletions test/resources/output/GetMethods.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ paths:
- name: age
in: query
schema:
type: string
type: integer
description: Filter by age
example: '45'
- name: name
Expand All @@ -26,9 +26,15 @@ paths:
- name: review
in: query
schema:
type: string
type: boolean
description: Indicate if should be reviewed or not
example: 'true'
- name: number
in: query
schema:
type: number
description: This is a number
example: '23.56'
responses:
'200':
description: Successful response
Expand Down
4 changes: 2 additions & 2 deletions test/resources/output/Headers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ paths:
- name: age
in: query
schema:
type: string
type: integer
description: Filter by age
example: '45'
- name: name
Expand All @@ -48,7 +48,7 @@ paths:
- name: review
in: query
schema:
type: string
type: boolean
description: Indicate if should be reviewed or not
example: 'true'
responses:
Expand Down

0 comments on commit 05fd1e8

Please sign in to comment.