Skip to content

feat(api/v2): ⭐ Implement pagination in URLs and responses #52

@Nudelsuppe42

Description

@Nudelsuppe42

[1] Feature Description
Resources with array return types should allow for pagination.
The pagination should happen using query parameters by the requester:

{url}?page={num}&limit={num}

The server should then respond with the data in the following format:

{
    "status": 200,
    "message": "Success",
    "data": [],
    "meta": {
        "page": {num},
        "perPage": {num},
        "totalItems": {num},
        "totalPages": {num}
    }
}

[2] Implementation
This feature is achieved by implementing two decorators:

  1. @Paginated(): Route-Level decorator to add documentation and metadata
  2. @Pagination(): Param-Level decorator to extract and validate types from the request and return them in a unified format

Additionally, some utility functions and types are implemented to modify the returned data with the correct meta keyword:

  • PaginatedControllerResponse: Type
  • ApiPaginatedResponseDto: DTO
  • PaginationParams: Type
  • Modify response.interceptor.ts to support PaginatedControllerResponse

[3] Example

import {
  Pagination,
  PaginationParams,
} from "src/common/decorators/pagination.decorator";
import { PaginatedControllerResponse } from "src/typings";
import { ApiPaginatedResponseDto } from "src/common/decorators/api-response.decorator";

  @Get("/")
  @Paginated()
  @ApiPaginatedResponseDto(ApplicationDto, { description: "Success" })

  async getPaginatedURL(
    @Pagination() pagination: PaginationParams,
  ): PaginatedControllerResponse {
    return [...]
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature/apiNew feature or request

    Projects

    Status

    Done

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions