py-fastapi-patterns
套用 FastAPI 最佳實務
使用 FastAPI 建構 API 需要理解相依性注入、回應模型與 OpenAPI 結構設計。本技能提供經實戰驗證的模式,可避免常見錯誤並改善前端團隊的開發體驗。
스킬 ZIP 다운로드
Claude에서 업로드
설정 → 기능 → 스킬 → 스킬 업로드로 이동
토글을 켜고 사용 시작
테스트해 보기
"py-fastapi-patterns" 사용 중입니다. Create a paginated response model for listing assessments
예상 결과:
- 定義一個泛型 PaginatedResponse 類別,包含 items、total、page 與 size 欄位
- 在列表端點使用 response_model=PaginatedResponse[AssessmentRead]
- FastAPI 會自動過濾回應,只包含 AssessmentRead 欄位
- OpenAPI 結構會正確記錄巢狀結構
"py-fastapi-patterns" 사용 중입니다. Add CORS middleware to my FastAPI app
예상 결과:
- 從 fastapi.middleware.cors 匯入 CORSMiddleware
- 使用 allow_origins、allow_credentials、allow_methods 與 allow_headers 加入 middleware
- 在 middleware 堆疊中儘早加入 CORS middleware 以確保順序正確
- 在生產環境使用特定來源而非萬用字元 (*)
"py-fastapi-patterns" 사용 중입니다. Create validation for an input field
예상 결과:
- 使用帶有 min_length、max_length 與 description 參數的 Pydantic Field
- 加入 field_validator 方法以針對允許值進行驗證
- 對無效輸入以清楚訊息拋出 ValueError
- FastAPI 會在驗證失敗時自動回傳 422 狀態
보안 감사
안전Pure documentation skill containing only markdown explanations and Python code examples. No executable scripts, network calls, file system access, or environment variable access. Static findings are false positives triggered by documentation keywords and markdown syntax.
위험 요인
🌐 네트워크 접근 (4)
⚙️ 외부 명령어 (25)
품질 점수
만들 수 있는 것
建置可維護的 API
套用相依鏈與回應模型,打造易於維護並可隨時間演進的 API。
改善前端整合
設計 OpenAPI 結構,產生準確的 TypeScript 型別以供前端程式碼產生工具使用。
標準化團隊模式
在多個 API 服務之間建立一致的錯誤處理與路由組織方式。
이 프롬프트를 사용해 보세요
Show me how to create a dependency chain in FastAPI for authentication. Include get_session, get_current_user, and get_current_active_user dependencies that can be used in endpoints.
Explain how to use response_model in FastAPI to prevent exposing internal fields like hashed_password. Show a User model with read and create variants.
How do I create custom exception handlers in FastAPI that return consistent error responses with error codes? Include both the exception class and the handler.
Explain why route ordering matters in FastAPI. Show an example of how putting /users/me before /users/{user_id} prevents matching issues.모범 사례
- 一律先定義具體路由,再定義泛型參數化路由以避免錯誤匹配
- 在每個端點使用 response_model 以控制輸出並產生準確的 OpenAPI 結構
- 相依鏈由最通用(session)到最具體(current_user)以提高可重用性
피하기
- 省略 response_model 會在 API 回應中暴露 hashed_password 等內部欄位
- 將泛型路由置於具體路由之前會導致 /users/me 被當成 user_id 參數
- 使用裸 except 子句或未指定狀態碼的泛型 HTTPException 會造成錯誤回應不一致