@real-router/core 0.2.4 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -66,9 +66,9 @@ router.start("/users/123", (err, state) => {
66
66
 
67
67
  Stops the router. [Wiki](https://github.com/greydragon888/real-router/wiki/stop)
68
68
 
69
- #### `router.isStarted()`
69
+ #### `router.isActive()`
70
70
 
71
- Returns whether the router is started. [Wiki](https://github.com/greydragon888/real-router/wiki/isStarted)
71
+ Returns whether the router is active (started and has current state). [Wiki](https://github.com/greydragon888/real-router/wiki/isActive)
72
72
 
73
73
  ---
74
74
 
@@ -159,10 +159,13 @@ Adds an event listener. Returns an unsubscribe function. [Wiki](https://github.c
159
159
  ```typescript
160
160
  import { events } from "@real-router/core";
161
161
 
162
- router.addEventListener(events.TRANSITION_START, (toState, fromState) => {
162
+ const unsubscribe = router.addEventListener(events.TRANSITION_START, (toState, fromState) => {
163
163
  console.log("Starting:", toState.name);
164
164
  });
165
165
 
166
+ // To remove listener, call the returned unsubscribe function
167
+ unsubscribe();
168
+
166
169
  // Available events:
167
170
  // ROUTER_START, ROUTER_STOP
168
171
  // TRANSITION_START, TRANSITION_SUCCESS, TRANSITION_ERROR, TRANSITION_CANCEL
@@ -197,6 +200,12 @@ router.useMiddleware((router) => (toState, fromState, done) => {
197
200
  });
198
201
  ```
199
202
 
203
+ #### `router.clearMiddleware()`
204
+
205
+ Clear all middleware.\
206
+ Returns: `void`\
207
+ [Wiki](https://github.com/greydragon888/real-router/wiki/clearMiddleware)
208
+
200
209
  ---
201
210
 
202
211
  ## Advanced API
@@ -208,37 +217,45 @@ Add a route definition at runtime.\
208
217
  `route: Route` — route configuration object\
209
218
  Returns: `void`\
210
219
  [Wiki](https://github.com/greydragon888/real-router/wiki/addRoute)
220
+
211
221
  #### `router.removeRoute(name: string): void`
212
222
  Remove a route by name.\
213
223
  `name: string` — route name to remove\
214
224
  Returns: `void`\
215
225
  [Wiki](https://github.com/greydragon888/real-router/wiki/removeRoute)
226
+
216
227
  #### `router.getRoute(name: string): Route | undefined`
217
228
  Get route definition by name.\
218
229
  `name: string` — route name\
219
230
  Returns: `Route | undefined`\
220
231
  [Wiki](https://github.com/greydragon888/real-router/wiki/getRoute)
232
+
221
233
  #### `router.hasRoute(name: string): boolean`
222
234
  Check if a route exists.\
223
235
  `name: string` — route name\
224
236
  Returns: `boolean`\
225
237
  [Wiki](https://github.com/greydragon888/real-router/wiki/hasRoute)
238
+
226
239
  #### `router.clearRoutes(): void`
227
240
  Remove all routes.
228
241
  Returns: `void`\
229
242
  [Wiki](https://github.com/greydragon888/real-router/wiki/clearRoutes)
243
+
230
244
  #### `router.updateRoute(name: string, updates: Partial<Route>): void`
231
245
  Update route configuration.\
232
246
  `name: string` — route name\
233
247
  `updates: Partial<Route>` — properties to update\
234
248
  Returns: `void`\
235
249
  [Wiki](https://github.com/greydragon888/real-router/wiki/updateRoute)
236
- #### `router.forward(fromRoute: string, toRoute: string): void`
237
- Set up route forwarding (redirect).\
238
- `fromRoute: string` — source route name\
239
- `toRoute: string` — target route name\
240
- Returns: `void`\
241
- [Wiki](https://github.com/greydragon888/real-router/wiki/forward)
250
+
251
+ **Note:** To set up route forwarding (redirect), use the `forwardTo` option in route configuration:
252
+
253
+ ```typescript
254
+ router.addRoute({ name: "old-url", path: "/old", forwardTo: "new-url" });
255
+ // Or update existing route
256
+ router.updateRoute("old-url", { forwardTo: "new-url" });
257
+ ```
258
+
242
259
  ---
243
260
 
244
261
  ### State Utilities
@@ -247,37 +264,21 @@ Returns: `void`\
247
264
  Get previous router state.\
248
265
  Returns: `State | undefined`\
249
266
  [Wiki](https://github.com/greydragon888/real-router/wiki/getPreviousState)
250
- #### `router.setState(state: State): void`
251
- Set state directly without navigation.\
252
- `state: State` state to set\
253
- Returns: `void`\
254
- [Wiki](https://github.com/greydragon888/real-router/wiki/setState)
255
- #### `router.makeState(name: string, params?: Params, path?: string, meta?: object): State`
256
- Create a state object.\
257
- `name: string` — route name\
258
- `params?: Params` — route parameters\
259
- `path?: string` — URL path\
260
- `meta?: object` — metadata\
261
- Returns: `State`\
262
- [Wiki](https://github.com/greydragon888/real-router/wiki/makeState)
263
- #### `router.buildState(name: string, params?: Params): State | undefined`
264
- Build state from route name.\
265
- `name: string` — route name\
266
- `params?: Params` — route parameters\
267
- Returns: `State | undefined`\
268
- [Wiki](https://github.com/greydragon888/real-router/wiki/buildState)
267
+
268
+ #### `router.shouldUpdateNode(nodeName: string): (toState, fromState?) => boolean`
269
+ Create a predicate to check if a route node should update during transition.\
270
+ `nodeName: string` — route node name\
271
+ Returns: predicate function\
272
+ [Wiki](https://github.com/greydragon888/real-router/wiki/shouldUpdateNode)
273
+
269
274
  #### `router.areStatesEqual(state1: State, state2: State, ignoreQueryParams?: boolean): boolean`
270
275
  Compare two states for equality.\
271
276
  `state1: State` — first state\
272
277
  `state2: State` — second state\
273
- `ignoreQueryParams?: boolean` — ignore query params (default: true)Returns: `boolean`\
274
- [Wiki](https://github.com/greydragon888/real-router/wiki/areStatesEqual)
275
- #### `router.areStatesDescendants(parentState: State, childState: State): boolean`
276
- Check if child state is descendant of parent.\
277
- `parentState: State` — parent state\
278
- `childState: State` — child state\
278
+ `ignoreQueryParams?: boolean` — ignore query params (default: true)\
279
279
  Returns: `boolean`\
280
- [Wiki](https://github.com/greydragon888/real-router/wiki/areStatesDescendants)
280
+ [Wiki](https://github.com/greydragon888/real-router/wiki/areStatesEqual)
281
+
281
282
  ---
282
283
 
283
284
  ### Path Operations
@@ -288,26 +289,24 @@ Build URL path from route name.\
288
289
  `params?: Params` — route parameters\
289
290
  Returns: `string`\
290
291
  [Wiki](https://github.com/greydragon888/real-router/wiki/buildPath)
291
- #### `router.matchPath(path: string): State | undefined`
292
- Match URL path to state.\
293
- `path: string` URL path to match\
294
- Returns: `State | undefined`\
295
- [Wiki](https://github.com/greydragon888/real-router/wiki/matchPath)
292
+
293
+ #### `router.buildUrl(name: string, params?: Params, options?: object): string`
294
+ Build full URL from route name (includes base path and query string).\
295
+ `name: string` route name\
296
+ `params?: Params` — route parameters\
297
+ `options?: object` — URL building options\
298
+ Returns: `string`\
299
+ [Wiki](https://github.com/greydragon888/real-router/wiki/buildUrl)
300
+
296
301
  #### `router.isActiveRoute(name: string, params?: Params, strictEquality?: boolean, ignoreQueryParams?: boolean): boolean`
297
302
  Check if route is currently active.\
298
303
  `name: string` — route name\
299
304
  `params?: Params` — route parameters\
300
- `strictEquality?: boolean` — exact match (default: false)`ignoreQueryParams?: boolean` — ignore query params (default: true)Returns: `boolean`\
305
+ `strictEquality?: boolean` — exact match (default: false)\
306
+ `ignoreQueryParams?: boolean` — ignore query params (default: true)\
307
+ Returns: `boolean`\
301
308
  [Wiki](https://github.com/greydragon888/real-router/wiki/isActiveRoute)
302
- #### `router.setRootPath(rootPath: string): void`
303
- Set root path prefix for all routes.\
304
- `rootPath: string` — root path prefix\
305
- Returns: `void`\
306
- [Wiki](https://github.com/greydragon888/real-router/wiki/setRootPath)
307
- #### `router.getRootPath(): string`
308
- Get root path prefix.\
309
- Returns: `string`\
310
- [Wiki](https://github.com/greydragon888/real-router/wiki/getRootPath)
309
+
311
310
  ---
312
311
 
313
312
  ### Dependencies
@@ -317,49 +316,64 @@ Get a dependency by name.\
317
316
  `name: string` — dependency name\
318
317
  Returns: `unknown`\
319
318
  [Wiki](https://github.com/greydragon888/real-router/wiki/getDependency)
319
+
320
320
  #### `router.getDependencies(): Dependencies`
321
321
  Get all dependencies.\
322
322
  Returns: `Dependencies`\
323
323
  [Wiki](https://github.com/greydragon888/real-router/wiki/getDependencies)
324
+
324
325
  #### `router.setDependency(name: string, value: unknown): void`
325
326
  Set a dependency.\
326
327
  `name: string` — dependency name\
327
328
  `value: unknown` — dependency value\
328
329
  Returns: `void`\
329
330
  [Wiki](https://github.com/greydragon888/real-router/wiki/setDependency)
331
+
330
332
  #### `router.setDependencies(deps: Dependencies): void`
331
333
  Set multiple dependencies.\
332
334
  `deps: Dependencies` — dependencies object\
333
335
  Returns: `void`\
334
336
  [Wiki](https://github.com/greydragon888/real-router/wiki/setDependencies)
337
+
335
338
  #### `router.hasDependency(name: string): boolean`
336
339
  Check if dependency exists.\
337
340
  `name: string` — dependency name\
338
341
  Returns: `boolean`\
339
342
  [Wiki](https://github.com/greydragon888/real-router/wiki/hasDependency)
343
+
340
344
  #### `router.removeDependency(name: string): void`
341
345
  Remove a dependency.\
342
346
  `name: string` — dependency name\
343
347
  Returns: `void`\
344
348
  [Wiki](https://github.com/greydragon888/real-router/wiki/removeDependency)
349
+
345
350
  #### `router.resetDependencies(): void`
346
351
  Remove all dependencies.\
347
352
  Returns: `void`\
348
353
  [Wiki](https://github.com/greydragon888/real-router/wiki/resetDependencies)
354
+
349
355
  ---
350
356
 
351
357
  ### Options
352
358
 
353
359
  #### `router.getOptions(): Options`
354
- Get router options.\
360
+ Get all router options.\
355
361
  Returns: `Options`\
356
362
  [Wiki](https://github.com/greydragon888/real-router/wiki/getOptions)
363
+
364
+ #### `router.getOption(name: keyof Options): unknown`
365
+ Get a single router option by name.\
366
+ `name: keyof Options` — option name\
367
+ Returns: option value\
368
+ [Wiki](https://github.com/greydragon888/real-router/wiki/getOption)
369
+
357
370
  #### `router.setOption(name: string, value: unknown): void`
358
371
  Set a router option. Must be called before `start()`.\
359
372
  `name: string` — option name\
360
373
  `value: unknown` — option value\
361
374
  Returns: `void`\
362
375
  [Wiki](https://github.com/greydragon888/real-router/wiki/setOption)
376
+
363
377
  ---
364
378
 
365
379
  ### Other
@@ -369,38 +383,72 @@ Clone router for SSR.\
369
383
  `dependencies?: Dependencies` — override dependencies\
370
384
  Returns: `Router`\
371
385
  [Wiki](https://github.com/greydragon888/real-router/wiki/clone)
372
- #### `router.isNavigating(): boolean`
373
- Check if navigation is in progress.\
374
- Returns: `boolean`\
375
- [Wiki](https://github.com/greydragon888/real-router/wiki/isNavigating)
376
- #### `router.isActive(name: string, params?: Params, strictEquality?: boolean, ignoreQueryParams?: boolean): boolean`
377
- Alias for `isActiveRoute`.\
386
+
387
+ #### `router.cancel(): void`
388
+ Cancel the current navigation in progress.\
389
+ Returns: `void`\
390
+ [Wiki](https://github.com/greydragon888/real-router/wiki/cancel)
391
+
392
+ ---
393
+
394
+ ## Plugin Development API
395
+
396
+ The following methods are designed for **plugin authors**. They provide low-level access for advanced use cases like browser history integration, persistent parameters, and custom navigation sources.
397
+
398
+ These methods are stable but intended for plugin development, not application code.
399
+
400
+ #### `router.matchPath(path: string, source?: string): State | undefined`
401
+ Match URL path to route state.\
402
+ `path: string` — URL path to match\
403
+ `source?: string` — navigation source identifier\
404
+ Returns: `State | undefined`\
405
+ [Wiki](https://github.com/greydragon888/real-router/wiki/matchPath)
406
+
407
+ #### `router.makeState(name, params?, path?, meta?, forceId?): State`
408
+ Create State with custom `meta.id` for history restoration.\
378
409
  `name: string` — route name\
379
410
  `params?: Params` — route parameters\
380
- `strictEquality?: boolean` — exact match\
381
- `ignoreQueryParams?: boolean` — ignore query params\
382
- Returns: `boolean`\
383
- [Wiki](https://github.com/greydragon888/real-router/wiki/isActive)
384
- #### `router.clearCanActivate(name: string): void`
385
- Clear activation guard for a route.\
411
+ `path?: string` — URL path\
412
+ `meta?: object` — metadata\
413
+ `forceId?: number` — force specific `meta.id` value\
414
+ Returns: `State`\
415
+ [Wiki](https://github.com/greydragon888/real-router/wiki/makeState)
416
+
417
+ #### `router.buildState(name: string, params?: Params): State | undefined`
418
+ Validate route and build state with segment metadata.\
386
419
  `name: string` — route name\
387
- Returns: `void`\
388
- [Wiki](https://github.com/greydragon888/real-router/wiki/clearCanActivate)
389
- #### `router.clearCanDeactivate(name: string): void`
390
- Clear deactivation guard for a route.\
420
+ `params?: Params` — route parameters\
421
+ Returns: `State | undefined`\
422
+ [Wiki](https://github.com/greydragon888/real-router/wiki/buildState)
423
+
424
+ #### `router.forwardState(name: string, params: Params): { name: string; params: Params }`
425
+ Resolve route forwarding and merge default params.\
391
426
  `name: string` — route name\
427
+ `params: Params` — route parameters\
428
+ Returns: `{ name, params }` — resolved route name and merged params\
429
+ [Wiki](https://github.com/greydragon888/real-router/wiki/forwardState)
430
+
431
+ #### `router.navigateToState(toState, fromState, opts, done, emitSuccess): CancelFn`
432
+ Navigate with pre-built State object.\
433
+ `toState: State` — target state\
434
+ `fromState: State | undefined` — current state\
435
+ `opts: NavigationOptions` — navigation options\
436
+ `done: DoneFn` — callback\
437
+ `emitSuccess: boolean` — whether to emit TRANSITION_SUCCESS\
438
+ Returns: `CancelFn`\
439
+ [Wiki](https://github.com/greydragon888/real-router/wiki/navigateToState)
440
+
441
+ #### `router.setRootPath(rootPath: string): void`
442
+ Dynamically modify router base path.\
443
+ `rootPath: string` — new root path prefix\
392
444
  Returns: `void`\
393
- [Wiki](https://github.com/greydragon888/real-router/wiki/clearCanDeactivate)
394
- #### `router.clearMiddleware(): void`
395
- Clear all middleware.\
396
- Returns: `void`\
397
- [Wiki](https://github.com/greydragon888/real-router/wiki/clearMiddleware)
398
- #### `router.removeEventListener(event: string, listener: Function): void`
399
- Remove event listener.\
400
- `event: string` — event name\
401
- `listener: Function` — listener to remove\
402
- Returns: `void`\
403
- [Wiki](https://github.com/greydragon888/real-router/wiki/removeEventListener)
445
+ [Wiki](https://github.com/greydragon888/real-router/wiki/setRootPath)
446
+
447
+ #### `router.getRootPath(): string`
448
+ Read current base path.\
449
+ Returns: `string`\
450
+ [Wiki](https://github.com/greydragon888/real-router/wiki/getRootPath)
451
+
404
452
  ---
405
453
 
406
454
  ## Configuration
@@ -468,6 +516,12 @@ See [RouterError](https://github.com/greydragon888/real-router/wiki/RouterError)
468
516
 
469
517
  ---
470
518
 
519
+ ## Migration from router5
520
+
521
+ See the [Migration Guide](https://github.com/greydragon888/real-router/wiki/migration-guide) for detailed guidance.
522
+
523
+ ---
524
+
471
525
  ## Related Packages
472
526
 
473
527
  - [@real-router/react](https://www.npmjs.com/package/@real-router/react) — React integration
@@ -1,5 +1,5 @@
1
- import { ErrorCodeKeys, ErrorCodeValues, ErrorCodeToValueMap, EventToNameMap, State, DefaultDependencies, Route, Options, Router } from '@real-router/types';
2
- export { ActivationFn, ActivationFnFactory, CancelFn, Config, DefaultDependencies, DoneFn, Listener, Middleware, MiddlewareFactory, NavigationOptions, Options, Params, Plugin, PluginFactory, Route, Router, SimpleState, State, StateMeta, SubscribeFn, SubscribeState, Subscription, Unsubscribe } from '@real-router/types';
1
+ import { EventToPluginMap, EventToNameMap, ErrorCodeKeys, ErrorCodeValues, ErrorCodeToValueMap, EventsKeys, State, DefaultDependencies, Options, Params, StateMetaInput, SimpleState, RouteTreeState, DoneFn, Unsubscribe, EventName, Plugin, NavigationOptions, CancelFn, SubscribeFn, Middleware, ActivationFn } from '@real-router/types';
2
+ export { ActivationFn, CancelFn, Config, DefaultDependencies, DoneFn, Listener, Middleware, NavigationOptions, Options, Params, Plugin, SimpleState, State, StateMeta, SubscribeFn, SubscribeState, Subscription, Unsubscribe } from '@real-router/types';
3
3
 
4
4
  type ConstantsKeys = "UNKNOWN_ROUTE";
5
5
  type Constants = Record<ConstantsKeys, string>;
@@ -15,12 +15,263 @@ declare const errorCodes: ErrorCodeToValueMap;
15
15
  * Special route names and identifiers.
16
16
  */
17
17
  declare const constants: Constants;
18
+ /**
19
+ * Plugin method names.
20
+ * Maps to methods that plugins can implement to hook into router lifecycle.
21
+ */
22
+ declare const plugins: EventToPluginMap;
18
23
  /**
19
24
  * Event names for router event system.
20
25
  * Used with addEventListener/removeEventListener for reactive subscriptions.
21
26
  */
22
27
  declare const events: EventToNameMap;
23
28
 
29
+ type EventMethodMap = {
30
+ [K in EventsKeys as (typeof events)[K]]: (typeof plugins)[K];
31
+ };
32
+ /**
33
+ * Observable state passed to subscribers
34
+ */
35
+ interface SubscribeState {
36
+ route: State;
37
+ previousRoute: State | undefined;
38
+ }
39
+ /**
40
+ * Observer interface per Observable spec
41
+ */
42
+ interface Observer {
43
+ next?: (value: SubscribeState) => void;
44
+ error?: (err: unknown) => void;
45
+ complete?: () => void;
46
+ }
47
+ /**
48
+ * Subscription interface per Observable spec
49
+ */
50
+ interface Subscription {
51
+ unsubscribe: () => void;
52
+ readonly closed: boolean;
53
+ }
54
+ /**
55
+ * Observable options for enhanced control
56
+ */
57
+ interface ObservableOptions {
58
+ /** AbortSignal for automatic unsubscription */
59
+ signal?: AbortSignal;
60
+ /** Replay current state to new subscribers (default: true) */
61
+ replay?: boolean;
62
+ }
63
+ /**
64
+ * Observable interface for TC39 compliance
65
+ */
66
+ interface RouterObservable {
67
+ [key: symbol]: () => RouterObservable;
68
+ subscribe: (observer: Observer | ((value: SubscribeState) => void), options?: ObservableOptions) => Subscription;
69
+ }
70
+
71
+ /**
72
+ * Symbol.observable polyfill declaration for TC39 proposal
73
+ *
74
+ * @see https://github.com/tc39/proposal-observable
75
+ */
76
+ declare global {
77
+ interface SymbolConstructor {
78
+ readonly observable: unique symbol;
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Router class with integrated namespace architecture.
84
+ *
85
+ * All functionality is provided by namespace classes:
86
+ * - OptionsNamespace: getOptions, setOption
87
+ * - DependenciesNamespace: get/set/remove dependencies
88
+ * - ObservableNamespace: event listeners, subscribe
89
+ * - StateNamespace: state storage (getState, setState, getPreviousState)
90
+ * - RoutesNamespace: route tree operations
91
+ * - RouteLifecycleNamespace: canActivate/canDeactivate guards
92
+ * - MiddlewareNamespace: middleware chain
93
+ * - PluginsNamespace: plugin lifecycle
94
+ * - NavigationNamespace: navigate, navigateToState
95
+ * - RouterLifecycleNamespace: start, stop, isStarted
96
+ *
97
+ * @internal This class implementation is internal. Use createRouter() instead.
98
+ */
99
+ declare class Router<Dependencies extends DefaultDependencies = DefaultDependencies> {
100
+ #private;
101
+ [key: string]: unknown;
102
+ /**
103
+ * @param routes - Route definitions
104
+ * @param options - Router options
105
+ * @param dependencies - DI dependencies
106
+ */
107
+ constructor(routes?: Route<Dependencies>[], options?: Partial<Options>, dependencies?: Dependencies);
108
+ addRoute(routes: Route<Dependencies>[] | Route<Dependencies>): this;
109
+ removeRoute(name: string): this;
110
+ clearRoutes(): this;
111
+ getRoute(name: string): Route<Dependencies> | undefined;
112
+ hasRoute(name: string): boolean;
113
+ updateRoute(name: string, updates: RouteConfigUpdate<Dependencies>): this;
114
+ isActiveRoute(name: string, params?: Params, strictEquality?: boolean, ignoreQueryParams?: boolean): boolean;
115
+ buildPath(route: string, params?: Params): string;
116
+ matchPath<P extends Params = Params, MP extends Params = Params>(path: string, source?: string): State<P, MP> | undefined;
117
+ setRootPath(rootPath: string): void;
118
+ getRootPath(): string;
119
+ makeState<P extends Params = Params, MP extends Params = Params>(name: string, params?: P, path?: string, meta?: StateMetaInput<MP>, forceId?: number): State<P, MP>;
120
+ getState<P extends Params = Params, MP extends Params = Params>(): State<P, MP> | undefined;
121
+ getPreviousState(): State | undefined;
122
+ areStatesEqual(state1: State | undefined, state2: State | undefined, ignoreQueryParams?: boolean): boolean;
123
+ forwardState<P extends Params = Params>(routeName: string, routeParams: P): SimpleState<P>;
124
+ buildState(routeName: string, routeParams: Params): RouteTreeState | undefined;
125
+ shouldUpdateNode(nodeName: string): (toState: State, fromState?: State) => boolean;
126
+ getOptions(): Options;
127
+ getOption<K extends keyof Options>(option: K): Options[K];
128
+ setOption(option: keyof Options, value: Options[keyof Options]): this;
129
+ isActive(): boolean;
130
+ start(...args: [] | [done: DoneFn] | [startPathOrState: string | State] | [startPathOrState: string | State, done: DoneFn]): this;
131
+ stop(): this;
132
+ canDeactivate(name: string, canDeactivateHandler: ActivationFnFactory<Dependencies> | boolean): this;
133
+ canActivate(name: string, canActivateHandler: ActivationFnFactory<Dependencies> | boolean): this;
134
+ usePlugin(...plugins: PluginFactory<Dependencies>[]): Unsubscribe;
135
+ useMiddleware(...middlewares: MiddlewareFactory<Dependencies>[]): Unsubscribe;
136
+ clearMiddleware(): this;
137
+ setDependency<K extends keyof Dependencies & string>(dependencyName: K, dependency: Dependencies[K]): this;
138
+ setDependencies(deps: Dependencies): this;
139
+ getDependency<K extends keyof Dependencies>(key: K): Dependencies[K];
140
+ getDependencies(): Partial<Dependencies>;
141
+ removeDependency(dependencyName: keyof Dependencies): this;
142
+ hasDependency(dependencyName: keyof Dependencies): boolean;
143
+ resetDependencies(): this;
144
+ addEventListener<E extends EventName>(eventName: E, cb: Plugin[EventMethodMap[E]]): Unsubscribe;
145
+ navigate(routeName: string, routeParamsOrDone?: Params | DoneFn, optionsOrDone?: NavigationOptions | DoneFn, done?: DoneFn): CancelFn;
146
+ navigateToDefault(optsOrDone?: NavigationOptions | DoneFn, done?: DoneFn): CancelFn;
147
+ navigateToState(toState: State, fromState: State | undefined, opts: NavigationOptions, callback: DoneFn, emitSuccess: boolean): CancelFn;
148
+ subscribe(listener: SubscribeFn): Unsubscribe;
149
+ /**
150
+ * TC39 Observable spec: router[Symbol.observable]() returns observable
151
+ */
152
+ [Symbol.observable](): RouterObservable;
153
+ /**
154
+ * RxJS compatibility: router["@@observable"]() returns observable
155
+ */
156
+ ["@@observable"](): RouterObservable;
157
+ clone(dependencies?: Dependencies): Router<Dependencies>;
158
+ }
159
+
160
+ /**
161
+ * Router-dependent types.
162
+ *
163
+ * These types reference the Router class and are therefore defined in core
164
+ * rather than core-types to avoid circular dependencies.
165
+ */
166
+
167
+ /**
168
+ * Extended build result that includes segments for path building.
169
+ * Used internally to avoid duplicate getSegmentsByName calls.
170
+ *
171
+ * @param segments - Route segments from getSegmentsByName (typed as unknown[] for cross-package compatibility)
172
+ * @internal
173
+ */
174
+ interface BuildStateResultWithSegments<P extends Params = Params> {
175
+ readonly state: RouteTreeState<P>;
176
+ readonly segments: readonly unknown[];
177
+ }
178
+ /**
179
+ * Route configuration.
180
+ */
181
+ interface Route<Dependencies extends DefaultDependencies = DefaultDependencies> {
182
+ [key: string]: unknown;
183
+ /** Route name (dot-separated for nested routes). */
184
+ name: string;
185
+ /** URL path pattern for this route. */
186
+ path: string;
187
+ /** Factory function that returns a guard for route activation. */
188
+ canActivate?: ActivationFnFactory<Dependencies>;
189
+ /**
190
+ * Redirects navigation to another route.
191
+ *
192
+ * IMPORTANT: forwardTo creates a URL alias, not a transition chain.
193
+ * Guards (canActivate) on the source route are NOT executed.
194
+ * Only guards on the final destination are executed.
195
+ *
196
+ * This matches Vue Router and Angular Router behavior.
197
+ *
198
+ * @example
199
+ * // Correct: guard on target
200
+ * { name: "old", path: "/old", forwardTo: "new" }
201
+ * { name: "new", path: "/new", canActivate: myGuard }
202
+ *
203
+ * // Wrong: guard on source (will be ignored with warning)
204
+ * { name: "old", path: "/old", forwardTo: "new", canActivate: myGuard }
205
+ */
206
+ forwardTo?: string;
207
+ /** Nested child routes. */
208
+ children?: Route<Dependencies>[];
209
+ /** Encodes state params to URL params. */
210
+ encodeParams?: (stateParams: Params) => Params;
211
+ /** Decodes URL params to state params. */
212
+ decodeParams?: (pathParams: Params) => Params;
213
+ /**
214
+ * Default parameters for this route.
215
+ *
216
+ * @remarks
217
+ * **Type Contract:**
218
+ * The type of defaultParams MUST match the expected params type P
219
+ * when using `router.makeState<P>()` or `router.navigate<P>()`.
220
+ *
221
+ * These values are merged into state.params when creating route states.
222
+ * Missing URL params are filled from defaultParams.
223
+ *
224
+ * @example
225
+ * ```typescript
226
+ * // Define route with pagination defaults
227
+ * {
228
+ * name: "users",
229
+ * path: "/users",
230
+ * defaultParams: { page: 1, limit: 10 }
231
+ * }
232
+ *
233
+ * // Navigate without specifying page/limit
234
+ * router.navigate("users", { filter: "active" });
235
+ * // Result: state.params = { page: 1, limit: 10, filter: "active" }
236
+ *
237
+ * // Correct typing — include defaultParams properties
238
+ * type UsersParams = { page: number; limit: number; filter?: string };
239
+ * ```
240
+ */
241
+ defaultParams?: Params;
242
+ }
243
+ /**
244
+ * Configuration update options for updateRoute().
245
+ * All properties are optional. Set to null to remove the configuration.
246
+ */
247
+ interface RouteConfigUpdate<Dependencies extends DefaultDependencies = DefaultDependencies> {
248
+ /** Set to null to remove forwardTo */
249
+ forwardTo?: string | null;
250
+ /** Set to null to remove defaultParams */
251
+ defaultParams?: Params | null;
252
+ /** Set to null to remove decoder */
253
+ decodeParams?: ((params: Params) => Params) | null;
254
+ /** Set to null to remove encoder */
255
+ encodeParams?: ((params: Params) => Params) | null;
256
+ /** Set to null to remove canActivate */
257
+ canActivate?: ActivationFnFactory<Dependencies> | null;
258
+ }
259
+ /**
260
+ * Factory function for creating activation guards.
261
+ * Receives the router instance and a dependency getter.
262
+ */
263
+ type ActivationFnFactory<Dependencies extends DefaultDependencies = DefaultDependencies> = (router: Router<Dependencies>, getDependency: <K extends keyof Dependencies>(key: K) => Dependencies[K]) => ActivationFn;
264
+ /**
265
+ * Factory function for creating middleware.
266
+ * Receives the router instance and a dependency getter.
267
+ */
268
+ type MiddlewareFactory<Dependencies extends DefaultDependencies = DefaultDependencies> = (router: Router<Dependencies>, getDependency: <K extends keyof Dependencies>(key: K) => Dependencies[K]) => Middleware;
269
+ /**
270
+ * Factory function for creating plugins.
271
+ * Receives the router instance and a dependency getter.
272
+ */
273
+ type PluginFactory<Dependencies extends DefaultDependencies = DefaultDependencies> = (router: Router<Dependencies>, getDependency: <K extends keyof Dependencies>(key: K) => Dependencies[K]) => Plugin;
274
+
24
275
  declare class RouterError extends Error {
25
276
  [key: string]: unknown;
26
277
  readonly segment: string | undefined;
@@ -210,7 +461,20 @@ declare class RouterError extends Error {
210
461
 
211
462
  /**
212
463
  * Creates a new router instance.
464
+ *
465
+ * @param routes - Array of route definitions
466
+ * @param options - Router configuration options
467
+ * @param dependencies - Dependencies to inject into the router
468
+ * @returns A new Router instance
469
+ *
470
+ * @example
471
+ * const router = createRouter([
472
+ * { name: 'home', path: '/' },
473
+ * { name: 'users', path: '/users' },
474
+ * ]);
475
+ *
476
+ * router.start('/');
213
477
  */
214
478
  declare const createRouter: <Dependencies extends DefaultDependencies = DefaultDependencies>(routes?: Route<Dependencies>[], options?: Partial<Options>, dependencies?: Dependencies) => Router<Dependencies>;
215
479
 
216
- export { type Constants, type ErrorCodes, RouterError, constants, createRouter, errorCodes, events };
480
+ export { type ActivationFnFactory, type BuildStateResultWithSegments, type Constants, type ErrorCodes, type MiddlewareFactory, type PluginFactory, type Route, type RouteConfigUpdate, Router, RouterError, constants, createRouter, errorCodes, events };