multiverse v2.0.0 Multiverse.Adapter behaviour

This module provides behaviour for Multiverse adapters.

Link to this section Summary

Callbacks

Fetch default client version

Initializes adapter configuration at compile time

Resolve version by string value from request header

Comparator that is used to order and filter versions that should be applied to a connection

Link to this section Types

Link to this type changes()
changes() :: [module()]
Link to this type config()
config() ::
  [
    endpoint: module(),
    adapter: module(),
    version_header: String.t(),
    gates: gates()
  ]
  | Keyword.t()
Link to this type gates()
gates() :: [{version(), changes()}]
Link to this type version()
version() :: any()

Link to this section Callbacks

Link to this callback fetch_default_version(conn, adapter_config)
fetch_default_version(conn :: Plug.Conn.t(), adapter_config :: config()) ::
  {:ok, version(), Plug.Conn.t()}

Fetch default client version.

This callback is used when version header is not set or empty. Additionally adapters may use it to fallback to default version in case of errors or when version header value is malformed.

Link to this callback init(adapter, opts)
init(adapter :: module(), opts :: Keyword.t()) :: {:ok, config()}

Initializes adapter configuration at compile time.

This callback can be used to fetch Multiverse configuration from application environment, file or other places.

Link to this callback resolve_version_or_channel(conn, channel_or_version, adapter_config)
resolve_version_or_channel(
  conn :: Plug.Conn.t(),
  channel_or_version :: String.t(),
  adapter_config :: config()
) :: {:ok, version(), Plug.Conn.t()}

Resolve version by string value from request header.

This callback can be used to set named channels for API versions, for eg. latest header value could be resolved to current date and edge to the most recent defined version.

Also, it is responsible for casting string value to adapter-specific version type and handling possible errors.

You can terminate connection if you want to return error without further processing of the request.

Link to this callback version_comparator(v1, v2)
version_comparator(v1 :: version(), v2 :: version()) :: boolean()

Comparator that is used to order and filter versions that should be applied to a connection.

It should compare two arguments, and return true if the first argument precedes the second one or they are equal.