您的位置:首页 > 其它

Web API文档生成工具apidoc

2016-06-30 18:06 579 查看
apidoc
可以根据代码注释生成
webapi
文档,支持大部分主流语言
javajavascriptphpcoffeescripterlangperlpythonrubygo...
,相对而言,
web
接口的注释维护起来更加方便,不需要额外再维护一份文档。

apidoc
从注释生成静态
html
网页文档,不仅支持项目版本号,还支持api版本号。

安装

主页:http://apidocjs.com
github:https://github.com/apidoc/apidoc
可以使用
npminstallapidoc-g
进行安装,目前
0.12.x/4.0
都可以使用。

apidoc
支持
Grunt
,主页https://github.com/apidoc/grunt-apidoc

在项目中使用
npminstallgrunt-apidoc--save-dev
安装。

Gruntfile.js
添加
grunt.loadNpmTasks('grunt-apidoc');


配置
grunttask


apidoc:{
myapp:{
src:"app/",
dest:"apidoc/"
}
}


//在sails中2和3可以直接添加一个task


module.exports=function(grunt){

grunt.config.set('clean',{
apidoc:{ myapp:{ src:"app/", dest:"apidoc/" } }
});

grunt.loadNpmTasks('grunt-apidoc');
};



用法

可以在
shell
中执行
apidoc-h
就可以看到很多用法。

下面讲讲常用的几个参数。

//典型用法
apidoc-iapi/-odoc/api[-c./]-f".*\.js$"

-i表示输入,后面是文件夹路径
-o表示输出,后面是文件夹路径
默认会带上-c,在当前路径下寻找配置文件(apidoc.json),如果找不到则会在package.json中寻找"apidoc":{}
-f为文件过滤,后面是正则表达式,示例为只选着js文件
与-f类似,还有一个-e的选项,表示要排除的文件/文件夹,也是使用正则表达式
如果项目输入和输出稳定,可以编辑Makefile保存命令,例如:

docs:
@apidoc-iapi/-odoc/apidoc


之后使用
makedocs
即可生成/更新api文档。

配置

项目配置

apidoc.json
示例:

{
"name":"mysails",
"version":"1.0.0",
"title":"mysails",//浏览器标题
"description":"xun'stestproject"
}


如果放入
package.json
中,相同的字段可以直接使用
package.json
的定义,额外的字段放入
apidoc


{
"name":"mysails",
"private":true,
"version":"1.0.0",
"description":"xun'stestproject",
"apidoc":{
"title":"mysails"
},
...
}


代码注释

apidoc
支持如下关键字

@api{method}path[title]
只有使用@api标注的注释块才会在解析之后生成文档,title会被解析为导航菜单(@apiGroup)下的小菜单
method可以有空格,如{POSTGET}
@apiGroupname
分组名称,被解析为导航栏菜单
@apiNamename
接口名称,在同一个@apiGroup下,名称相同的@api通过@apiVersion区分,否者后面@api会覆盖前面定义的@api
@apiDescriptiontext
接口描述,支持html语法
@apiVersionverison
接口版本,major.minor.patch的形式

@apiIgnore[hint]
apidoc会忽略使用@apiIgnore标注的接口,hint为描述
@apiSampleRequesturl
接口测试地址以供测试,发送请求时,@apimethod必须为POST/GET等其中一种

@apiDefinename[title][description]
定义一个注释块(不包含@api),配合@apiUse使用可以引入注释块
在@apiDefine内部不可以使用@apiUse
@apiUsename
引入一个@apiDefine的注释块

@apiParam[(group)][{type}][field=defaultValue][description]
@apiHeader[(group)][{type}][field=defaultValue][description]
@apiError[(group)][{type}]field[description]
@apiSuccess[(group)][{type}]field[description]
用法基本类似,分别描述请求参数、头部,响应错误和成功
group表示参数的分组,type表示类型(不能有空格),入参可以定义默认值(不能有空格)
@apiParamExample[{type}][title]example
@apiHeaderExample[{type}][title]example
@apiErrorExample[{type}][title]example
@apiSuccessExample[{type}][title]example
用法完全一致,但是type表示的是example的语言类型
example书写成什么样就会解析成什么样,所以最好是书写的时候注意格式化,(许多编辑器都有列模式,可以使用列模式快速对代码添加*号)

@apiPermissionname
name必须独一无二,描述@api的访问权限,如admin/anyone


示例:

定义一个
200
的返回结果

/**js
*@apiDefineCODE_200
*@apiSuccess(Reponse200){number}code200
*@apiSuccess(Reponse200){json}[data='""']如果有数据返回
*@apiSuccessExample{json}Response200Example
*HTTP/1.1200OK
*{
*"code":200,
*"data":""
*}
*/


定义一个
500
的返回结果

/**
*@apiDefineCODE_500
*@apiSuccess(Response500){number}code500
*@apiSuccess(Response500){string}[message]errordescription
*@apiSuccessExample{json}Response500Example
*HTTP/1.1500InternalServerError
*{
*"code":500
*"message":"xxx"
*}
*/


定义接口

/**
*@apiDefineData
*
*@apiParam(data){string}[firstname]OptionalFirstnameoftheUser.
*@apiParam(data){string}lastnameMandatoryLastname.
*@apiParam(data){string}country="cn"Mandatorywithdefaultvalue"DE".
*@apiParam(data){number}[age=18]OptionalAgewithdefault18.
*/

/**
*@api{POSTGET}/api/test/hello[/:id]/api/test/hello[/:id]
*@apiNametestapi
*@apiGroupHelloWorld
*@apiVersion1.0.0
*@apiDescriptionjustatest
*@apiPermissionanyone
*@apiSampleRequesthttp://test.github.com*
*@apiParam{number}[id]anyid
*@apiParam{json}dataobject
*@apiUseData
*
*@apiParamExample{json}RequestExample
*POST/api/test/hello/1
*{
*"data":{
*"firstname":"test",
*"lastname":"sails",
*"country":"cn"
*}
*}
*
*@apiUseCODE_200
*@apiUseCODE_500
*/


最后生成文档之后,结果如下所示



文/xun(简书作者)
原文链接:http://www.jianshu.com/p/bb5a4f5e588a
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: