Skip to content

Commit 620a5fd

Browse files
upd: egg-swagger-doc
1 parent a10e091 commit 620a5fd

File tree

6 files changed

+98
-0
lines changed

6 files changed

+98
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const JsonBody = {
2+
code: { type: 'number', required: true, example: 0 },
3+
message: { type: 'string', required: true, example: 'success' },
4+
data: { type: 'Enum', required: true, example: [] },
5+
}
6+
7+
module.exports = {
8+
indexJsonBody: {
9+
...JsonBody,
10+
data: { type: 'string', example: 'test' },
11+
},
12+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const Controller = require('egg').Controller;
2+
3+
/**
4+
* @controller HomeController
5+
*/
6+
class HomeController extends Controller {
7+
/**
8+
* @router get /index 路径
9+
* @summary 接口的小标题信息
10+
* @description 接口的描述信息
11+
* @request query integer id 对参数id的描述
12+
* @request query string name 对参数name的描述
13+
* @response 200 indexJsonBody
14+
*/
15+
async index () {
16+
console.log(111, this.ctx.query)
17+
this.ctx.body = {
18+
code: 0,
19+
message: '',
20+
data: {
21+
result: this.ctx.query.name || 'none'
22+
}
23+
}
24+
}
25+
}
26+
27+
module.exports = HomeController;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = app => {
2+
const { router, controller } = app;
3+
4+
router.get('/index', controller.home.index);
5+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
exports.keys = 'csxiaoyao';
2+
3+
// egg-swagger-doc 配置信息
4+
exports.swaggerdoc = {
5+
dirScanner: './app/controller', // 配置自动扫描的控制器路径
6+
apiInfo: {
7+
title: '接口文档', // 接口文档的标题
8+
description: 'swagger 测试接口文档', // 接口文档描述
9+
version: '1.0.0', // 接口文档版本
10+
termsOfService: 'http://swagger.io/terms/', // 服务条件
11+
contact: {
12+
email: 'sunjianfeng@csxiaoyao.com' // 联系方式
13+
},
14+
license: {
15+
name: 'Apache 2.0',
16+
url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
17+
},
18+
},
19+
basePath: '/', // 配置基础路径
20+
schemes: ['http', 'https'], // 配置支持的协议
21+
consumes: ['application/json'], // 指定处理请求的提交内容类型 (Content-Type),如 application/json、text/html
22+
produces: ['application/json'], // 指定返回的内容类型,仅当 request 请求头中的(Accept)类型中包含该指定类型才返回
23+
securityDefinitions: {}, // 配置接口安全授权方式
24+
enableSecurity: false, // 是否启用授权,默认 false
25+
// enableValidate: true, // 是否启用参数校验,默认 true
26+
routerMap: false, // 是否启用自动生成路由(实验功能),默认 true
27+
enable: true // 默认 true
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// 配置 egg-swagger-doc 插件信息。
2+
exports.swaggerdoc = {
3+
enable: true, // 是否启用。
4+
package: 'egg-swagger-doc', // 指定包名称。
5+
};
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "egg-example",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1",
8+
"dev": "egg-bin dev"
9+
},
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"egg": "^2.29.4",
14+
"egg-swagger-doc": "^2.3.2",
15+
"koa2-swagger-ui": "^5.1.0",
16+
"swagger-jsdoc": "*"
17+
},
18+
"devDependencies": {
19+
"egg-bin": "^4.16.2"
20+
}
21+
}

0 commit comments

Comments
 (0)