fastapi-router-py
microsoft/skills
依照既定模式,建立具備 CRUD 操作、身份驗證依賴項及適當回應模型的 FastAPI 路由器。
...展開全部FastAPI 路由器
依照既定模式建立 FastAPI 路由器,並包含適當的身份驗證、回應模型及 HTTP 狀態碼。
快速入門
從 assets/template.py 複製範本,並替換佔位符:
{{ResourceName}}→ PascalCase 名稱(例如,Project){{resource_name}}→ 蛇形命名法名稱(例如,project){{resource_plural}}→ 複數形式(例如:projects)
驗證模式
# Optional auth - returns None if not authenticated
current_user: Optional[User] = Depends(get_current_user)
# Required auth - raises 401 if not authenticated
current_user: User = Depends(get_current_user_required)
回應模型
@router.get("/items/{item_id}", response_model=Item)
async def get_item(item_id: str) -> Item:
...
@router.get("/items", response_model=list[Item])
async def list_items() -> list[Item]:
...
HTTP 狀態碼
@router.post("/items", status_code=status.HTTP_201_CREATED)
@router.delete("/items/{id}", status_code=status.HTTP_204_NO_CONTENT)
整合步驟
- 在
src/backend/app/routers/ - 掛載至
src/backend/app/main.py - 建立對應的 Pydantic 模型
- 如有需要,建立服務層
- 新增前端 API 函式
最佳實務
- 根據是否呼叫非同步 I/O,針對每個端點選擇
def或async def;請勿在同一個處理常式中混合使用同步與非同步阻塞呼叫。 - 在
lifespan中管理長壽命資源(資料庫池、HTTP 客戶端),並透過Depends進行注入;針對每個請求的資源,請使用with/async with來處理每請求的資源。
---
name: fastapi-router-py
description: Create FastAPI routers with CRUD operations, authentication dependencies, and proper response models following established patterns.
license: MIT
---
# FastAPI Router
Create FastAPI routers following established patterns with proper authentication, response models, and HTTP status codes.
## Quick Start
Copy the template from [assets/template.py](assets/template.py) and replace placeholders:
- `{{ResourceName}}` → PascalCase name (e.g., `Project`)
- `{{resource_name}}` → snake_case name (e.g., `project`)
- `{{resource_plural}}` → plural form (e.g., `projects`)
## Authentication Patterns
```python
# Optional auth - returns None if not authenticated
current_user: Optional[User] = Depends(get_current_user)
# Required auth - raises 401 if not authenticated
current_user: User = Depends(get_current_user_required)
```
## Response Models
```python
@router.get("/items/{item_id}", response_model=Item)
async def get_item(item_id: str) -> Item:
...
@router.get("/items", response_model=list[Item])
async def list_items() -> list[Item]:
...
```
## HTTP Status Codes
```python
@router.post("/items", status_code=status.HTTP_201_CREATED)
@router.delete("/items/{id}", status_code=status.HTTP_204_NO_CONTENT)
```
## Integration Steps
1. Create router in `src/backend/app/routers/`
2. Mount in `src/backend/app/main.py`
3. Create corresponding Pydantic models
4. Create service layer if needed
5. Add frontend API functions
## Best Practices
1. **Pick `def` or `async def` per endpoint based on whether you call async I/O;** do not mix sync and async blocking calls in one handler.
2. **Manage long-lived resources (DB pools, HTTP clients) in `lifespan` and inject via `Depends`;** use `with`/`async with` for per-request resources.
所有檔案
0 個檔案安裝 fastapi-router-py
請下載技能檔案,並將其解壓縮至您的 .claude/skills/ 目錄中。
下載 ZIP複製儲存庫並將技能檔案複製到您的專案中。
git clone https://github.com/microsoft/skills/tree/main/.github/plugins/azure-sdk-python/skills/fastapi-router-py # Copy SKILL.md to your .claude/skills/ directory
複製





首頁
