@real-router/core 0.3.0 → 0.5.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,44 @@ 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
+ ```
242
258
 
243
259
  ---
244
260
 
@@ -248,42 +264,13 @@ Returns: `void`\
248
264
  Get previous router state.\
249
265
  Returns: `State | undefined`\
250
266
  [Wiki](https://github.com/greydragon888/real-router/wiki/getPreviousState)
251
- #### `router.setState(state: State): void`
252
- Set state directly without navigation.\
253
- `state: State` — state to set\
254
- Returns: `void`\
255
- [Wiki](https://github.com/greydragon888/real-router/wiki/setState)
256
- #### `router.makeState(name: string, params?: Params, path?: string, meta?: object): State`
257
- Create a state object.\
258
- `name: string` — route name\
259
- `params?: Params` — route parameters\
260
- `path?: string` — URL path\
261
- `meta?: object` — metadata\
262
- Returns: `State`\
263
- [Wiki](https://github.com/greydragon888/real-router/wiki/makeState)
264
- #### `router.buildState(name: string, params?: Params): State | undefined`
265
- Build state from route name.\
266
- `name: string` — route name\
267
- `params?: Params` — route parameters\
268
- Returns: `State | undefined`\
269
- [Wiki](https://github.com/greydragon888/real-router/wiki/buildState)
270
- #### `router.buildStateWithSegments(name: string, params?: Params): { state: State; segments: RouteSegment[] } | undefined`
271
- Build state with route segments for advanced use cases.\
272
- `name: string` — route name\
273
- `params?: Params` — route parameters\
274
- Returns: `{ state, segments } | undefined`\
275
- [Wiki](https://github.com/greydragon888/real-router/wiki/buildStateWithSegments)
276
- #### `router.forwardState(name: string, params: Params): { name: string; params: Params }`
277
- Resolve route forwarding and merge default params.\
278
- `name: string` — route name\
279
- `params: Params` — route parameters\
280
- Returns: `{ name, params }` — resolved route name and merged params\
281
- [Wiki](https://github.com/greydragon888/real-router/wiki/forwardState)
267
+
282
268
  #### `router.shouldUpdateNode(nodeName: string): (toState, fromState?) => boolean`
283
269
  Create a predicate to check if a route node should update during transition.\
284
270
  `nodeName: string` — route node name\
285
271
  Returns: predicate function\
286
272
  [Wiki](https://github.com/greydragon888/real-router/wiki/shouldUpdateNode)
273
+
287
274
  #### `router.areStatesEqual(state1: State, state2: State, ignoreQueryParams?: boolean): boolean`
288
275
  Compare two states for equality.\
289
276
  `state1: State` — first state\
@@ -291,12 +278,6 @@ Compare two states for equality.\
291
278
  `ignoreQueryParams?: boolean` — ignore query params (default: true)\
292
279
  Returns: `boolean`\
293
280
  [Wiki](https://github.com/greydragon888/real-router/wiki/areStatesEqual)
294
- #### `router.areStatesDescendants(parentState: State, childState: State): boolean`
295
- Check if child state is descendant of parent.\
296
- `parentState: State` — parent state\
297
- `childState: State` — child state\
298
- Returns: `boolean`\
299
- [Wiki](https://github.com/greydragon888/real-router/wiki/areStatesDescendants)
300
281
 
301
282
  ---
302
283
 
@@ -308,11 +289,15 @@ Build URL path from route name.\
308
289
  `params?: Params` — route parameters\
309
290
  Returns: `string`\
310
291
  [Wiki](https://github.com/greydragon888/real-router/wiki/buildPath)
311
- #### `router.matchPath(path: string): State | undefined`
312
- Match URL path to state.\
313
- `path: string` URL path to match\
314
- Returns: `State | undefined`\
315
- [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
+
316
301
  #### `router.isActiveRoute(name: string, params?: Params, strictEquality?: boolean, ignoreQueryParams?: boolean): boolean`
317
302
  Check if route is currently active.\
318
303
  `name: string` — route name\
@@ -321,15 +306,6 @@ Check if route is currently active.\
321
306
  `ignoreQueryParams?: boolean` — ignore query params (default: true)\
322
307
  Returns: `boolean`\
323
308
  [Wiki](https://github.com/greydragon888/real-router/wiki/isActiveRoute)
324
- #### `router.setRootPath(rootPath: string): void`
325
- Set root path prefix for all routes.\
326
- `rootPath: string` — root path prefix\
327
- Returns: `void`\
328
- [Wiki](https://github.com/greydragon888/real-router/wiki/setRootPath)
329
- #### `router.getRootPath(): string`
330
- Get root path prefix.\
331
- Returns: `string`\
332
- [Wiki](https://github.com/greydragon888/real-router/wiki/getRootPath)
333
309
 
334
310
  ---
335
311
 
@@ -340,31 +316,37 @@ Get a dependency by name.\
340
316
  `name: string` — dependency name\
341
317
  Returns: `unknown`\
342
318
  [Wiki](https://github.com/greydragon888/real-router/wiki/getDependency)
319
+
343
320
  #### `router.getDependencies(): Dependencies`
344
321
  Get all dependencies.\
345
322
  Returns: `Dependencies`\
346
323
  [Wiki](https://github.com/greydragon888/real-router/wiki/getDependencies)
324
+
347
325
  #### `router.setDependency(name: string, value: unknown): void`
348
326
  Set a dependency.\
349
327
  `name: string` — dependency name\
350
328
  `value: unknown` — dependency value\
351
329
  Returns: `void`\
352
330
  [Wiki](https://github.com/greydragon888/real-router/wiki/setDependency)
331
+
353
332
  #### `router.setDependencies(deps: Dependencies): void`
354
333
  Set multiple dependencies.\
355
334
  `deps: Dependencies` — dependencies object\
356
335
  Returns: `void`\
357
336
  [Wiki](https://github.com/greydragon888/real-router/wiki/setDependencies)
337
+
358
338
  #### `router.hasDependency(name: string): boolean`
359
339
  Check if dependency exists.\
360
340
  `name: string` — dependency name\
361
341
  Returns: `boolean`\
362
342
  [Wiki](https://github.com/greydragon888/real-router/wiki/hasDependency)
343
+
363
344
  #### `router.removeDependency(name: string): void`
364
345
  Remove a dependency.\
365
346
  `name: string` — dependency name\
366
347
  Returns: `void`\
367
348
  [Wiki](https://github.com/greydragon888/real-router/wiki/removeDependency)
349
+
368
350
  #### `router.resetDependencies(): void`
369
351
  Remove all dependencies.\
370
352
  Returns: `void`\
@@ -378,11 +360,13 @@ Returns: `void`\
378
360
  Get all router options.\
379
361
  Returns: `Options`\
380
362
  [Wiki](https://github.com/greydragon888/real-router/wiki/getOptions)
363
+
381
364
  #### `router.getOption(name: keyof Options): unknown`
382
365
  Get a single router option by name.\
383
366
  `name: keyof Options` — option name\
384
367
  Returns: option value\
385
368
  [Wiki](https://github.com/greydragon888/real-router/wiki/getOption)
369
+
386
370
  #### `router.setOption(name: string, value: unknown): void`
387
371
  Set a router option. Must be called before `start()`.\
388
372
  `name: string` — option name\
@@ -399,34 +383,71 @@ Clone router for SSR.\
399
383
  `dependencies?: Dependencies` — override dependencies\
400
384
  Returns: `Router`\
401
385
  [Wiki](https://github.com/greydragon888/real-router/wiki/clone)
402
- #### `router.isNavigating(): boolean`
403
- Check if navigation is in progress.\
404
- Returns: `boolean`\
405
- [Wiki](https://github.com/greydragon888/real-router/wiki/isNavigating)
406
- #### `router.isActive(): boolean`
407
- Check if router is active (started and has current state).\
408
- Returns: `boolean`\
409
- [Wiki](https://github.com/greydragon888/real-router/wiki/isActive)
410
- #### `router.clearCanActivate(name: string): void`
411
- Clear activation guard for a route.\
412
- `name: string` — route name\
386
+
387
+ #### `router.cancel(): void`
388
+ Cancel the current navigation in progress.\
413
389
  Returns: `void`\
414
- [Wiki](https://github.com/greydragon888/real-router/wiki/clearCanActivate)
415
- #### `router.clearCanDeactivate(name: string): void`
416
- Clear deactivation guard for a route.\
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.\
417
409
  `name: string` — route name\
410
+ `params?: Params` — route parameters\
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.\
419
+ `name: string` — route name\
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.\
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\
418
444
  Returns: `void`\
419
- [Wiki](https://github.com/greydragon888/real-router/wiki/clearCanDeactivate)
420
- #### `router.clearMiddleware(): void`
421
- Clear all middleware.\
422
- Returns: `void`\
423
- [Wiki](https://github.com/greydragon888/real-router/wiki/clearMiddleware)
424
- #### `router.removeEventListener(event: string, listener: Function): void`
425
- Remove event listener.\
426
- `event: string` — event name\
427
- `listener: Function` — listener to remove\
428
- Returns: `void`\
429
- [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)
430
451
 
431
452
  ---
432
453
 
@@ -495,6 +516,12 @@ See [RouterError](https://github.com/greydragon888/real-router/wiki/RouterError)
495
516
 
496
517
  ---
497
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
+
498
525
  ## Related Packages
499
526
 
500
527
  - [@real-router/react](https://www.npmjs.com/package/@real-router/react) — React integration
@@ -1,4 +1,4 @@
1
- import { EventToPluginMap, EventToNameMap, ErrorCodeKeys, ErrorCodeValues, ErrorCodeToValueMap, EventsKeys, State, DefaultDependencies, Options, Params, StateMetaInput, NavigationOptions, SimpleState, RouteTreeState, DoneFn, ActivationFn, Unsubscribe, Middleware, RouterError as RouterError$1, EventName, Plugin, CancelFn, SubscribeFn } 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
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";
@@ -117,42 +117,23 @@ declare class Router<Dependencies extends DefaultDependencies = DefaultDependenc
117
117
  setRootPath(rootPath: string): void;
118
118
  getRootPath(): string;
119
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
- makeNotFoundState(path: string, options?: NavigationOptions): State;
121
120
  getState<P extends Params = Params, MP extends Params = Params>(): State<P, MP> | undefined;
122
- setState<P extends Params = Params, MP extends Params = Params>(state?: State<P, MP>): void;
123
121
  getPreviousState(): State | undefined;
124
122
  areStatesEqual(state1: State | undefined, state2: State | undefined, ignoreQueryParams?: boolean): boolean;
125
- areStatesDescendants(parentState: State, childState: State): boolean;
126
123
  forwardState<P extends Params = Params>(routeName: string, routeParams: P): SimpleState<P>;
127
124
  buildState(routeName: string, routeParams: Params): RouteTreeState | undefined;
128
- buildStateWithSegments<P extends Params = Params>(routeName: string, routeParams: P): BuildStateResultWithSegments<P> | undefined;
129
125
  shouldUpdateNode(nodeName: string): (toState: State, fromState?: State) => boolean;
130
126
  getOptions(): Options;
131
127
  getOption<K extends keyof Options>(option: K): Options[K];
132
128
  setOption(option: keyof Options, value: Options[keyof Options]): this;
133
- isStarted(): boolean;
134
129
  isActive(): boolean;
135
- isNavigating(): boolean;
136
130
  start(...args: [] | [done: DoneFn] | [startPathOrState: string | State] | [startPathOrState: string | State, done: DoneFn]): this;
137
131
  stop(): this;
138
132
  canDeactivate(name: string, canDeactivateHandler: ActivationFnFactory<Dependencies> | boolean): this;
139
- clearCanDeactivate(name: string, silent?: boolean): this;
140
133
  canActivate(name: string, canActivateHandler: ActivationFnFactory<Dependencies> | boolean): this;
141
- clearCanActivate(name: string, silent?: boolean): this;
142
- getLifecycleFactories(): [
143
- Record<string, ActivationFnFactory<Dependencies>>,
144
- Record<string, ActivationFnFactory<Dependencies>>
145
- ];
146
- getLifecycleFunctions(): [
147
- Map<string, ActivationFn>,
148
- Map<string, ActivationFn>
149
- ];
150
134
  usePlugin(...plugins: PluginFactory<Dependencies>[]): Unsubscribe;
151
- getPlugins(): PluginFactory<Dependencies>[];
152
135
  useMiddleware(...middlewares: MiddlewareFactory<Dependencies>[]): Unsubscribe;
153
136
  clearMiddleware(): this;
154
- getMiddlewareFactories(): MiddlewareFactory<Dependencies>[];
155
- getMiddlewareFunctions(): Middleware[];
156
137
  setDependency<K extends keyof Dependencies & string>(dependencyName: K, dependency: Dependencies[K]): this;
157
138
  setDependencies(deps: Dependencies): this;
158
139
  getDependency<K extends keyof Dependencies>(key: K): Dependencies[K];
@@ -160,11 +141,7 @@ declare class Router<Dependencies extends DefaultDependencies = DefaultDependenc
160
141
  removeDependency(dependencyName: keyof Dependencies): this;
161
142
  hasDependency(dependencyName: keyof Dependencies): boolean;
162
143
  resetDependencies(): this;
163
- invokeEventListeners(eventName: EventToNameMap[EventsKeys], toState?: State, fromState?: State, arg?: RouterError$1 | NavigationOptions): void;
164
- hasListeners(eventName: EventToNameMap[EventsKeys]): boolean;
165
- removeEventListener<E extends EventName>(eventName: E, cb: Plugin[EventMethodMap[E]]): void;
166
144
  addEventListener<E extends EventName>(eventName: E, cb: Plugin[EventMethodMap[E]]): Unsubscribe;
167
- forward(fromRoute: string, toRoute: string): this;
168
145
  navigate(routeName: string, routeParamsOrDone?: Params | DoneFn, optionsOrDone?: NavigationOptions | DoneFn, done?: DoneFn): CancelFn;
169
146
  navigateToDefault(optsOrDone?: NavigationOptions | DoneFn, done?: DoneFn): CancelFn;
170
147
  navigateToState(toState: State, fromState: State | undefined, opts: NavigationOptions, callback: DoneFn, emitSuccess: boolean): CancelFn;