0.4.3 • Published 5 years ago

koa-joi-to-swagger v0.4.3

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

Koa-joi-to-swagger

Koa route validator and auto docs base on koa-router, joi and swagger

Features:

Installation

Install using npm:

npm install --save koa-joi-to-swagger

Example

See example

Usage

Koa-joi-to-swagger returns a constructor which you use to define your routes.

const Koa = require('koa');
const KoaBody = require('koa-body');
const Joi = require('@hapi/joi');
const Router = require('koa-joi-to-swagger');

const app = new Koa();
const router = new Router(app, {docs: {prefix: '/docs'}});

app.use(KoaBody());
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(3000);

router.register({
  method: 'get',
  path: '/users',
  config: {
    summary: 'query users',
    response: {
      200: {
        body: Joi.array().items(Joi.object({name: Joi.string()}))
      }
    },
    validate: {
      query: {
        page: Joi.number().integer().min(1).default(1),
        size: Joi.number().integer().min(1).max(50).default(10)
      }
    },
    handler: async ctx => {
      ctx.body = [{name: 'Jack'}];
    }
  }
});

API documentation url: http://localhost:3000/docs/

API Reference

Instance

Create router instance

new Router(app, [options])

Methods

.register(params)

Register a route

Test

npm run test