Created
October 13, 2017 03:00
-
-
Save qianyan/f072d05e8c23f0da4746877fc18a6d7f to your computer and use it in GitHub Desktop.
view flatten APIs with Node.js
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
const _ = require('lodash') | |
const fs = require('fs') | |
var Lazy = require('lazy') | |
const swaggerDoc = "your.yaml" | |
const prefixOfURI = "/" | |
new Lazy(fs.createReadStream(swaggerDoc)) | |
.lines | |
.map( l => l.toString().trim().slice(0, -1) ) // delete the last char ':' | |
.filter(line => line.startsWith(prefixOfURI) || ['get', 'post', 'put', 'patch', 'delete'].includes(line) ) | |
.foldr( (x, acc) => { | |
if(x.startsWith(prefixOfURI)) { | |
acc.push([x]) | |
} else { | |
_.last(acc).push(x) | |
} | |
return acc; | |
}, | |
[], | |
results => { | |
var xs = _.map(results, uri_verbs => { | |
return _.tail(uri_verbs).map(verb => format(verb, _.head(uri_verbs))) | |
}) | |
var flatMap = _.reduce(xs, (apis, acc) => { | |
return acc.concat(apis) | |
}, []) | |
console.log(flatMap.join('\n')) | |
console.log("total amount:", flatMap.length) | |
}) | |
function format(verb, uri) { | |
return (verb.toUpperCase() + _.repeat(' ', 4).substring(0, 7) + uri. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment