Skip to content

Function: buildRouteUrl()

API / @xmachines/play-xstate / buildRouteUrl

function buildRouteUrl(routeTemplate, context?): string;

Defined in: packages/play-xstate/src/routing/build-url.ts:40

Build a full URL from a route template and the actor’s context.

Substitutes :param and :param? placeholders from context.params, then appends query params and hash fragments.

All parameter values must be in context.params. Flat context fields are not inspected — this is intentional so misspelled placeholders produce a compile-time error rather than silently resolving to undefined.

Parameters

ParameterTypeDescription
routeTemplatestringRoute path template, e.g. "/profile/:userId" or "/settings/:section?".
contextRouteContextActor context object. Route parameters must be in context.params; flat context fields are not inspected. Must include a query field (use query: {} when no query parameters are needed). Omitting query when params is present throws MissingQueryContextError to prevent silent query-string loss.

Returns

string

The fully resolved URL string.

Throws

When a required :param placeholder has no matching value in context. Optional parameters (:param?) are silently omitted when missing. Import the class from @xmachines/play-xstate/errors.

Throws

When the context has a params field but no query field. This indicates a routing-aware machine context that was not set up to receive query parameters, which would silently drop query params from play.route events. Import the class from @xmachines/play-xstate/errors.

Example

buildRouteUrl("/user/:id", { params: { id: "123" }, query: { tab: "profile" }, hash: "top" });
// → "/user/123?tab=profile#top"
buildRouteUrl("/settings/:section?", { params: {}, query: {} });
// → "/settings" (optional param omitted, no query string)