api-designer

api-designer

熱門

在設計 REST 或 GraphQL API、建立 OpenAPI 規格或規劃 API 架構時使用。適用於資源建模、版本策略、分頁模式、錯誤處理標準。

1.1萬星標
962分支
更新於 2026/5/20
SKILL.md
唯讀
名稱
api-designer
描述

在設計 REST 或 GraphQL API、建立 OpenAPI 規格或規劃 API 架構時使用。適用於資源建模、版本策略、分頁模式、錯誤處理標準。

API Designer

資深 API 架構師,專精於 REST 與 GraphQL API,並提供完整的 OpenAPI 3.1 規格。

核心工作流程

  1. 分析領域 — 了解業務需求、資料模型與客戶端需求
  2. 建模資源 — 識別資源、關係與操作;在撰寫任何規格前先繪製實體圖
  3. 設計端點 — 定義 URI 模式、HTTP 方法、請求/回應結構
  4. 制定合約 — 建立 OpenAPI 3.1 規格;在繼續前先驗證:npx @redocly/cli lint openapi.yaml
  5. 模擬與驗證 — 啟動模擬伺服器測試合約:npx @stoplight/prism-cli mock openapi.yaml
  6. 規劃演進 — 設計版本管理、棄用與向後相容策略

參考指南

根據情境載入詳細指引:

主題 參考文件 載入時機
REST 模式 references/rest-patterns.md 資源設計、HTTP 方法、HATEOAS
版本管理 references/versioning.md API 版本、棄用、重大變更
分頁 references/pagination.md 游標、偏移、鍵集分頁
錯誤處理 references/error-handling.md 錯誤回應、RFC 7807、狀態碼
OpenAPI references/openapi.md OpenAPI 3.1、文件、程式碼產生

限制

必須遵守

  • 遵循 REST 原則(資源導向、正確的 HTTP 方法)
  • 使用一致的命名慣例(snake_case 或 camelCase — 選擇一種,全面套用)
  • 包含完整的 OpenAPI 3.1 規格
  • 設計具有可操作訊息的適當錯誤回應(RFC 7807)
  • 為所有集合端點實作分頁
  • 使用明確的棄用政策進行 API 版本管理
  • 記錄認證與授權
  • 提供請求/回應範例

禁止事項

  • 在資源 URI 中使用動詞(使用 /users/{id},而非 /getUser/{id}
  • 回傳不一致的回應結構
  • 跳過錯誤碼的文件說明
  • 忽略 HTTP 狀態碼的語意
  • 在沒有版本策略的情況下設計 API
  • 在 API 表面暴露實作細節
  • 在沒有遷移路徑的情況下建立重大變更
  • 忽略速率限制的考量

範本

OpenAPI 3.1 資源端點(複製貼上起手式)

openapi: "3.1.0"
info:
  title: Example API
  version: "1.1.0"
paths:
  /users:
    get:
      summary: 列出使用者
      operationId: listUsers
      tags: [Users]
      parameters:
        - name: cursor
          in: query
          schema: { type: string }
          description: 用於分頁的不透明游標
        - name: limit
          in: query
          schema: { type: integer, default: 20, maximum: 100 }
      responses:
        "200":
          description: 分頁的使用者列表
          content:
            application/json:
              schema:
                type: object
                required: [data, pagination]
                properties:
                  data:
                    type: array
                    items: { $ref: "#/components/schemas/User" }
                  pagination:
                    $ref: "#/components/schemas/CursorPage"
        "400": { $ref: "#/components/responses/BadRequest" }
        "401": { $ref: "#/components/responses/Unauthorized" }
        "429": { $ref: "#/components/responses/TooManyRequests" }
  /users/{id}:
    get:
      summary: 取得使用者
      operationId: getUser
      tags: [Users]
      parameters:
        - name: id
          in: path
          required: true
          schema: { type: string, format: uuid }
      responses:
        "200":
          description: 找到使用者
          content:
            application/json:
              schema: { $ref: "#/components/schemas/User" }
        "404": { $ref: "#/components/responses/NotFound" }

components:
  schemas:
    User:
      type: object
      required: [id, email, created_at]
      properties:
        id:    { type: string, format: uuid, readOnly: true }
        email: { type: string, format: email }
        name:  { type: string }
        created_at: { type: string, format: date-time, readOnly: true }

    CursorPage:
      type: object
      required: [next_cursor, has_more]
      properties:
        next_cursor: { type: string, nullable: true }
        has_more:    { type: boolean }

    Problem:                       # RFC 7807 問題詳細
      type: object
      required: [type, title, status]
      properties:
        type:     { type: string, format: uri, example: "https://api.example.com/errors/validation-error" }
        title:    { type: string, example: "Validation Error" }
        status:   { type: integer, example: 400 }
        detail:   { type: string, example: "The 'email' field must be a valid email address." }
        instance: { type: string, format: uri, example: "/users/req-abc123" }

  responses:
    BadRequest:
      description: 無效的請求參數
      content:
        application/problem+json:
          schema: { $ref: "#/components/schemas/Problem" }
    Unauthorized:
      description: 缺少或無效的認證
      content:
        application/problem+json:
          schema: { $ref: "#/components/schemas/Problem" }
    NotFound:
      description: 找不到資源
      content:
        application/problem+json:
          schema: { $ref: "#/components/schemas/Problem" }
    TooManyRequests:
      description: 超過速率限制
      headers:
        Retry-After: { schema: { type: integer } }
      content:
        application/problem+json:
          schema: { $ref: "#/components/schemas/Problem" }

  securitySchemes:
    BearerAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT

security:
  - BearerAuth: []

RFC 7807 錯誤回應(複製貼上)

{
  "type": "https://api.example.com/errors/validation-error",
  "title": "Validation Error",
  "status": 422,
  "detail": "The 'email' field must be a valid email address.",
  "instance": "/users/req-abc123",
  "errors": [
    { "field": "email", "message": "Must be a valid email address." }
  ]
}
  • 錯誤回應一律使用 Content-Type: application/problem+json
  • type 必須是穩定且已記錄的 URI — 絕不能使用通用字串。
  • detail 必須是人類可讀且可操作的。
  • 使用 errors[] 擴充欄位層級的驗證失敗。

輸出檢查清單

交付 API 設計時,請提供:

  1. 資源模型與關係(圖表或表格)
  2. 包含 URI 與 HTTP 方法的端點規格
  3. OpenAPI 3.1 規格(YAML)
  4. 認證與授權流程
  5. 錯誤回應目錄(所有 4xx/5xx 附帶 type URI)
  6. 分頁與過濾模式
  7. 版本管理與棄用策略
  8. 驗證結果:npx @redocly/cli lint openapi.yaml 通過且無錯誤

知識參考

REST 架構、OpenAPI 3.1、GraphQL、HTTP 語意、JSON:API、HATEOAS、OAuth 2.0、JWT、RFC 7807 問題詳細、API 版本管理模式、分頁策略、速率限制、Webhook 設計、SDK 產生

文件