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
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_"
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"]
Converts a value to lowerCamelCase.
iex> PhoenixAssets.Generators.TS.camelize(:public_portfolios)
"publicPortfolios"
@spec header() :: String.t()
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.
iex> PhoenixAssets.Generators.TS.object_key(:user_id)
"user_id"
iex> PhoenixAssets.Generators.TS.object_key("kebab-case")
~s("kebab-case")
Converts a value to UpperCamelCase (PascalCase).
iex> PhoenixAssets.Generators.TS.pascalize(:portfolio_row)
"PortfolioRow"
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"]
Maps a primitive Elixir type atom to a TypeScript type, for inline payload shapes.
iex> PhoenixAssets.Generators.TS.primitive(:integer)
"number"
Renders a TypeScript string-literal union from a list of values.
iex> PhoenixAssets.Generators.TS.string_union([:public, :private])
~s("public" | "private")
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)
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"
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))}"