IdempotencyKit.Phoenix.Action (idempotency_kit v0.1.0)

Copy Markdown View Source

Reusable Plug/Phoenix adapter for idempotent controller actions.

This module is host-app agnostic. Supply app-specific callbacks via options:

  • :idempotency_module (required): module implementing request_hash/1, claim_request/4, and complete_request/4
  • :render_error_fun (optional): (conn, status, detail, metadata) -> conn

Summary

Types

error_spec()

@type error_spec() :: %{
  :status => response_status(),
  :detail => String.t(),
  optional(:code) => String.t(),
  optional(:metadata) => map()
}

persist_response_fun()

@type persist_response_fun() :: (Plug.Conn.t(), term() -> persist_result())

persist_result()

@type persist_result() :: {:ok, String.t(), pos_integer(), map()} | {:error, term()}

render_error_fun()

@type render_error_fun() :: (Plug.Conn.t(), response_status(), String.t(), map() ->
                         Plug.Conn.t())

replay_response_fun()

@type replay_response_fun() :: (Plug.Conn.t(), term() ->
                            {:ok, Plug.Conn.t()} | :default | {:error, term()})

response_status()

@type response_status() :: atom() | integer()

Functions

maybe_run(conn, opts, execute_fun)

@spec maybe_run(Plug.Conn.t(), keyword(), (Plug.Conn.t() -> Plug.Conn.t())) ::
  {:handled, Plug.Conn.t()} | {:no_key, Plug.Conn.t()}

maybe_run_for_user(conn, user_id, scope, request_payload, execute_fun, opts \\ [])

@spec maybe_run_for_user(
  Plug.Conn.t(),
  pos_integer(),
  String.t(),
  term(),
  (Plug.Conn.t() -> Plug.Conn.t()),
  keyword()
) :: {:handled, Plug.Conn.t()} | {:no_key, Plug.Conn.t()}