0.0.22 • Published 7 years ago
swagger-decorator v0.0.22
swagger-decorator: Node.js 应用中一处注解,多处使用
- use yarn or npm to install:
$ yarn add swagger-decorator
$ yarn add transform-decorators-legacy -D
- import
wrappingKoaRouter
and wrap the router created by koa-router:
import { wrappingKoaRouter } from "swagger-decorator";
...
const Router = require("koa-router");
const router = new Router();
wrappingKoaRouter(router, "localhost:8080", "/api", {
title: "Node Server Boilerplate",
version: "0.0.1",
description: "Koa2, koa-router,Webpack"
});
// define default route
router.get("/", async function(ctx, next) {
ctx.body = { msg: "Node Server Boilerplate" };
});
// use scan to auto add method in class
router.scan(UserController);
- define Controller and use decorator to attach description for api:
export default class UserController extends UserControllerDoc {
@apiRequestMapping("get", "/users")
@apiDescription("get all users list")
static async getUsers(ctx, next): [User] {
ctx.body = [new User()];
}
@apiRequestMapping("get", "/user/:id")
@apiDescription("get user object by id, only access self or friends")
static async getUserByID(ctx, next): User {
ctx.body = new User();
}
@apiRequestMapping("post", "/user")
@apiDescription("create new user")
static async postUser(): number {
ctx.body = {
statusCode: 200
};
}
}
export default class UserControllerDoc {
@apiResponse(200, "get users successfully", [User])
static async getUsers(ctx, next): [User] {}
@pathParameter({
name: "id",
description: "user id",
type: "integer",
defaultValue: 1
})
@queryParameter({
name: "tags",
description: "user tags, for filtering users",
required: false,
type: "array",
items: ["string"]
})
@apiResponse(200, "get user successfully", User)
static async getUserByID(ctx, next): User {}
@bodyParameter({
name: "user",
description: "the new user object, must include user name",
required: true,
schema: User
})
@apiResponse(200, "create new user successfully", {
statusCode: 200
})
static async postUser(): number {}
}
- decorate the Entity:
// @flow
import { entityProperty } from "swagger-decorator";
export default class User {
@entityProperty({
type: "integer",
description: "user id, auto-generated",
required: false
})
id: string = 0;
@entityProperty({
type: "string",
description: "user name, 3~12 characters",
required: true
})
name: string = "name";
friends: [number] = [1];
properties: {
address: string
} = {
address: "address"
};
}
- run your application and open swagger docs (PS. swagger-decorator contains Swagger UI):
/swagger
/swagger/api.json
0.0.22
7 years ago
0.0.21
7 years ago
0.0.20
7 years ago
0.0.19
7 years ago
0.0.17
7 years ago
0.0.16
7 years ago
0.0.15
7 years ago
0.0.14
7 years ago
0.0.13
7 years ago
0.0.12
7 years ago
0.0.10
7 years ago
0.0.9
7 years ago
0.0.8
7 years ago
0.0.7
7 years ago
0.0.6
7 years ago
0.0.5
7 years ago
0.0.4
7 years ago
0.0.3
7 years ago
0.0.2
7 years ago
0.0.1
7 years ago