PgRest.Request.Prefer (PgRest v0.2.0)

Copy Markdown View Source

Parses Prefer headers into a PgRest.Prefer and renders Preference-Applied.

Matches PostgREST: tokens are comma separated, whitespace around a whole token is ignored (not around =), and for each preference the first recognized value wins, in request order across repeated headers. Any timezone= or max-affected= token is recognized whatever its value (an unparseable max-affected becomes nil and still claims the first slot; 0 and negative integers parse, as in PostgREST). Everything else, including an empty token from a trailing comma and an unrecognized value of a known key (return=foo), goes to unknown.

validate/2 then applies the server-side rules: a timezone the server does not know is dropped (so it is neither applied nor echoed), and under handling=strict any invalid token is a 400 PGRST122.

Summary

Functions

Renders the Preference-Applied value for the preferences named in applied_keys that are set, in PostgREST's order. max_affected only appears under handling=strict. Returns nil when nothing is applied.

Parses the values of every Prefer request header, in request order.

Applies the server-side rules to a parsed Prefer (PostgREST v14 semantics).

Functions

applied_header(prefer, applied_keys)

@spec applied_header(PgRest.Prefer.t(), [atom()]) :: String.t() | nil

Renders the Preference-Applied value for the preferences named in applied_keys that are set, in PostgREST's order. max_affected only appears under handling=strict. Returns nil when nothing is applied.

parse(header_values)

@spec parse([String.t()]) :: PgRest.Prefer.t()

Parses the values of every Prefer request header, in request order.

validate(prefer, facts)

@spec validate(PgRest.Prefer.t(), PgRest.Catalog.Facts.t()) ::
  {:ok, PgRest.Prefer.t()} | {:error, PgRest.Error.t()}

Applies the server-side rules to a parsed Prefer (PostgREST v14 semantics).

A timezone not in facts.timezones (decided by the first timezone= token, as PostgREST does) becomes timezone: nil. Under handling=strict, every invalid token (the unknown ones and, when the timezone is invalid, every timezone= token) makes the request a 400 PGRST122 listing them in request order, for every method and for RPC. Under lenient nothing fails.