APIDOC個(gè)人實(shí)踐

APIDOC

Inline Docmentation for RESTful web APIs

官網(wǎng)地址:http://apidocjs.com/#getting-started

apiDoc通過源代碼里面的API注解生成一份文檔
/** 
* @api {get} /user/:id Request User information 
* @apiName GetUser 
* @apiGroup User 
* 
* @apiParam {Number} id Users unique ID. 
* 
* @apiSuccess {String} firstname Firstname of the User. 
* @apiSuccess {String} lastname Lastname of the User. 
*/

官方例子: watch Demo output

前言

 文檔中所有的例子都使用Javadoc風(fēng)格(適用于C#,Go,Dart,Java,JavaScript, PHP, TypeScript)
  /** 
    * This is a comment. 
    */

安裝

npm install apidoc -g

運(yùn)行

apidoc -i myproject/ -o mydoc/ -t mytemplate/

通過mytemplate文件夾下的模板文件,把myproject文件夾下的所有文件的注釋內(nèi)容生成api文檔,并放在mydoc文件夾下.

終端命令行

提示幫助

apidoc -h
參數(shù) 描述
-f 通過正則篩選哪些文件需要生成文檔的.(默認(rèn): .cs .dart .erl .go .java .js .php .py .rb .ts )
-i 你的項(xiàng)目文件所在位置
-o 輸出文檔的位置
-t 自定義模板的位置

Grunt 模塊

A separate grunt module is supported, visit github.com/apidoc/grunt-apidocor install via npm:

npm install grunt-apidoc --save-dev

模板

apiDoc 的默認(rèn)模板是使用handlebars, Bootstrap, RequireJSjQuery生成
包含api_data.js 和 api_project.js 的html頁面.

默認(rèn)模板支持, 版本信息 和 版本之間的對比

view default demo

自定義模板

通過使用apiDoc生成的api_data.js和api_project.js,或者使用api_data.json,api_project.json等配置文件自定義模板.
View the source on https://github.com/apidoc/apidoc/tree/master/template

Extend

apiDoc can be extended with your own parameters (if something is not available that you need). Look at lib/parsers/, lib/workers/, and lib/parsers/ directories in the apidoc/apidoc-core project for examples. parser
split the parameter data, worker
processes additional functions with all found data and filter
reduce the data to needed things.
Example usage: apidoc --parse-filters myFilter=pathToMyFilter/myFilter.js

Or fork the whole project and create a pull request to make apiDoc better.

配置

放置在項(xiàng)目根目錄的配置項(xiàng)文件apidoc.json包含了關(guān)于項(xiàng)目的基本信息.例如: 標(biāo)題,簡述, 版本號, 配置項(xiàng)如頭尾設(shè)置.

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

如果你使用package.json,可以在其中添加一個(gè)選項(xiàng)

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

Settins for apidoc.json

name Description
name 項(xiàng)目名稱.如果apidoc.json不存在,apidoc會自動(dòng)尋找package.json里面的配置項(xiàng)
version 項(xiàng)目版本號
description 項(xiàng)目介紹
title 瀏覽器標(biāo)題
url 文檔路徑前綴. 例如:https://api.github/com
sampleUrl 為測試api,提供的測試地址.詳情見@apiSampleRequest
header title 導(dǎo)航欄開頭標(biāo)題, filename 以md形式存儲內(nèi)容.
footer title 導(dǎo)航欄結(jié)尾標(biāo)題, filename 以md形式存儲內(nèi)容.
order 排序api名稱或者組名. 默認(rèn):"order": [ "Error", "Define", "PostTitleAndError", "PostError"]

模板設(shè)置

"template":{
  "forceLanguage":"en", //瀏覽器會根據(jù)本地語言自動(dòng)設(shè)置,不建議配置
  "withCompare":true/false, //默認(rèn)true,比較不同版本的接口
  "withGenerator":true/false, //默認(rèn)true,在footer輸出生成信息,例如創(chuàng)建時(shí)間
  "JQueryAjaxSetup":Object //Set [default values](http://api.jquery.com/jquery.ajaxsetup/) for Ajax requests.
}

Header / Footer

{ 
  "header": { 
    "title": "My own header title", 
    "filename": "header.md" 
  }, 
  "footer": { 
    "title": "My own footer title", 
    "filename": "footer.md" 
  }
}

apiDoc-Params

@api

 @api {method} path [title]
Name Description
method 請求方式 DELETE,GET,POST,PUT,...
path 接口路徑
title 可選, 簡單說明

例子:

/**
 * @api {get} /user/:id Users unique ID.
 */

@apiDefine

  @apiDefine name [title] 
  [description]

類似自定義方法
在每個(gè)接口塊里只用使用一次@apiDefine
通過@apiUse導(dǎo)入到接口塊里

Name Description
name 唯一值,同一個(gè)name值可以在不同的@apiVersion中定義
title 可選, 小標(biāo)題,僅能在@apiPermission和@apiParam(name)等方法中使用
description 可選,方法的詳細(xì)說明不能與name同行,需要新起一行,可以換行.只能使用在@apiPermission中

例子:

 /**
  * @apiDefine MyError
  * @apiError UserNotFound The <code>id</code> of the User was not found.
  */
/**
  * @apiDefine admin User access only
  * This optional description belong to to the group admin.
  */

  /**
  * @api {get} /user/:id
  * @apiPermission admin
  */
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,545評論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,268評論 6 342
  • 原文地址:RESTful web API文檔生成器 問:開發(fā)業(yè)務(wù)模塊代碼最重要的是什么?答:API接口文檔 如果你...
    brucewar閱讀 5,025評論 0 51
  • afinalAfinal是一個(gè)android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,861評論 2 45
  • 雷雨天的沼澤 映射著蒼天憤怒的黯淡 我是濁惡的沼澤 你是威懾的蒼天 我只待暴風(fēng)雨的到來
    茅矛予閱讀 319評論 0 0

友情鏈接更多精彩內(nèi)容