JSPM

  • Created
  • Published
  • Downloads 3738
  • Score
    100M100P100Q128036F
  • License MIT

using decorator to automatically generate swagger doc for koa-router

Package Exports

  • koa-swagger-decorator
  • koa-swagger-decorator/dist/swaggerTemplate

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (koa-swagger-decorator) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

koa-swagger-decorator npm-url

using decorator to auto generate swagger json docs

Installation

npm install koa-swagger-decorator

Introduction

Koa Swagger Decorator

[working in progress] using decorator to auto generate swagger json docs

Requirements

  • Koa2
  • koa-router
  • babel support for decorator
// add [transform-decorators-legacy] to .babelrc

npm install --save-dev babel-plugin-transform-decorators-legacy

{
  "presets": [
    ["env", {"targets": {"node": "current"}}]
  ],
  "plugins": ["transform-decorators-legacy"]
}

Introduction

first wrapper the koa-router object

// router.js
import Router from 'koa-router';

import Test from './test';

import { wrapper } from 'koa-swagger-decorator';

const router = new Router();

wrapper(router);

// open /swagger-html to show the swagger ui page
// open /swagger-json to show the swagger json data
router.swagger({ title: 'SWAGGER API DOC', description: 'API DOC', version: '1.0.0' });

// map all static methods at Test class for router
router.map(Test);

using decorator to make api definition

// test.js

import User from 'models/user';
import { request, summary, query, path, body, tags } from 'koa-swagger-decorator';

const testTag = tags(['test']);

const userSchema = {
  name: { type: 'string', required: true },
  gender: { type: 'string', required: false, example: '男' },
  groups: {
    type: 'array',
    required: true,
    items: { type: 'string', example: '组1' }
  }
};

export default class Test {
  @request('get', '/users')
  @summary('获取用户列表')
  @testTag
  @query({
    type: { type: 'number', required: true, default: 1, description: '筛选的种类' }
  })
  static async getUsers(ctx) {
    const users = await User.findAll();
    ctx.body = { users };
  }

  @request('get', '/users/{id}')
  @summary('根据id获取用户信息')
  @testTag
  @path({
    id: { type: 'number', required: true, default: 1, description: '对应用户 id' }
  })
  static async getUser(ctx) {
    const { id } = ctx.params;
    const user = await User.findById(id);
    ctx.body = { user };
  }

  @request('post', '/users')
  @testTag
  @body(userSchema)
  static async postUser(ctx) {
    const body = ctx.request.body;
    ctx.body = { result: body };
  }

  static async temp(ctx) {
    ctx.body = { result: 'success' };
  }
}

runing the project and it will generate docs through swagger ui

image.png

License

© MIT