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

Commit

Permalink
feat: var replacer first implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
joolfe committed Jul 30, 2021
1 parent 9cc56b0 commit 768e59f
Show file tree
Hide file tree
Showing 7 changed files with 439 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ This library automatically transform query and headers parameters from Postman o

The default schema used for parameters is `string` but the library try to infer the type of the parameters based on the value using regular expressions, the detected types are `integer`, `number`, `boolean` and `string`, if you find any problem in the inference process please open an issue.

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. To provide additional information about a path parameter you can [Pass Meta-information as markdown](#pass-meta-information-as-markdown).
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. To provide additional information about a path parameter you can [Pass Meta-information as markdown](#pass-meta-information-as-markdown).

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...).

Expand Down
21 changes: 21 additions & 0 deletions lib/var-replacer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const Mustache = require('mustache')

/**
* Rewrite escapedValue() function to not delete undefined variables
*/
Mustache.Writer.prototype.escapedValue = function escapedValue (token, context, config) {
const value = context.lookup(token[1]) || `{{${token[1]}}}`
return String(value)
}

function replacePostmanVariables (collectionString) {
const postmanJson = JSON.parse(collectionString)
const { variable } = postmanJson
const context = variable.reduce((obj, { key, value }) => {
obj[key] = value
return obj
}, {})
return Mustache.render(collectionString, context)
}

module.exports = replacePostmanVariables
16 changes: 15 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@
"dependencies": {
"commander": "^7.2.0",
"js-yaml": "^4.1.0",
"marked": "^2.0.7"
"marked": "^2.0.7",
"mustache": "^4.2.0"
},
"husky": {
"hooks": {
Expand Down
192 changes: 192 additions & 0 deletions test/resources/input/v21/Variables.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
{
"info": {
"_postman_id": "747abd37-e913-4bf8-a7e7-01730737b973",
"name": "Variables",
"description": "Mi super test collection from postman",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Create new User",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\n \"example\": \"field\",\n \"other\": {\n \"data1\": \"yes\",\n \"service\": \"{{service}}\"\n }\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{server}}/users",
"host": [
"{{server}}"
],
"path": [
"users"
]
},
"description": "Create a new user into your amazing API"
},
"response": []
},
{
"name": "Create Post",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "formdata",
"formdata": [
{
"key": "company",
"value": "{{company}}",
"type": "text"
},
{
"key": "text",
"value": "This is an example text",
"type": "text"
},
{
"key": "auditor",
"value": "{{auditor}}",
"type": "text"
}
],
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{server}}/post",
"host": [
"{{server}}"
],
"path": [
"post"
]
}
},
"response": []
},
{
"name": "Consult User data",
"request": {
"method": "GET",
"header": [
{
"key": "X-Company",
"value": "{{company}}",
"type": "text"
},
{
"key": "",
"value": "",
"type": "text",
"disabled": true
}
],
"url": {
"raw": "{{server}}/users/{{user_id}}?company={{company}}",
"host": [
"{{server}}"
],
"path": [
"users",
"{{user_id}}"
],
"query": [
{
"key": "company",
"value": "{{company}}"
}
]
},
"description": "Get one user instance data"
},
"response": []
},
{
"name": "Get a list of user",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{server}}/users?size={{page-size}}&company={{company}}",
"host": [
"{{server}}"
],
"path": [
"users"
],
"query": [
{
"key": "size",
"value": "{{page-size}}",
"description": "size of the list"
},
{
"key": "company",
"value": "{{company}}",
"description": "company for filter users"
}
]
},
"description": "Get a list of users"
},
"response": []
}
],
"event": [
{
"listen": "prerequest",
"script": {
"type": "text/javascript",
"exec": [
""
]
}
},
{
"listen": "test",
"script": {
"type": "text/javascript",
"exec": [
""
]
}
}
],
"variable": [
{
"key": "version",
"value": "1.1.0"
},
{
"key": "server",
"value": "https://api.io"
},
{
"key": "page-size",
"value": "10"
},
{
"key": "company",
"value": "ServicesLTD"
},
{
"key": "auditor",
"value": "IHA"
},
{
"key": "service",
"value": "s23434"
}
]
}
Loading

0 comments on commit 768e59f

Please sign in to comment.