You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

koa-swapi

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-swapi

Build restful api like HapiJs

1.1.5
latest
Source
npmnpm
Version published
Weekly downloads
64
6.67%
Maintainers
1
Weekly downloads
 
Created
Source

koa-swapi

node npm GitHub last commit PRs Welcome Packagist

Super charged router for Koa

English | 中文

Koa-swapi is a user-friendly Koa middleware, design your koa router with koa-swapi like Hapijs, koa-swapi will validate each request components, and generate OpenAPI documant (fka Swagger RESTful API Documentation Specification).

Install

npm i koa-swapi --save

Usage Guide

Build Schema

const Joi = require('joi')
const { Route, Validator } = require('koa-swapi')

const catSchemas = [
  Route
    .get('/cat/:id')
    .tags(['catt', 'aninaml'])
    .summary('获得一只帅气猫')
    .description('想获得一只帅气猫的时候可以调用这个接口')
    .validate(
      Validator
        .params({
          id: Joi.string().required().min(2).max(4).description('猫的id')
        })
        .query({
          name: Joi.string().required().min(3).max(100).description('猫的名字'),
          sex: Joi.any().required().valid(['0', '1']).description('猫的性别, 0:男, 1:女')
        })
        .output({
          200: {
            body: Joi.string()
          }
        })
    )
    .create('getCat')
]

Build Controller

const controller = module.exports = {}

controller.getCat = async (ctx) => {
  ctx.status = 200;
  ctx.body = 'miaomiaomiao'
}

Build Api

const { Api } = require('koa-swapi')

const apis = [
  Api.schemas(catSchemas).handler(catController)
]

Register

// app.js
const Koa = require('koa')
const { Swapi } = require('koa-swapi')

const app = new Koa()
const swapi = new Swapi()

swapi.register(app, {
  basePath: '/api',
  // swagger: {...}: SwaggerSetting
  apis: apis,
  logger,// custom logger instance
  middleware,// custom middleware use before validate happens
})

app.listen(3333)

Keywords

koa

FAQs

Package last updated on 29 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.