StarView.Signals (star_view v0.7.0)

Copy Markdown View Source

Datastar signal reading and datastar-patch-signals helpers.

Summary

Functions

Fetches an object signal document and returns Plug's updated connection.

Formats a signal patch without writing to a connection.

Formats a raw JSON signal patch without writing to a connection.

Formats a signal removal without writing to a connection.

Sends a signal patch from a map.

Sends a signal patch from a JSON string.

Reads Datastar signals from a Plug connection.

Reads Datastar signals and raises on errors.

Removes one or more signals by setting dot-notated paths to null.

Sends a plain HTTP 400 or 413 response for a signal input error before SSE starts.

Functions

fetch(conn, opts \\ [])

@spec fetch(Plug.Conn.t(), keyword()) ::
  {:ok, map(), Plug.Conn.t()} | {:error, term(), Plug.Conn.t()}

Fetches an object signal document and returns Plug's updated connection.

Raw bodies and the datastar query JSON are limited to 1,000,000 bytes by default, configurable with :max_bytes. Already-parsed body params rely on Plug.Parsers's configured length limit. Always thread the returned conn.

format_patch(signals, opts \\ [])

@spec format_patch(map(), keyword()) :: String.t()

Formats a signal patch without writing to a connection.

format_patch_raw(signals_json, opts \\ [])

@spec format_patch_raw(String.t(), keyword()) :: String.t()

Formats a raw JSON signal patch without writing to a connection.

format_remove(paths, opts \\ [])

@spec format_remove(String.t() | [String.t()], keyword()) :: String.t()

Formats a signal removal without writing to a connection.

patch(conn, signals, opts \\ [])

@spec patch(Plug.Conn.t(), map(), keyword()) :: Plug.Conn.t()

Sends a signal patch from a map.

patch_raw(conn, signals_json, opts \\ [])

@spec patch_raw(Plug.Conn.t(), String.t(), keyword()) :: Plug.Conn.t()

Sends a signal patch from a JSON string.

read(conn)

@spec read(Plug.Conn.t()) :: {:ok, map()} | {:error, term()}

Reads Datastar signals from a Plug connection.

GET and DELETE requests read the datastar query parameter. Other methods read JSON from the request body unless a parser has already populated conn.body_params. For raw bodies, prefer fetch/2 so the updated conn reaches subsequent plugs; this compatibility API returns only the result.

read!(conn)

@spec read!(Plug.Conn.t()) :: map()

Reads Datastar signals and raises on errors.

remove_signals(conn, paths, opts \\ [])

@spec remove_signals(Plug.Conn.t(), String.t() | [String.t()], keyword()) ::
  Plug.Conn.t()

Removes one or more signals by setting dot-notated paths to null.

send_error(conn, reason)

@spec send_error(Plug.Conn.t(), term()) :: Plug.Conn.t()

Sends a plain HTTP 400 or 413 response for a signal input error before SSE starts.