igorbenav / fastcrud

FastCRUD is a Python package for FastAPI, offering robust async CRUD operations and flexible endpoint creation utilities.
MIT License
697 stars 58 forks source link

Rename "data" to "items" in multi response #155

Open feluelle opened 2 months ago

feluelle commented 2 months ago

Is your feature request related to a problem? Please describe.

The multi-response field data is generic and doesn't describe the returned data very well.

Describe the solution you'd like

I would like to use a more descriptive term e.g. items that indicate that a list is being returned.

ianbuss commented 2 months ago

Another option might be to make the key configurable on the FastCRUD instance.

sombathoudom commented 1 month ago

here is my current solution

SchemaType = TypeVar("SchemaType", bound=BaseModel)
class ListResponse(BaseModel, Generic[SchemaType]):
    items: list[SchemaType]

class PaginatedListResponses(ListResponse[SchemaType]):
    total_count: int
    has_more: bool
    page: Optional[int] = None
    items_per_page: Optional[int] = None

@router.get("/foods", response_model=PaginatedListResponses[FoodRead])
async def get_foods(
    db: Annotated[AsyncSession, Depends(async_get_db)],
    page: int = 1,
    items_per_page: int = 10,
):
    food_lists = await crud_foods.get_multi(
        db=db,
        offset=compute_offset(page, items_per_page),
        limit=items_per_page,
    )
    response: dict[str, Any] = paginated_response(
        crud_data=food_lists, page=page, items_per_page=items_per_page
    )

    if 'data' in response:
        response['items'] = response.pop('data')   
    return response