【ApiDoc】官方文檔(翻譯)

本文主要參考

一、apidoc簡介

apidoc是一款可以有源代碼中的註釋直接自動生成api接口文檔的工具,它幾乎支持目前主流的所有風格的註釋。例如:
Javadoc風格註釋(可以在C#, Go, Dart, Java, JavaScript, PHP, TypeScript等語言中使用)

/**
 * This is a comment.
 */

CoffeeScript

###
This is a comment.
###

Elixir

@apidoc """
This is a comment.
"""

Erlang (註釋中的‘%’是可選的)

%{
% This is a comment.
%}

Perl (Doxygen)

#**
# This is a comment.
#*

Python

"""
This is a comment.
"""

Ruby

=begin
This is a comment.
=end

Lua

--[[
This is a comment.
--]]

二、apidoc使用

可以通過以下命令安裝apidoc:

npm install apidoc -g

運行:

apidoc -i myapp/ -o apidoc/ -t mytemplate/

2.1 apidoc 命令參數列表:

參數 描述
-h, --help 查看幫助文檔
-f --file-filters 指定讀取文件的文件名過濾正則表達式(可指定多個)
例如: apidoc -f ".*\\.js$" -f ".*\\.ts$" 意爲只讀取後綴名爲js和ts的文件
默認值:.clj .cls .coffee .cpp .cs .dart .erl .exs?
.go .groovy .ino? .java .js .jsx .kt .litcoffee lua .p .php? .pl .pm .py .rb .scala .ts .vue
-e --exclude-filters 指定不讀取的文件名過濾正則表達式(可指定多個)
例如:apidoc -e ".*\\.js$" 意爲不讀取後綴名爲js的文件
默認:''
-i, --input 指定讀取源文件的目錄
例如:apidoc -i myapp/ 意爲讀取myapp/目錄下面的源文件
默認值:./
-o, --output 指定輸出文檔的目錄
例如:apidoc -o doc/ 意爲輸出文檔到doc目錄下
默認值:./doc/
-t, --template 指定輸出的模板文件
例如:apidoc -t mytemplate/
默認:path.join(__dirname, '../template/')(使用默認模板)
-c, --config 指定包含配置文件(apidoc.json)的目錄
例如:apidoc -c config/
默認:./
-p, --private 輸出的文檔中是否包含私有api
例如:apidoc -p true
默認:false
-v, --verbose 是否輸出詳細的debug信息
例如:apidoc -v true
默認:false

2.2 配置(apidoc.json)

每次導出接口文檔都必須要讓apidoc讀取到apidoc.json文件(如果未添加配置文件,導出報錯),你可以在你項目的根目錄下添加apidoc.json文件,這個文件主要包含一些項目的描述信息,比如標題、簡短的描述、版本等,你也可以加入一些可選的配置項,比如頁眉、頁腳、模板等。
apidoc.json

{
  "name": "example",
  "version": "0.1.0",
  "description": "apiDoc basic example",
  "title": "Custom apiDoc browser title",
  "url" : "https://api.github.com/v1"
}

如果你的項目中使用了package.json文件(例如:node.js工程),那麼你可以將apidoc.json文件中的所有配置信息放到package.json文件中的apidoc參數中:
package.json

{
  "name": "example",
  "version": "0.1.0",
  "description": "apiDoc basic example",
  "apidoc": {
    "title": "Custom apiDoc browser title",
    "url" : "https://api.github.com/v1"
  }
}

apidoc.json配置項

參數 描述
name 工程名稱
如果apidoc.json文件中沒有配置該參數,apidoc會嘗試從pakcage.json文件中讀取
version 版本
如果apidoc.json文件中沒有配置該參數,apidoc會嘗試從pakcage.json文件中讀取
description 工程描述
如果apidoc.json文件中沒有配置該參數,apidoc會嘗試從pakcage.json文件中讀取
title 瀏覽器標題
url api路徑前綴
例如:https://api.github.com/v1
sampleUrl 如果設置了該參數,那麼在文檔中便可以看到用於測試接口的一個表單(詳情可以查看參數@apiSampleReques)
header.title 頁眉導航標題
header.filename 頁眉文件名(markdown)
footer.title 頁腳導航標題
footer.filename 頁腳文件名(markdown)
order 接口名稱或接口組名稱的排序列表
如果未定義,那麼所有名稱會自動排序
"order":[
     "Error",
     "Define",
     "PostTitleAndError",
     "PostError"
]

三、 apidoc註釋參數

3.1 @api

【必填字段】否則,apidoc會忽略該條註釋

@api {method} path [title]

參數列表:

參數 必填 描述
method yes 請求類型:DELETE, GET, POST, PUT, ...更多
path yes 請求路徑
title no 接口標題

例:

/**
 * @api {get} /user/:id
 */

3.2 @apiDefine

@apiDefine name [title]
                     [description]

定義註釋模塊(類似於代碼中定義一個常量),對於一些通用可複用的註釋模塊(例如:接口錯誤響應模塊),只需要在源代碼中定義一次,便可以在其他註釋模塊中隨便引用,最後在文檔導出時會自動替換所引用的註釋模塊,定義之後您可以通過@apiUse來引入所定義的註釋模塊。(注:可以同時使用@apiVersion來定義註釋模塊的版本)
參數列表:

參數 必填 描述
name yes 註釋模塊名稱(唯一),不同@apiVersion可以定義相同名稱的註釋模塊
title no 註釋模塊標題
description no 註釋模塊詳細描述(詳細描述另起一行,可包含多行)

例:

/**
 * @apiDefine MyError
 * @apiError UserNotFound The <code>id</code> of the User was not found.
 */

/**
 * @api {get} /user/:id
 * @apiUse MyError
 */
/**
 * @apiDefine admin User access only
 * This optional description belong to to the group admin.
 */

/**
 * @api {get} /user/:id
 * @apiPermission admin
 */

3.3 @apiDeprecated

@apiDeprecated [text]

標註一個接口已經被棄用
參數列表:

參數 必填 描述
text yes 多行文字描述

例:

**
 * @apiDeprecated
 */

/**
 * @apiDeprecated use now (#Group:Name).
 *
 * Example: to set a link to the GetDetails method of your group User
 * write (#User:GetDetails)
 */

3.4 @apiDescription

@apiDescription text

api接口的詳細描述
參數列表:

參數 必填 描述
text yes 多行文字描述
/**
 * @apiDescription This is the Description.
 * It is multiline capable.
 *
 * Last line of Description.
 */

3.5 @apiError

@apiError [(group)] [{type}] field [description]

錯誤返回參數
參數列表:

參數 必填 描述
(group) no 所有的參數都會通過這個參數進行分組,如果未設置,默認值爲Error 4xx
{type} no 返回類型(例如:{Boolean}, {Number}, {String}, {Object}, {String[]})
field yes 返回id
description no 參數描述

例:

/**
 * @api {get} /user/:id
 * @apiError UserNotFound The <code>id</code> of the User was not found.
 */

3.6 @apiErrorExample

@apiErrorExample [{type}] [title]
                 example

接口錯誤返回示例(格式化輸出)
參數列表:

參數 必填 描述
type no 響應類型
title no 示例標題
example yes 示例詳情(兼容多行)

例:

/**
 * @api {get} /user/:id
 * @apiErrorExample {json} Error-Response:
 *     HTTP/1.1 404 Not Found
 *     {
 *       "error": "UserNotFound"
 *     }
 */

3.7 @apiExample

@apiExample [{type}] title
            example

接口方式請求示例
參數列表:

參數 必填 描述
type no 請求內容格式
title yes 示例標題
example yes 示例詳情(兼容多行)
/**
 * @api {get} /user/:id
 * @apiExample {curl} Example usage:
 *     curl -i http://localhost/user/4711
 */

3.8 @apiGroup

@apiGroup name

定義接口所屬的接口組,雖然接口定義裏不需要這個參數,但是您應該在每個接口註釋裏都添加這個參數,因爲導出的接口文檔會以接口組的形式導航展示。
參數列表:

參數 必填 描述
name yes 接口組名稱(用於導航,不支持中文)

例:

/**
 * @api {get} /user/:id
 * @apiGroup User
 */

3.9 @apiHeader

@apiHeader [(group)] [{type}] [field=defaultValue] [description]

描述接口請求頭部需要的參數(功能類似@apiParam)
參數列表:

參數 必填 描述
(group) no 所有的參數都會以該參數值進行分組(默認Parameter)
{type} no 返回類型(例如:{Boolean}, {Number}, {String}, {Object}, {String[]})
field yes 參數名稱(定義該頭部參數爲必填)
[field] yes 參數名稱(定義該頭部參數爲可選)
=defaultValue no 參數默認值
description no 參數描述

例:

/**
 * @api {get} /user/:id
 * @apiHeader {String} access-key Users unique access-key.
 */

3.10 @apiHeaderExample

@apiHeaderExample [{type}] [title]
                   example

請求頭部參數示例
參數列表:

參數 必填 描述
type no 請求內容格式
title no 請求示例標題
example yes 請求示例詳情(兼容多行)

例:

/**
 * @api {get} /user/:id
 * @apiHeaderExample {json} Header-Example:
 *     {
 *       "Accept-Encoding": "Accept-Encoding: gzip, deflate"
 *     }
 */

3.11 @apiIgnore

@apiIgnore [hint]

如果你需要使用該參數,請把它放到註釋塊的最前面。如果設置了該參數,那麼該註釋模塊將不會被解析(當有些接口還未完成或未投入使用時,可以使用該字段)
參數列表:

參數 必填 描述
hint no 描接口忽略原因描述

例:

/**
 * @apiIgnore Not finished Method
 * @api {get} /user/:id
 */

3.12 @apiName

@apiName name

接口名稱,每一個接口註釋裏都應該添加該字段,在導出的接口文檔裏會已該字段值作爲導航子標題,如果兩個接口的@apiVersion@apiName一樣,那麼有一個接口的註釋將會被覆蓋(接口文檔裏不會展示)
參數列表:

參數 必填 描述
name yes 接口名稱(相同接口版本下所有接口名稱應該是唯一的)

例:

/**
 * @api {get} /user/:id
 * @apiName GetUser
 */

3.13 @apiParam

@apiParam [(group)] [{type}] [field=defaultValue] [description]

接口請求體參數
參數列表:

參數 必填 描述
(group) no 所有的參數都會以該參數值進行分組(默認Parameter)
{type} no 返回類型(例如:{Boolean}, {Number}, {String}, {Object}, {String[]})
{type{size}} no 返回類型,同時定義參數的範圍
{string{..5}}意爲字符串長度不超過5
{string{2..5}}意爲字符串長度介於25之間<br/>`{number{100-999}}`意爲數值介於100999之間
{type=allowedValues} no 參數可選值
{string="small"}意爲字符串僅允許值爲"small"
{string="small","huge"}意爲字符串允許值爲"small"、"huge"
{number=1,2,3,99}意爲數值允許值爲1、2、3、99
{string {..5}="small","huge"意爲字符串最大長度爲5並且值允許爲:"small"、"huge"
field yes 參數名稱(定義該請求體參數爲必填)
[field] yes 參數名稱(定義該請求體參數爲可選)
=defaultValue no 參數默認值
description no 參數描述

例:

/**
 * @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.
 */

3.14 @apiParamExample

@apiParamExample [{type}] [title]
                   example

請求體參數示例
參數列表:

參數 必填 描述
type no 請求內容格式
title no 請求示例標題
example yes 請求示例詳情(兼容多行)

例:

/**
 * @api {get} /user/:id
 * @apiParamExample {json} Request-Example:
 *     {
 *       "id": 4711
 *     }
 */

3.15 @apiPermission

允許訪問該接口的角色名稱

@apiPermission name

參數列表:

參數 必填 描述
name yes 允許訪問的角色名稱(唯一)

例:

/**
 * @api {get} /user/:id
 * @apiPermission none
 */

3.16 @apiPrivate

@apiPrivate

定義私有接口,對於定義爲私有的接口,可以在生成接口文檔的時候,通過在命令行中設置參數 --private false|true來決定導出的文檔中是否包含私有接口
例:

/**
 * @api {get} /user/:id
 * @apiPrivate
 */

3.17 @apiSampleRequest

@apiSampleRequest url

設置了該參數後,導出的html接口文檔中會包含模擬接口請求的form表單;如果在配置文件apidoc.json中設置了參數sampleUrl,那麼導出的文檔中每一個接口都會包含模擬接口請求的form表單,如果既設置了sampleUrl參數,同時也不希望當前這個接口不包含模擬接口請求的form表單,可以使用@apiSampleRequest off來關閉。
參數列表:

參數 必填 描述
url yes 模擬接口請求的url
@apiSampleRequest http://www.example.com意爲覆蓋apidoc.json中的sampleUrl參數
@apiSampleRequest off意爲關閉接口測試功能

例:
發送測試請求到:http://api.github.com/user/:id

Configuration parameter sampleUrl: "http://api.github.com"
/**
 * @api {get} /user/:id
 */

發送測試請求到:http://test.github.com/some_path/user/:id(覆蓋apidoc.json中的sampleUrl參數)

Configuration parameter sampleUrl: "http://api.github.com"
/**
 * @api {get} /user/:id
 * @apiSampleRequest http://test.github.com/some_path/
 */

關閉接口測試功能

Configuration parameter sampleUrl: "http://api.github.com"
/**
 * @api {get} /user/:id
 * @apiSampleRequest off
 */

發送測試請求到http://api.github.com/some_path/user/:id(由於沒有設置apidoc.json中的sampleUrl參數,所以只有當前接口有模擬測試功能)

Configuration parameter sampleUrl is not set
/**
 * @api {get} /user/:id
 * @apiSampleRequest http://api.github.com/some_path/
 */

3.18 @apiSuccess

@apiSuccess [(group)] [{type}] field [description]

接口成功返回參數
參數列表:

參數 必填 描述
(group) no 所有的參數都會以該參數值進行分組,默認值:Success 200
{type} no 返回類型(例如:{Boolean}, {Number}, {String}, {Object}, {String[]})
field yes 返回值(返回成功碼)
=defaultValue no 參數默認值
description no 參數描述

例:

/**
 * @api {get} /user/:id
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */

包含(group):

/**
 * @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.
 */

返回參數中有數組:

/**
 * @api {get} /users
 * @apiSuccess {Object[]} profiles       List of user profiles.
 * @apiSuccess {Number}   profiles.age   Users age.
 * @apiSuccess {String}   profiles.image Avatar-Image.
 */

3.19 @apiSuccessExample

@apiSuccessExample [{type}] [title]
                   example

返回成功示例
參數列表:

參數 必填 描述
type no 返回內容格式
title no 返回示例標題
example yes 返回示例詳情(兼容多行)

例:

/**
 * @api {get} /user/:id
 * @apiSuccessExample {json} Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "firstname": "John",
 *       "lastname": "Doe"
 *     }
 */

3.20@apiUse

@apiUse name

引入註釋模塊,如果當前模塊定義了@apiVersion,那麼版本相同或版本最近的註釋模塊會被引入
參數列表:

參數 必填 描述
name yes 引入註釋模塊的名稱

例:

/**
 * @apiDefine MySuccess
 * @apiSuccess {string} firstname The users firstname.
 * @apiSuccess {number} age The users age.
 */

/**
 * @api {get} /user/:id
 * @apiUse MySuccess
 */

3.21 @apiVersion

@apiVersion version

定義接口/註釋模塊版本
參數列表:

參數 必填 描述
version yes 版本號(支持APR版本規則:major.minor.patch)

例:

/**
 * @api {get} /user/:id
 * @apiVersion 1.6.2
 */

注:文中如有任何錯誤,請各位批評指正!



作者:liqingbiubiu
鏈接:https://www.jianshu.com/p/9353d5cc1ef8
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯繫作者獲得授權並註明出處。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章