Generic lazy pagination helper for Discord API endpoints.
Builds a Stream.resource/3 that fetches pages on demand, yielding items
one at a time with O(page_size) memory. The stream halts automatically when
Discord returns an empty or incomplete page, or when an error occurs.
Example
EDA.Paginator.stream(
fetch: fn cursor ->
EDA.API.Ban.list(guild_id, limit: 1000, after: cursor)
end,
cursor_key: ["user", "id"],
direction: :after,
per_page: 1000
)
|> Stream.take(50)
|> Enum.to_list()Options
:fetch(required) —(cursor | nil -> {:ok, [item]} | {:error, term()}). Receivesnilon the first call, then the extracted cursor for subsequent pages.:cursor_key— How to extract the cursor from each item. Accepts:- a string key (e.g.
"id") — usesitem["id"] - a list of string keys (e.g.
["user", "id"]) — usesget_in/2 - a function
(item -> cursor)— arbitrary extraction Defaults to"id".
- a string key (e.g.
:direction—:beforeor:after. Defaults to:before.:per_page— Page size, used to detect incomplete (final) pages. Defaults to100.:initial_cursor— Starting cursor value. Defaults tonil.
Summary
Functions
@spec stream(keyword()) :: Enumerable.t()
Returns a lazy Stream that paginates through a Discord API endpoint.
See module documentation for the full list of options.