Skip to content

@btravstack/htmx-server


@btravstack/htmx-server ​

Classes ​

HtmxFragmentsPort ​

Defined in: htmx-server/src/htmx-route.ts:241

Every route a fragment declares, composed into one port — the answerer's own reads it through routes and calls resolvePrincipal itself, which is why requirements and authenticators ride the port rather than staying in this closure.

Extends ​

  • PortInstance<"HtmxFragments", { authenticators: Readonly<Record<string, AuthenticatorService<unknown>>>; principals: Readonly<Record<string, AnyPort>>; routes: readonly FragmentAnswer[]; }>

Constructors ​

Constructor ​
ts
new HtmxFragmentsPort(): HtmxFragmentsPort;

Defined in: di/dist/index.d.mts:16

Returns ​

HtmxFragmentsPort

Inherited from ​
ts
Port("HtmxFragments")<{
  readonly routes: readonly FragmentAnswer[];
  readonly authenticators: Readonly<Record<string, AuthenticatorService<unknown>>>;
  / One port per scheme, so htmx() can seed the fork it opens for the caller. /
  readonly principals: Readonly<Record<string, AnyPort>>;
}>.constructor

Properties ​

PropertyModifierTypeDescriptionInherited fromDefined in
[ID]readonly"HtmxFragments"-Port("HtmxFragments").[ID]di/dist/index.d.mts:12
[SERVICE]readonlyobject-Port("HtmxFragments").[SERVICE]di/dist/index.d.mts:13
[SERVICE].authenticatorsreadonlyReadonly<Record<string, AuthenticatorService<unknown>>>--htmx-server/src/htmx-route.ts:243
[SERVICE].principalsreadonlyReadonly<Record<string, AnyPort>>One port per scheme, so htmx() can seed the fork it opens for the caller.-htmx-server/src/htmx-route.ts:245
[SERVICE].routesreadonlyreadonly FragmentAnswer[]--htmx-server/src/htmx-route.ts:242
portIdreadonly"HtmxFragments"-Port("HtmxFragments").portIddi/dist/index.d.mts:17

Type Aliases ​

FragmentAnswer ​

ts
type FragmentAnswer = object;

Defined in: htmx-server/src/htmx-route.ts:218

What the answerer reads back for one route, principal and body erased to unknown.

Properties ​

PropertyModifierTypeDescriptionDefined in
handlereadonly(context, params, input) => AsyncResult<Html, never>-htmx-server/src/htmx-route.ts:225
inputreadonlyFragmentInputSchema | undefined-htmx-server/src/htmx-route.ts:221
methodreadonly"GET" | "POST"-htmx-server/src/htmx-route.ts:219
pathreadonlystring-htmx-server/src/htmx-route.ts:220
requirementsreadonlyRequirements | undefined-htmx-server/src/htmx-route.ts:222
unitreadonlyReadonly<Record<string, AnyPort>>The declared unit: record, which the answerer resolves out of the fork it opens.htmx-server/src/htmx-route.ts:224

Html ​

ts
type Html = object;

Defined in: htmx-server/src/html.ts:13

A rendered HTML fragment — the output of html or raw, and nothing else.

An object rather than a string, so that what this package escaped is known by the value rather than by a registry, and so a fragment handler returning a bare template literal is a compile error rather than a stored-XSS bug.

Properties ​

PropertyModifierTypeDefined in
[HTML]readonlytruehtmx-server/src/html.ts:13
valuereadonlystringhtmx-server/src/html.ts:13

HtmxOptions ​

ts
type HtmxOptions = object;

Defined in: htmx-server/src/htmx.ts:15

Properties ​

PropertyModifierTypeDescriptionDefined in
login?readonly`/${string}`The login ROUTE — the path the login answerer serves, /auth/login for an oidc()({ prefix: "/auth" }), not the prefix it is mounted under. Set it and a route whose requires resolves Unauthenticated sends the caller there carrying ?return= — 303 Location for a navigating browser, 401 HX-Redirect for a request htmx made. Unset, that route answers a bare 401, and an under-scoped caller answers 403 either way.htmx-server/src/htmx.ts:26
prefix?readonly`/${string}`Where fragments are mounted. Default /.htmx-server/src/htmx.ts:17

ParamsOf ​

ts
type ParamsOf<P> = { readonly [K in ParamNames<P>]: string };

Defined in: htmx-server/src/fragments.ts:22

The parameters a path template names. "/orders/:id/row" is { readonly id: string }; a template naming none is an empty record.

Type Parameters ​

Type Parameter
P extends string

Functions ​

html() ​

ts
function html(strings, ...values): Html;

Defined in: htmx-server/src/html.ts:51

An HTML fragment, with every interpolation escaped.

ts
html`<tr id="order-${order.id}"><td>${order.customerName}</td></tr>`

A nested Html is spliced as it is, and an array of them is concatenated — so a list of rows needs no join. Anything else is stringified and escaped.

The escaping is context-blind: it protects element text and a quoted attribute value, and nothing else. An unquoted attribute, an attribute name, a URL scheme (href="${url}" does not vet javascript:), and <script>/<style> contents are the caller's own responsibility.

Parameters ​

ParameterType
stringsTemplateStringsArray
...valuesreadonly unknown[]

Returns ​

Html


htmx() ​

ts
function htmx(options?): Provider<HttpHandler, never, HtmxFragmentsPort | HttpConfig | HttpUnit> & object;

Defined in: htmx-server/src/htmx.ts:45

The htmx starter: fragments, as ONE answerer under the HTTP runtime. A request no route claims — outside every path, or on a path whose only route names a different method — resolves unwritten and the runtime answers its own 404.

routes is matched in the ORDER the composition root's piece array gave them, first match wins — and that ordering is a SECURITY property, not only a routing one. A public route declared BEFORE a requires-carrying route whose path can also match the same request answers it itself, and no authentication ever runs: two routes are two port ids, minted from their own method and path, so di has nothing to see collide, and a specificity rule is deliberately not provided (the ordering is the composition root's, on purpose). Declare a route that requires authentication before any public route whose path could also match its requests.

Parameters ​

ParameterType
optionsHtmxOptions

Returns ​

Provider<HttpHandler, never, HtmxFragmentsPort | HttpConfig | HttpUnit> & object


raw() ​

ts
function raw(markup): Html;

Defined in: htmx-server/src/html.ts:63

Markup admitted whole, unescaped. The only way past html's escaping, and a visible act at the call site — which is the point.

Parameters ​

ParameterType
markupstring

Returns ​

Html

Released under the MIT License.