AshTypescript.TypedController

Copy Markdown View Source

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
end

typed_controller

Define typed controller routes

Nested DSLs

Options

NameTypeDefaultDocs
module_nameatomThe module name for the generated Phoenix controller (e.g. MyAppWeb.SessionController)
namespaceString.tDefault namespace (filename) for all routes in this controller. Can be overridden per-route.

typed_controller.route

route name, method \\ nil

Define 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

NameTypeDefaultDocs
nameatomThe controller action name (e.g. :login, :auth)
method:get | :post | :patch | :put | :delete:getThe HTTP method. Defaults to :get.

Options

NameTypeDefaultDocs
run(any, any -> any) | atomThe handler — an fn/2 closure or a module implementing AshTypescript.TypedController.Route
descriptionString.tJSDoc description for the generated TypeScript path helper
deprecatedboolean | String.tMark this route as deprecated. Set to true for a default message, or provide a custom deprecation notice.
seelist(atom)[]List of related route names to reference in JSDoc @see tags.
zod_schema_nameString.tOverride 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.
namespaceString.tNamespace for organizing this route into a separate file (becomes the filename). Overrides controller-level namespace.

typed_controller.route.argument

argument name, type

Define an argument for this route.

Arguments

NameTypeDefaultDocs
nameatomThe argument name
typeatom | {atom, keyword}The Ash type (e.g. :string, :boolean, :integer)

Options

NameTypeDefaultDocs
constraintskeyword[]Type constraints
allow_nil?booleantrueWhether this argument can be nil. Set to false to make it required.
defaultanyDefault value for this argument

Introspection

Target: AshTypescript.TypedController.Dsl.RouteArgument

Introspection

Target: AshTypescript.TypedController.Dsl.Route

typed_controller.get

get name

Define a GET route. Shorthand for route :name, :get.

Nested DSLs

Arguments

NameTypeDefaultDocs
nameatomThe controller action name (e.g. :login, :auth)

Options

NameTypeDefaultDocs
run(any, any -> any) | atomThe handler — an fn/2 closure or a module implementing AshTypescript.TypedController.Route
descriptionString.tJSDoc description for the generated TypeScript path helper
deprecatedboolean | String.tMark this route as deprecated. Set to true for a default message, or provide a custom deprecation notice.
seelist(atom)[]List of related route names to reference in JSDoc @see tags.
zod_schema_nameString.tOverride 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.
namespaceString.tNamespace for organizing this route into a separate file (becomes the filename). Overrides controller-level namespace.

typed_controller.get.argument

argument name, type

Define an argument for this route.

Arguments

NameTypeDefaultDocs
nameatomThe argument name
typeatom | {atom, keyword}The Ash type (e.g. :string, :boolean, :integer)

Options

NameTypeDefaultDocs
constraintskeyword[]Type constraints
allow_nil?booleantrueWhether this argument can be nil. Set to false to make it required.
defaultanyDefault value for this argument

Introspection

Target: AshTypescript.TypedController.Dsl.RouteArgument

Introspection

Target: AshTypescript.TypedController.Dsl.Route

typed_controller.post

post name

Define a POST route. Shorthand for route :name, :post.

Nested DSLs

Arguments

NameTypeDefaultDocs
nameatomThe controller action name (e.g. :login, :auth)

Options

NameTypeDefaultDocs
run(any, any -> any) | atomThe handler — an fn/2 closure or a module implementing AshTypescript.TypedController.Route
descriptionString.tJSDoc description for the generated TypeScript path helper
deprecatedboolean | String.tMark this route as deprecated. Set to true for a default message, or provide a custom deprecation notice.
seelist(atom)[]List of related route names to reference in JSDoc @see tags.
zod_schema_nameString.tOverride 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.
namespaceString.tNamespace for organizing this route into a separate file (becomes the filename). Overrides controller-level namespace.

typed_controller.post.argument

argument name, type

Define an argument for this route.

Arguments

NameTypeDefaultDocs
nameatomThe argument name
typeatom | {atom, keyword}The Ash type (e.g. :string, :boolean, :integer)

Options

NameTypeDefaultDocs
constraintskeyword[]Type constraints
allow_nil?booleantrueWhether this argument can be nil. Set to false to make it required.
defaultanyDefault value for this argument

Introspection

Target: AshTypescript.TypedController.Dsl.RouteArgument

Introspection

Target: AshTypescript.TypedController.Dsl.Route

typed_controller.patch

patch name

Define a PATCH route. Shorthand for route :name, :patch.

Nested DSLs

Arguments

NameTypeDefaultDocs
nameatomThe controller action name (e.g. :login, :auth)

Options

NameTypeDefaultDocs
run(any, any -> any) | atomThe handler — an fn/2 closure or a module implementing AshTypescript.TypedController.Route
descriptionString.tJSDoc description for the generated TypeScript path helper
deprecatedboolean | String.tMark this route as deprecated. Set to true for a default message, or provide a custom deprecation notice.
seelist(atom)[]List of related route names to reference in JSDoc @see tags.
zod_schema_nameString.tOverride 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.
namespaceString.tNamespace for organizing this route into a separate file (becomes the filename). Overrides controller-level namespace.

typed_controller.patch.argument

argument name, type

Define an argument for this route.

Arguments

NameTypeDefaultDocs
nameatomThe argument name
typeatom | {atom, keyword}The Ash type (e.g. :string, :boolean, :integer)

Options

NameTypeDefaultDocs
constraintskeyword[]Type constraints
allow_nil?booleantrueWhether this argument can be nil. Set to false to make it required.
defaultanyDefault value for this argument

Introspection

Target: AshTypescript.TypedController.Dsl.RouteArgument

Introspection

Target: AshTypescript.TypedController.Dsl.Route

typed_controller.put

put name

Define a PUT route. Shorthand for route :name, :put.

Nested DSLs

Arguments

NameTypeDefaultDocs
nameatomThe controller action name (e.g. :login, :auth)

Options

NameTypeDefaultDocs
run(any, any -> any) | atomThe handler — an fn/2 closure or a module implementing AshTypescript.TypedController.Route
descriptionString.tJSDoc description for the generated TypeScript path helper
deprecatedboolean | String.tMark this route as deprecated. Set to true for a default message, or provide a custom deprecation notice.
seelist(atom)[]List of related route names to reference in JSDoc @see tags.
zod_schema_nameString.tOverride 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.
namespaceString.tNamespace for organizing this route into a separate file (becomes the filename). Overrides controller-level namespace.

typed_controller.put.argument

argument name, type

Define an argument for this route.

Arguments

NameTypeDefaultDocs
nameatomThe argument name
typeatom | {atom, keyword}The Ash type (e.g. :string, :boolean, :integer)

Options

NameTypeDefaultDocs
constraintskeyword[]Type constraints
allow_nil?booleantrueWhether this argument can be nil. Set to false to make it required.
defaultanyDefault value for this argument

Introspection

Target: AshTypescript.TypedController.Dsl.RouteArgument

Introspection

Target: AshTypescript.TypedController.Dsl.Route

typed_controller.delete

delete name

Define a DELETE route. Shorthand for route :name, :delete.

Nested DSLs

Arguments

NameTypeDefaultDocs
nameatomThe controller action name (e.g. :login, :auth)

Options

NameTypeDefaultDocs
run(any, any -> any) | atomThe handler — an fn/2 closure or a module implementing AshTypescript.TypedController.Route
descriptionString.tJSDoc description for the generated TypeScript path helper
deprecatedboolean | String.tMark this route as deprecated. Set to true for a default message, or provide a custom deprecation notice.
seelist(atom)[]List of related route names to reference in JSDoc @see tags.
zod_schema_nameString.tOverride 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.
namespaceString.tNamespace for organizing this route into a separate file (becomes the filename). Overrides controller-level namespace.

typed_controller.delete.argument

argument name, type

Define an argument for this route.

Arguments

NameTypeDefaultDocs
nameatomThe argument name
typeatom | {atom, keyword}The Ash type (e.g. :string, :boolean, :integer)

Options

NameTypeDefaultDocs
constraintskeyword[]Type constraints
allow_nil?booleantrueWhether this argument can be nil. Set to false to make it required.
defaultanyDefault value for this argument

Introspection

Target: AshTypescript.TypedController.Dsl.RouteArgument

Introspection

Target: AshTypescript.TypedController.Dsl.Route