Skip to content
API/DrfResponseStrategy

DrfResponseStrategy

src/strategies/drf-response.strategy.ts1 public methodsclass

Response strategy for the Django REST Framework (DRF) driver

Parses DRF PageNumberPagination responses:

{
"count": 100,
"next": "http://api.example.com/items/?page=3",
"previous": "http://api.example.com/items/?page=1",
"results": [...]
}

DRF emits no current_page field in the body, so this strategy derives the current page (and the page size) by inspecting the next / previous URLs:

  • previous === null → current page is 1.
  • previous set but has no ?page=N param → DRF omits page=1 from URLs when the previous page is the first, so we infer 2.
  • previous has ?page=N → current page is N + 1.

Similarly, perPage is parsed from any ?page_size=N query param on next or previous, and lastPage is computed as ceil(count / perPage). When perPage cannot be discovered (e.g. on a single-page response that emits both URLs as null), perPage and lastPage are left undefined.

Key paths are resolved through DrfResponseOptions, which defaults data → 'results', total → 'count', nextPageUrl → 'next', prevPageUrl → 'previous'. The current-page / per-page / last-page paths default to empty strings — the strategy ignores options for those slots and uses URL inspection instead.

new DrfResponseStrategy()
paginate(response: RawResponse, options: ResponseOptions): PaginatedCollection<T>

Parse a DRF pagination response into a PaginatedCollection

Parameter Type Description
response RawResponse The raw API response body
options ResponseOptions The response key name configuration

Returns — A typed PaginatedCollection instance