apidoc官网。本文大部份内容摘自:ApiDoc 后端接口注释文档的使用javascript
# 全局安装
yarn global add apidoc
# 查看是否安装成功
apidoc -h 复制代码
在项目文件夹下新建 apidoc.json ,或者在 package.json 下建立 apidoc 节点。示例:php
{
"name": "example",
"version": "0.1.0",
"description": "apiDoc basic example",
"title": "Custom apiDoc browser title",
"url" : "https://api.github.com/v1"
}复制代码
{
"name": "example",
"version": "0.1.0",
"description": "apiDoc basic example",
"apidoc": {
"title": "Custom apiDoc browser title",
"url" : "https://api.github.com/v1"
}
}复制代码
详细配置说明:java
/**
* @api {get} /user/:id Request User information
* @apiVersion 0.1.0
* @apiName GetUser
* @apiGroup User
*
* @apiParam {Number} id Users unique ID.
*
* @apiSuccess {String} firstname Firstname of the User.
* @apiSuccess {String} lastname Lastname of the User.
*
* @apiSuccessExample Success-Response:
* HTTP/1.1 200 OK
* {
* "firstname": "John",
* "lastname": "Doe"
* }
*
* @apiError UserNotFound The id of the User was not found.
*
* @apiErrorExample Error-Response:
* HTTP/1.1 404 Not Found
* {
* "error": "UserNotFound"
* }
*/复制代码
文档块以 /**
和 */
结尾,其中@api为必须字段,格式同例子:@api + {请求类型} + 接口路径 + 接口描述,在生成的时候没有@api 的文档块会被忽略。@apiName
和 @apiGroup,
在版本迭代时应保持一致,其余字段为可选。
node
@api {method} path [title]复制代码
示例:@api {get} /user/:id Users unique ID.
git
@apiParam [(group)] [{type}] [field=defaultValue] [description]复制代码
说明:1. 带中括号[]的Fieldname将Variable定义为可选,[]内部是须要的格式。2. =defaultValue 参数默认值。3.入参能够用[(group)]进行分组。
github
示例:(2个)
json
/**
* @api {get} /user/:id
* @apiParam {Number} id Users unique ID.
*/
/**
* @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]复制代码
示例:(3个)后端
/** * @api {get} /user/:id * @apiSuccess {String} firstname Firstname of the User. * @apiSuccess {String} lastname Lastname of the User. */
/** * @api {get} /user/:id * @apiSuccess (200) {String} firstname Firstname of the User. * @apiSuccess (200) {String} lastname Lastname of the User. */
/** * @api {get} /user/:id * @apiSuccess {Boolean} active Specify if the account is active. * @apiSuccess {Object} profile User profile information. * @apiSuccess {Number} profile.age Users age. * @apiSuccess {String} profile.image Avatar-Image. */复制代码
apidoc -e node_modules
,或者添加-i,如 apidoc -i src/
。{"message":"parser plugin 'return' not found in block: 13","level":"warn"}
{"message":"parser plugin 'param' not found in block: 14","level":"warn"}
{"message":"parser plugin 'return' not found in block: 14","level":"warn"}复制代码