1.0.5 • Published 3 years ago

hapi-doc v1.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

hapi-doc

Create an API documentation endpoint for your hapi application

  1. Introduction
  2. Installation
  3. Usage

Introduction

hapi-doc is a plugin for hapi which generate an API documentation based on hapi's routes settings.

A demo is accessible here!

overview

Installation

Using npm:

$ npm install hapi-doc

Using yarn:

$ yarn add hapi-doc

Usage

Import

Using CommonJS:

const hapidoc = require('hapi-doc')

Using ESM:

import hapidoc from 'hapi-doc'

Create hapi server

Afterwards create your hapi server if not already done:

const hapi = require("@hapi/hapi")

const server = hapi.server({ port: 8080 })

Registration

Finally register the plugin. Available options are described in the next section.

await server.register({ plugin: hapidoc, options })

Options

namedescriptiondefault
pathpath where the UI must be serevd/doc
titledocumentation titleDocumentation
versionAPI versionpackage.json version || '0.0.0'
descriptiondescription of your API. May accept markdown formatting
tagslist of tags to appear on the UIall
serverslist of serverscurrent

This is an example of valid options

let options = {
    path: '/path/to/documentation',
    title: 'Documentation Title',
    version: '0.42.0',
    description: 'Description of the API',
    tags: ['pet', 'store', 'user'], 
    servers: [{url: 'http://localhost:8080'}],
}

Configure

hapi-doc will observe server's routes options in order to build the API model. Here are the following routes options that are used.

The schema description for request and response objects must be done using Joi.

namedescription
route.methodendpoint method
route.pathendpoint path
route.options.tagslist of tags
route.options.descriptiondescription of the endpoint
route.options.validate.paramsJoi object representing path parameters
route.options.validate.queryJoi object representing query parameters
route.options.validate.payloadJoi object representing request body parameters
route.options.response.statuskey-value object where keys must be a valid HTTP status code and the value the Joi object representation of the response

Here is an example of hapi route configuration and the corresponding documentation endpoint generated.

server.route({
  method: 'PUT',
  path: '/users/{userId}',
  handler: async (req, h) => {},
  options:{
    tags: ['users'],
    description: 'Update a user',
    validate:{
      params: Joi.object({
        userId: Joi.string()
          .guid({version:'uuidv4'})
          .description('Id of the user')
          .required()
      }), 
      payload: Joi.object({
        email: Joi.string().regex(EMAIL_RGX),
        password: Joi.string(),
        bio: Joi.string()
      })
    },
    response:{
      status:{
        200: Joi.string().description('successful operation'),
        404: Joi.string().description('user not found'),
      }
    }
  }
})

routeExample

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago