drakonian

Generate an API server from API Blueprint documentation.

Usage no npm install needed!

<script type="module">
  import drakonian from 'https://cdn.skypack.dev/drakonian';
</script>

README

drakonian

Generate an API server from API Blueprint documentation.

Installing

npm install -g drakonian

Running from console

$ drakonian blueprint.apib [[-p | --port] 8080] [-s | --silent] [-c | --customHandlers] handlers.js [-f | --formatters] formatters.js]]

Running programatically

var drakonian = require('drakonian');
drakonian.start('test/test_simple_resource.apib', 4000, done, { silent: true };

You can also configure custom headers to be allowed (due to CORS):

drakonian.start('test/test_simple_resource.apib', 4000, done, { silent: true }, ['authorization']);

Custom handlers

Add business logic to your API Blueprint by adding custom handlers.

drakonian.addHandler('GET', '/things/{id}', function(req, res, accept, action) {
    var response = drakonian.findResponse(/200/, accept, action);
    drakonian.setHeaders(res, response.headers);
    if (req.headers.accept === 'application/json') {
        res.send({
            id: parseInt(req.params.id),
            name: 'Thing ' + req.params.id
        });
    } else {
        res.send('id: ' + req.params.id + ', name: "Thing ' + req.params.id + '"');
    }
});

drakonian.start('test/test_simple_resource.apib', 4000, done, { silent: true });

or

var handlers = [
    {
        method: 'GET',
        uriTemplate: '/things/{id}',
        handler: function(req, res, accept, action) {
            // Handler code
        }
    }
];
    
drakonian.addHandlers(handlerObj.handlers);

Public methods

  • start = function(blueprintFile, port, cb, options)

  • close = function()

  • addHandler = function(verb, uriTemplate, cb)

Other helper methods

  • setHeaders = function(res, headers)

  • findResponse = function(statusCode, accept, action)

  • yieldDefaultResponse = function(res, accept, action)

Testing

npm test