Files
life-echo/api/app/core/pagination.py

30 lines
687 B
Python
Raw Normal View History

"""
通用分页offset/limit 参数与分页结果结构
feature router 按需使用成功响应直接返回 Pydantic model不强制包装
"""
2026-03-19 14:36:14 +08:00
from typing import Generic, TypeVar
from pydantic import BaseModel
T = TypeVar("T")
class PageParams(BaseModel):
"""常用分页查询参数router 中通过 Depends 注入)。"""
offset: int = 0
limit: int = 20
def clamp_limit(self, max_limit: int = 100) -> int:
return min(max(1, self.limit), max_limit)
class Page(BaseModel, Generic[T]):
"""分页结果items + total可选"""
items: list[T]
total: int | None = None
offset: int = 0
limit: int = 20