This repository has been archived by the owner on May 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
413 lines (360 loc) · 13.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
'use strict';
var fs = require('fs');
var path = require('path');
var schemaGenerator = require('json2schema');
var Converter = module.exports = {};
var newSwaggerDocument = {};
var postmanDocument = {};
var requestOptions = {};
var environmentMapping = Object.create(null);
var requestUrls = [];
var commonUrl;
var basePath;
var hostUrl;
Converter.convert = function(options, callback) {
requestOptions = options;
newSwaggerDocument = {};
var filepath = path.resolve(requestOptions.url);
//Get access to JSON object from user passed in Postman JSON.
fs.readFile(filepath, 'utf8', function(err, data) {
if (err) {
throw err;
}
postmanDocument = JSON.parse(data);
if (requestOptions.environment) {
var envFilepath = path.resolve(requestOptions.environment);
//Get access to JSON object from user passed in Environment JSON.
fs.readFile(envFilepath, 'utf8', function(err, environment) {
var environmentVariables = JSON.parse(environment);
replaceEnvironmentVariables(environmentVariables.values);
convertToPostman(postmanDocument);
})
} else {
convertToPostman(postmanDocument);
}
fs.writeFile(requestOptions.outPut, JSON.stringify(newSwaggerDocument), 'utf8', function() {
//successfull file write.
});
});
}
/**
* Replace all environment variables.
*
* @param values {array} Array of all Environment variables for Postman.
*/
function replaceEnvironmentVariables(values) {
for (var value = 0; value < values.length; value++) {
if (typeof values[value].value === 'string' && values[value].value.length > 0) {
environmentMapping[values[value].key] = values[value].value;
}
}
postmanDocument = objectWalker(postmanDocument);
}
/**
* Replace all environment variables with value from environment JSON passed in by user.
*
* @param {object} obj Postman JSON passed in my user.
* @return {obj} Altered Postman JSON with replaced environment variables.
*/
function objectWalker(obj) {
var k;
var has = Object.prototype.hasOwnProperty.bind(obj);
for (k in obj) {
if (has(k)) {
switch (typeof obj[k]) {
case 'object':
objectWalker(obj[k]);
break;
case 'string':
var variablesFound = obj[k].match(/{{\s*[\w\.]+\s*}}/g);
if (variablesFound && variablesFound.length) {
for (var variable = 0; variable < variablesFound.length; variable++) {
var variableName = variablesFound[variable].replace('{{', '').replace('}}', '');
obj[k] = obj[k].replace(variablesFound[variable], environmentMapping[variableName])
}
}
}
}
}
return obj;
}
/**
* Call all function to convert Postman JSON to Swagger JSON.
*
* @param {object} data Postman JSON object from file passed in by user.
*/
function convertToPostman(data) {
setSwaggerVersion();
setSwaggerInfo();
findBaseUrl();
setSwaggerHost();
setSwaggerBasePath();
setSwaggerPaths();
}
/**
* Set the swagger verison in JSON.
*/
function setSwaggerVersion() {
//TODO(jcarter): Make dynamic when Swagger Version 1 is supported.
newSwaggerDocument.swagger = "2.0"
}
/**
* Build the swagger info object. http://swagger.io/specification/#infoObject
*/
function setSwaggerInfo() {
/*
* Not included:
* termsOfService: string: The Terms of Service for the API.
* contact: http://swagger.io/specification/#contactObject: The contact information for the exposed API.
* license: http://swagger.io/specification/#licenseObject: The license information for the exposed API.
*/
newSwaggerDocument.info = {
"version": "",
"title": postmanDocument.name,
"description": postmanDocument.description
}
}
/**
* Find the common base of all requests to use as base Url.
*
* @return {string} The most commonly found substring in all request URLs.
*/
function findBaseUrl() {
requestUrls = [];
for (var request = 0; request < postmanDocument.requests.length; request++) {
requestUrls.push(postmanDocument.requests[request].url);
}
commonUrl = sharedSegments(requestUrls);
return commonUrl;
}
/**
* Set the swagger host.
*/
function setSwaggerHost() {
//Find & remove protocol (http, ftp, etc.) and get domain
if (commonUrl.length === 0) {
hostUrl = '';
} else {
if (commonUrl.indexOf("://") > -1) {
hostUrl = commonUrl.split('/')[2];
}
else {
hostUrl = commonUrl.split('/')[0];
}
//find & remove port number
hostUrl = hostUrl.split(':')[0];
}
if (requestOptions.host) {
newSwaggerDocument.host = requestOptions.host;
} else {
newSwaggerDocument.host = hostUrl;
}
}
/**
* Get the base path from request URLs.
*/
function setSwaggerBasePath() {
newSwaggerDocument.basePath = basePath = commonUrl.replace(hostUrl, '').replace('http://','').replace('https://','').replace('www.','');
}
/**
* Set the swagger paths.
*/
function setSwaggerPaths() {
//Set paths to empty object.
newSwaggerDocument.paths = {};
//Loop through requests from Postman file.
for (var request = 0; request < postmanDocument.requests.length; request++) {
//Set variable equal to request for this iteration from Postman file.
var activePostmanRequest = postmanDocument.requests[request];
var tempPath = '';
//Getting path to set in swagger paths object.
tempPath = activePostmanRequest.url.replace(commonUrl, '')
//Strip params
var tempParams = getUrlVars(tempPath);
tempPath = tempPath.split('?')[0];
//Checking to see if entry already exists for this path.
if (newSwaggerDocument.paths[tempPath] === undefined) {
newSwaggerDocument.paths[tempPath] = {};
}
//Checking to see if method entry already exists for this path.
if (newSwaggerDocument.paths[tempPath][activePostmanRequest.method.toLowerCase()] === undefined) {
newSwaggerDocument.paths[tempPath][activePostmanRequest.method.toLowerCase()] = {};
} else {
//TODO(jcarter): Need to do a graceful merge of information.
continue;
}
//TODO(jcarter): Set tags option in CLI?
newSwaggerDocument.paths[tempPath][activePostmanRequest.method.toLowerCase()].tags = [];
newSwaggerDocument.paths[tempPath][activePostmanRequest.method.toLowerCase()].summary = activePostmanRequest.description;
newSwaggerDocument.paths[tempPath][activePostmanRequest.method.toLowerCase()].operationId = activePostmanRequest.name + '_';
newSwaggerDocument.paths[tempPath][activePostmanRequest.method.toLowerCase()].produces = [];
newSwaggerDocument.paths[tempPath][activePostmanRequest.method.toLowerCase()].consumes = [];
//Set Headers for request.
//Get content type if available.
activePostmanRequest.headers.replace('\n', '').replace('\r', '');
var headersArray = activePostmanRequest.headers.match(/("[^"]+"|[^"\s]+)/g);
var contentTypeIndex = headersArray.indexOf("Content-Type:");
if (contentTypeIndex >= 0) {
var contentTypeValue = headersArray[(contentTypeIndex + 1)];
newSwaggerDocument.paths[tempPath][activePostmanRequest.method.toLowerCase()].produces.push(contentTypeValue);
newSwaggerDocument.paths[tempPath][activePostmanRequest.method.toLowerCase()].consumes.push(contentTypeValue);
}
newSwaggerDocument.paths[tempPath][activePostmanRequest.method.toLowerCase()].parameters = [];
//Set headers for request
/*
* All headers found are set to required by default.
* All header types are set to string by default.
*/
for (var header = 0; header < headersArray.length; header++) {
var headerObject = {}
//searching for key in header array.
if (headersArray[header].endsWith(':')) {
headerObject = {
name: headersArray[header].substring(0, (headersArray[header].length - 1)),
in: 'header',
description: '',
required: true,
type: 'string'
}
newSwaggerDocument.paths[tempPath][activePostmanRequest.method.toLowerCase()].parameters.push(headerObject);
}
}
//Set parameters if any were found in path.
if (Object.keys(tempParams).length > 0) {
for (var param in tempParams) {
var parameterObject = {
name: param,
in: 'query',
required: true,
'x-is-map': false,
type: 'string'
}
newSwaggerDocument.paths[tempPath][activePostmanRequest.method.toLowerCase()].parameters.push(parameterObject);
}
}
//Set response on swagger JSON.
//TODO(jcarter): I would really like to parse the test JS file in the Postman JSON to get reponse body and status code.
if (activePostmanRequest.responses && activePostmanRequest.responses.length === 0) {
//No responsed were stored in Postman request so set blank 200 response.
newSwaggerDocument.paths[tempPath][activePostmanRequest.method.toLowerCase()].responses = {
'200': {
description: ''
}
};
//Responses found in Postman Collection JSON.
} else if (activePostmanRequest.responses && activePostmanRequest.responses.length > 0) {
//Create responses empty object.
newSwaggerDocument.paths[tempPath][activePostmanRequest.method.toLowerCase()].responses = {};
//Create property for each status in reponse.
for (var response = 0; response < activePostmanRequest.responses.length; response++) {
//TODO(jcarter): Remove hard coding of application/json in example type.
newSwaggerDocument.paths[tempPath][activePostmanRequest.method.toLowerCase()].responses[activePostmanRequest.responses[response].responseCode.code] = {
description: activePostmanRequest.responses[response].responseCode.detail,
schema: schemaGenerator.convert({data: JSON.parse(activePostmanRequest.responses[response].text)}),
headers: buildResponseHeaders_(activePostmanRequest.responses[response].headers),
example: {
'application/json': JSON.parse(activePostmanRequest.responses[response].text)
}
}
}
function buildResponseHeaders_(headersArray) {
var headersObj = {};
for (var header = 0; header < headersArray.length; header++) {
headersObj[headersArray[header].key] = {
description: headersArray[header].description,
type: typeof headersArray[header].value
}
}
return headersObj;
}
}
//Set security property on swagger JSON.
newSwaggerDocument.paths[tempPath][activePostmanRequest.method.toLowerCase()].security = [];
//Set body parameter.
//TODO(jcarter): Need to support other content types beside application/json.
/**
* Assumptions being made:
* All properties are required.
* No enums or descriptions for any properties are created.
* Multiple types not supported at this time.
*/
if (activePostmanRequest.rawModeData &&
(newSwaggerDocument.paths[tempPath][activePostmanRequest.method.toLowerCase()].produces.indexOf('application/json') >= 0)) {
var bodyData = JSON.parse(activePostmanRequest.rawModeData);
var bodyParam = {
"in": "body",
"name": "body",
"description": "",
"required": true,
"schema": schemaGenerator.convert({data: bodyData})
}
newSwaggerDocument.paths[tempPath][activePostmanRequest.method.toLowerCase()].parameters.push(bodyParam);
}
}
}
//Helper Functions//
/**
* Find the longest shared sub string to strip out common parts of request URLs
*
* @param {array.<string>} array An array of all request URLs.
* @return {string} String representing shared URI for all requests.
*/
//Abandoned this and switched to checking common segments.
function sharedSubString(array){
var sortedArray = array.concat().sort();
var firstString = sortedArray[0];
var lastString = sortedArray[sortedArray.length-1];
var firstStringLength = firstString.length
var i = 0;
while (i < firstStringLength && firstString.charAt(i) === lastString.charAt(i)) {
i++;
}
return firstString.substring(0, (i - 1));
}
/**
* Find the longest shared portion of the URI.
*
* @param {array.<string>} array An array of all request URLs.
* @return {string} String representing shared URI for all requests.
*/
function sharedSegments(array){
var sortedArray = array.concat().sort();
if (array.length === 1) {
return '';
}
for (var uri = 0; uri < sortedArray.length; uri++) {
sortedArray[uri] = sortedArray[uri].split('/');
}
var firstSegmentArray = sortedArray[0];
var lastSegmentArray = sortedArray[sortedArray.length-1];
var segment = 0;
while (firstSegmentArray[segment] === lastSegmentArray[segment]) {
segment++;
}
//If segment to splice is undefined, go back one before returning commonUrl.
var spliceCount = segment;
if (!firstSegmentArray[segment]) {
spliceCount = segment -1;
}
return firstSegmentArray.splice(0, spliceCount).join('/');
}
/**
* Get parameters from path.
*
* @param {string} path Path that we are parsing for params.
* @return {objcet} Key value pair of params.
*/
function getUrlVars(path) {
var vars = {};
var hash;
if (path.indexOf('?') >= 0) {
var hashes = path.slice(path.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars[hash[0]] = hash[1];
}
return vars;
} else {
return false;
}
}