Spark DSL extension for defining typed controller routes.
This extension generates TypeScript path helper functions and a thin Phoenix
controller from routes configured in the DSL. Unlike AshTypescript.ControllerResource,
this is a standalone Spark DSL — not attached to Ash.Resource.
Routes contain colocated arguments and handler functions (inline closures or
handler modules implementing AshTypescript.TypedController.Route).
Usage
Three syntaxes are supported for defining routes:
defmodule MyApp.Session do
use AshTypescript.TypedController
typed_controller do
module_name MyAppWeb.SessionController
# Verb shortcut (preferred)
post :login do
run fn conn, params -> Plug.Conn.send_resp(conn, 200, "OK") end
argument :code, :string, allow_nil?: false
argument :remember_me, :boolean
end
# Positional method arg
route :auth, :get do
run fn conn, _params -> Plug.Conn.send_resp(conn, 200, "Auth") end
end
# Method defaults to :get when omitted
route :home do
run fn conn, _params -> Plug.Conn.send_resp(conn, 200, "Home") end
end
end
endtyped_controller
Define typed controller routes
Nested DSLs
Options
| Name | Type | Default | Docs |
|---|---|---|---|
module_name | atom | The module name for the generated Phoenix controller (e.g. MyAppWeb.SessionController) | |
namespace | String.t | Default namespace (filename) for all routes in this controller. Can be overridden per-route. |
typed_controller.route
route name, method \\ nilDefine a route that maps a controller action to a handler.
Supports three syntaxes:
route :name, :post do ... end(positional method arg)route :name do ... end(defaults to GET)post :name do ... end(verb shortcut)
The handler can be an inline function (fn/2) or a module implementing
the AshTypescript.TypedController.Route behaviour. Handlers receive
(conn, params) and must return a %Plug.Conn{}.
Nested DSLs
Arguments
| Name | Type | Default | Docs |
|---|---|---|---|
name | atom | The controller action name (e.g. :login, :auth) | |
method | :get | :post | :patch | :put | :delete | :get | The HTTP method. Defaults to :get. |
Options
| Name | Type | Default | Docs |
|---|---|---|---|
run | (any, any -> any) | atom | The handler — an fn/2 closure or a module implementing AshTypescript.TypedController.Route | |
description | String.t | JSDoc description for the generated TypeScript path helper | |
deprecated | boolean | String.t | Mark this route as deprecated. Set to true for a default message, or provide a custom deprecation notice. | |
see | list(atom) | [] | List of related route names to reference in JSDoc @see tags. |
zod_schema_name | String.t | Override the generated Zod schema name (used as-is for the exported const). Use when the default name collides with an RPC action's Zod schema. | |
namespace | String.t | Namespace for organizing this route into a separate file (becomes the filename). Overrides controller-level namespace. |
typed_controller.route.argument
argument name, typeDefine an argument for this route.
Arguments
| Name | Type | Default | Docs |
|---|---|---|---|
name | atom | The argument name | |
type | atom | {atom, keyword} | The Ash type (e.g. :string, :boolean, :integer) |
Options
| Name | Type | Default | Docs |
|---|---|---|---|
constraints | keyword | [] | Type constraints |
allow_nil? | boolean | true | Whether this argument can be nil. Set to false to make it required. |
default | any | Default value for this argument |
Introspection
Target: AshTypescript.TypedController.Dsl.RouteArgument
Introspection
Target: AshTypescript.TypedController.Dsl.Route
typed_controller.get
get nameDefine a GET route. Shorthand for route :name, :get.
Nested DSLs
Arguments
| Name | Type | Default | Docs |
|---|---|---|---|
name | atom | The controller action name (e.g. :login, :auth) |
Options
| Name | Type | Default | Docs |
|---|---|---|---|
run | (any, any -> any) | atom | The handler — an fn/2 closure or a module implementing AshTypescript.TypedController.Route | |
description | String.t | JSDoc description for the generated TypeScript path helper | |
deprecated | boolean | String.t | Mark this route as deprecated. Set to true for a default message, or provide a custom deprecation notice. | |
see | list(atom) | [] | List of related route names to reference in JSDoc @see tags. |
zod_schema_name | String.t | Override the generated Zod schema name (used as-is for the exported const). Use when the default name collides with an RPC action's Zod schema. | |
namespace | String.t | Namespace for organizing this route into a separate file (becomes the filename). Overrides controller-level namespace. |
typed_controller.get.argument
argument name, typeDefine an argument for this route.
Arguments
| Name | Type | Default | Docs |
|---|---|---|---|
name | atom | The argument name | |
type | atom | {atom, keyword} | The Ash type (e.g. :string, :boolean, :integer) |
Options
| Name | Type | Default | Docs |
|---|---|---|---|
constraints | keyword | [] | Type constraints |
allow_nil? | boolean | true | Whether this argument can be nil. Set to false to make it required. |
default | any | Default value for this argument |
Introspection
Target: AshTypescript.TypedController.Dsl.RouteArgument
Introspection
Target: AshTypescript.TypedController.Dsl.Route
typed_controller.post
post nameDefine a POST route. Shorthand for route :name, :post.
Nested DSLs
Arguments
| Name | Type | Default | Docs |
|---|---|---|---|
name | atom | The controller action name (e.g. :login, :auth) |
Options
| Name | Type | Default | Docs |
|---|---|---|---|
run | (any, any -> any) | atom | The handler — an fn/2 closure or a module implementing AshTypescript.TypedController.Route | |
description | String.t | JSDoc description for the generated TypeScript path helper | |
deprecated | boolean | String.t | Mark this route as deprecated. Set to true for a default message, or provide a custom deprecation notice. | |
see | list(atom) | [] | List of related route names to reference in JSDoc @see tags. |
zod_schema_name | String.t | Override the generated Zod schema name (used as-is for the exported const). Use when the default name collides with an RPC action's Zod schema. | |
namespace | String.t | Namespace for organizing this route into a separate file (becomes the filename). Overrides controller-level namespace. |
typed_controller.post.argument
argument name, typeDefine an argument for this route.
Arguments
| Name | Type | Default | Docs |
|---|---|---|---|
name | atom | The argument name | |
type | atom | {atom, keyword} | The Ash type (e.g. :string, :boolean, :integer) |
Options
| Name | Type | Default | Docs |
|---|---|---|---|
constraints | keyword | [] | Type constraints |
allow_nil? | boolean | true | Whether this argument can be nil. Set to false to make it required. |
default | any | Default value for this argument |
Introspection
Target: AshTypescript.TypedController.Dsl.RouteArgument
Introspection
Target: AshTypescript.TypedController.Dsl.Route
typed_controller.patch
patch nameDefine a PATCH route. Shorthand for route :name, :patch.
Nested DSLs
Arguments
| Name | Type | Default | Docs |
|---|---|---|---|
name | atom | The controller action name (e.g. :login, :auth) |
Options
| Name | Type | Default | Docs |
|---|---|---|---|
run | (any, any -> any) | atom | The handler — an fn/2 closure or a module implementing AshTypescript.TypedController.Route | |
description | String.t | JSDoc description for the generated TypeScript path helper | |
deprecated | boolean | String.t | Mark this route as deprecated. Set to true for a default message, or provide a custom deprecation notice. | |
see | list(atom) | [] | List of related route names to reference in JSDoc @see tags. |
zod_schema_name | String.t | Override the generated Zod schema name (used as-is for the exported const). Use when the default name collides with an RPC action's Zod schema. | |
namespace | String.t | Namespace for organizing this route into a separate file (becomes the filename). Overrides controller-level namespace. |
typed_controller.patch.argument
argument name, typeDefine an argument for this route.
Arguments
| Name | Type | Default | Docs |
|---|---|---|---|
name | atom | The argument name | |
type | atom | {atom, keyword} | The Ash type (e.g. :string, :boolean, :integer) |
Options
| Name | Type | Default | Docs |
|---|---|---|---|
constraints | keyword | [] | Type constraints |
allow_nil? | boolean | true | Whether this argument can be nil. Set to false to make it required. |
default | any | Default value for this argument |
Introspection
Target: AshTypescript.TypedController.Dsl.RouteArgument
Introspection
Target: AshTypescript.TypedController.Dsl.Route
typed_controller.put
put nameDefine a PUT route. Shorthand for route :name, :put.
Nested DSLs
Arguments
| Name | Type | Default | Docs |
|---|---|---|---|
name | atom | The controller action name (e.g. :login, :auth) |
Options
| Name | Type | Default | Docs |
|---|---|---|---|
run | (any, any -> any) | atom | The handler — an fn/2 closure or a module implementing AshTypescript.TypedController.Route | |
description | String.t | JSDoc description for the generated TypeScript path helper | |
deprecated | boolean | String.t | Mark this route as deprecated. Set to true for a default message, or provide a custom deprecation notice. | |
see | list(atom) | [] | List of related route names to reference in JSDoc @see tags. |
zod_schema_name | String.t | Override the generated Zod schema name (used as-is for the exported const). Use when the default name collides with an RPC action's Zod schema. | |
namespace | String.t | Namespace for organizing this route into a separate file (becomes the filename). Overrides controller-level namespace. |
typed_controller.put.argument
argument name, typeDefine an argument for this route.
Arguments
| Name | Type | Default | Docs |
|---|---|---|---|
name | atom | The argument name | |
type | atom | {atom, keyword} | The Ash type (e.g. :string, :boolean, :integer) |
Options
| Name | Type | Default | Docs |
|---|---|---|---|
constraints | keyword | [] | Type constraints |
allow_nil? | boolean | true | Whether this argument can be nil. Set to false to make it required. |
default | any | Default value for this argument |
Introspection
Target: AshTypescript.TypedController.Dsl.RouteArgument
Introspection
Target: AshTypescript.TypedController.Dsl.Route
typed_controller.delete
delete nameDefine a DELETE route. Shorthand for route :name, :delete.
Nested DSLs
Arguments
| Name | Type | Default | Docs |
|---|---|---|---|
name | atom | The controller action name (e.g. :login, :auth) |
Options
| Name | Type | Default | Docs |
|---|---|---|---|
run | (any, any -> any) | atom | The handler — an fn/2 closure or a module implementing AshTypescript.TypedController.Route | |
description | String.t | JSDoc description for the generated TypeScript path helper | |
deprecated | boolean | String.t | Mark this route as deprecated. Set to true for a default message, or provide a custom deprecation notice. | |
see | list(atom) | [] | List of related route names to reference in JSDoc @see tags. |
zod_schema_name | String.t | Override the generated Zod schema name (used as-is for the exported const). Use when the default name collides with an RPC action's Zod schema. | |
namespace | String.t | Namespace for organizing this route into a separate file (becomes the filename). Overrides controller-level namespace. |
typed_controller.delete.argument
argument name, typeDefine an argument for this route.
Arguments
| Name | Type | Default | Docs |
|---|---|---|---|
name | atom | The argument name | |
type | atom | {atom, keyword} | The Ash type (e.g. :string, :boolean, :integer) |
Options
| Name | Type | Default | Docs |
|---|---|---|---|
constraints | keyword | [] | Type constraints |
allow_nil? | boolean | true | Whether this argument can be nil. Set to false to make it required. |
default | any | Default value for this argument |
Introspection
Target: AshTypescript.TypedController.Dsl.RouteArgument