mcpField
A
api-doc-generator

api-doc-generator

github.com/wu529778790Development4550 installsUpdated 5w ago
Repository

About api-doc-generator

api-doc-generator scans project routes and endpoint definitions to produce OpenAPI 3.0 documentation. It is for developers documenting REST or GraphQL endpoints, and can also produce an interactive documentation page.

Key features

  1. 01Detect frameworks and scan routes
  2. 02Extract parameters, responses, and comments
  3. 03Generate OpenAPI 3.0 YAML
  4. 04Output YAML or JSON
  5. 05Create an interactive documentation page

Use cases

  1. 01Document existing HTTP endpoints
  2. 02Convert code routes into an OpenAPI specification
  3. 03Preview interactive API documentation

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/wu529778790/shenzjd-skills --skill api-doc-generator

After installing, type/api-doc-generatorin Claude Code to confirm it loaded.

Bundled filesREADME.mdtemplates/openapi.yaml

Details

From the project README

API Doc Generator

从代码自动生成 API 文档,支持 OpenAPI/Swagger 规范。

Overview

扫描项目代码中的路由和接口定义,自动提取参数、返回值、注释,生成符合 OpenAPI 3.0 规范的 YAML 文件和可交互的文档页面。

When to Use

  • User wants to generate API documentation
  • User mentions OpenAPI, Swagger, or API docs
  • User has REST/GraphQL endpoints that need documentation
  • User says "生成 API 文档" / "create API docs"
  • User inputs /api-doc-generator

When NOT to Use:

  • User only wants to write API descriptions in README
  • User wants to test APIs (use testing tools)
  • User's project has no HTTP endpoints
  • User wants to generate SDK/client code
  • User wants to mock APIs (use Mockoon, WireMock)

Core Pattern

Step 1: 检测框架和路由
框架检测方式路由提取
Express/Koa/Fastifyrequire('express') / 路由文件扫描 app.get/post/put/delete
Next.js API Routesapp/api/ 目录扫描 route.ts 文件
Gin (Go)r.GET/POST扫描 router 注册
Flask/FastAPI (Python)@app.route / @router扫描装饰器
Spring Boot (Java)@GetMapping / @RestController扫描注解
# 快速检测
grep -rE "app\.(get|post|put|delete|patch)" --include="*.ts" --include="*.js" --include="*.go" --include="*.py" -l
Step 2: 提取接口信息

对每个路由提取:

  • HTTP Method + Path
  • 请求参数:query、params、body(从类型定义/JSDoc/注解推断)
  • 响应格式:从 return 语句或类型定义推断
  • 注释/描述:从代码注释提取
  • 认证要求:检查是否有 auth middleware
Step 3: 生成 OpenAPI YAML

使用 templates/openapi.yaml(Handlebars 模板,含 {{variable}} 与 {{#each}} 占位符)作为基础。将提取的接口信息注入对应变量,渲染后输出 openapi.yaml。渲染结果大致如下:

openapi: 3.0.3
info:
  title: 项目名 API
  version: 1.0.0
paths:
  /api/users:
    get:
      summary: 获取用户列表
      parameters: [...]
      responses:
        '200':
          description: 成功
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/User'
Step 4: 生成交互式文档页面(可选)

使用 Swagger UI 或 Redoc 生成可浏览的 HTML 页面:

# 使用 npx 快速预览
npx @redocly/cli preview-docs openapi.yaml

Quick Reference

/api-doc-generator                    # 扫描当前项目,生成 openapi.yaml
/api-doc-generator --format yaml      # 输出 YAML(默认)
/api-doc-generator --format json      # 输出 JSON
/api-doc-generator --preview          # 生成并预览交互式文档
参数说明默认值
--format输出格式 yaml / jsonyaml
--preview生成交互式 HTML 文档false

Common Mistakes

错误正确做法原因
只扫描一个文件递归扫描所有路由文件可能遗漏接口
硬编码请求体结构从类型定义推断保持文档与代码一致
不处理嵌套路由分析路由注册的完整路径/api/users/:id 的 :id 需要标注
忽略错误响应添加 4xx/5xx 响应完整的 API 文档需要错误说明
忽略认证要求检查 auth middleware客户端需要知道哪些接口需要认证

Comments

Markdown supported