Astral.Route.Pattern (Astral v0.1.4)

Copy Markdown View Source

Elixir-style route patterns for generated and dynamic Astral routes.

Patterns use Plug/Phoenix-inspired path parameters in Elixir APIs:

/blog/:slug
/blog/*page
/tags/:tag/*page

:name captures one path segment. *name captures the rest of the path and must be the last segment. A missing glob is allowed, which makes patterns like /blog/*page useful for pagination where page one lives at /blog and later pages live at /blog/2, /blog/3, and so on.

Summary

Functions

Generate a concrete route path from a pattern and params.

Match a concrete path against a pattern and return captured params.

Return true when a concrete path matches a pattern.

Normalize route params to string keys.

Parse a route pattern.

Types

part()

@type part() :: {:static, String.t()} | {:param, String.t()} | {:glob, String.t()}

t()

@type t() :: %Astral.Route.Pattern{parts: [part()], source: String.t()}

Functions

generate(pattern, params \\ %{})

@spec generate(String.t() | t(), map() | keyword()) :: String.t()

Generate a concrete route path from a pattern and params.

match(pattern, path)

@spec match(String.t() | t(), String.t()) :: {:ok, map()} | :error

Match a concrete path against a pattern and return captured params.

match?(pattern, path)

@spec match?(String.t() | t(), String.t()) :: boolean()

Return true when a concrete path matches a pattern.

normalize_params(params)

@spec normalize_params(map() | keyword()) :: map()

Normalize route params to string keys.

parse(pattern)

@spec parse(String.t()) :: t()

Parse a route pattern.