Standalone Spark DSL for defining typed controller routes.
Generates TypeScript path helper functions and a thin Phoenix controller
from routes configured in the DSL. This is completely independent from
Ash.Resource — routes contain colocated arguments and handler functions.
Usage
defmodule MyApp.Session do
use AshTypescript.TypedController
typed_controller do
module_name MyAppWeb.SessionController
route :login do
method :post
run fn conn, params -> Plug.Conn.send_resp(conn, 200, "OK") end
argument :code, :string, allow_nil?: false
end
route :auth do
method :get
run fn conn, _params -> Plug.Conn.send_resp(conn, 200, "Auth") end
end
end
endOptions
:extensions(list of module that adoptsSpark.Dsl.Extension) - A list of DSL extensions to add to theSpark.Dsl:otp_app(atom/0) - The otp_app to use for any application configurable options:fragments(list ofmodule/0) - Fragments to include in theSpark.Dsl. See the fragments guide for more.
Summary
Functions
Formats data against the current route's returns type without sending it.
Sends data as the JSON response of the current route, formatted against the
route's returns type.
Functions
@spec format_result(Plug.Conn.t(), term()) :: term()
Formats data against the current route's returns type without sending it.
See json/2. Useful when the response is sent some other way (e.g. with a
custom status via Plug.Conn.put_status/2 before Phoenix.Controller.json/2).
@spec json(Plug.Conn.t(), term()) :: Plug.Conn.t()
Sends data as the JSON response of the current route, formatted against the
route's returns type.
Keys are formatted with the configured output_field_formatter (and any
typescript_field_names mappings) at every level, so the body matches the
route's generated TypeScript result type. Use it from a route handler:
post :login do
returns :map
constraints fields: [user_id: [type: :uuid, allow_nil?: false]]
run fn conn, params ->
user = log_in!(params.code)
AshTypescript.TypedController.json(conn, %{user_id: user.id})
end
endPass maps (with atom keys) for typed containers. Raises if the route does not
declare returns, or if conn was not dispatched by a typed controller.