Skip to content

flareState

function flareState<T>(name?: string): StateTokenBuilder<T>

Creates a new Flare state token.

A token is a unique, nominal identifier for a piece of request-scoped state. Tokens are passed to ctx.require(), ctx.get(), and ctx.set() to read and write state on an FlareRequest.

The builder returned supports one optional call to each of .withDefault() and .from(), in either order.

When both .withDefault() and .from() are set on the same token, the derivation function is always tried first. The default acts as a fallback only when the derivation is absent or produces no value.

name? string · Optional display name used in error messages. Should match the variable name the token is assigned to.

returns StateTokenBuilder<T> A builder for declaring defaults and derivations.

Example:

const TenantState = flareState<{ tenantId: string }>("TenantState")
.withDefault({ tenantId: "anonymous" })
.from((ctx) => ({ tenantId: resolveFromRequest(ctx) }));

Learn: Declaring state

Last updated: