Shared helper functions for PhoenixDatastar modules.
Summary
Functions
Extracts the base path from a request path by stripping Datastar suffixes.
Extracts the session map from a Plug.Conn.
Extracts the view module name (last part of module path).
Renders a view's template with the socket's assigns.
Functions
Extracts the base path from a request path by stripping Datastar suffixes.
Removes /stream suffix and /_event/:event patterns to get the view's base path.
Examples
iex> PhoenixDatastar.Helpers.get_base_path("/counter/stream")
"/counter"
iex> PhoenixDatastar.Helpers.get_base_path("/counter/_event/increment")
"/counter"
iex> PhoenixDatastar.Helpers.get_base_path("/counter")
"/counter"
@spec get_session_map(Plug.Conn.t()) :: map()
Extracts the session map from a Plug.Conn.
Merges the Plug session (from the session store) with all conn.assigns
set by plugs in the pipeline. Assign keys are stringified so they can be
pattern-matched in mount/3 the same way as regular session keys.
Returns the session data if available, otherwise an empty map.
Extracts the view module name (last part of module path).
Examples
iex> PhoenixDatastar.Helpers.get_view_name(MyApp.CounterStar)
"CounterStar"
@spec render_html(module(), PhoenixDatastar.Socket.t()) :: Phoenix.HTML.Safe.t()
Renders a view's template with the socket's assigns.