Created
May 12, 2016 18:18
-
-
Save meaku/7e3145b949f0ad1ffe7ccc549bfed8a6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
"use strict"; | |
/** | |
* convert blueprint to a postman collection | |
* pretty much a dirty port of https://github.com/pixelfusion/blueman | |
*/ | |
var fs = require("fs"), | |
path = require("path"), | |
inspect = require("util").inspect, | |
uid = require("uuid"); | |
var protagonist = require("protagonist"); | |
var apiDocsPath = path.resolve(__dirname, "../docs/Api.md"), | |
outputPath = path.resolve(__dirname, "../docs/api.json.postman_collection"), | |
baseUrl = "localhost:8080"; | |
var docsMd = fs.readFileSync(apiDocsPath); | |
function convertHeaders(headers) { | |
headers = headers.map(function(header) { | |
return header.name + ":" + header.value; | |
}); | |
return headers.join("\n"); | |
} | |
function replaceUriParameters(uri, parameters) { | |
parameters.forEach(function(attribute) { | |
uri = uri.replace("{" + attribute.name + "}", attribute.example); | |
}); | |
return uri; | |
} | |
function replaceQueryParameters(uri) { | |
var queryIndex = uri.indexOf("{?"); | |
if(queryIndex === -1) { | |
return uri; | |
} | |
return uri.substr(0, queryIndex); | |
} | |
function blueman(blueprint) { | |
var collectionId = uid.v4(); | |
var postman = { | |
id: collectionId, | |
name: blueprint.name, | |
description: blueprint.description, | |
"public": false, | |
requests: [] | |
}; | |
var resources = blueprint.resourceGroups[0].resources, | |
request, | |
dataMode = "params"; | |
resources.forEach(function(resource) { | |
resource.actions.forEach(function(action) { | |
request = action.examples[0].requests[0] || {}; | |
if(request.body) { | |
dataMode = "raw"; | |
} | |
postman.requests.push({ | |
"id": uid.v4(), | |
"collection": collectionId, | |
"collectionId": collectionId, | |
"name": action.name, | |
"description": action.description, | |
"headers": convertHeaders(request.headers || []), | |
"rawModeData": request.body || "", | |
"url": baseUrl + replaceQueryParameters(replaceUriParameters(action.attributes.uriTemplate || resource.uriTemplate, action.parameters)), | |
"method": action.method || "GET", | |
"data": [], | |
"dataMode": dataMode, | |
"version": 2, | |
"currentHelper": "normal", | |
"helperAttributes": {}, | |
"responses": [] | |
}); | |
}); | |
}); | |
return postman; | |
} | |
protagonist.parse(docsMd.toString(), function(err, result) { | |
if (err) { | |
console.error(err); | |
return; | |
} | |
//console.log(inspect(result.ast, { depth: null, colors: true })); | |
//console.log(inspect(blueman(result.ast), { depth: null, colors: true })); | |
//console.log(JSON.stringify(blueman(result.ast))); | |
fs.writeFileSync(outputPath, JSON.stringify(blueman(result.ast), null, " ")); | |
console.log("updated api docs"); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment