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"}複製代碼