@real-router/browser-plugin 0.6.3 → 0.7.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/src/factory.ts ADDED
@@ -0,0 +1,88 @@
1
+ import { getPluginApi } from "@real-router/core";
2
+
3
+ import { createSafeBrowser } from "./browser";
4
+ import { defaultOptions, LOGGER_CONTEXT, source } from "./constants";
5
+ import { BrowserPlugin } from "./plugin";
6
+ import { createRegExpCache } from "./url-utils";
7
+ import { validateOptions } from "./validation";
8
+
9
+ import type {
10
+ BrowserPluginOptions,
11
+ Browser,
12
+ SharedFactoryState,
13
+ } from "./types";
14
+ import type { PluginFactory, Router } from "@real-router/core";
15
+
16
+ /**
17
+ * Browser plugin factory for real-router.
18
+ * Integrates router with browser history API.
19
+ *
20
+ * @param opts - Plugin configuration options
21
+ * @param browser - Browser API abstraction (for testing/SSR)
22
+ * @returns Plugin factory function
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * // Hash routing
27
+ * router.usePlugin(browserPluginFactory({ useHash: true, hashPrefix: "!" }));
28
+ *
29
+ * // History routing with hash preservation
30
+ * router.usePlugin(browserPluginFactory({ useHash: false, preserveHash: true }));
31
+ * ```
32
+ */
33
+ export function browserPluginFactory(
34
+ opts?: Partial<BrowserPluginOptions>,
35
+ browser: Browser = createSafeBrowser(),
36
+ ): PluginFactory {
37
+ const hasInvalidTypes = validateOptions(opts, defaultOptions);
38
+
39
+ let options = { ...defaultOptions, ...opts } as BrowserPluginOptions;
40
+
41
+ if (hasInvalidTypes) {
42
+ console.warn(
43
+ `[${LOGGER_CONTEXT}] Using default options due to invalid types`,
44
+ );
45
+ options = { ...defaultOptions } as BrowserPluginOptions;
46
+ }
47
+
48
+ if (options.useHash === true) {
49
+ delete (options as unknown as Record<string, unknown>).preserveHash;
50
+ } else {
51
+ delete (options as unknown as Record<string, unknown>).hashPrefix;
52
+ }
53
+
54
+ if (options.base) {
55
+ if (!options.base.startsWith("/")) {
56
+ options.base = `/${options.base}`;
57
+ }
58
+
59
+ if (options.base.endsWith("/")) {
60
+ options.base = options.base.slice(0, -1);
61
+ }
62
+ }
63
+
64
+ const regExpCache = createRegExpCache();
65
+
66
+ const forceDeactivate = options.forceDeactivate;
67
+ /* v8 ignore next 4 -- @preserve both branches tested, coverage tool limitation */
68
+ const transitionOptions =
69
+ forceDeactivate === undefined
70
+ ? { source, replace: true as const }
71
+ : { forceDeactivate, source, replace: true as const };
72
+
73
+ const shared: SharedFactoryState = { removePopStateListener: undefined };
74
+
75
+ return function browserPlugin(routerBase) {
76
+ const plugin = new BrowserPlugin(
77
+ routerBase as Router,
78
+ getPluginApi(routerBase),
79
+ options,
80
+ browser,
81
+ regExpCache,
82
+ transitionOptions,
83
+ shared,
84
+ );
85
+
86
+ return plugin.getPlugin();
87
+ };
88
+ }
package/src/index.ts CHANGED
@@ -5,13 +5,13 @@
5
5
  import type { Params, State } from "@real-router/core";
6
6
 
7
7
  // Main plugin factory
8
- export { browserPluginFactory } from "./plugin";
8
+ export { browserPluginFactory } from "./factory";
9
9
 
10
10
  // Types
11
- export type { BrowserPluginOptions, Browser, HistoryState } from "./types";
11
+ export type { BrowserPluginOptions, Browser } from "./types";
12
12
 
13
- // Type guards (maybe useful for consumers)
14
- export { isStateStrict as isState, isHistoryState } from "type-guards";
13
+ // Type guards
14
+ export { isStateStrict as isState } from "type-guards";
15
15
 
16
16
  /**
17
17
  * Module augmentation for real-router.
@@ -41,12 +41,6 @@ declare module "@real-router/core" {
41
41
  title?: string,
42
42
  ) => void;
43
43
 
44
- /**
45
- * Last known router state.
46
- * Added by browser plugin.
47
- */
48
- lastKnownState?: State;
49
-
50
44
  start(path?: string): Promise<State>;
51
45
  }
52
46
  }