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

Commit

Permalink
feat: Mandatory params/headers #41
Browse files Browse the repository at this point in the history
Closes #41
  • Loading branch information
joolfe committed Aug 8, 2020
1 parent a5662e3 commit 78fe141
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 231 deletions.
457 changes: 239 additions & 218 deletions docs/assets/img/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
- Basic info API from Postman info or customizable.
- Basic method conversion (GET, POST, PUT...).
- Support Postman folders as tags.
- Transform query, headers and path parameters.
- Transform query, headers and path parameters (description, required...).
- Postman variables as Path parameters.
- Automatic infer types from query and headers parameters.
- Support Json and Text body formats.
Expand Down Expand Up @@ -203,6 +203,8 @@ The default schema used for parameters is `string` but the library try to infer

Path parameters are also automatically detected, this library look for [Postman variables](https://learning.postman.com/docs/sending-requests/variables/) in the url as `{{variable}}` and transform to a single curly brace expression as `{variable}` as supported by OpenAPI, also create the parameter definition using the variable name.

For headers and query fields you can indicate that this parameter is mandatory/required adding into the description the literal `[required]`. The library use a case insensitive regexp so all variations are supported (`[REQUIRED]`, `[Required]`...) and never mind the location inside the description (at the beginning, at the end...).

Have a look to the [GetMethods collection](https://github.com/joolfe/postman-to-openapi/blob/master/test/resources/input/GetMethods.json), [Headers collection](https://github.com/joolfe/postman-to-openapi/blob/master/test/resources/input/Headers.json) and [PathParams collection](https://github.com/joolfe/postman-to-openapi/blob/master/test/resources/input/PathParams.json) files for examples of how to use this features.

## Global authorization
Expand Down
4 changes: 3 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,13 @@ function parseParameters (query = [], header, paths) {
/* Accumulator function for different types of parameters */
function mapParameters (type) {
return (parameters, { key, description, value }) => {
const required = /\[required\]/gi.test(description)
parameters.push({
name: key,
in: type,
schema: { type: inferType(value) },
...(description ? { description } : {}),
...(required ? { required } : {}),
...(description ? { description: description.replace(/ ?\[required\] ?/gi, '') } : {}),
...(value ? { example: value } : {})
})
return parameters
Expand Down
17 changes: 11 additions & 6 deletions test/resources/input/GetMethods.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"info": {
"_postman_id": "2c1cccde-7a4d-4dea-bcd8-ce39544b0432",
"_postman_id": "e0553220-9270-4359-aa97-b16161df07ad",
"name": "Get Methods",
"description": "API to manage GET methods",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
Expand All @@ -12,7 +12,7 @@
"method": "GET",
"header": [],
"url": {
"raw": "https://api.io/users?age=45&name=Jhon&review=true&number=23.56",
"raw": "https://api.io/users?age=45&name=Jhon&review=true&number=23.56&required=my value",
"protocol": "https",
"host": [
"api",
Expand All @@ -25,12 +25,12 @@
{
"key": "age",
"value": "45",
"description": "Filter by age"
"description": "Filter by age [required]"
},
{
"key": "name",
"value": "Jhon",
"description": "Filter by name"
"description": "Filter by name [REQUIRED]"
},
{
"key": "review",
Expand All @@ -41,6 +41,11 @@
"key": "number",
"value": "23.56",
"description": "This is a number"
},
{
"key": "required",
"value": "my value",
"description": "[required] mandatory paraemeter"
}
]
},
Expand All @@ -53,7 +58,7 @@
{
"listen": "prerequest",
"script": {
"id": "631b8a30-dcaf-449a-9f2e-83a9f13044ae",
"id": "5ca076cb-bc5f-4366-9a0e-90af3f9a8027",
"type": "text/javascript",
"exec": [
""
Expand All @@ -63,7 +68,7 @@
{
"listen": "test",
"script": {
"id": "b43e5a36-2b9e-47a0-aab9-edc036b968a6",
"id": "40709de0-c637-46fb-a742-ba8bb1659db4",
"type": "text/javascript",
"exec": [
""
Expand Down
10 changes: 5 additions & 5 deletions test/resources/input/Headers.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"info": {
"_postman_id": "e73be420-bb04-4dd6-bc29-296a80feec7c",
"_postman_id": "d2298b96-8188-42ae-a927-0246cb933ed9",
"name": "Headers",
"description": "API with headers",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
Expand All @@ -14,13 +14,13 @@
{
"key": "X-My-Header",
"value": "hudjilksns78jsijns090",
"description": "Custom header",
"description": "Custom header [required]",
"type": "text"
},
{
"key": "X-Other",
"value": "other",
"description": "Another header",
"description": "Another header [REQUIRED]",
"type": "text"
},
{
Expand Down Expand Up @@ -115,7 +115,7 @@
{
"listen": "prerequest",
"script": {
"id": "442a1ec6-72b9-416f-abb3-d2477ca14c0e",
"id": "b666269b-e186-4f62-989b-f3b4c08eaab7",
"type": "text/javascript",
"exec": [
""
Expand All @@ -125,7 +125,7 @@
{
"listen": "test",
"script": {
"id": "127cb01a-be4f-4a62-97f9-d779d318a9a1",
"id": "b2980011-49b5-4d4c-8e2d-3913e868b486",
"type": "text/javascript",
"exec": [
""
Expand Down
9 changes: 9 additions & 0 deletions test/resources/output/GetMethods.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ paths:
in: query
schema:
type: integer
required: true
description: Filter by age
example: '45'
- name: name
in: query
schema:
type: string
required: true
description: Filter by name
example: Jhon
- name: review
Expand All @@ -37,6 +39,13 @@ paths:
type: number
description: This is a number
example: '23.56'
- name: required
in: query
schema:
type: string
required: true
description: mandatory paraemeter
example: my value
responses:
'200':
description: Successful response
Expand Down
2 changes: 2 additions & 0 deletions test/resources/output/Headers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ paths:
in: header
schema:
type: string
required: true
description: Custom header
example: hudjilksns78jsijns090
- name: X-Other
in: header
schema:
type: string
required: true
description: Another header
example: other
- name: No-description
Expand Down

0 comments on commit 78fe141

Please sign in to comment.