@real-router/core 0.25.3 → 0.26.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.
Files changed (49) hide show
  1. package/README.md +163 -325
  2. package/dist/cjs/index.d.ts +47 -178
  3. package/dist/cjs/index.js +1 -1
  4. package/dist/cjs/index.js.map +1 -1
  5. package/dist/cjs/metafile-cjs.json +1 -1
  6. package/dist/esm/index.d.mts +47 -178
  7. package/dist/esm/index.mjs +1 -1
  8. package/dist/esm/index.mjs.map +1 -1
  9. package/dist/esm/metafile-esm.json +1 -1
  10. package/package.json +3 -3
  11. package/src/Router.ts +84 -574
  12. package/src/api/cloneRouter.ts +106 -0
  13. package/src/api/getDependenciesApi.ts +216 -0
  14. package/src/api/getLifecycleApi.ts +67 -0
  15. package/src/api/getPluginApi.ts +118 -0
  16. package/src/api/getRoutesApi.ts +566 -0
  17. package/src/api/index.ts +16 -0
  18. package/src/api/types.ts +7 -0
  19. package/src/getNavigator.ts +5 -2
  20. package/src/index.ts +17 -3
  21. package/src/internals.ts +115 -0
  22. package/src/namespaces/DependenciesNamespace/dependenciesStore.ts +30 -0
  23. package/src/namespaces/DependenciesNamespace/index.ts +3 -1
  24. package/src/namespaces/DependenciesNamespace/validators.ts +2 -4
  25. package/src/namespaces/EventBusNamespace/EventBusNamespace.ts +1 -20
  26. package/src/namespaces/EventBusNamespace/validators.ts +36 -0
  27. package/src/namespaces/NavigationNamespace/NavigationNamespace.ts +1 -10
  28. package/src/namespaces/NavigationNamespace/transition/errorHandling.ts +2 -0
  29. package/src/namespaces/NavigationNamespace/transition/{executeLifecycleHooks.ts → executeLifecycleGuards.ts} +9 -7
  30. package/src/namespaces/NavigationNamespace/transition/index.ts +3 -3
  31. package/src/namespaces/RouteLifecycleNamespace/RouteLifecycleNamespace.ts +1 -16
  32. package/src/namespaces/RoutesNamespace/RoutesNamespace.ts +133 -1089
  33. package/src/namespaces/RoutesNamespace/forwardToValidation.ts +411 -0
  34. package/src/namespaces/RoutesNamespace/helpers.ts +1 -407
  35. package/src/namespaces/RoutesNamespace/index.ts +2 -0
  36. package/src/namespaces/RoutesNamespace/routesStore.ts +388 -0
  37. package/src/namespaces/RoutesNamespace/validators.ts +209 -3
  38. package/src/namespaces/StateNamespace/StateNamespace.ts +1 -44
  39. package/src/namespaces/StateNamespace/validators.ts +46 -0
  40. package/src/namespaces/index.ts +3 -5
  41. package/src/types.ts +12 -138
  42. package/src/wiring/RouterWiringBuilder.ts +30 -36
  43. package/src/wiring/types.ts +3 -6
  44. package/src/wiring/wireRouter.ts +0 -1
  45. package/src/namespaces/CloneNamespace/CloneNamespace.ts +0 -120
  46. package/src/namespaces/CloneNamespace/index.ts +0 -3
  47. package/src/namespaces/CloneNamespace/types.ts +0 -42
  48. package/src/namespaces/DependenciesNamespace/DependenciesNamespace.ts +0 -248
  49. package/src/namespaces/RoutesNamespace/stateBuilder.ts +0 -70
@@ -1,38 +1,33 @@
1
- import { EventToPluginMap, EventToNameMap, ErrorCodeKeys, ErrorCodeValues, ErrorCodeToValueMap, DefaultDependencies, Options, Params, State, StateMetaInput, SimpleState, RouteTreeState, Unsubscribe, EventName, Plugin, SubscribeFn, NavigationOptions, GuardFn, ForwardToCallback, EventsKeys, ActivationFn, Navigator } from '@real-router/types';
2
- export { ActivationFn, Config, DefaultDependencies, GuardFn, Listener, NavigationOptions, Navigator, Options, Params, Plugin, SimpleState, State, StateMeta, SubscribeFn, SubscribeState, Subscription, Unsubscribe } from '@real-router/types';
1
+ import { Params, RouteTreeState, DefaultDependencies, Router as Router$1, Route, Options, State, PluginFactory, Unsubscribe, SubscribeFn, NavigationOptions, ErrorCodeKeys, ErrorCodeValues, ErrorCodeToValueMap, EventToNameMap, Navigator, PluginApi, RoutesApi, DependenciesApi, LifecycleApi } from '@real-router/types';
2
+ export { Config, DefaultDependencies, DependenciesApi, GuardFn, GuardFnFactory, LifecycleApi, Listener, NavigationOptions, Navigator, Options, Params, Plugin, PluginApi, PluginFactory, Route, RouteConfigUpdate, RoutesApi, SimpleState, State, StateMeta, SubscribeFn, SubscribeState, Subscription, Unsubscribe } from '@real-router/types';
3
3
 
4
- type ConstantsKeys = "UNKNOWN_ROUTE";
5
- type Constants = Record<ConstantsKeys, string>;
6
- type ErrorCodes = Record<ErrorCodeKeys, ErrorCodeValues>;
7
- /**
8
- * Error codes for router operations.
9
- * Used to identify specific failure scenarios in navigation and lifecycle.
10
- * Frozen to prevent accidental modifications.
11
- */
12
- declare const errorCodes: ErrorCodeToValueMap;
13
- /**
14
- * General router constants.
15
- * Special route names and identifiers.
16
- */
17
- declare const constants: Constants;
18
4
  /**
19
- * Plugin method names.
20
- * Maps to methods that plugins can implement to hook into router lifecycle.
5
+ * Core-internal types + re-exports from @real-router/types.
6
+ *
7
+ * Factory types (PluginFactory, GuardFnFactory) and
8
+ * route config types (Route, RouteConfigUpdate) are canonical in @real-router/types
9
+ * and re-exported here for backward compatibility.
21
10
  */
22
- declare const plugins: EventToPluginMap;
11
+
23
12
  /**
24
- * Event names for router event system.
25
- * Used with addEventListener/removeEventListener for reactive subscriptions.
13
+ * Extended build result that includes segments for path building.
14
+ * Used internally to avoid duplicate getSegmentsByName calls.
15
+ *
16
+ * @param segments - Route segments from getSegmentsByName (typed as unknown[] for cross-package compatibility)
17
+ * @internal
26
18
  */
27
- declare const events: EventToNameMap;
19
+ interface BuildStateResultWithSegments<P extends Params = Params> {
20
+ readonly state: RouteTreeState<P>;
21
+ readonly segments: readonly unknown[];
22
+ }
28
23
 
29
24
  /**
30
25
  * Router class with integrated namespace architecture.
31
26
  *
32
27
  * All functionality is provided by namespace classes:
33
28
  * - OptionsNamespace: getOptions (immutable)
34
- * - DependenciesNamespace: get/set/remove dependencies
35
- * - EventEmitter: event listeners, subscribe
29
+ * - DependenciesStore: get/set/remove dependencies
30
+ * - EventEmitter: subscribe
36
31
  * - StateNamespace: state storage (getState, setState, getPreviousState)
37
32
  * - RoutesNamespace: route tree operations
38
33
  * - RouteLifecycleNamespace: canActivate/canDeactivate guards
@@ -42,7 +37,7 @@ declare const events: EventToNameMap;
42
37
  *
43
38
  * @internal This class implementation is internal. Use createRouter() instead.
44
39
  */
45
- declare class Router<Dependencies extends DefaultDependencies = DefaultDependencies> {
40
+ declare class Router<Dependencies extends DefaultDependencies = DefaultDependencies> implements Router$1<Dependencies> {
46
41
  #private;
47
42
  [key: string]: unknown;
48
43
  /**
@@ -51,178 +46,42 @@ declare class Router<Dependencies extends DefaultDependencies = DefaultDependenc
51
46
  * @param dependencies - DI dependencies
52
47
  */
53
48
  constructor(routes?: Route<Dependencies>[], options?: Partial<Options>, dependencies?: Dependencies);
54
- addRoute(routes: Route<Dependencies>[] | Route<Dependencies>, options?: {
55
- parent?: string;
56
- }): this;
57
- removeRoute(name: string): this;
58
- clearRoutes(): this;
59
- getRoute(name: string): Route<Dependencies> | undefined;
60
- getRouteConfig(name: string): Record<string, unknown> | undefined;
61
- hasRoute(name: string): boolean;
62
- updateRoute(name: string, updates: RouteConfigUpdate<Dependencies>): this;
63
49
  isActiveRoute(name: string, params?: Params, strictEquality?: boolean, ignoreQueryParams?: boolean): boolean;
64
50
  buildPath(route: string, params?: Params): string;
65
- matchPath<P extends Params = Params, MP extends Params = Params>(path: string): State<P, MP> | undefined;
66
- setRootPath(rootPath: string): void;
67
- getRootPath(): string;
68
- makeState<P extends Params = Params, MP extends Params = Params>(name: string, params?: P, path?: string, meta?: StateMetaInput<MP>, forceId?: number): State<P, MP>;
69
51
  getState<P extends Params = Params, MP extends Params = Params>(): State<P, MP> | undefined;
70
52
  getPreviousState(): State | undefined;
71
53
  areStatesEqual(state1: State | undefined, state2: State | undefined, ignoreQueryParams?: boolean): boolean;
72
- forwardState<P extends Params = Params>(routeName: string, routeParams: P): SimpleState<P>;
73
- buildState(routeName: string, routeParams: Params): RouteTreeState | undefined;
74
- buildNavigationState(name: string, params?: Params): State | undefined;
75
54
  shouldUpdateNode(nodeName: string): (toState: State, fromState?: State) => boolean;
76
- getOptions(): Options;
77
55
  isActive(): boolean;
78
56
  start(startPath: string): Promise<State>;
79
57
  stop(): this;
80
58
  dispose(): void;
81
- addDeactivateGuard(name: string, canDeactivateHandler: GuardFnFactory<Dependencies> | boolean): this;
82
- addActivateGuard(name: string, canActivateHandler: GuardFnFactory<Dependencies> | boolean): this;
83
- removeActivateGuard(name: string): void;
84
- removeDeactivateGuard(name: string): void;
85
59
  canNavigateTo(name: string, params?: Params): boolean;
86
60
  usePlugin(...plugins: PluginFactory<Dependencies>[]): Unsubscribe;
87
- setDependency<K extends keyof Dependencies & string>(dependencyName: K, dependency: Dependencies[K]): this;
88
- setDependencies(deps: Dependencies): this;
89
- getDependency<K extends keyof Dependencies>(key: K): Dependencies[K];
90
- getDependencies(): Partial<Dependencies>;
91
- removeDependency(dependencyName: keyof Dependencies): this;
92
- hasDependency(dependencyName: keyof Dependencies): boolean;
93
- resetDependencies(): this;
94
- addEventListener<E extends EventName>(eventName: E, cb: Plugin[EventMethodMap[E]]): Unsubscribe;
95
61
  subscribe(listener: SubscribeFn): Unsubscribe;
96
- navigate(routeName: string): Promise<State>;
97
- navigate(routeName: string, routeParams: Params): Promise<State>;
98
- navigate(routeName: string, routeParams: Params, options: NavigationOptions): Promise<State>;
99
- navigateToDefault(): Promise<State>;
100
- navigateToDefault(options: NavigationOptions): Promise<State>;
101
- navigateToState(toState: State, fromState: State | undefined, opts: NavigationOptions): Promise<State>;
102
- clone(dependencies?: Dependencies): Router<Dependencies>;
62
+ navigate(routeName: string, routeParams?: Params, options?: NavigationOptions): Promise<State>;
63
+ navigateToDefault(options?: NavigationOptions): Promise<State>;
103
64
  }
104
65
 
66
+ type ConstantsKeys = "UNKNOWN_ROUTE";
67
+ type Constants = Record<ConstantsKeys, string>;
68
+ type ErrorCodes = Record<ErrorCodeKeys, ErrorCodeValues>;
105
69
  /**
106
- * Router-dependent types.
107
- *
108
- * These types reference the Router class and are therefore defined in core
109
- * rather than core-types to avoid circular dependencies.
110
- */
111
-
112
- type EventMethodMap = {
113
- [K in EventsKeys as (typeof events)[K]]: (typeof plugins)[K];
114
- };
115
- /**
116
- * Extended build result that includes segments for path building.
117
- * Used internally to avoid duplicate getSegmentsByName calls.
118
- *
119
- * @param segments - Route segments from getSegmentsByName (typed as unknown[] for cross-package compatibility)
120
- * @internal
121
- */
122
- interface BuildStateResultWithSegments<P extends Params = Params> {
123
- readonly state: RouteTreeState<P>;
124
- readonly segments: readonly unknown[];
125
- }
126
- /**
127
- * Route configuration.
128
- */
129
- interface Route<Dependencies extends DefaultDependencies = DefaultDependencies> {
130
- [key: string]: unknown;
131
- /** Route name (dot-separated for nested routes). */
132
- name: string;
133
- /** URL path pattern for this route. */
134
- path: string;
135
- /** Factory function that returns a guard for route activation. */
136
- canActivate?: GuardFnFactory<Dependencies>;
137
- /** Factory function that returns a guard for route deactivation. */
138
- canDeactivate?: GuardFnFactory<Dependencies>;
139
- /**
140
- * Redirects navigation to another route.
141
- *
142
- * IMPORTANT: forwardTo creates a URL alias, not a transition chain.
143
- * Guards (canActivate) on the source route are NOT executed.
144
- * Only guards on the final destination are executed.
145
- *
146
- * This matches Vue Router and Angular Router behavior.
147
- *
148
- * @example
149
- * // Correct: guard on target
150
- * { name: "old", path: "/old", forwardTo: "new" }
151
- * { name: "new", path: "/new", canActivate: myGuard }
152
- *
153
- * // Wrong: guard on source (will be ignored with warning)
154
- * { name: "old", path: "/old", forwardTo: "new", canActivate: myGuard }
155
- */
156
- forwardTo?: string | ForwardToCallback<Dependencies>;
157
- /** Nested child routes. */
158
- children?: Route<Dependencies>[];
159
- /** Encodes state params to URL params. */
160
- encodeParams?: (stateParams: Params) => Params;
161
- /** Decodes URL params to state params. */
162
- decodeParams?: (pathParams: Params) => Params;
163
- /**
164
- * Default parameters for this route.
165
- *
166
- * @remarks
167
- * **Type Contract:**
168
- * The type of defaultParams MUST match the expected params type P
169
- * when using `router.makeState<P>()` or `router.navigate<P>()`.
170
- *
171
- * These values are merged into state.params when creating route states.
172
- * Missing URL params are filled from defaultParams.
173
- *
174
- * @example
175
- * ```typescript
176
- * // Define route with pagination defaults
177
- * {
178
- * name: "users",
179
- * path: "/users",
180
- * defaultParams: { page: 1, limit: 10 }
181
- * }
182
- *
183
- * // Navigate without specifying page/limit
184
- * router.navigate("users", { filter: "active" });
185
- * // Result: state.params = { page: 1, limit: 10, filter: "active" }
186
- *
187
- * // Correct typing — include defaultParams properties
188
- * type UsersParams = { page: number; limit: number; filter?: string };
189
- * ```
190
- */
191
- defaultParams?: Params;
192
- }
193
- /**
194
- * Configuration update options for updateRoute().
195
- * All properties are optional. Set to null to remove the configuration.
196
- */
197
- interface RouteConfigUpdate<Dependencies extends DefaultDependencies = DefaultDependencies> {
198
- /** Set to null to remove forwardTo */
199
- forwardTo?: string | ForwardToCallback<Dependencies> | null;
200
- /** Set to null to remove defaultParams */
201
- defaultParams?: Params | null;
202
- /** Set to null to remove decoder */
203
- decodeParams?: ((params: Params) => Params) | null;
204
- /** Set to null to remove encoder */
205
- encodeParams?: ((params: Params) => Params) | null;
206
- /** Set to null to remove canActivate */
207
- canActivate?: GuardFnFactory<Dependencies> | null;
208
- /** Set to null to remove canDeactivate */
209
- canDeactivate?: GuardFnFactory<Dependencies> | null;
210
- }
211
- /**
212
- * Factory function for creating activation guards.
213
- * Receives the router instance and a dependency getter.
70
+ * Error codes for router operations.
71
+ * Used to identify specific failure scenarios in navigation and lifecycle.
72
+ * Frozen to prevent accidental modifications.
214
73
  */
215
- type ActivationFnFactory<Dependencies extends DefaultDependencies = DefaultDependencies> = (router: Router<Dependencies>, getDependency: <K extends keyof Dependencies>(key: K) => Dependencies[K]) => ActivationFn;
74
+ declare const errorCodes: ErrorCodeToValueMap;
216
75
  /**
217
- * Factory function for creating guards.
218
- * Receives the router instance and a dependency getter.
76
+ * General router constants.
77
+ * Special route names and identifiers.
219
78
  */
220
- type GuardFnFactory<Dependencies extends DefaultDependencies = DefaultDependencies> = (router: Router<Dependencies>, getDependency: <K extends keyof Dependencies>(key: K) => Dependencies[K]) => GuardFn;
79
+ declare const constants: Constants;
221
80
  /**
222
- * Factory function for creating plugins.
223
- * Receives the router instance and a dependency getter.
81
+ * Event names for router event system.
82
+ * Used with addEventListener/removeEventListener for reactive subscriptions.
224
83
  */
225
- type PluginFactory<Dependencies extends DefaultDependencies = DefaultDependencies> = (router: Router<Dependencies>, getDependency: <K extends keyof Dependencies>(key: K) => Dependencies[K]) => Plugin;
84
+ declare const events: EventToNameMap;
226
85
 
227
86
  declare class RouterError extends Error {
228
87
  [key: string]: unknown;
@@ -429,6 +288,16 @@ declare class RouterError extends Error {
429
288
  */
430
289
  declare const createRouter: <Dependencies extends DefaultDependencies = DefaultDependencies>(routes?: Route<Dependencies>[], options?: Partial<Options>, dependencies?: Dependencies) => Router<Dependencies>;
431
290
 
432
- declare const getNavigator: <Dependencies extends DefaultDependencies = DefaultDependencies>(router: Router<Dependencies>) => Navigator;
291
+ declare const getNavigator: <Dependencies extends DefaultDependencies = DefaultDependencies>(router: Router$1<Dependencies>) => Navigator;
292
+
293
+ declare function getPluginApi<Dependencies extends DefaultDependencies = DefaultDependencies>(router: Router$1<Dependencies>): PluginApi;
294
+
295
+ declare function getRoutesApi<Dependencies extends DefaultDependencies = DefaultDependencies>(router: Router$1<Dependencies>): RoutesApi<Dependencies>;
296
+
297
+ declare function getDependenciesApi<Dependencies extends DefaultDependencies = DefaultDependencies>(router: Router$1<Dependencies>): DependenciesApi<Dependencies>;
298
+
299
+ declare function getLifecycleApi<Dependencies extends DefaultDependencies = DefaultDependencies>(router: Router$1<Dependencies>): LifecycleApi<Dependencies>;
300
+
301
+ declare function cloneRouter<Dependencies extends DefaultDependencies = DefaultDependencies>(router: Router$1<Dependencies>, dependencies?: Dependencies): Router<Dependencies>;
433
302
 
434
- export { type ActivationFnFactory, type BuildStateResultWithSegments, type Constants, type ErrorCodes, type GuardFnFactory, type PluginFactory, type Route, type RouteConfigUpdate, Router, RouterError, constants, createRouter, errorCodes, events, getNavigator };
303
+ export { type BuildStateResultWithSegments, type Constants, type ErrorCodes, Router, RouterError, cloneRouter, constants, createRouter, errorCodes, events, getDependenciesApi, getLifecycleApi, getNavigator, getPluginApi, getRoutesApi };