AshTypescript.TypedController (ash_typescript v0.19.0)

Copy Markdown View Source

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
end

Options

  • :extensions (list of module that adopts Spark.Dsl.Extension) - A list of DSL extensions to add to the Spark.Dsl

  • :otp_app (atom/0) - The otp_app to use for any application configurable options

  • :fragments (list of module/0) - Fragments to include in the Spark.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

format_result(conn, data)

@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).

json(conn, data)

@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
end

Pass 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.