1.0.8 • Published 5 years ago

salak-router v1.0.8

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

salak-router

NPM version build status David deps NPM download

base on koa-router & joi

  • Support request validate.
  • Support response validate.

Installation

Install using npm:

npm install --save salak-router

API Reference

  • salak-router
    • Router (extends koa-router)
      new Router(opts)
      instance
      * .addRoute(opts, handler)

Router

new Router(opts)

ParamTypeDescription
optsObject
opts.prefixStringprefix router paths
opts.requestFailureNumberrequest validation error status
opts.responseFailureStringresponse validation error status

.addRoute(opts, handler)

add route for app

ParamTypeDescription
optsObject
opts.pathStringrouter path
opts.methodArrayStringrouter method
opts.validateObjectrouter validation schema
handlerFunctionrouter handler

Example

const Koa = require('koa')
const Router = require('salak-router')
const Joi = Router.Joi

const app = new Koa()
const router = new Router()

router.addRoute({
  path: '/test',
  method: 'GET',
  validate: {
    query: {
      id: Joi.number().required().description('文章id')
    },
    responses: {
      200: {
        body: Joi.object().keys({
          code: Joi.number(),
          msg: Joi.string()
        }).description('文章详情')
      }
    }
  }
}, async (ctx) => {
  ctx.body = {
    code: 0,
    msg: 'ok'
  }
})

app.use(router.routes())

app.listen(3000)

LICENSE

MIT

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago