Layr8.Handler (layr8 v0.2.6)

Copy Markdown View Source

Registry mapping DIDComm message types to handler functions.

Handler Signature

A handler is a function with the signature:

(Layr8.Message.t()) -> {:reply, Layr8.Message.t()} | :noreply | :pass | {:error, term()}

Registration

registry = Layr8.Handler.new()
registry = Layr8.Handler.register(registry, "https://example.com/proto/1.0/request", fn msg ->
  {:reply, %Layr8.Message{type: "https://example.com/proto/1.0/response", body: %{text: "pong"}}}
end)

Protocol Derivation

protocols/1 returns the unique protocol base URIs derived from registered handler types by stripping the last path segment:

"https://layr8.io/protocols/echo/1.0/request"  "https://layr8.io/protocols/echo/1.0"

Summary

Functions

Looks up a handler entry for the given message type.

Creates a new empty handler registry.

Returns the unique protocol base URIs derived from registered handler types.

Registers a handler for a DIDComm message type.

Registers a catch-all handler invoked when no specific handler matches.

Types

entry()

@type entry() :: %{fn: handler_fn()}

handler_fn()

@type handler_fn() :: (Layr8.Message.t() ->
                   {:reply, Layr8.Message.t()}
                   | :noreply
                   | :pass
                   | {:error, term()})

t()

@type t() :: %{optional(String.t()) => entry(), optional(:catch_all) => entry()}

Functions

lookup(registry, msg_type)

@spec lookup(t(), String.t()) :: {:ok, entry()} | :error

Looks up a handler entry for the given message type.

Returns {:ok, entry} or :error. Falls back to the catch-all handler when no exact match exists.

new()

@spec new() :: t()

Creates a new empty handler registry.

protocols(registry)

@spec protocols(t()) :: [String.t()]

Returns the unique protocol base URIs derived from registered handler types.

Strips the last path segment from each registered type.

Example

iex> registry = Layr8.Handler.new()
iex> registry = Layr8.Handler.register(registry, "https://example.com/proto/1.0/request", fn _ -> :noreply end)
iex> Layr8.Handler.protocols(registry)
["https://example.com/proto/1.0"]

register(registry, msg_type, fun)

@spec register(t(), String.t(), handler_fn()) :: t()

Registers a handler for a DIDComm message type.

Raises if a handler is already registered for msg_type.

register_catch_all(registry, fun)

@spec register_catch_all(t(), handler_fn()) :: t()

Registers a catch-all handler invoked when no specific handler matches.

Raises if a catch-all handler is already registered.