PhoenixAssets.Generators.TS (PhoenixAssets v0.1.0)

View Source

Shared helpers for rendering generated TypeScript.

Every generator (routes, env, and -- in the stack package -- types, electric, pubsub, locales) emits TypeScript through these helpers so the output style is uniform: the same do-not-edit header, the same camelCase/PascalCase casing, and the same string-literal union rendering.

Summary

Functions

Converts a value to a lowerCamelCase identifier safe as a TS parameter name.

Converts a list of raw parameter names to unique lowerCamelCase argument names, in order.

Converts a value to lowerCamelCase.

The standard do-not-edit header prepended to every generated file.

Renders a value as a TypeScript object key, quoting when it is not a plain identifier.

Converts a value to UpperCamelCase (PascalCase).

Returns the path-parameter names in a route path, covering both :param segments and *glob segments (without their sigil).

Maps a primitive Elixir type atom to a TypeScript type, for inline payload shapes.

Renders a TypeScript string-literal union from a list of values.

Renders the import type { … } from "$phoenix/types" line for a list of type names, or an empty string when there are none.

Derives a TypeScript type name from a string or a module-like atom.

Renders a JS template-literal body for a route path, interpolating camelCased, URL-encoded parameters.

Functions

arg_name(value)

@spec arg_name(atom() | String.t()) :: String.t()

Converts a value to a lowerCamelCase identifier safe as a TS parameter name.

Reserved words gain a trailing underscore and a leading digit gains a leading underscore, so a route param named :new or :class cannot emit invalid TypeScript.

iex> PhoenixAssets.Generators.TS.arg_name("user_id")
"userId"
iex> PhoenixAssets.Generators.TS.arg_name("new")
"new_"

arg_names(params)

@spec arg_names([atom() | String.t()]) :: [String.t()]

Converts a list of raw parameter names to unique lowerCamelCase argument names, in order.

Two distinct route placeholders can camelize to the same identifier (user_id and userId both become userId); a collision gains a numeric suffix so the emitted TypeScript never declares duplicate parameters.

iex> PhoenixAssets.Generators.TS.arg_names(["user_id", "userId"])
["userId", "userId2"]

camelize(value)

@spec camelize(atom() | String.t()) :: String.t()

Converts a value to lowerCamelCase.

iex> PhoenixAssets.Generators.TS.camelize(:public_portfolios)
"publicPortfolios"

header()

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

The standard do-not-edit header prepended to every generated file.

object_key(value)

@spec object_key(atom() | String.t()) :: String.t()

Renders a value as a TypeScript object key, quoting when it is not a plain identifier.

iex> PhoenixAssets.Generators.TS.object_key(:user_id)
"user_id"
iex> PhoenixAssets.Generators.TS.object_key("kebab-case")
~s("kebab-case")

pascalize(value)

@spec pascalize(atom() | String.t()) :: String.t()

Converts a value to UpperCamelCase (PascalCase).

iex> PhoenixAssets.Generators.TS.pascalize(:portfolio_row)
"PortfolioRow"

path_params(path)

@spec path_params(String.t()) :: [String.t()]

Returns the path-parameter names in a route path, covering both :param segments and *glob segments (without their sigil).

iex> PhoenixAssets.Generators.TS.path_params("/users/:user_id/files/*path")
["user_id", "path"]

primitive(type)

@spec primitive(atom()) :: String.t()

Maps a primitive Elixir type atom to a TypeScript type, for inline payload shapes.

iex> PhoenixAssets.Generators.TS.primitive(:integer)
"number"

string_union(values)

@spec string_union([atom() | String.t()]) :: String.t()

Renders a TypeScript string-literal union from a list of values.

iex> PhoenixAssets.Generators.TS.string_union([:public, :private])
~s("public" | "private")

type_import(types)

@spec type_import([String.t()]) :: String.t()

Renders the import type { … } from "$phoenix/types" line for a list of type names, or an empty string when there are none.

iex> PhoenixAssets.Generators.TS.type_import([])
""
iex> PhoenixAssets.Generators.TS.type_import(["ArticleRow"])
~s(import type { ArticleRow } from "$phoenix/types"\n)

type_name(type)

@spec type_name(String.t() | atom()) :: String.t()

Derives a TypeScript type name from a string or a module-like atom.

iex> PhoenixAssets.Generators.TS.type_name(PhoenixAssets.Generators.TS)
"TS"
iex> PhoenixAssets.Generators.TS.type_name("PortfolioRow")
"PortfolioRow"

url_template(path)

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

Renders a JS template-literal body for a route path, interpolating camelCased, URL-encoded parameters.

:param segments encode as one URI component; *glob segments span multiple path segments, so each segment is encoded individually and the slashes are preserved.

iex> PhoenixAssets.Generators.TS.url_template("/users/:user_id")
"/users/${encodeURIComponent(String(userId))}"