@real-router/core 0.56.0 → 0.57.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 (108) hide show
  1. package/dist/cjs/Router-BSGzVINO.js +6 -0
  2. package/dist/cjs/Router-BSGzVINO.js.map +1 -0
  3. package/dist/cjs/api.d.ts +1 -1
  4. package/dist/cjs/api.js +1 -1
  5. package/dist/cjs/{cloneRouter-DRieJvam.js → cloneRouter-7z-60z_f.js} +2 -2
  6. package/dist/cjs/{cloneRouter-DRieJvam.js.map → cloneRouter-7z-60z_f.js.map} +1 -1
  7. package/dist/cjs/{index-C-i6vx5Y.d.ts → index-BWUmnecT.d.ts} +1 -2
  8. package/dist/cjs/index-BWUmnecT.d.ts.map +1 -0
  9. package/dist/cjs/index.d.ts +1 -1
  10. package/dist/cjs/index.js +1 -1
  11. package/dist/cjs/utils.js +1 -1
  12. package/dist/cjs/validation.d.ts +1 -1
  13. package/dist/esm/Router-B7txWo9N.mjs +6 -0
  14. package/dist/esm/Router-B7txWo9N.mjs.map +1 -0
  15. package/dist/esm/api.d.mts +1 -1
  16. package/dist/esm/api.mjs +1 -1
  17. package/dist/esm/{cloneRouter-DHrH6D_z.mjs → cloneRouter-BNCQ7tIa.mjs} +2 -2
  18. package/dist/esm/{cloneRouter-DHrH6D_z.mjs.map → cloneRouter-BNCQ7tIa.mjs.map} +1 -1
  19. package/dist/esm/{index-C-i6vx5Y.d.mts → index-BWUmnecT.d.mts} +1 -2
  20. package/dist/esm/index-BWUmnecT.d.mts.map +1 -0
  21. package/dist/esm/index.d.mts +1 -1
  22. package/dist/esm/index.mjs +1 -1
  23. package/dist/esm/utils.mjs +1 -1
  24. package/dist/esm/validation.d.mts +1 -1
  25. package/package.json +2 -3
  26. package/dist/cjs/Router-IEGavTKk.js +0 -6
  27. package/dist/cjs/Router-IEGavTKk.js.map +0 -1
  28. package/dist/cjs/index-C-i6vx5Y.d.ts.map +0 -1
  29. package/dist/esm/Router-B3aeavRb.mjs +0 -6
  30. package/dist/esm/Router-B3aeavRb.mjs.map +0 -1
  31. package/dist/esm/index-C-i6vx5Y.d.mts.map +0 -1
  32. package/src/Router.ts +0 -737
  33. package/src/RouterError.ts +0 -324
  34. package/src/api/cloneRouter.ts +0 -159
  35. package/src/api/getDependenciesApi.ts +0 -160
  36. package/src/api/getLifecycleApi.ts +0 -65
  37. package/src/api/getPluginApi.ts +0 -228
  38. package/src/api/getRoutesApi.ts +0 -831
  39. package/src/api/helpers.ts +0 -10
  40. package/src/api/index.ts +0 -16
  41. package/src/api/types.ts +0 -12
  42. package/src/constants.ts +0 -101
  43. package/src/createRouter.ts +0 -32
  44. package/src/fsm/index.ts +0 -5
  45. package/src/fsm/routerFSM.ts +0 -130
  46. package/src/getNavigator.ts +0 -30
  47. package/src/guards.ts +0 -46
  48. package/src/helpers.ts +0 -197
  49. package/src/index.ts +0 -66
  50. package/src/internals.ts +0 -228
  51. package/src/namespaces/DependenciesNamespace/dependenciesStore.ts +0 -30
  52. package/src/namespaces/DependenciesNamespace/index.ts +0 -5
  53. package/src/namespaces/EventBusNamespace/EventBusNamespace.ts +0 -522
  54. package/src/namespaces/EventBusNamespace/index.ts +0 -5
  55. package/src/namespaces/EventBusNamespace/types.ts +0 -11
  56. package/src/namespaces/NavigationNamespace/NavigationNamespace.ts +0 -552
  57. package/src/namespaces/NavigationNamespace/constants.ts +0 -55
  58. package/src/namespaces/NavigationNamespace/index.ts +0 -5
  59. package/src/namespaces/NavigationNamespace/transition/completeTransition.ts +0 -108
  60. package/src/namespaces/NavigationNamespace/transition/errorHandling.ts +0 -124
  61. package/src/namespaces/NavigationNamespace/transition/guardPhase.ts +0 -283
  62. package/src/namespaces/NavigationNamespace/types.ts +0 -110
  63. package/src/namespaces/OptionsNamespace/OptionsNamespace.ts +0 -28
  64. package/src/namespaces/OptionsNamespace/constants.ts +0 -19
  65. package/src/namespaces/OptionsNamespace/helpers.ts +0 -50
  66. package/src/namespaces/OptionsNamespace/index.ts +0 -7
  67. package/src/namespaces/OptionsNamespace/validators.ts +0 -13
  68. package/src/namespaces/PluginsNamespace/PluginsNamespace.ts +0 -291
  69. package/src/namespaces/PluginsNamespace/constants.ts +0 -34
  70. package/src/namespaces/PluginsNamespace/index.ts +0 -7
  71. package/src/namespaces/PluginsNamespace/types.ts +0 -22
  72. package/src/namespaces/PluginsNamespace/validators.ts +0 -28
  73. package/src/namespaces/RouteLifecycleNamespace/RouteLifecycleNamespace.ts +0 -558
  74. package/src/namespaces/RouteLifecycleNamespace/index.ts +0 -5
  75. package/src/namespaces/RouteLifecycleNamespace/types.ts +0 -10
  76. package/src/namespaces/RouterLifecycleNamespace/RouterLifecycleNamespace.ts +0 -81
  77. package/src/namespaces/RouterLifecycleNamespace/constants.ts +0 -25
  78. package/src/namespaces/RouterLifecycleNamespace/index.ts +0 -5
  79. package/src/namespaces/RouterLifecycleNamespace/types.ts +0 -30
  80. package/src/namespaces/RoutesNamespace/RoutesNamespace.ts +0 -582
  81. package/src/namespaces/RoutesNamespace/constants.ts +0 -6
  82. package/src/namespaces/RoutesNamespace/forwardChain.ts +0 -34
  83. package/src/namespaces/RoutesNamespace/helpers.ts +0 -204
  84. package/src/namespaces/RoutesNamespace/index.ts +0 -11
  85. package/src/namespaces/RoutesNamespace/routeGuards.ts +0 -62
  86. package/src/namespaces/RoutesNamespace/routesStore.ts +0 -566
  87. package/src/namespaces/RoutesNamespace/types.ts +0 -81
  88. package/src/namespaces/StateNamespace/StateNamespace.ts +0 -224
  89. package/src/namespaces/StateNamespace/helpers.ts +0 -24
  90. package/src/namespaces/StateNamespace/index.ts +0 -5
  91. package/src/namespaces/StateNamespace/types.ts +0 -15
  92. package/src/namespaces/index.ts +0 -35
  93. package/src/stateMetaStore.ts +0 -15
  94. package/src/transitionPath.ts +0 -440
  95. package/src/typeGuards.ts +0 -59
  96. package/src/types/RouterValidator.ts +0 -156
  97. package/src/types.ts +0 -77
  98. package/src/utils/createRequestScope.ts +0 -174
  99. package/src/utils/getStaticPaths.ts +0 -50
  100. package/src/utils/hydrateRouter.ts +0 -89
  101. package/src/utils/index.ts +0 -27
  102. package/src/utils/serializeRouterState.ts +0 -120
  103. package/src/utils/serializeState.ts +0 -63
  104. package/src/validation.ts +0 -12
  105. package/src/wiring/RouterWiringBuilder.ts +0 -275
  106. package/src/wiring/index.ts +0 -7
  107. package/src/wiring/types.ts +0 -47
  108. package/src/wiring/wireRouter.ts +0 -26
@@ -1,50 +0,0 @@
1
- // packages/core/src/namespaces/OptionsNamespace/helpers.ts
2
-
3
- import type { Options, Params } from "@real-router/types";
4
-
5
- /**
6
- * Recursively freezes an object and all nested objects.
7
- * Only freezes plain objects, not primitives or special objects.
8
- */
9
- export function deepFreeze<T extends object>(obj: T): Readonly<T> {
10
- Object.freeze(obj);
11
-
12
- for (const key of Object.keys(obj)) {
13
- const value = (obj as Record<string, unknown>)[key];
14
-
15
- if (value && typeof value === "object" && value.constructor === Object) {
16
- deepFreeze(value);
17
- }
18
- }
19
-
20
- return obj;
21
- }
22
-
23
- /**
24
- * Resolves an option value that can be static or a callback.
25
- * If the value is a function, calls it with getDependency and returns the result.
26
- * Otherwise, returns the value as-is.
27
- */
28
- export function resolveOption(
29
- value: Options["defaultRoute"],
30
- getDependency: (name: string) => unknown,
31
- ): string;
32
-
33
- export function resolveOption(
34
- value: Options["defaultParams"],
35
- getDependency: (name: string) => unknown,
36
- ): Params;
37
-
38
- export function resolveOption(
39
- value: Options["defaultRoute"] | Options["defaultParams"],
40
- getDependency: (name: string) => unknown,
41
- ): string | Params {
42
- if (typeof value === "function") {
43
- // Runtime getDependency is (name: string) => unknown, but DefaultRouteCallback<object>
44
- // expects <K extends keyof object>(name: K) => object[K] where keyof object = never.
45
- // Cast needed to bridge generic constraint mismatch.
46
- return value(getDependency as never);
47
- }
48
-
49
- return value;
50
- }
@@ -1,7 +0,0 @@
1
- // packages/core/src/namespaces/OptionsNamespace/index.ts
2
-
3
- export { OptionsNamespace } from "./OptionsNamespace";
4
-
5
- export { defaultOptions } from "./constants";
6
-
7
- export { deepFreeze, resolveOption } from "./helpers";
@@ -1,13 +0,0 @@
1
- // packages/core/src/namespaces/OptionsNamespace/validators.ts
2
-
3
- /**
4
- * Minimal crash guard for options.
5
- * Full DX validation moved to @real-router/validation-plugin (retrospective pattern).
6
- */
7
- export function validateOptionsIsObject(
8
- options: unknown,
9
- ): asserts options is Record<string, unknown> {
10
- if (!options || typeof options !== "object" || Array.isArray(options)) {
11
- throw new TypeError("[router.constructor] options must be a plain object");
12
- }
13
- }
@@ -1,291 +0,0 @@
1
- // packages/core/src/namespaces/PluginsNamespace/PluginsNamespace.ts
2
-
3
- import { logger } from "@real-router/logger";
4
-
5
- import { EVENTS_MAP, EVENT_METHOD_NAMES, LOGGER_CONTEXT } from "./constants";
6
- import { validatePlugin } from "./validators";
7
- import { DEFAULT_LIMITS } from "../../constants";
8
-
9
- import type { PluginsDependencies } from "./types";
10
- import type { Limits, PluginFactory } from "../../types";
11
- import type { RouterValidator } from "../../types/RouterValidator";
12
- import type {
13
- DefaultDependencies,
14
- Plugin,
15
- Unsubscribe,
16
- } from "@real-router/types";
17
-
18
- /**
19
- * Independent namespace for managing plugins.
20
- *
21
- * Static methods handle validation (called by facade).
22
- * Instance methods handle storage and business logic.
23
- */
24
- export class PluginsNamespace<
25
- Dependencies extends DefaultDependencies = DefaultDependencies,
26
- > {
27
- readonly #plugins = new Set<PluginFactory<Dependencies>>();
28
- readonly #unsubscribes = new Set<Unsubscribe>();
29
-
30
- #deps!: PluginsDependencies<Dependencies>;
31
- #limits: Limits = DEFAULT_LIMITS;
32
- #getValidator: (() => RouterValidator | null) | null = null;
33
-
34
- // =========================================================================
35
- // Static validation methods (called by facade before instance methods)
36
- // Proxy to functions in validators.ts for separation of concerns
37
- // =========================================================================
38
-
39
- static validatePlugin(plugin: Plugin): void {
40
- validatePlugin(plugin);
41
- }
42
-
43
- static validateNoDuplicatePlugins<D extends DefaultDependencies>(
44
- newFactories: PluginFactory<D>[],
45
- hasPlugin: (factory: PluginFactory<D>) => boolean,
46
- ): void {
47
- for (const factory of newFactories) {
48
- if (hasPlugin(factory)) {
49
- throw new Error(
50
- `[router.usePlugin] Plugin factory already registered. ` +
51
- `To re-register, first unsubscribe the existing plugin.`,
52
- );
53
- }
54
- }
55
- }
56
-
57
- // =========================================================================
58
- // Dependency injection
59
- // =========================================================================
60
-
61
- setDependencies(deps: PluginsDependencies<Dependencies>): void {
62
- this.#deps = deps;
63
- }
64
-
65
- setLimits(limits: Limits): void {
66
- this.#limits = limits;
67
- // eslint-disable-next-line sonarjs/void-use -- @preserve: limits passed to validator via RouterInternals; void suppresses TS6133 until plugin implements validateCountThresholds
68
- void this.#limits;
69
- }
70
-
71
- setValidatorGetter(getter: () => RouterValidator | null): void {
72
- this.#getValidator = getter;
73
- }
74
-
75
- // =========================================================================
76
- // Instance methods (trust input - already validated by facade)
77
- // =========================================================================
78
-
79
- /**
80
- * Returns the number of registered plugins.
81
- * Used by facade for limit validation.
82
- */
83
- /* v8 ignore next 3 -- @preserve: only called via validator interface (ctx.validator?.plugins.validatePluginLimit), not reachable without validation plugin */
84
- count(): number {
85
- return this.#plugins.size;
86
- }
87
-
88
- /**
89
- * Registers one or more plugin factories.
90
- * Returns unsubscribe function to remove all added plugins.
91
- * Input already validated by facade (limit, duplicates).
92
- *
93
- * @param factories - Already validated by facade
94
- */
95
- use(...factories: PluginFactory<Dependencies>[]): Unsubscribe {
96
- // Emit warnings for count thresholds (not validation, just warnings)
97
- this.#getValidator?.()?.plugins.validateCountThresholds(
98
- this.#plugins.size + factories.length,
99
- );
100
-
101
- // Fast path for single plugin (common case)
102
- if (factories.length === 1) {
103
- const factory = factories[0];
104
- const cleanup = this.#startPlugin(factory);
105
-
106
- this.#plugins.add(factory);
107
-
108
- let unsubscribed = false;
109
-
110
- const unsubscribe: Unsubscribe = () => {
111
- if (unsubscribed) {
112
- return;
113
- }
114
-
115
- unsubscribed = true;
116
- this.#plugins.delete(factory);
117
- this.#unsubscribes.delete(unsubscribe);
118
- try {
119
- cleanup();
120
- } catch (error) {
121
- logger.error(LOGGER_CONTEXT, "Error during cleanup:", error);
122
- }
123
- };
124
-
125
- this.#unsubscribes.add(unsubscribe);
126
-
127
- return unsubscribe;
128
- }
129
-
130
- // Deduplicate batch with warning (validation already done by facade)
131
- const seenInBatch = this.#deduplicateBatch(factories);
132
-
133
- // Track successfully initialized plugins for cleanup
134
- const initializedPlugins: {
135
- factory: PluginFactory<Dependencies>;
136
- cleanup: Unsubscribe;
137
- }[] = [];
138
-
139
- // Initialize deduplicated plugins sequentially
140
- try {
141
- for (const plugin of seenInBatch) {
142
- const cleanup = this.#startPlugin(plugin);
143
-
144
- initializedPlugins.push({ factory: plugin, cleanup });
145
- }
146
- } catch (error) {
147
- // Rollback on failure - cleanup all initialized plugins
148
- for (const { cleanup } of initializedPlugins) {
149
- try {
150
- cleanup();
151
- } catch (cleanupError) {
152
- logger.error(LOGGER_CONTEXT, "Cleanup error:", cleanupError);
153
- }
154
- }
155
-
156
- throw error;
157
- }
158
-
159
- // Commit phase - add to registry
160
- for (const { factory } of initializedPlugins) {
161
- this.#plugins.add(factory);
162
- }
163
-
164
- // Return unsubscribe function
165
- let unsubscribed = false;
166
-
167
- const unsubscribe: Unsubscribe = () => {
168
- if (unsubscribed) {
169
- return;
170
- }
171
-
172
- unsubscribed = true;
173
- this.#unsubscribes.delete(unsubscribe);
174
-
175
- for (const { factory } of initializedPlugins) {
176
- this.#plugins.delete(factory);
177
- }
178
-
179
- for (const { cleanup } of initializedPlugins) {
180
- try {
181
- cleanup();
182
- } catch (error) {
183
- logger.error(LOGGER_CONTEXT, "Error during cleanup:", error);
184
- }
185
- }
186
- };
187
-
188
- this.#unsubscribes.add(unsubscribe);
189
-
190
- return unsubscribe;
191
- }
192
-
193
- /**
194
- * Returns registered plugin factories.
195
- */
196
- getAll(): PluginFactory<Dependencies>[] {
197
- return [...this.#plugins];
198
- }
199
-
200
- /**
201
- * Checks if a plugin factory is registered.
202
- * Used internally by validation to avoid array allocation.
203
- */
204
- /* v8 ignore next 3 -- @preserve: only called via validator interface, not reachable without validation plugin */
205
- has(factory: PluginFactory<Dependencies>): boolean {
206
- return this.#plugins.has(factory);
207
- }
208
-
209
- /**
210
- * Disposes all registered plugins by running their teardown callbacks
211
- * and removing event listener subscriptions.
212
- * Active disposal is required because plugins have an active lifecycle
213
- * (event subscriptions, teardown hooks).
214
- * Named "dispose" (not "clear") because there is active cleanup to perform.
215
- */
216
- disposeAll(): void {
217
- for (const unsubscribe of this.#unsubscribes) {
218
- unsubscribe();
219
- }
220
-
221
- this.#plugins.clear();
222
- this.#unsubscribes.clear();
223
- }
224
-
225
- // =========================================================================
226
- // Private methods
227
- // =========================================================================
228
-
229
- /**
230
- * Deduplicates batch with warning for duplicates within batch.
231
- * Validation (existing duplicates) is done by facade.
232
- */
233
- #deduplicateBatch(
234
- plugins: PluginFactory<Dependencies>[],
235
- ): Set<PluginFactory<Dependencies>> {
236
- const seenInBatch = new Set<PluginFactory<Dependencies>>();
237
-
238
- for (const plugin of plugins) {
239
- if (seenInBatch.has(plugin)) {
240
- this.#getValidator?.()?.plugins.warnBatchDuplicates(plugins);
241
- } else {
242
- seenInBatch.add(plugin);
243
- }
244
- }
245
-
246
- return seenInBatch;
247
- }
248
-
249
- #startPlugin(pluginFactory: PluginFactory<Dependencies>): Unsubscribe {
250
- const appliedPlugin = this.#deps.compileFactory(pluginFactory);
251
-
252
- PluginsNamespace.validatePlugin(appliedPlugin);
253
- this.#getValidator?.()?.plugins.validatePluginKeys(appliedPlugin);
254
-
255
- Object.freeze(appliedPlugin);
256
-
257
- // Collect all unsubscribe functions
258
- const removeEventListeners: Unsubscribe[] = [];
259
-
260
- // Subscribe plugin methods to corresponding router events
261
- for (const methodName of EVENT_METHOD_NAMES) {
262
- if (methodName in appliedPlugin) {
263
- if (typeof appliedPlugin[methodName] === "function") {
264
- removeEventListeners.push(
265
- this.#deps.addEventListener(
266
- EVENTS_MAP[methodName],
267
- appliedPlugin[methodName],
268
- ),
269
- );
270
-
271
- if (methodName === "onStart" && this.#deps.canNavigate()) {
272
- this.#getValidator?.()?.plugins.warnPluginAfterStart(methodName);
273
- }
274
- } else {
275
- this.#getValidator?.()?.plugins.warnPluginMethodType(methodName);
276
- }
277
- }
278
- }
279
-
280
- // Return composite cleanup function
281
- return () => {
282
- for (const removeListener of removeEventListeners) {
283
- removeListener();
284
- }
285
-
286
- if (typeof appliedPlugin.teardown === "function") {
287
- appliedPlugin.teardown();
288
- }
289
- };
290
- }
291
- }
@@ -1,34 +0,0 @@
1
- // packages/core/src/namespaces/PluginsNamespace/constants.ts
2
-
3
- import {
4
- events as EVENTS_CONST,
5
- plugins as PLUGINS_CONST,
6
- } from "../../constants";
7
-
8
- import type { EventName } from "@real-router/types";
9
-
10
- /**
11
- * Maps plugin method names to router event names.
12
- */
13
- export const EVENTS_MAP = {
14
- [PLUGINS_CONST.ROUTER_START]: EVENTS_CONST.ROUTER_START,
15
- [PLUGINS_CONST.ROUTER_STOP]: EVENTS_CONST.ROUTER_STOP,
16
- [PLUGINS_CONST.TRANSITION_SUCCESS]: EVENTS_CONST.TRANSITION_SUCCESS,
17
- [PLUGINS_CONST.TRANSITION_START]: EVENTS_CONST.TRANSITION_START,
18
- [PLUGINS_CONST.TRANSITION_LEAVE_APPROVE]:
19
- EVENTS_CONST.TRANSITION_LEAVE_APPROVE,
20
- [PLUGINS_CONST.TRANSITION_ERROR]: EVENTS_CONST.TRANSITION_ERROR,
21
- [PLUGINS_CONST.TRANSITION_CANCEL]: EVENTS_CONST.TRANSITION_CANCEL,
22
- } as const satisfies Record<
23
- (typeof PLUGINS_CONST)[keyof typeof PLUGINS_CONST],
24
- EventName
25
- >;
26
-
27
- /**
28
- * Plugin method names that correspond to router events.
29
- */
30
- export const EVENT_METHOD_NAMES = Object.keys(
31
- EVENTS_MAP,
32
- ) as (keyof typeof EVENTS_MAP)[];
33
-
34
- export const LOGGER_CONTEXT = "router.usePlugin";
@@ -1,7 +0,0 @@
1
- // packages/core/src/namespaces/PluginsNamespace/index.ts
2
-
3
- export { PluginsNamespace } from "./PluginsNamespace";
4
-
5
- export { EVENTS_MAP, EVENT_METHOD_NAMES } from "./constants";
6
-
7
- export type { PluginsDependencies } from "./types";
@@ -1,22 +0,0 @@
1
- // packages/core/src/namespaces/PluginsNamespace/types.ts
2
-
3
- import type { EventMethodMap, PluginFactory } from "../../types";
4
- import type {
5
- DefaultDependencies,
6
- EventName,
7
- Plugin,
8
- Unsubscribe,
9
- } from "@real-router/types";
10
-
11
- export interface PluginsDependencies<
12
- Dependencies extends DefaultDependencies = DefaultDependencies,
13
- > {
14
- addEventListener: <E extends EventName>(
15
- eventName: E,
16
- cb: Plugin[EventMethodMap[E]],
17
- ) => Unsubscribe;
18
-
19
- canNavigate: () => boolean;
20
-
21
- compileFactory: (factory: PluginFactory<Dependencies>) => Plugin;
22
- }
@@ -1,28 +0,0 @@
1
- // packages/core/src/namespaces/PluginsNamespace/validators.ts
2
-
3
- /**
4
- * Static validation functions for PluginsNamespace.
5
- * Called by Router facade before instance methods.
6
- */
7
-
8
- import type { Plugin } from "@real-router/types";
9
-
10
- /**
11
- * Validates that a plugin factory returned a valid plugin object.
12
- */
13
- export function validatePlugin(plugin: Plugin): void {
14
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
15
- if (!(plugin && typeof plugin === "object") || Array.isArray(plugin)) {
16
- throw new TypeError(
17
- `[router.usePlugin] Plugin factory must return an object, got ${typeof plugin}`,
18
- );
19
- }
20
-
21
- // Detect async factory (returns Promise)
22
- if (typeof (plugin as unknown as { then?: unknown }).then === "function") {
23
- throw new TypeError(
24
- `[router.usePlugin] Async plugin factories are not supported. ` +
25
- `Factory returned a Promise instead of a plugin object.`,
26
- );
27
- }
28
- }