1.0.0 • Published 2 years ago

koa-router-joi-swagger v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

koa-router-joi-swagger

Validate router input and generate swagger ui based on router and validation

Installation

$ npm install koa-router-joi-swagger

Uses Joi, @koa/router And koa2-swagger-ui

Usage

Import Packages

const Koa = require('koa');
const { Router, Validator, Joi, Swagger } = require('koa-router-joi-swagger');
const app = new Koa();
const router = new Router();

Validate Input (See Joi Documentation)

router.post(
  '/api/:param1',
  Validator({
    query: {
      queryParam: Joi.string().required(),
    },
    body: {
      bodyParam: Joi.number().optional(),
    },
    params: {
      param1: Joi.string().required(),
    },
  })
);

Serve Swagger Docs (pass koa2-swagger-ui config as uiConfig)

router.get(
  '/docs',
  Swagger({
    // Pass router as parameter
    router,
    uiConfig: {
      routePrefix: false,
      swaggerOptions: {
        spec: {
          info: {
            title: 'Test Api',
            version: '1.0.0',
            description: 'This is test api specs',
            ...: ...
          },
        },
      },
    },
  })
);