GamendWeb.Pagination (gamend_web v1.0.1215)

Copy Markdown View Source

The one way list endpoints page: parse the window, build the meta.

Every paginated response carries all six meta keys (docs/specs/api-conventions.md), so a client never has to branch on which endpoint it called:

%{page: 1, page_size: 25, count: 25,
  total_count: 130, total_pages: 6, has_more: true}

Four controllers used to hand-roll this with a private parse_int/2 and a literal min(size, 100), which silently ignored the configurable max_page_size limit and shipped metas of three, four and six keys. Both halves live here now, and mix gamend.api.lint (R7/R8) rejects a new copy.

Usage

{page, page_size} = Pagination.params(params)
entries = Context.list(page: page, page_size: page_size)
json(conn, Pagination.envelope(entries, page, page_size, total_count))

Summary

Functions

A complete list response: the entries under data, their window under meta.

Pagination meta for one page of count entries out of total_count.

The requested page window, clamped to [1, max_page_size].

Types

window()

@type window() :: {pos_integer(), pos_integer()}

Functions

envelope(entries, page, page_size, total_count)

@spec envelope(list(), integer(), integer(), integer()) :: map()

A complete list response: the entries under data, their window under meta.

count is taken from entries, which is what it always means — how many came back on this page.

meta(page, page_size, count, total_count)

@spec meta(integer(), integer(), integer(), integer()) :: map()

Pagination meta for one page of count entries out of total_count.

params(params)

@spec params(map()) :: window()

The requested page window, clamped to [1, max_page_size].

Reads string- or atom-keyed params, so it works on both a controller's params and an internal call.