apidoc详解

ApiDoc使用详解

参考项目文档:apidoc官方文档

1. 安装apidoc

注意:首先要安装npm网上搜索
安装命令:npm install apidoc -g

2.安装grunt module
npm install grunt-apidoc --save-dev

3.配置apidoc.json或者package.json文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
"name": "项目名称",
"version": "版本号",
"description": "项目描述",
"title":"浏览器网页标题",
"url":"请求的路径前面的服务器地址",
"sampleUrl":"模拟请求的地址"(如果设置,所有接口都会有个模拟请求的界面),
"header": {//没试过
"title": "My own header title",
"filename": "header.md"
},
"footer": {
"title": "My own footer title",
"filename": "footer.md"
},
"order":{
//排序,没试过
},
"template":{
"forceLanguage":"zh_cn",//语言
"withCompare":true //是否开启比较版本功能(默认true),
"withGenerator":true //是否开启生成信息在页脚(默认true),
"jQueryAjaxSetup":object //ajax请求(未尝试)
}
}

4.apidoc接口文档参数详解

  • @api {method} /path/to/:uid [title]

    注:定义接口名,请求方式,路径(必填,除了apiDefine)

    1
    2
    3
    /**
    * @api {post} /user/reg 用户注册
    */
  • @apiDefine name [title]

    注:定义公共文档块,可使用apiUse在其他文档中插入

    1
    2
    3
    4
    5
    6
    7
    8
    9
    /**
    * @apiDefine MyError
    * @apiError UserNotFound The <code>id</code> of the User was not found.
    */

    /**
    * @api {get} /user/:id
    * @apiUse MyError
    */
  • @apiDescription text

    注:接口描述

    1
    2
    3
    /**
    *@apiDescription 用户登录接口
    */
  • @apiName name

    注:接口名(必填,用户文档左侧菜单二级分类)

    1
    2
    3
    /**
    *@apiName login
    */
  • @apiGroup group

    注:接口分组(必填,用户文档左侧菜单一级分类)

    1
    2
    3
    /**
    *@apiGroup user
    */
  • @apiVersion 1.1.1

    注:接口版本号(必须是:major.minor.patch格式:1.0.0)

    1
    2
    3
    /**
    *@apiVersion 1.0.0
    */
  • @apiDeprecated text

    注:未测试

    1
    2
    3
    /**
    *@apiDeprecated text
    */
  • @apiIgnore [hint]

    注:忽略的接口[hint]为忽略的原因,可选

    1
    2
    3
    /**
    *@apiIgnore hint
    */
  • @apiPermission name

    注:api的权限名

    1
    2
    3
    /**
    *@apiPermission admin
    */
  • @apiParam [(group)] [{type}] [field=defaultValue] [description]

    注:接口参数
    {type}包括:{String} 字符串,{Boolean}布尔值, {Number}数字, {Object}对象, {String[]}字符串数组
    [field] 可选参数field
    field 必选参数 field
    =’string’ 参数默认值
    {type{size}} 代表字符长度{String{6-12}}
    [description] 字段描述

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    /**
    * @api {post} /user/
    * @apiParam {String} [firstname] Optional Firstname of the User.
    * @apiParam {String} lastname Mandatory Lastname.
    * @apiParam {String} country="DE" Mandatory with default value "DE".
    * @apiParam {Number} [age=18] Optional Age with default 18.
    *
    * @apiParam (Login) {String} pass Only logged in users can post this.
    * In generated documentation a separate
    * "Login" Block will be generated.
    */
  • @apiSuccess [(group)] [{type}] field [description]

    注:接口返回值显示
    {type}包括:{String} 字符串,{Boolean}布尔值, {Number}数字, {Object}对象, {String[]}字符串数组
    field 返回字段名
    [description] 字段描述

    1
    2
    3
    4
    5
    /**
    * @api {get} /user/:id
    * @apiSuccess (200) {String} firstname Firstname of the User.
    * @apiSuccess (200) {String} lastname Lastname of the User.
    */
  • @apiSuccessExample [{type}] [title] example

    注:接口返回值显示
    {type}包括:{json}
    title 返回说明:
    [example] 返回例子

    1
    2
    3
    4
    5
    6
    7
    8
    9
    /**
    * @api {get} /user/:id
    * @apiSuccessExample {json} Success-Response:
    * HTTP/1.1 200 OK
    * {
    * "firstname": "John",
    * "lastname": "Doe"
    * }
    */

5.版本比较

要保留两份,后新的版本的时候。保留原来的版本说明,复制修改为新的版本,另外修改新的版本号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* @api {post} /index/index 首页接口
* @apiVersion 1.1.0
* @apiDescription 首页获取数据
* @apiName index
* @apiGroup Index
*
* @apiUse token
* @apiParam {string} [page=1] 页数
* @apiParam {string} [limit=10] 每页条数
* @apiParam {string} [name] 搜索名
*
* @apiUse tokenError
*
* @apiSuccessExample {json} 成功返回:
* {
"status": "1",
"code": "200",
"message": "成功",
"data": {
"noticeList": [
{
"pic": "http://xblj.oss-cn-hangzhou.aliyuncs.com/khw_server/user/1/credential/1498805411211761.png",
"url": "http://www.local.com/xblj/khw/server/api/web/index.php/page/notice?id=6",
"id": "6"
}
],
"courseList": [
{
"type_name": "客运",
"type": "1",
"course_name": "测试",
"price": "20.00",
"cover_pic": "http://xblj.oss-cn-hangzhou.aliyuncs.com/khw_server/user/1/credential/1498805411211761.png",
"buy_times": "10",
"course_id": "1"
}
]
}
}
*/

/**
* @api {post} /index/index 首页接口
* @apiVersion 1.2.0
* @apiDescription 首页获取数据
* @apiName index
* @apiGroup Index
*
* @apiUse token
* @apiParam {string} [page=1] 页数
* @apiParam {string} [limit=10] 每页条数
*
* @apiUse tokenError
*
*
* @apiSuccessExample {json} 成功返回:
* {
"status": "1",
"code": "200",
"message": "成功",
"data": {
"noticeList": [
{
"pic": "http://xblj.oss-cn-hangzhou.aliyuncs.com/khw_server/user/1/credential/1498805411211761.png",
"url": "http://www.local.com/xblj/khw/server/api/web/index.php/page/notice?id=6",
"id": "6"
}
],
"courseList": [
{
"type_name": "客运",
"type": "1",
"course_name": "测试",
"price": "20.00",
"cover_pic": "http://xblj.oss-cn-hangzhou.aliyuncs.com/khw_server/user/1/credential/1498805411211761.png",
"buy_times": "10",
"course_id": "1"
}
]
}
}
*/