groutcho › Core @diso.io/groutcho-core
Reference
history
History |
Pluggable history/location seam. |
createBrowserHistory |
Browser history backed by window.location + popstate. |
createMemoryHistory |
In-memory history for SSR, tests, and non-DOM runtimes. |
RouterStoreConfig |
|
StoreListener |
Snapshot listener (React's useSyncExternalStore shape). |
GoListener |
Fires on each successful nav with both the previous and the new match. |
TitleListener |
Fires when the title changes (from a nav or an explicit setTitle). |
ErrorListener |
Fires when an error is set on the current match (typically from an adapter). |
GoOptions |
A framework-agnostic router store: wraps a Router + a History, keeps a current snapshot in sync with the location, and exposes subscribe/getSnapshot for React useSyncExternalStore plus richer channels (onGo, onTitle, onError) that adapters wire to effects. |
RouterStore |
|
createRouter |
History
interface History
Pluggable history/location seam. The core never touches window directly —
only createBrowserHistory does — so the matching engine and store run in any
JS runtime (browser, Node, workers, React Native) with an appropriate history.
location(): string |
Current location as a root-relative url (pathname + search + hash). |
push(url: string): void |
Navigate, pushing a new entry. |
replace(url: string): void |
Navigate, replacing the current entry (used for redirects). |
back(): void |
Go back one entry in the history stack. |
forward(): void |
Go forward one entry in the history stack. |
subscribe(listener: () => void): () => void |
Subscribe to external location changes (e.g. back/forward). Returns an unsubscribe. |
createBrowserHistory
function createBrowserHistory(): History
Browser history backed by window.location + popstate.
createMemoryHistory
function createMemoryHistory(initial = "/"): History
In-memory history for SSR, tests, and non-DOM runtimes.
RouterStoreConfig
interface RouterStoreConfig extends RouterConfig
history?: History |
History implementation. Defaults to browser when a DOM is present, else memory. |
StoreListener
type StoreListener = (match: MatchResult) => void;
Snapshot listener (React's useSyncExternalStore shape). Fires with the new match.
GoListener
type GoListener = (prev: MatchResult, current: MatchResult) => void;
Fires on each successful nav with both the previous and the new match.
TitleListener
type TitleListener = (title: string) => void;
Fires when the title changes (from a nav or an explicit setTitle).
ErrorListener
type ErrorListener = (error: RouteError | undefined) => void;
Fires when an error is set on the current match (typically from an adapter).
GoOptions
interface GoOptions
A framework-agnostic router store: wraps a Router + a History, keeps a
current snapshot in sync with the location, and exposes subscribe/getSnapshot
for React useSyncExternalStore plus richer channels (onGo, onTitle,
onError) that adapters wire to effects.
Naming: the router action is go everywhere. Subscriptions to that action
are onGo. Never navigate.
replace?: boolean |
Replace the current history entry instead of pushing a new one. |
RouterStore
interface RouterStore
readonly router: Router |
|
readonly history: History |
|
readonly errorPage: Component |
Fallback error UI (from config). Adapters use this when a route has no errorPage. |
match(input: Input): MatchResult |
Match without navigating. |
go(input: Input, options?: GoOptions): MatchResult |
Navigate to input, updating history and notifying subscribers. Pass { replace: true } to overwrite the current history entry (useful for search-param edits that shouldn't add a back button entry). |
get(name: string): Route |
Shortcut for router.get(name) — look up a route by name. |
getSnapshot: () => MatchResult |
Current resolved match for the location. |
subscribe: (listener: StoreListener) => () => void |
Subscribe to snapshot changes. Returns an unsubscribe. |
onGo(listener: GoListener): () => void |
Subscribe to nav events. Fires with (prev, current) after each successful navigation. Returns an unsubscribe. |
readonly title: string |
Current title string. Recomputed from match.route.title on each nav. |
setTitle(title: string): void |
Set the title (fires onTitle; overrides route title until next nav). |
onTitle(listener: TitleListener): () => void |
|
setError(error: RouteError | undefined): void |
Set an error on the current match (fires onError). Called by adapters. |
onError(listener: ErrorListener): () => void |
|
destroy(): void |
Detach from history and drop all subscribers. |
createRouter
function createRouter(config: RouterStoreConfig): RouterStore
logger
const logger
MatchResult
MatchResultInit |
|
MatchResult |
The result of a match: which route (if any) matched, the resolved params and url, whether it represents a redirect, and — for redirects — the original match that triggered it. |
MatchResultInit
interface MatchResultInit {
input: InputObject
route?: Route | null
url?: string | null
params?: Params
redirect?: boolean
}
MatchResult
class MatchResult
The result of a match: which route (if any) matched, the resolved params and
url, whether it represents a redirect, and — for redirects — the original
match that triggered it.
key is populated by the store and increments on each successful navigation;
it's useful as an effect-dependency when you want to run something on every
nav even if the URL/params look the same.
error is populated by adapters when a page render throws.
input: InputObject |
|
route: Route | null |
|
params: Params |
|
redirect: boolean |
|
original: MatchResult | null |
|
url: string |
|
key: number |
|
error: RouteError | undefined |
|
constructor({ input, route = null, url = null, params = {}, redirect = false, }: MatchResultInit) |
|
isRedirect({ original }: { original: MatchResult | false }): void |
Mark this result as a redirect, recording the original match it came from. |
Route
class Route
name!: string |
|
pattern!: string |
|
page: unknown |
|
redirect?: RouteRedirect |
|
[key: string]: unknown |
|
constructor(config: RouteConfig) |
|
match(input: InputObject): MatchResult | false |
Match against a url input or a route input. |
is(test: string): boolean |
Test whether this route matches. A /-containing string is matched against the pattern; otherwise it's compared to the route name. |
href(params: Params = {}): string |
Build a URL for this route from the given params. |
Router
class Router
routes: Route[] |
|
redirects: RedirectEntry[] |
|
max_redirects: number |
|
constructor({ routes, redirects = {}, max_redirects = 10 }: RouterConfig) |
|
addRoutes(routes: Record<string, RouteConfig>): void |
|
get(name: string): Route |
Lookup a route by name. Throws if none is registered under that name. |
find(query: Record<string, unknown>): Route | undefined |
Find a route by arbitrary field equality. Returns undefined when none match. |
match(input: Input): MatchResult |
Match input against the routes, following any redirects. Returns the resolved (possibly redirect) MatchResult. |
normalizeInput(input: Input): InputObject |
|
go(input: Input): MatchResult |
Resolve an input. Kept as an alias for {@link match} so Router can be used directly in tests without going through a store. The store's go (in history.ts) is what apps use — it updates history and notifies listeners. |
types
Params |
Route/query params. |
Component |
Opaque component reference. |
TitleValue |
A route-defined title. |
RouteError |
Structured error surfaced through a match. |
RouteInput |
A route reference by name, optionally with params. |
InputObject |
The object form of router input. |
Input |
Anything accepted by Router.match/go: a string or an input object. |
RedirectResult |
What a redirect returns: a new input to follow, or a falsy value meaning "no redirect". |
RouteRedirect |
Per-route redirect: receives the current match, returns where to go (or false). |
RedirectTest |
Global redirect test: receives the current match (or false when nothing matched, e.g. |
RouteConfig |
|
RouterConfig |
Params
type Params = Record<string, string | string[] | undefined>;
Route/query params. Values come from URLPattern matching (strings) or query
string parsing (string, or string[] for repeated keys), and may be
undefined for optional path params that weren't present in the URL.
Component
type Component = unknown;
Opaque component reference. Core doesn't render anything — adapters (react,
lit, etc.) interpret whatever the app stores under page, layout, and
errorPage. Kept as unknown so core stays framework-agnostic.
TitleValue
type TitleValue = string | ((match: MatchResult) => string);
A route-defined title. Either a fixed string, or a function that derives one
from the current match (e.g. (m) => Album: ${m.params.slug}``).
RouteError
interface RouteError {
message: string
cause?: unknown
status?: number
}
Structured error surfaced through a match. Adapters populate this when a page
render throws (see react/lit <Outlet> equivalents). useError / onError
read it.
RouteInput
interface RouteInput {
name: string
params?: Params
}
A route reference by name, optionally with params.
InputObject
interface InputObject {
url?: string
route?: RouteInput
name?: string
params?: Params
[key: string]: unknown
}
The object form of router input. Either a url, a route reference, or a
name shorthand — plus any arbitrary "extra" keys that redirect logic can read
(these are threaded through redirects but ignored by matching).
Input
type Input = string | InputObject;
Anything accepted by Router.match/go: a string or an input object.
RedirectResult
type RedirectResult = Input | false | null | undefined;
What a redirect returns: a new input to follow, or a falsy value meaning "no redirect".
RouteRedirect
type RouteRedirect = (match: MatchResult) => RedirectResult;
Per-route redirect: receives the current match, returns where to go (or false).
RedirectTest
type RedirectTest = (match: MatchResult | false) => RedirectResult;
Global redirect test: receives the current match (or false when nothing
matched, e.g. for a NotFound rule), returns where to go (or false).
RouteConfig
interface RouteConfig
name?: string |
Set automatically from the routes map key; may be provided directly too. |
pattern: string |
URLPattern pathname pattern, e.g. /show/:title or /optional/:opt?. |
page: Component |
Arbitrary payload associated with the route (component, handler, etc). |
redirect?: RouteRedirect |
Optional per-route redirect. |
title?: TitleValue |
Route title. String or (match) => string. When the router navigates to this route the store applies it via setTitle; adapters mirror it to document.title. Runtime overrides via store.setTitle (see React useTitle) win until the next navigation. |
layout?: Component |
Layout component (or ordered chain, outermost first) wrapping page. Adapters render layouts.reduceRight((child, L) => <L>{child}</L>, page). Sibling routes sharing the same layout component identity get instance persistence via the adapter's reconciliation. |
errorPage?: Component |
Per-route error UI. Adapters render this when the page render throws. |
[key: string]: unknown |
Arbitrary route metadata (e.g. session, role) readable by redirects. |
RouterConfig
interface RouterConfig
routes: Record<string, RouteConfig> |
|
redirects?: Record<string, RedirectTest> |
|
max_redirects?: number |
|
errorPage?: Component |
Fallback error UI. Adapters use this when a route has no errorPage. |