惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

I
InfoQ
Spread Privacy
Spread Privacy
GbyAI
GbyAI
F
Fortinet All Blogs
小众软件
小众软件
B
Blog RSS Feed
博客园_首页
量子位
Y
Y Combinator Blog
美团技术团队
H
Hackread – Cybersecurity News, Data Breaches, AI and More
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
大猫的无限游戏
大猫的无限游戏
Jina AI
Jina AI
T
The Blog of Author Tim Ferriss
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Vercel News
Vercel News
Last Week in AI
Last Week in AI
F
Full Disclosure
Stack Overflow Blog
Stack Overflow Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
宝玉的分享
宝玉的分享
Microsoft Azure Blog
Microsoft Azure Blog
有赞技术团队
有赞技术团队
A
About on SuperTechFans
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
The Cloudflare Blog
Hugging Face - Blog
Hugging Face - Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Tenable Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Project Zero
Project Zero
C
CXSECURITY Database RSS Feed - CXSecurity.com
Engineering at Meta
Engineering at Meta
博客园 - 叶小钗
S
SegmentFault 最新的问题
T
Threat Research - Cisco Blogs
博客园 - 司徒正美
MyScale Blog
MyScale Blog
云风的 BLOG
云风的 BLOG
V
V2EX
酷 壳 – CoolShell
酷 壳 – CoolShell
The GitHub Blog
The GitHub Blog
V
Vulnerabilities – Threatpost
S
Schneier on Security
Latest news
Latest news
I
Intezer
A
Arctic Wolf
T
Threatpost

PixelNest

会议是效率的杀手 国庆出游记录 对居家办公的一些想法 2022 春节 《搏击俱乐部》的结局 纪念 PAI 头一回达到 100 卡片笔记法 《刑法学讲义》的部分摘录 迁移博客 《UNIX 传奇》摘录 就地过年 一点记录:docker-compose 编排不同项目间的网络 近期读的几本书 办新身份证 git 获取其他分支的单个文件或目录 我的高考成绩 推荐 2020 年上半年的 8 部影视作品 ChromeOS 开发者模式 终于下单了《守望者》 联通来电管家服务 一些实用的 bash 代码片段 关于 鸟枪换炮 《架构整洁之道》摘录 惨淡的星球大战 旧笔记本电脑 if...then... 命令行检测 SSL 证书过期时间 《程序员的职业素养》读书笔记 2019 国庆假期 在 Ubuntu 里按空格键预览 《基本穿搭》笔记 我在 Ubuntu 里安装的一些软件 让人郁闷的英语学习过程 色彩理论基础 向百度主动推送网站链接 两部电视剧 《历史的教训》摘录 新工具 Koa 获取客户端 IP 备案了 《代码整洁之道》摘录 BBC 纪录片 《逻辑的乐趣》 读不下去的《原则》 蓝牙鼠标唤醒休眠的 Ubuntu Google 发布会 2018 国庆假期 开发者的时间追踪和统计 使用 curl 发送 POST 请求的几种方式 《重来 2》读书笔记 Linux 和 MacOS 命令行转换图片用法整理 《Go 语言实战》笔记 Win10 微软拼音添加小鹤双拼以及其他配置 针对不同场景临时禁用 eslint 规则的方法 《好妈妈胜过好老师》读书笔记 配置 Win10 Linux 子系统作为开发环境 又读完一本尤·奈斯博的小说 近期写 Go 的一点语法碎片整理 ffmpeg 合并 MP4 M4A 文件 睡前看手机的大问题 《局外人》读书笔记 菊次郎与佐纪 读书笔记 3 菊次郎与佐纪 读书笔记 2 菊次郎与佐纪 读书笔记 1 豆瓣图片服务器防盗链 《如何有效阅读一本书》读书笔记 - 3 《如何有效阅读一本书》读书笔记 - 2 《如何有效阅读一本书》读书笔记 - 1 Vim iskeyword 济南 图个乐 开始看不懂《西部世界》了 剧透 听到几个好故事 过周末 相亲 让人失望的 Apple 悬疑故事 AI 记忆 世纪三部曲 上帝的梦
apiDoc 基础语法
2019-02-26 · via PixelNest

apiDoc 可以通过文本生成体验良好的 API 文档页面,这些文本可以以注释的形式放在代码里,apiDoc 读取源码注释,就可以生成页面了,当然也可以与代码分开,写一些全是注释的文档,作为 apiDoc 生成文档页面的「源文件」。

整理一下 apiDoc 「源文件」内容写法的“最小必要知识”。其余内容,查文档。

基础

用一个「文档块」来描述一个 API,这个「文档块」也是实现 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.
 *
 * @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"
 *     }
 */

这个例子描述了一个获取用户信息的 GET 方法。

  • @api {get} /user/:id Request User information 这一行内容是必需的,表示了这个 API 的 HTTP 方法(method),路径(path,这个例子中的路径带了一个参数),Request User information 是这个 API 的 title
  • @apiName 必须是唯一的,最好总是加上这个字段。
  • @apiGroup 最好也总是加上,用于把有关联的 API 放到一个组。
  • 其他字段比如 @apiError, @apiSuccess 等是可选的。

继承

可以通过「继承」复用「文档块」

/**
 * @apiDefine UserNotFoundError
 *
 * @apiError UserNotFound The id of the User was not found.
 *
 * @apiErrorExample Error-Response:
 *     HTTP/1.1 404 Not Found
 *     {
 *       "error": "UserNotFound"
 *     }
 */

/**
 * @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.
 *
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *       "firstname": "John",
 *       "lastname": "Doe"
 *     }
 *
 * @apiUse UserNotFoundError
 */

/**
 * @api {put} /user/ Modify User information
 * @apiName PutUser
 * @apiGroup User
 *
 * @apiParam {Number} id          Users unique ID.
 * @apiParam {String} [firstname] Firstname of the User.
 * @apiParam {String} [lastname]  Lastname of the User.
 *
 * @apiSuccessExample Success-Response:
 *     HTTP/1.1 200 OK
 *
 * @apiUse UserNotFoundError
 */

上面的例子用 @apiDefine 定义了一个可复用的文档块 UserNotFoundError,然后在 GetUserPutUser 两个 API 文档块中通过 @apiUse UserNotFoundError 复用文档块。

注意,只能继承一层。


下面把 apiParams 专门挑出来。

apiParams

apiParams 用来描述 API 的参数,它并没有提供什么手段去区分是路由参数、URL 查询参数还是在 HTTP BODY 里的参数。

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

除了 field,其余内容都是可选的。如果写 @apiParam 时,给 field 加了一对中括号,这样是指定了这个 field 是个可选参数。

group 是给参数分组,type 指定参数的数据类型。

例子:

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

@apiParamExample 用来描述参数样例

@apiParamExample [{type}] [title]
                   example

typetitle 都是可选的。

例子:

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

一个完整的例子: