Created
March 10, 2015 19:49
-
-
Save bpedro/e8632b39fe525e9af514 to your computer and use it in GitHub Desktop.
Generates API Blueprint documentation from a POSTMAN Collection JSON file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var fs = require('fs') | |
, url = require('url'); | |
if (process.argv.length < 3) { | |
console.log('Usage:\n', process.argv[1] + ' <postman collection JSON file>\n\n'); | |
process.exit(); | |
} | |
var postmanCollectionFile = process.argv[process.argv.length - 1]; | |
var postmanCollection = JSON.parse(fs.readFileSync(postmanCollectionFile)); | |
var service = { | |
'name': postmanCollection.name, | |
'description': postmanCollection.description, | |
'methods': [] | |
} | |
for (var i in postmanCollection.requests) { | |
// handle headers | |
var headers = postmanCollection.requests[i].headers.split("\n"); | |
for (var j in headers) { | |
if (headers[j].length == 0) { | |
headers.splice(j); | |
} | |
} | |
// extract parameters from endpoint | |
var getParams = postmanCollection.requests[i].url.match(/{{.+?}}/g); | |
if (null != getParams) { | |
for (var j in getParams) { | |
getParams[j] = getParams[j].match(/{{(.+)}}/)[1]; | |
} | |
} else { | |
getParams = []; | |
} | |
service.methods.push({ | |
'name': postmanCollection.requests[i].name, | |
'description': postmanCollection.requests[i].description, | |
'method': postmanCollection.requests[i].method, | |
'endpoint': postmanCollection.requests[i].url, | |
'headers': headers, | |
'parameters': getParams | |
}); | |
} | |
var output = 'FORMAT: 1A\n'; | |
var urlParts = url.parse(service.methods[0].endpoint); | |
output += 'HOST: ' + urlParts.protocol + '//' + urlParts.host + '\n\n'; | |
output += '# ' + service.name + '\n'; | |
output += service.description + '\n\n'; | |
for (var i in service.methods) { | |
var urlParts = url.parse(service.methods[i].endpoint); | |
output += '## ' + urlParts.path + '\n'; | |
output += '### ' + service.methods[i].name + ' [' + service.methods[i].method + ']\n'; | |
output += service.methods[i].description + '\n'; | |
output += '+ Request\n+ Headers\n'; | |
output += service.methods[i].headers + '\n'; | |
output += '+ Response 200\n\n'; | |
} | |
console.log(output); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment