SelectoComponents.QueryContract.ChoiceSource.Client (selecto_components v0.4.8)

Consumer helpers for query-contract choice-source links.

The client reads choice_sources[].links from a JSON-ready query contract, builds transport-neutral requests for option lookup and membership validation, and can execute those requests through a caller-supplied transport function.

A transport receives %SelectoComponents.QueryContract.ChoiceSource.Request{} and may return %{status: status, body: body}, {:ok, response}, or {:error, reason}. Bodies may already be decoded maps/lists or JSON strings.

Link to this section Summary

Functions

Finds a choice-source entry by id.

Returns the choice-source entries advertised by a query contract.

Fetches options through a configured transport.

Infers the field bound to a choice source when the contract has one match.

Returns the advertised link map for a choice source.

Builds a GET request for a choice-source options endpoint.

Validates membership through a configured transport.

Builds a POST request for a choice-source membership endpoint.

Link to this section Types

Link to this type

choice_source_id()

@type choice_source_id() :: atom() | String.t()
@type contract() :: map()
@type result() :: {:ok, map() | list()} | {:error, map()}

Link to this section Functions

Link to this function

choice_source(contract, choice_source_id)

@spec choice_source(contract(), choice_source_id()) :: {:ok, map()} | {:error, map()}

Finds a choice-source entry by id.

Link to this function

choice_sources(contract)

@spec choice_sources(contract()) :: [map()]

Returns the choice-source entries advertised by a query contract.

Link to this function

fetch_options(contract, choice_source_id, opts \\ [])

@spec fetch_options(contract(), choice_source_id(), keyword()) :: result()

Fetches options through a configured transport.

Link to this function

inferred_field(contract, choice_source_id)

@spec inferred_field(contract(), choice_source_id()) :: String.t() | nil

Infers the field bound to a choice source when the contract has one match.

Link to this function

links(contract, choice_source_id)

@spec links(contract(), choice_source_id()) :: {:ok, map()} | {:error, map()}

Returns the advertised link map for a choice source.

Link to this function

options_request(contract, choice_source_id, opts \\ [])

@spec options_request(contract(), choice_source_id(), keyword()) ::
  {:ok, SelectoComponents.QueryContract.ChoiceSource.Request.t()}
  | {:error, map()}

Builds a GET request for a choice-source options endpoint.

Supported options:

  • :search
  • :limit
  • :offset
  • :params for extra query parameters
  • :headers for transport headers
  • :base_url for resolving relative links
Link to this function

validate_choice(contract, choice_source_id, value, opts \\ [])

@spec validate_choice(contract(), choice_source_id(), term(), keyword()) :: result()

Validates membership through a configured transport.

Link to this function

validate_request(contract, choice_source_id, value, opts \\ [])

@spec validate_request(contract(), choice_source_id(), term(), keyword()) ::
  {:ok, SelectoComponents.QueryContract.ChoiceSource.Request.t()}
  | {:error, map()}

Builds a POST request for a choice-source membership endpoint.

If :field is omitted and the query contract has exactly one field_choice_bindings entry for the choice source, that field is included in the request body.