Package Exports
- api-blueprint-visitors
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (api-blueprint-visitors) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
API Blueprint Visitor
A Visitor base class that lets you derive your own visitors for manipulating API-Blueprint's AST, so that you can build awesome tools.
Installing
npm install api-blueprint-visitors
Getting Started
Two things are provided in api-blueprint-visitors:
- A Visitor base class
- The
makeASTVisitablefunction, that makes Protagonist's AST visitable.
Let's imagine we want to create a script that counts all the Resources, Requests and Responses in a blueprint file.
Our visitor could look something like this:
class CounterVisitor extends Visitor {
constructor() {
this.resources = 0;
this.requests = 0;
this.responses = 0;
}
// Each `visit` method is called when the visitor visits the corresponding node.
visitResource(/*node, ctx*/) {
this.resources++;
}
visitRequest(/*node, ctx*/) {
this.requests++;
}
visitResponse(/*node, ctx*/) {
this.responses++;
}
// Once all the AST has been traversed, `postVisit` is called.
postVisit() {
console.log(this.resources);
console.log(this.requests);
console.log(this.responses);
}
}Now, in order to visit the AST, we need to make that AST visitable:
fs.readFile('blueprint.md', 'utf8', function (err, data) {
protagonist.parse(data, function(error, result) {
let ast = result.ast,
myVisitor = new CounterVisitor();
makeASTVisitable(ast);
// Now the AST has an `accept` method:
ast.accept(myVisitor);
});
});And that's it! The visitor can visit Group, Resource, Action, Request, Response and Example.
Check the full example here.
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request
License
MIT