xiaohongshu
xiaohongshu
api-builder
Generate complete FastAPI backend scaffolds from OpenAPI 3.x specifications. Automatically creates SQLAlchemy models, Pydantic schemas, FastAPI routers, CRUD operations, database migrations, pytest tests, and Next.js TypeScript clients. Use when user provides an OpenAPI/OpenSpec file (.yaml/.json...
Full skill instructions
从 OpenAPI 3.x 规范自动生成完整的 FastAPI 后端脚手架,包括数据库模型、API 路由、测试和 Next.js client。
获取 OpenAPI Spec
验证 Spec 格式
scripts/validate_spec.py:
python3 ${CLAUDE_PLUGIN_ROOT}/scripts/validate_spec.py <spec-file>
如果验证失败:停止并告知用户具体错误,询问是否修正
选择生成范围
scripts/parse_openapi.py 查看可用 endpoints:
python3 ${CLAUDE_PLUGIN_ROOT}/scripts/parse_openapi.py <spec-file>
--resources 参数)检查是否为现有项目
app/ 或 src/ 目录pyproject.tomldatabase.py、routers/、models/ 等确定集成策略
database.pyapp/routers/users.py 已存在)assets/fastapi-template/ 复制基础结构使用 CLI 脚本(推荐):
运行完整的生成流程:
cd ${CLAUDE_PLUGIN_ROOT}/scripts
python3 cli.py \
--spec <spec-file> \
--output <output-dir> \
[--resources users,posts] # 可选:只生成特定资源
生成内容:
models/ - SQLAlchemy 数据库模型schemas/ - Pydantic API schemasrouters/ - FastAPI 路由__init__.py - 包初始化文件parsed.json - 解析后的 spec 数据关键点:
references/type-mapping.md)assets/templates/)生成代码手动步骤执行(高级):
如果需要更细粒度控制,可分步执行:
cd ${CLAUDE_PLUGIN_ROOT}/scripts
# 1. 解析 spec
python3 parse_openapi.py <spec-file> --output parsed.json
# 2. 生成代码
python3 generate_code.py \
--parsed-data parsed.json \
--output-dir <output-dir> \
--templates-dir ${CLAUDE_PLUGIN_ROOT}/assets/templates
参考文档:
references/type-mapping.md - 类型映射规则references/fastapi-patterns.md - FastAPI 最佳实践如果不存在 app/main.py,创建它:
assets/fastapi-template/app/main.py 复制基础结构如果已存在,询问用户是否添加新的 router 注册代码。
使用 CLI(推荐):
# 自动配置并运行迁移
cd ${CLAUDE_PLUGIN_ROOT}/scripts
python3 cli.py \
--spec <spec-file> \
--output <output-dir> \
--setup-alembic \
--run-migrations
手动执行:
如果用户想手动控制:
# 1. 配置 Alembic
cd ${CLAUDE_PLUGIN_ROOT}/scripts
python3 cli.py --spec <spec-file> --output <output-dir> --setup-alembic
# 2. 进入项目目录手动运行迁移
cd <output-dir>
alembic revision --autogenerate -m "Initial tables"
alembic upgrade head
生成内容:
alembic.ini - Alembic 配置文件alembic/env.py - 迁移环境配置alembic/versions/ - 迁移文件目录alembic/script.py.mako - 迁移模板何时使用迁移:
--setup-alembic --run-migrations 立即创建数据库alembic revision --autogeneratetests/conftest.py 是否存在assets/fastapi-template/tests/conftest.py 复制sample_{resource}_datacreate_sample_{resource}assets/templates/conftest_fixture.py.j2 模板assets/templates/test_api.py.j2 模板tests/test_api/test_{resource}_api.py参考:references/testing-patterns.md
assets/templates/test_crud.py.j2 模板tests/test_crud/test_{resource}_crud.py询问用户是否需要生成 Next.js TypeScript client。
如果需要:
使用 CLI(推荐):
cd ${CLAUDE_PLUGIN_ROOT}/scripts
python3 cli.py \
--spec <spec-file> \
--output <backend-output-dir> \
--generate-client \
[--client-output <frontend-path>/api-client.ts]
单独生成Client:
cd ${CLAUDE_PLUGIN_ROOT}/scripts
python3 generate_client.py \
--parsed-data parsed.json \
--output <client-output-file>
生成内容:
参考:references/type-mapping.md 的 TypeScript 映射
生成的client使用示例:
// Next.js App Router
import { apiClient } from '@/lib/api/api-client'
export default async function Page() {
const users = await apiClient.list_users()
return <div>{users.map(u => <div>{u.name}</div>)}</div>
}
安装依赖:
uv add fastapi sqlalchemy alembic pydantic[email] python-multipart
uv add --dev pytest pytest-cov httpx
运行类型检查:
mypy app/
如果有错误,修复后重新检查。
运行迁移:
alembic upgrade head
运行测试:
pytest tests/ -v
目标:所有测试通过
手动验证:
uvicorn app.main:app --reload生成总结文档:
TODO 清单:
.env 文件)详细的 OpenAPI 到 Python/TypeScript 类型映射见:references/type-mapping.md
快速参考:
string → str / stringinteger → int / numberstring(email) → EmailStr / stringstring(date-time) → datetime / stringarray → List[T] / Array<T>enum → Enum / enum 或 union type生成的项目结构(模块化):
project/
├── app/
│ ├── main.py # FastAPI 应用入口
│ ├── database.py # 数据库配置
│ ├── models/ # SQLAlchemy 模型
│ │ ├── __init__.py
│ │ └── {resource}.py
│ ├── schemas/ # Pydantic schemas
│ │ ├── __init__.py
│ │ └── {resource}.py
│ ├── routers/ # API 路由
│ │ ├── __init__.py
│ │ └── {resource}.py
│ └── crud/ # CRUD 操作
│ ├── __init__.py
│ ├── base.py
│ └── {resource}.py
├── alembic/ # 数据库迁移
│ ├── versions/
│ └── env.py
├── tests/ # 测试
│ ├── conftest.py
│ ├── test_api/
│ └── test_crud/
├── .env.example # 环境变量示例
├── alembic.ini # Alembic 配置
└── pyproject.toml # 项目配置
详见:references/project-structure.md
详细的 FastAPI 最佳实践见:references/fastapi-patterns.md
关键模式:
Depends(get_db))详细的测试模式见:references/testing-patterns.md
测试结构:
conftest.py - 共享 fixtures(DB session, test client)test_api/ - API 集成测试test_crud/ - CRUD 单元测试运行测试:
pytest tests/ -v --cov=app
症状:YAML 解析错误、缺少必需字段
处理:
症状:要生成的文件已存在
处理:
症状:OpenAPI 类型无法映射到 Python
处理:
Any)症状:pytest 报错
处理:
用户:"我有一个 petstore.yaml OpenAPI spec,帮我生成完整的 FastAPI 后端"
执行:
用户:"根据这个 spec(粘贴内容)生成 API,集成到现有项目"
执行:
用户:"从 spec 里只生成 /users 和 /posts 相关的 API"
执行:
.env 文件xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience