npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

audoku

v0.2.0-0

Published

Autodocumentate your rest apis on Express

Downloads

6

Readme

Audoku

Audoku is an Express middleware for documenting and validating/reporting Rest API endpoints. It allows you to get the documentation of your APIs in Json format by calling single endpoints. It follows in some way the unix man pages behaviour, showing how to use (basically which parameters and headers set for) your endpoints, by calling them with a special parameter.

From the v0.1.0 Audoku can also generate and serve the api documentation in web pages format using apidoc, just writing json instead of writing with the apidoc syntax.

Installation

To use audoku install it in your Express project by typing:

npm install audoku

Using Audoku

### Include Audoku

Just require it like a simple package:

var au = require('audoku');

Using Audoku

Audoku provides a middleware for Express to describe your endpoint parameters and info.

Here the function documentation:

function doku(config)

Builds a function to be used as middleware for single endpoint. Like this:

var router = require('express').Router();
var au = require('audoku');
router.get('/endpoint', au.doku(config), function(req,res){

  ...
});

config parameters

The config argument should be a JSON dictionary containing any of the keys in this example:

{
  'description' : 'This endpoint returns ...'
  'headers' : { ... },
  'fields' : { ... },
  'params' : { ... },
  'bodyFields' : { ... },
  'options' : { ... }
}

Each dictionary can contain several elements defined like the example:

{
  ...
  'fields' : {
    //element definition
      'elementName' : {  // the element name is the key
        'description' : 'The elmement description',  
        'type' : 'string',  // type can be string, integer, float or date
        'required' : true   // true if the element should always be present in the request, default false
      },

      // next elements
      ...
     },
  ...
}
headers (dictionary)

A dictionary of elements passed as key-value headers in the request.

fields (dictionary)

The fields passed through the URL after the question mark, like http://example.com/endpoint?key1=value1&key2=value2&etc...

params (dictionary)

The positional parameters in URLS, like:

http://example.com/endpoint/:firstPar/someAction/:secondPar

bodyFields (dictionary)

The fields passed as json dictionary in request body, by using all methods except GET. For example a body of this form:

{
  "field1" : "value1",
  "field2" : "value2",
  ...
}
options (dictionary)

raiseOnError return http 400 with error information if errors are detected

description (string)

A text that describes the endpoint.

Examples

Example of writing the doc with a JSON config, on the endpoint you want to document:

var au = require('audoku');

router.get('/books', au.doku({
        'headers': {
            'wonderful': {
                'description': 'A wonderful header',
                'type': 'string',
                'required': false
            }
        },
        'fields': {
            'author': {
                'description': "Book author",
                'type': 'string',
                'required': true
            },
            'genre': {
                'description': 'Book genre',
                'type': 'string',
                'required': true
            },
            'year': {
                'description': 'Book edition year',
                'type': 'integer',
                'required': true
            }
        }
}), function (req, res) {

...

};

To get the help info just call the endpoint adding the field audoku=help, for example http://example.com/books?audoku=help

and you'll get the following help based upon your endpoint documentation:

{
  "method": "GET",
  "url": "/books",
  "headers": {
    "wonderful": {
      "description": "A wonderful header",
      "type": "string",
      "required": false
    },
    "audoku": {
      "description": "Set the audoku mode as help or report",
      "type": "string",
      "required": false
    }
  },
  "fields": {
    "author": {
      "description": "Book author",
      "type": "string",
      "required": true
    },
    "genre": {
      "description": "Book genre",
      "type": "string",
      "required": true
    },
    "year": {
      "description": "Book edition year",
      "type": "integer",
      "required": true
    },
    "audoku": {
      "description": "Set the audoku mode as help or report",
      "type": "string",
      "required": false
    }
  }
}

Audoku can also provide a way to check if your call to an endpoint is missing any fields, parameters or headers. To do that you have to set the audoku field to report like the others fields, as in this example

http://example.com/books?myfield=200&price=3$&author=kafka&year=last&audoku=report

and you'll get the following JSON:

{
  "fullurl": "http://example.com/books?myfield=200&price=3$&author=kafka&year=last&audoku=report",
  "url": "/books?myfield=200&price=3$&author=kafka&year=last&audoku=report",
  "method": "GET",
  "report": {
    "inputs": {
      "fields": {
        "author": {
          "type": "string",
          "expectedType": "string",
          "correct": true,
          "value": "kafka"
        },
        "audoku": {
          "type": "string",
          "expectedType": "string",
          "correct": true,
          "value": "report"
        }
      }
    },
    "totErrors": 2,
    "totWarnings": 11,
    "errors": {
      "fields": {
        "genre": {
          "message": "Required field 'genre' not found",
          "expectedType": "string"
        },
        "year": {
          "type": "string",
          "expectedType": "integer",
          "correct": false,
          "value": "last",
          "message": "Wrong type"
        }
      }
    },
    "warnings": {
      "headers": {
        "host": {
          "message": "Found undocumented header 'host'",
          "value": "example.com"
        },
        "connection": {
          "message": "Found undocumented header 'connection'",
          "value": "keep-alive"
        }
      },
      "fields": {
        "myfield": {
          "message": "Found undocumented field 'myfield'",
          "value": "200"
        },
        "price": {
          "message": "Found undocumented field 'price'",
          "value": "3$"
        }
      }
    }
  }
}

function apidocs(config)

The apidocs function generate the apidoc struct for your documentation and serves it with your app web server.

The config should have the fields like the following example:

var config = {
    metadata: {
            "name": "Api Project",
            "version": "1.0.0",
            "title": "REst web API",
            "url": "https://api.example.com",
            "header": {
                "title": "API Overview",
                "content": "<p>A wonderful set of APIs</p>"
            },
            "footer": {
                "title": "Maintained by ACME",
                "content": "<p>Codebase maintained by ACME</p>\n"
            }
        },
    app: app, // the espress main app
    docspath : '/docs',
    routers: [{
        basepath: "http://localhost:3000/api/v1/books" ,
        router: books // the router passed to "app.use('/api/v1/books', books); "
    },
    {
        basepath: "http://localhost:3000/api/v1/authors" ,
        router: authors // the router passed to "app.use('/api/v1/authors', authors); "
    }]

}

Author

Mauro Mereu ([email protected])