@real-router/core 0.45.0 → 0.45.1

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 (75) hide show
  1. package/package.json +6 -7
  2. package/src/Router.ts +0 -684
  3. package/src/RouterError.ts +0 -324
  4. package/src/api/cloneRouter.ts +0 -77
  5. package/src/api/getDependenciesApi.ts +0 -168
  6. package/src/api/getLifecycleApi.ts +0 -65
  7. package/src/api/getPluginApi.ts +0 -167
  8. package/src/api/getRoutesApi.ts +0 -573
  9. package/src/api/helpers.ts +0 -10
  10. package/src/api/index.ts +0 -16
  11. package/src/api/types.ts +0 -12
  12. package/src/constants.ts +0 -87
  13. package/src/createRouter.ts +0 -32
  14. package/src/fsm/index.ts +0 -5
  15. package/src/fsm/routerFSM.ts +0 -120
  16. package/src/getNavigator.ts +0 -30
  17. package/src/guards.ts +0 -46
  18. package/src/helpers.ts +0 -179
  19. package/src/index.ts +0 -50
  20. package/src/internals.ts +0 -173
  21. package/src/namespaces/DependenciesNamespace/dependenciesStore.ts +0 -30
  22. package/src/namespaces/DependenciesNamespace/index.ts +0 -5
  23. package/src/namespaces/EventBusNamespace/EventBusNamespace.ts +0 -311
  24. package/src/namespaces/EventBusNamespace/index.ts +0 -5
  25. package/src/namespaces/EventBusNamespace/types.ts +0 -11
  26. package/src/namespaces/NavigationNamespace/NavigationNamespace.ts +0 -405
  27. package/src/namespaces/NavigationNamespace/constants.ts +0 -55
  28. package/src/namespaces/NavigationNamespace/index.ts +0 -5
  29. package/src/namespaces/NavigationNamespace/transition/completeTransition.ts +0 -100
  30. package/src/namespaces/NavigationNamespace/transition/errorHandling.ts +0 -124
  31. package/src/namespaces/NavigationNamespace/transition/guardPhase.ts +0 -221
  32. package/src/namespaces/NavigationNamespace/types.ts +0 -100
  33. package/src/namespaces/OptionsNamespace/OptionsNamespace.ts +0 -28
  34. package/src/namespaces/OptionsNamespace/constants.ts +0 -19
  35. package/src/namespaces/OptionsNamespace/helpers.ts +0 -50
  36. package/src/namespaces/OptionsNamespace/index.ts +0 -7
  37. package/src/namespaces/OptionsNamespace/validators.ts +0 -13
  38. package/src/namespaces/PluginsNamespace/PluginsNamespace.ts +0 -291
  39. package/src/namespaces/PluginsNamespace/constants.ts +0 -34
  40. package/src/namespaces/PluginsNamespace/index.ts +0 -7
  41. package/src/namespaces/PluginsNamespace/types.ts +0 -22
  42. package/src/namespaces/PluginsNamespace/validators.ts +0 -28
  43. package/src/namespaces/RouteLifecycleNamespace/RouteLifecycleNamespace.ts +0 -377
  44. package/src/namespaces/RouteLifecycleNamespace/index.ts +0 -5
  45. package/src/namespaces/RouteLifecycleNamespace/types.ts +0 -10
  46. package/src/namespaces/RouterLifecycleNamespace/RouterLifecycleNamespace.ts +0 -81
  47. package/src/namespaces/RouterLifecycleNamespace/constants.ts +0 -25
  48. package/src/namespaces/RouterLifecycleNamespace/index.ts +0 -5
  49. package/src/namespaces/RouterLifecycleNamespace/types.ts +0 -26
  50. package/src/namespaces/RoutesNamespace/RoutesNamespace.ts +0 -535
  51. package/src/namespaces/RoutesNamespace/constants.ts +0 -6
  52. package/src/namespaces/RoutesNamespace/forwardChain.ts +0 -34
  53. package/src/namespaces/RoutesNamespace/helpers.ts +0 -126
  54. package/src/namespaces/RoutesNamespace/index.ts +0 -11
  55. package/src/namespaces/RoutesNamespace/routeGuards.ts +0 -62
  56. package/src/namespaces/RoutesNamespace/routesStore.ts +0 -346
  57. package/src/namespaces/RoutesNamespace/types.ts +0 -81
  58. package/src/namespaces/StateNamespace/StateNamespace.ts +0 -211
  59. package/src/namespaces/StateNamespace/helpers.ts +0 -24
  60. package/src/namespaces/StateNamespace/index.ts +0 -5
  61. package/src/namespaces/StateNamespace/types.ts +0 -15
  62. package/src/namespaces/index.ts +0 -35
  63. package/src/stateMetaStore.ts +0 -15
  64. package/src/transitionPath.ts +0 -436
  65. package/src/typeGuards.ts +0 -59
  66. package/src/types/RouterValidator.ts +0 -154
  67. package/src/types.ts +0 -69
  68. package/src/utils/getStaticPaths.ts +0 -50
  69. package/src/utils/index.ts +0 -5
  70. package/src/utils/serializeState.ts +0 -22
  71. package/src/validation.ts +0 -12
  72. package/src/wiring/RouterWiringBuilder.ts +0 -261
  73. package/src/wiring/index.ts +0 -7
  74. package/src/wiring/types.ts +0 -47
  75. package/src/wiring/wireRouter.ts +0 -26
@@ -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
- }