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

Commit 768e59f

Browse files
committed
feat: var replacer first implementation
1 parent 9cc56b0 commit 768e59f

7 files changed

Lines changed: 439 additions & 3 deletions

File tree

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ This library automatically transform query and headers parameters from Postman o
352352

353353
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.
354354

355-
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).
355+
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).
356356

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

lib/var-replacer.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const Mustache = require('mustache')
2+
3+
/**
4+
* Rewrite escapedValue() function to not delete undefined variables
5+
*/
6+
Mustache.Writer.prototype.escapedValue = function escapedValue (token, context, config) {
7+
const value = context.lookup(token[1]) || `{{${token[1]}}}`
8+
return String(value)
9+
}
10+
11+
function replacePostmanVariables (collectionString) {
12+
const postmanJson = JSON.parse(collectionString)
13+
const { variable } = postmanJson
14+
const context = variable.reduce((obj, { key, value }) => {
15+
obj[key] = value
16+
return obj
17+
}, {})
18+
return Mustache.render(collectionString, context)
19+
}
20+
21+
module.exports = replacePostmanVariables

package-lock.json

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@
8888
"dependencies": {
8989
"commander": "^7.2.0",
9090
"js-yaml": "^4.1.0",
91-
"marked": "^2.0.7"
91+
"marked": "^2.0.7",
92+
"mustache": "^4.2.0"
9293
},
9394
"husky": {
9495
"hooks": {
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
{
2+
"info": {
3+
"_postman_id": "747abd37-e913-4bf8-a7e7-01730737b973",
4+
"name": "Variables",
5+
"description": "Mi super test collection from postman",
6+
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
7+
},
8+
"item": [
9+
{
10+
"name": "Create new User",
11+
"request": {
12+
"method": "POST",
13+
"header": [],
14+
"body": {
15+
"mode": "raw",
16+
"raw": "{\n \"example\": \"field\",\n \"other\": {\n \"data1\": \"yes\",\n \"service\": \"{{service}}\"\n }\n}",
17+
"options": {
18+
"raw": {
19+
"language": "json"
20+
}
21+
}
22+
},
23+
"url": {
24+
"raw": "{{server}}/users",
25+
"host": [
26+
"{{server}}"
27+
],
28+
"path": [
29+
"users"
30+
]
31+
},
32+
"description": "Create a new user into your amazing API"
33+
},
34+
"response": []
35+
},
36+
{
37+
"name": "Create Post",
38+
"request": {
39+
"method": "POST",
40+
"header": [],
41+
"body": {
42+
"mode": "formdata",
43+
"formdata": [
44+
{
45+
"key": "company",
46+
"value": "{{company}}",
47+
"type": "text"
48+
},
49+
{
50+
"key": "text",
51+
"value": "This is an example text",
52+
"type": "text"
53+
},
54+
{
55+
"key": "auditor",
56+
"value": "{{auditor}}",
57+
"type": "text"
58+
}
59+
],
60+
"options": {
61+
"raw": {
62+
"language": "json"
63+
}
64+
}
65+
},
66+
"url": {
67+
"raw": "{{server}}/post",
68+
"host": [
69+
"{{server}}"
70+
],
71+
"path": [
72+
"post"
73+
]
74+
}
75+
},
76+
"response": []
77+
},
78+
{
79+
"name": "Consult User data",
80+
"request": {
81+
"method": "GET",
82+
"header": [
83+
{
84+
"key": "X-Company",
85+
"value": "{{company}}",
86+
"type": "text"
87+
},
88+
{
89+
"key": "",
90+
"value": "",
91+
"type": "text",
92+
"disabled": true
93+
}
94+
],
95+
"url": {
96+
"raw": "{{server}}/users/{{user_id}}?company={{company}}",
97+
"host": [
98+
"{{server}}"
99+
],
100+
"path": [
101+
"users",
102+
"{{user_id}}"
103+
],
104+
"query": [
105+
{
106+
"key": "company",
107+
"value": "{{company}}"
108+
}
109+
]
110+
},
111+
"description": "Get one user instance data"
112+
},
113+
"response": []
114+
},
115+
{
116+
"name": "Get a list of user",
117+
"request": {
118+
"method": "GET",
119+
"header": [],
120+
"url": {
121+
"raw": "{{server}}/users?size={{page-size}}&company={{company}}",
122+
"host": [
123+
"{{server}}"
124+
],
125+
"path": [
126+
"users"
127+
],
128+
"query": [
129+
{
130+
"key": "size",
131+
"value": "{{page-size}}",
132+
"description": "size of the list"
133+
},
134+
{
135+
"key": "company",
136+
"value": "{{company}}",
137+
"description": "company for filter users"
138+
}
139+
]
140+
},
141+
"description": "Get a list of users"
142+
},
143+
"response": []
144+
}
145+
],
146+
"event": [
147+
{
148+
"listen": "prerequest",
149+
"script": {
150+
"type": "text/javascript",
151+
"exec": [
152+
""
153+
]
154+
}
155+
},
156+
{
157+
"listen": "test",
158+
"script": {
159+
"type": "text/javascript",
160+
"exec": [
161+
""
162+
]
163+
}
164+
}
165+
],
166+
"variable": [
167+
{
168+
"key": "version",
169+
"value": "1.1.0"
170+
},
171+
{
172+
"key": "server",
173+
"value": "https://api.io"
174+
},
175+
{
176+
"key": "page-size",
177+
"value": "10"
178+
},
179+
{
180+
"key": "company",
181+
"value": "ServicesLTD"
182+
},
183+
{
184+
"key": "auditor",
185+
"value": "IHA"
186+
},
187+
{
188+
"key": "service",
189+
"value": "s23434"
190+
}
191+
]
192+
}

0 commit comments

Comments
 (0)