选项
首页首页 Skill 开发者工具 fastapi-router-py

fastapi-router-py

microsoft/skills microsoft/skills

遵循既定模式,创建包含 CRUD 操作、身份验证依赖项和正确响应模型的 FastAPI 路由器。

...展开全部
10
更新时间 2026-09-12

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)

集成步骤

  1. src/backend/app/routers/
  2. 挂载到 src/backend/app/main.py
  3. 创建相应的 Pydantic 模型
  4. 如有需要,创建服务层
  5. 添加前端 API 函数

最佳实践

  1. 根据是否调用异步 I/O,为每个端点选择 defasync def切勿在同一个处理程序中混合使用同步和异步阻塞调用。
  2. lifespan 中管理长生命周期资源(数据库连接池、HTTP 客户端),并通过 Depends 进行注入;使用 with/async with 用于按请求管理的资源。
在 GitHub 上查看
---
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

复制 复制
快速设置: 将技能文件夹复制到 .claude/skills/ Claude 会自动检测并使用该技能

相关技能

algorithmic-art
更新时间 2026-08-27
receiving-code-review
更新时间 2026-09-03
tech-debt-tracker
更新时间 2026-08-29
deprecation-and-migration
更新时间 2026-09-03
OR