Giter Club home page Giter Club logo

hapi-docs's Introduction

hapi-docs

An indispensable hapi plugin that automatically documents the major features of your hapi server

hapi-docs will document:

  • routes

  • server methods

  • server events

  • server plugins

  • server auth schemes

Installation

npm install hapi-docs

Usage

hapi-docs is a standard hapi plugin, the quickest way to use it is to just register it like so:
await server.register({
  plugin: require('hapi-docs'),
  options: {
    docsEndpoint: '/docs'
  }
});

and this will register a route at /docs that will return server information in HTML form. In addition this will decorate your server with a server.docs object that provides a set of functions that return document information.

  • server.docs.plugins()

    Just returns the result of server.registrations

  • server.docs.events()

    Will list all of your registered server events and listeners like so:

    {
      log: { handlers: ['foo1', 'foo2'] },
      request: { handlers: ['debug'] },
      response: { handlers: ['(anonymous)'] },
      route: { handlers: ['foo2'] }
    }
  • server.docs.routes(options)

    Will return a list of the server route configuration and corresponding joi validation data like so:

    [{
      path: '/someRoute',
      method: 'post',
      payload: {
        type: 'object',
        children: {
          name: {
            type: 'string',
            flags: { presence: 'required' },
            invalids: ['']
          },
          hash: {
            type: 'string',
            flags: { presence: 'required' },
            invalids: ['']
          },
          id: {
            type: 'string',
            flags: { presence: 'required' },
            invalids: ['']
          }
        }
      }
    }]

    options can contain a comma-separated list of tags in string form, so that you only list out the routes that match that tag:

    server.docs.routes({ tags: 'secure,api' });

    will only list info for routes that have the 'secure' or 'api' tag.

  • server.docs.auth(routeList)

    Takes in a list of route configurations (which you can get by calling server.docs.routes()) and returns a corresponding list of auth strategies for the routes passed:

       [{ name: 'local' }, { name: 'default' }]
  • server.docs.methods()

    Returns the list of server methods and any associated meta-data. For example if you define a function with annotiations like so:

    const annotated = function func3() {
      return 'scheme';
    };
    annotated.schema = {
      payload: {
        id: Joi.string().required(),
      }
    };
    annotated.description = 'A function that has some annotations';
    server.method('func3', annotated);

    Calling server.docs.methods() returns:

    [{
        name: 'func3',
        description: 'A function that has some annotations',
        schema: {
          type: 'object',
          children: {
            payload: {
              type: 'object',
              children: {
                id: {
                  type: 'string',
                  flags: { presence: 'required' },
                  invalids: ['']
                },
              }
            }
          }
        }
      },]
  • server.docs.html()

    This will return a tidy HTML page listing out the methods, routes, auth, events, and plugins data.

Options

  • getMeta

    You can pass a getMeta function that returns an object containing additional information you want documented for your server, eg:

    await server.register({
      plugin: require('../'),
      options: {
        getMeta: {
          routes: {
            '/some/{name}/page': {
              description: 'really this is a good route',
              tags: ['meta']
            }
          },
          methods: {
            'api.create': {
              description: 'a meta description',
              tags: ['meta']
            }
          },
          events: {
            response: {
              description: 'triggered when a route responds to a request'
            }
          },
          strategies: {
            local: {
              description: 'lets everyone in'
            },
            default: {
              description: 'nobody gets in'
            }
          }
        }
      }
    });
  • docsEndpoint

You can pass an endpoint to register a route that allows you to fetch the HTML version of the server docs. You can also pass ?tags=tag1,tag2 in the request query to list only routes that match the indicated tags.

await server.register({
  plugin: require('../'),
  options: {
    docsEndpoint: '/docsEndpoint'
  }
});

Then you can GET /docsEndpoint and get back a page listing your server docs.

hapi-docs's People

Contributors

orthagonal avatar jgallen23 avatar

Stargazers

Josh Goldsmith avatar

Watchers

 avatar James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.