|
| 1 | +-- | Pagination types and utilities for query endpoints. |
| 2 | +-- |
| 3 | +-- Provides 'QueryPageRequest' for parsing client pagination parameters |
| 4 | +-- and 'QueryPageResponse' for wrapping paginated results with metadata. |
| 5 | +-- |
| 6 | +-- Pagination is applied automatically by the query endpoint handler. |
| 7 | +-- Jess doesn't need to do anything — all query endpoints are paginated |
| 8 | +-- by default with sensible limits. |
| 9 | +-- |
| 10 | +-- To override the maximum results per page for a specific query, define |
| 11 | +-- @maxResults :: Int@ in the query module before calling @deriveQuery@. |
| 12 | +module Service.Query.Pagination ( |
| 13 | + -- * Types |
| 14 | + QueryPageRequest (..), |
| 15 | + QueryPageResponse (..), |
| 16 | + |
| 17 | + -- * Constants |
| 18 | + defaultLimit, |
| 19 | + absoluteMaxLimit, |
| 20 | + |
| 21 | + -- * Parsing |
| 22 | + parsePageRequest, |
| 23 | +) where |
| 24 | + |
| 25 | +import Array (Array) |
| 26 | +import Basics |
| 27 | +import Json qualified |
| 28 | +import Maybe (Maybe (..)) |
| 29 | +import Maybe qualified |
| 30 | +import Text (Text) |
| 31 | +import Text qualified |
| 32 | + |
| 33 | + |
| 34 | +-- | Pagination request parameters parsed from query string. |
| 35 | +-- |
| 36 | +-- @ |
| 37 | +-- -- Parse from raw query params: |
| 38 | +-- let pageRequest = parsePageRequest (Just "25") (Just "50") |
| 39 | +-- -- QueryPageRequest { limit = 25, offset = 50 } |
| 40 | +-- @ |
| 41 | +data QueryPageRequest = QueryPageRequest |
| 42 | + { limit :: !Int |
| 43 | + , offset :: !Int |
| 44 | + } |
| 45 | + deriving (Eq, Show, Generic) |
| 46 | + |
| 47 | + |
| 48 | +-- | Paginated response wrapper with metadata. |
| 49 | +-- |
| 50 | +-- The @total@ field reflects the count of items AFTER authorization filtering. |
| 51 | +-- It never exposes counts of records the user is not authorized to see. |
| 52 | +-- |
| 53 | +-- The @effectiveLimit@ field surfaces any capping that was applied. |
| 54 | +-- When the client requests @limit=5000@ but the server caps to 1000, |
| 55 | +-- @effectiveLimit@ will be 1000 so the client can detect the difference. |
| 56 | +-- |
| 57 | +-- @ |
| 58 | +-- -- Example response: |
| 59 | +-- -- { "items": [...], "total": 142, "hasMore": true, "effectiveLimit": 100 } |
| 60 | +-- @ |
| 61 | +data QueryPageResponse a = QueryPageResponse |
| 62 | + { items :: !(Array a) |
| 63 | + , total :: !Int |
| 64 | + , hasMore :: !Bool |
| 65 | + , effectiveLimit :: !Int |
| 66 | + } |
| 67 | + deriving (Eq, Show, Generic) |
| 68 | + |
| 69 | +instance (Json.ToJSON a) => Json.ToJSON (QueryPageResponse a) |
| 70 | +instance (Json.FromJSON a) => Json.FromJSON (QueryPageResponse a) |
| 71 | + |
| 72 | + |
| 73 | +-- | Default page size when no limit is specified (100). |
| 74 | +defaultLimit :: Int |
| 75 | +defaultLimit = 100 |
| 76 | + |
| 77 | + |
| 78 | +-- | Absolute maximum page size — hard cap (1000). |
| 79 | +absoluteMaxLimit :: Int |
| 80 | +absoluteMaxLimit = 1000 |
| 81 | + |
| 82 | + |
| 83 | +-- | Parse pagination parameters from raw query string values. |
| 84 | +-- |
| 85 | +-- Invalid or missing values fall back to defaults. |
| 86 | +-- Limit is clamped to @[1, absoluteMaxLimit]@. |
| 87 | +-- Offset is clamped to @[0, 10_000_000]@. |
| 88 | +-- |
| 89 | +-- @ |
| 90 | +-- parsePageRequest (Just "25") (Just "50") |
| 91 | +-- -- QueryPageRequest { limit = 25, offset = 50 } |
| 92 | +-- |
| 93 | +-- parsePageRequest Nothing Nothing |
| 94 | +-- -- QueryPageRequest { limit = 100, offset = 0 } |
| 95 | +-- |
| 96 | +-- parsePageRequest (Just "abc") (Just "-5") |
| 97 | +-- -- QueryPageRequest { limit = 100, offset = 0 } |
| 98 | +-- @ |
| 99 | +parsePageRequest :: Maybe Text -> Maybe Text -> QueryPageRequest |
| 100 | +parsePageRequest maybeLimitText maybeOffsetText = do |
| 101 | + let parsedLimit = |
| 102 | + maybeLimitText |
| 103 | + |> Maybe.andThen Text.toInt |
| 104 | + |> Maybe.withDefault defaultLimit |
| 105 | + let parsedOffset = |
| 106 | + maybeOffsetText |
| 107 | + |> Maybe.andThen Text.toInt |
| 108 | + |> Maybe.withDefault 0 |
| 109 | + QueryPageRequest |
| 110 | + { limit = parsedLimit |> max 1 |> min absoluteMaxLimit |
| 111 | + , offset = parsedOffset |> max 0 |> min 10_000_000 |
| 112 | + } |
0 commit comments