AgentSkillsCN

fastapi-routing

以高水准的设计质量打造独具特色的生产级前端界面。当用户提出构建网页组件、页面、作品、海报,或开发各类应用(例如网站、着陆页、仪表盘、React组件、HTML/CSS布局,或任何需要美化与优化的Web UI时),此技能都能助您创作出富有创意、精致优雅的代码与UI设计,彻底摆脱千篇一律的AI审美桎梏。

SKILL.md
--- frontmatter
name: fastapi-routing
description: Guidance on defining routes, path parameters, query parameters, and HTTP methods in FastAPI. Activate for questions about endpoints, URLs, or handling requests.

Instructions for FastAPI Routing

Handle FastAPI routing as follows:

  1. Path Operations:

    • Use decorators: @app.get("/items/{item_id}"), @app.post("/items/"), etc.
    • Support GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD, TRACE.
  2. Path Parameters:

    • Define in path: {item_id}.
    • Type hint: def get_item(item_id: int): ....
    • Validation: Automatic type conversion and error handling.
  3. Query Parameters:

    • Not in path: def list_items(skip: int = 0, limit: int = 10): ....
    • Optional: Use = None or Query(None) for advanced.
  4. Request Body:

    • Use Pydantic models: from pydantic import BaseModel.
    • def create_item(item: Item): ... where Item is a BaseModel.
  5. Advanced Routing:

    • Path converters: {item_id:uuid}.
    • Sub-applications: app.include_router(router, prefix="/api/v1").
  6. Best Practices:

    • Keep routes organized in routers.
    • Handle status codes: return {"item": item}, status_code=201.
    • Use enums for fixed values.

References

Use the shared references located at: ../_shared/reference.md