server_utils v0.1.4 ServerUtils.Parsers.ParamsParser

Module to get an integer() or a PageParams.t struct from a request map.

In case of page params parse, it validates the maximum page size request and provides the configured maximum param if it is exceeded.

This project provides a default value, but they can be overriding when using this module as well.

Link to this section Summary

Functions

Parses a page params from a cursor pagination request

Parses a integer value from a request params map

Parses a page params from a request params map

Link to this section Functions

Link to this function parse_cursor_page_request(params_map, opts \\ [])

Parses a page params from a cursor pagination request.

It returns the CursorPageRequest.t with the present values and with the default.

Examples

iex> ServerUtils.Parsers.ParamsParser.parse_cursor_page_request!(%{"page_number": 5, "page_size": 23})
%PageParams{page_number: 5, page_size: 23}

# With a configured max_page_size of 50
iex> ServerUtils.Parsers.ParamsParser.parse_cursor_page_request!(%{"page_number": 5, "page_size": 9000})
%PageParams{page_number: 5, page_size: 50}
Link to this function parse_integer_param(params_map, attr_name, default)
parse_integer_param(map(), String.t(), integer()) :: integer()

Parses a integer value from a request params map.

It returns the integer() with the parsed value or the default value if the param key is not present or is not an integer.

Examples

iex> ServerUtils.Parsers.ParamsParser.parse_integer_param(%{a: 5}, :a, 10)
5

iex> ServerUtils.Parsers.ParamsParser.parse_integer_param(%{a: 5}, :b, 10)
10
Link to this function parse_integer_param!(params_map, attr_name)
parse_integer_param!(map(), String.t()) :: integer()
Link to this function parse_page_params(params_map, opts \\ [])

Parses a page params from a request params map.

It returns the PageParams.t with the present values and with the default.

Examples

iex> ServerUtils.Parsers.ParamsParser.parse_page_params!(%{"page_number": 5, "page_size": 23})
%PageParams{page_number: 5, page_size: 23}

# With a configured max_page_size of 50
iex> ServerUtils.Parsers.ParamsParser.parse_page_params!(%{"page_number": 5, "page_size": 9000})
%PageParams{page_number: 5, page_size: 50}