@molecule/app-routing-react-router 1.0.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/LICENSE ADDED
@@ -0,0 +1,115 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work.
38
+
39
+ "Derivative Works" shall mean any work, whether in Source or Object
40
+ form, that is based on (or derived from) the Work and for which the
41
+ editorial revisions, annotations, elaborations, or other modifications
42
+ represent, as a whole, an original work of authorship.
43
+
44
+ "Contribution" shall mean any work of authorship, including the
45
+ original version of the Work and any modifications or additions
46
+ to that Work, that is intentionally submitted to the Licensor for
47
+ inclusion in the Work by the copyright owner or by an individual or
48
+ Legal Entity authorized to submit on behalf of the copyright owner.
49
+
50
+ "Contributor" shall mean Licensor and any individual or Legal Entity
51
+ on behalf of whom a Contribution has been received by the Licensor and
52
+ subsequently incorporated within the Work.
53
+
54
+ 2. Grant of Copyright License. Subject to the terms and conditions of
55
+ this License, each Contributor hereby grants to You a perpetual,
56
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
57
+ copyright license to reproduce, prepare Derivative Works of,
58
+ publicly display, publicly perform, sublicense, and distribute the
59
+ Work and such Derivative Works in Source or Object form.
60
+
61
+ 3. Grant of Patent License. Subject to the terms and conditions of
62
+ this License, each Contributor hereby grants to You a perpetual,
63
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
64
+ patent license to make, have made, use, offer to sell, sell, import,
65
+ and otherwise transfer the Work.
66
+
67
+ 4. Redistribution. You may reproduce and distribute copies of the
68
+ Work or Derivative Works thereof in any medium, with or without
69
+ modifications, and in Source or Object form, provided that You
70
+ meet the following conditions:
71
+
72
+ (a) You must give any other recipients of the Work or
73
+ Derivative Works a copy of this License; and
74
+
75
+ (b) You must cause any modified files to carry prominent notices
76
+ stating that You changed the files; and
77
+
78
+ (c) You must retain, in the Source form of any Derivative Works
79
+ that You distribute, all copyright, patent, trademark, and
80
+ attribution notices from the Source form of the Work,
81
+ excluding those notices that do not pertain to any part of
82
+ the Derivative Works; and
83
+
84
+ (d) If the Work includes a "NOTICE" text file as part of its
85
+ distribution, then any Derivative Works that You distribute must
86
+ include a readable copy of the attribution notices contained
87
+ within such NOTICE file.
88
+
89
+ 5. Submission of Contributions.
90
+
91
+ 6. Trademarks. This License does not grant permission to use the trade
92
+ names, trademarks, service marks, or product names of the Licensor.
93
+
94
+ 7. Disclaimer of Warranty. Unless required by applicable law or
95
+ agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
96
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
97
+
98
+ 8. Limitation of Liability. In no event and under no legal theory shall
99
+ any Contributor be liable to You for damages.
100
+
101
+ 9. Accepting Warranty or Additional Liability.
102
+
103
+ Copyright 2026 Molecule Dev, Inc.
104
+
105
+ Licensed under the Apache License, Version 2.0 (the "License");
106
+ you may not use this file except in compliance with the License.
107
+ You may obtain a copy of the License at
108
+
109
+ http://www.apache.org/licenses/LICENSE-2.0
110
+
111
+ Unless required by applicable law or agreed to in writing, software
112
+ distributed under the License is distributed on an "AS IS" BASIS,
113
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
114
+ See the License for the specific language governing permissions and
115
+ limitations under the License.
@@ -0,0 +1,133 @@
1
+ /**
2
+ * React hooks for React Router integration.
3
+ *
4
+ * @module
5
+ */
6
+ import React, { type ReactNode } from 'react';
7
+ import type { QueryParams, RouteDefinition, Router } from './types.js';
8
+ /**
9
+ * Provider props.
10
+ */
11
+ export interface MoleculeRouterProviderProps {
12
+ /**
13
+ * Child components.
14
+ */
15
+ children: ReactNode;
16
+ /**
17
+ * Optional route definitions for named routes.
18
+ */
19
+ routes?: RouteDefinition[];
20
+ /**
21
+ * Callback when router is ready.
22
+ */
23
+ onRouterReady?: (router: Router) => void;
24
+ }
25
+ /**
26
+ * Provider component that creates a molecule Router from React Router hooks and
27
+ * bonds it as the active singleton via `@molecule/app-routing`'s `setRouter`.
28
+ *
29
+ * Bonding happens in an effect on mount (and again whenever the location/params
30
+ * change), so `@molecule/app-routing`'s `navigate()`/`getRouter()` drive THIS real
31
+ * React Router adapter — no manual `setRouter` wiring required. Mount it once inside
32
+ * `<BrowserRouter>`.
33
+ *
34
+ * @param props - The provider props (see {@link MoleculeRouterProviderProps}):
35
+ * `children` (rendered inside the router context), `routes` (optional named-route
36
+ * definitions), and `onRouterReady` (optional callback invoked with the router
37
+ * after it is bonded, e.g. to register guards — bonding via `setRouter` happens
38
+ * regardless).
39
+ * @returns The rendered provider wrapping children with the router context.
40
+ * @example
41
+ * ```tsx
42
+ * import { BrowserRouter, Routes, Route } from 'react-router'
43
+ * import { MoleculeRouterProvider } from '@molecule/app-routing-react-router'
44
+ *
45
+ * function App() {
46
+ * // Bonds automatically — molecule packages' navigate() now drive React Router.
47
+ * return (
48
+ * <BrowserRouter>
49
+ * <MoleculeRouterProvider>
50
+ * <Routes>
51
+ * <Route path="/" element={<Home />} />
52
+ * <Route path="/users/:id" element={<UserProfile />} />
53
+ * </Routes>
54
+ * </MoleculeRouterProvider>
55
+ * </BrowserRouter>
56
+ * )
57
+ * }
58
+ * ```
59
+ */
60
+ export declare function MoleculeRouterProvider(props: MoleculeRouterProviderProps): React.JSX.Element;
61
+ /**
62
+ * Hook to get the molecule Router.
63
+ *
64
+ * @returns The molecule Router instance
65
+ * @throws {Error} If used outside MoleculeRouterProvider.
66
+ *
67
+ * @example
68
+ * ```tsx
69
+ * function NavigationButton() {
70
+ * const router = useMoleculeRouter()
71
+ *
72
+ * return (
73
+ * <button onClick={() => router.navigate('/dashboard')}>
74
+ * Go to Dashboard
75
+ * </button>
76
+ * )
77
+ * }
78
+ * ```
79
+ */
80
+ export declare function useMoleculeRouter(): Router;
81
+ /**
82
+ * Hook to check if a path is currently active.
83
+ *
84
+ * @param path - Path to check
85
+ * @param exact - Whether to require exact match
86
+ * @returns Whether the path is active
87
+ *
88
+ * @example
89
+ * ```tsx
90
+ * function NavLink({ to, children }) {
91
+ * const isActive = useIsActive(to)
92
+ * return (
93
+ * <a href={to} className={isActive ? 'active' : ''}>
94
+ * {children}
95
+ * </a>
96
+ * )
97
+ * }
98
+ * ```
99
+ */
100
+ export declare function useIsActive(path: string, exact?: boolean): boolean;
101
+ /**
102
+ * Hook to get a navigate function with molecule Router options.
103
+ *
104
+ * @returns Navigate function
105
+ *
106
+ * @example
107
+ * ```tsx
108
+ * function Form() {
109
+ * const navigate = useMoleculeNavigate()
110
+ *
111
+ * const handleSubmit = async () => {
112
+ * await saveData()
113
+ * navigate('/success', { state: { fromForm: true } })
114
+ * }
115
+ * }
116
+ * ```
117
+ */
118
+ export declare function useMoleculeNavigate(): Router['navigate'];
119
+ /**
120
+ * Hook to get current query params as an object.
121
+ *
122
+ * @returns Query params object
123
+ *
124
+ * @example
125
+ * ```tsx
126
+ * function SearchResults() {
127
+ * const query = useMoleculeQuery()
128
+ * const searchTerm = query.q
129
+ * }
130
+ * ```
131
+ */
132
+ export declare function useMoleculeQuery(): QueryParams;
133
+ //# sourceMappingURL=hooks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../src/hooks.tsx"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,EAAiB,KAAK,SAAS,EAAkC,MAAM,OAAO,CAAA;AAO5F,OAAO,KAAK,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,EAAE,MAAM,YAAY,CAAA;AAOtE;;GAEG;AACH,MAAM,WAAW,2BAA2B;IAC1C;;OAEG;IACH,QAAQ,EAAE,SAAS,CAAA;IAEnB;;OAEG;IACH,MAAM,CAAC,EAAE,eAAe,EAAE,CAAA;IAE1B;;OAEG;IACH,aAAa,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,IAAI,CAAA;CACzC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,2BAA2B,GAAG,KAAK,CAAC,GAAG,CAAC,OAAO,CAgC5F;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,iBAAiB,IAAI,MAAM,CAU1C;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,OAAO,GAAG,OAAO,CAKlE;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,mBAAmB,IAAI,MAAM,CAAC,UAAU,CAAC,CAGxD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,gBAAgB,IAAI,WAAW,CAK9C"}
package/dist/hooks.js ADDED
@@ -0,0 +1,170 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ /**
3
+ * React hooks for React Router integration.
4
+ *
5
+ * @module
6
+ */
7
+ import { createContext, useContext, useEffect, useMemo } from 'react';
8
+ import { useLocation, useNavigate, useParams, useSearchParams } from 'react-router';
9
+ import { t } from '@molecule/app-i18n';
10
+ import { setRouter } from '@molecule/app-routing';
11
+ import { createReactRouter } from './provider.js';
12
+ /**
13
+ * Context for the molecule Router.
14
+ */
15
+ const MoleculeRouterContext = createContext(null);
16
+ /**
17
+ * Provider component that creates a molecule Router from React Router hooks and
18
+ * bonds it as the active singleton via `@molecule/app-routing`'s `setRouter`.
19
+ *
20
+ * Bonding happens in an effect on mount (and again whenever the location/params
21
+ * change), so `@molecule/app-routing`'s `navigate()`/`getRouter()` drive THIS real
22
+ * React Router adapter — no manual `setRouter` wiring required. Mount it once inside
23
+ * `<BrowserRouter>`.
24
+ *
25
+ * @param props - The provider props (see {@link MoleculeRouterProviderProps}):
26
+ * `children` (rendered inside the router context), `routes` (optional named-route
27
+ * definitions), and `onRouterReady` (optional callback invoked with the router
28
+ * after it is bonded, e.g. to register guards — bonding via `setRouter` happens
29
+ * regardless).
30
+ * @returns The rendered provider wrapping children with the router context.
31
+ * @example
32
+ * ```tsx
33
+ * import { BrowserRouter, Routes, Route } from 'react-router'
34
+ * import { MoleculeRouterProvider } from '@molecule/app-routing-react-router'
35
+ *
36
+ * function App() {
37
+ * // Bonds automatically — molecule packages' navigate() now drive React Router.
38
+ * return (
39
+ * <BrowserRouter>
40
+ * <MoleculeRouterProvider>
41
+ * <Routes>
42
+ * <Route path="/" element={<Home />} />
43
+ * <Route path="/users/:id" element={<UserProfile />} />
44
+ * </Routes>
45
+ * </MoleculeRouterProvider>
46
+ * </BrowserRouter>
47
+ * )
48
+ * }
49
+ * ```
50
+ */
51
+ export function MoleculeRouterProvider(props) {
52
+ const { children, routes = [], onRouterReady } = props;
53
+ const navigate = useNavigate();
54
+ const location = useLocation();
55
+ const params = useParams();
56
+ useSearchParams(); // Subscribe to search param changes
57
+ const router = useMemo(() => createReactRouter({
58
+ navigate,
59
+ location: {
60
+ pathname: location.pathname,
61
+ search: location.search,
62
+ hash: location.hash,
63
+ state: location.state,
64
+ key: location.key,
65
+ },
66
+ params,
67
+ routes,
68
+ }), [navigate, location, params, routes]);
69
+ useEffect(() => {
70
+ // Bond THIS adapter so @molecule/app-routing's navigate()/getRouter() drive the
71
+ // real React Router instead of the core's auto-created fallback browser router.
72
+ setRouter(router);
73
+ onRouterReady?.(router);
74
+ }, [router, onRouterReady]);
75
+ return _jsx(MoleculeRouterContext.Provider, { value: router, children: children });
76
+ }
77
+ /**
78
+ * Hook to get the molecule Router.
79
+ *
80
+ * @returns The molecule Router instance
81
+ * @throws {Error} If used outside MoleculeRouterProvider.
82
+ *
83
+ * @example
84
+ * ```tsx
85
+ * function NavigationButton() {
86
+ * const router = useMoleculeRouter()
87
+ *
88
+ * return (
89
+ * <button onClick={() => router.navigate('/dashboard')}>
90
+ * Go to Dashboard
91
+ * </button>
92
+ * )
93
+ * }
94
+ * ```
95
+ */
96
+ export function useMoleculeRouter() {
97
+ const router = useContext(MoleculeRouterContext);
98
+ if (!router) {
99
+ throw new Error(t('routing.error.useMoleculeRouterOutsideProvider', undefined, {
100
+ defaultValue: 'useMoleculeRouter must be used within a MoleculeRouterProvider',
101
+ }));
102
+ }
103
+ return router;
104
+ }
105
+ /**
106
+ * Hook to check if a path is currently active.
107
+ *
108
+ * @param path - Path to check
109
+ * @param exact - Whether to require exact match
110
+ * @returns Whether the path is active
111
+ *
112
+ * @example
113
+ * ```tsx
114
+ * function NavLink({ to, children }) {
115
+ * const isActive = useIsActive(to)
116
+ * return (
117
+ * <a href={to} className={isActive ? 'active' : ''}>
118
+ * {children}
119
+ * </a>
120
+ * )
121
+ * }
122
+ * ```
123
+ */
124
+ export function useIsActive(path, exact) {
125
+ const router = useMoleculeRouter();
126
+ const location = useLocation();
127
+ // Re-evaluate when location changes
128
+ return useMemo(() => router.isActive(path, exact), [router, path, exact, location.pathname]);
129
+ }
130
+ /**
131
+ * Hook to get a navigate function with molecule Router options.
132
+ *
133
+ * @returns Navigate function
134
+ *
135
+ * @example
136
+ * ```tsx
137
+ * function Form() {
138
+ * const navigate = useMoleculeNavigate()
139
+ *
140
+ * const handleSubmit = async () => {
141
+ * await saveData()
142
+ * navigate('/success', { state: { fromForm: true } })
143
+ * }
144
+ * }
145
+ * ```
146
+ */
147
+ export function useMoleculeNavigate() {
148
+ const router = useMoleculeRouter();
149
+ return router.navigate.bind(router);
150
+ }
151
+ /**
152
+ * Hook to get current query params as an object.
153
+ *
154
+ * @returns Query params object
155
+ *
156
+ * @example
157
+ * ```tsx
158
+ * function SearchResults() {
159
+ * const query = useMoleculeQuery()
160
+ * const searchTerm = query.q
161
+ * }
162
+ * ```
163
+ */
164
+ export function useMoleculeQuery() {
165
+ const router = useMoleculeRouter();
166
+ const location = useLocation();
167
+ // Re-evaluate when search changes
168
+ return useMemo(() => router.getQuery(), [router, location.search]);
169
+ }
170
+ //# sourceMappingURL=hooks.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hooks.js","sourceRoot":"","sources":["../src/hooks.tsx"],"names":[],"mappings":";AAAA;;;;GAIG;AAEH,OAAc,EAAE,aAAa,EAAkB,UAAU,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,OAAO,CAAA;AAC5F,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,cAAc,CAAA;AAEnF,OAAO,EAAE,CAAC,EAAE,MAAM,oBAAoB,CAAA;AACtC,OAAO,EAAE,SAAS,EAAE,MAAM,uBAAuB,CAAA;AAEjD,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAA;AAGjD;;GAEG;AACH,MAAM,qBAAqB,GAAG,aAAa,CAAgB,IAAI,CAAC,CAAA;AAsBhE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,UAAU,sBAAsB,CAAC,KAAkC;IACvE,MAAM,EAAE,QAAQ,EAAE,MAA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AAC;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,WAAW,CAAC,IAAY,EAAE,KAAe;IACvD,MAAM,MAAM,GAAG,iBAAiB,EAAE,CAAA;IAClC,MAAM,QAAQ,GAAG,WAAW,EAAE,CAAA;IAC9B,oCAAoC;IACpC,OAAO,OAAO,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC,EAAE,CAAC,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAA;AAC9F,CAAC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,UAAU,mBAAmB;IACjC,MAAM,MAAM,GAAG,iBAAiB,EAAE,CAAA;IAClC,OAAO,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;AACrC,CAAC;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,UAAU,gBAAgB;IAC9B,MAAM,MAAM,GAAG,iBAAiB,EAAE,CAAA;IAClC,MAAM,QAAQ,GAAG,WAAW,EAAE,CAAA;IAC9B,kCAAkC;IAClC,OAAO,OAAO,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,QAAQ,EAAE,EAAE,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAA;AACpE,CAAC"}
@@ -0,0 +1,49 @@
1
+ /**
2
+ * React Router (v7/v8) provider for `@molecule/app-routing`.
3
+ *
4
+ * This package provides a React Router implementation of the molecule Router interface,
5
+ * allowing you to use molecule's routing abstractions with React Router.
6
+ *
7
+ * @example
8
+ * ```tsx
9
+ * import { BrowserRouter, Routes, Route } from 'react-router'
10
+ * import { MoleculeRouterProvider } from '@molecule/app-routing-react-router'
11
+ *
12
+ * function App() {
13
+ * // MoleculeRouterProvider bonds the adapter automatically (calls setRouter in an
14
+ * // effect on mount), so @molecule/app-routing's navigate()/getRouter() drive THIS
15
+ * // real React Router — no manual setRouter wiring needed.
16
+ * return (
17
+ * <BrowserRouter>
18
+ * <MoleculeRouterProvider>
19
+ * <Routes>
20
+ * <Route path="/" element={<Home />} />
21
+ * <Route path="/users/:id" element={<UserProfile />} />
22
+ * </Routes>
23
+ * </MoleculeRouterProvider>
24
+ * </BrowserRouter>
25
+ * )
26
+ * }
27
+ * ```
28
+ *
29
+ * @remarks
30
+ * - **`MoleculeRouterProvider` bonds the router for you.** On mount it calls
31
+ * `@molecule/app-routing`'s `setRouter` with the live adapter, so other molecule
32
+ * packages' `navigate()`/`getRouter()` drive React Router (real SPA navigation) —
33
+ * no manual wiring. Pass `onRouterReady` only if you also need the router instance
34
+ * (e.g. to register guards); bonding happens either way.
35
+ * - **Do NOT wire the exported `provider` const** in a React Router app — it is a
36
+ * no-hooks fallback (empty params, `window.location.href` navigation). Use
37
+ * `MoleculeRouterProvider` (or `createReactRouter` with hook values) instead.
38
+ * - The provider must live INSIDE `<BrowserRouter>` (it calls React Router hooks).
39
+ * - **Import from `'react-router'`, never `'react-router-dom'`.** The `-dom`
40
+ * package was discontinued after v7 (it was only a re-export shim); every API
41
+ * (`BrowserRouter`, `Routes`, `Route`, `Link`, hooks) lives in `react-router`.
42
+ *
43
+ * @module @molecule/app-routing-react-router
44
+ */
45
+ export * from './hooks.js';
46
+ export * from './provider.js';
47
+ export * from './types.js';
48
+ export * from './utilities.js';
49
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AAEH,cAAc,YAAY,CAAA;AAC1B,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,gBAAgB,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,49 @@
1
+ /**
2
+ * React Router (v7/v8) provider for `@molecule/app-routing`.
3
+ *
4
+ * This package provides a React Router implementation of the molecule Router interface,
5
+ * allowing you to use molecule's routing abstractions with React Router.
6
+ *
7
+ * @example
8
+ * ```tsx
9
+ * import { BrowserRouter, Routes, Route } from 'react-router'
10
+ * import { MoleculeRouterProvider } from '@molecule/app-routing-react-router'
11
+ *
12
+ * function App() {
13
+ * // MoleculeRouterProvider bonds the adapter automatically (calls setRouter in an
14
+ * // effect on mount), so @molecule/app-routing's navigate()/getRouter() drive THIS
15
+ * // real React Router — no manual setRouter wiring needed.
16
+ * return (
17
+ * <BrowserRouter>
18
+ * <MoleculeRouterProvider>
19
+ * <Routes>
20
+ * <Route path="/" element={<Home />} />
21
+ * <Route path="/users/:id" element={<UserProfile />} />
22
+ * </Routes>
23
+ * </MoleculeRouterProvider>
24
+ * </BrowserRouter>
25
+ * )
26
+ * }
27
+ * ```
28
+ *
29
+ * @remarks
30
+ * - **`MoleculeRouterProvider` bonds the router for you.** On mount it calls
31
+ * `@molecule/app-routing`'s `setRouter` with the live adapter, so other molecule
32
+ * packages' `navigate()`/`getRouter()` drive React Router (real SPA navigation) —
33
+ * no manual wiring. Pass `onRouterReady` only if you also need the router instance
34
+ * (e.g. to register guards); bonding happens either way.
35
+ * - **Do NOT wire the exported `provider` const** in a React Router app — it is a
36
+ * no-hooks fallback (empty params, `window.location.href` navigation). Use
37
+ * `MoleculeRouterProvider` (or `createReactRouter` with hook values) instead.
38
+ * - The provider must live INSIDE `<BrowserRouter>` (it calls React Router hooks).
39
+ * - **Import from `'react-router'`, never `'react-router-dom'`.** The `-dom`
40
+ * package was discontinued after v7 (it was only a re-export shim); every API
41
+ * (`BrowserRouter`, `Routes`, `Route`, `Link`, hooks) lives in `react-router`.
42
+ *
43
+ * @module @molecule/app-routing-react-router
44
+ */
45
+ export * from './hooks.js';
46
+ export * from './provider.js';
47
+ export * from './types.js';
48
+ export * from './utilities.js';
49
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2CG;AAEH,cAAc,YAAY,CAAA;AAC1B,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAC1B,cAAc,gBAAgB,CAAA"}
@@ -0,0 +1,44 @@
1
+ /**
2
+ * React Router (v7/v8) provider implementation.
3
+ *
4
+ * @module
5
+ */
6
+ import type { ReactRouterConfig, Router } from './types.js';
7
+ /**
8
+ * Creates a React Router adapter that implements the molecule Router interface.
9
+ *
10
+ * @example
11
+ * ```tsx
12
+ * import { useNavigate, useLocation, useParams, useSearchParams } from 'react-router'
13
+ * import { createReactRouter, setRouter } from '@molecule/app-routing-react-router'
14
+ * import { useEffect, useMemo } from 'react'
15
+ *
16
+ * function RouterProvider({ children }) {
17
+ * const navigate = useNavigate()
18
+ * const location = useLocation()
19
+ * const params = useParams()
20
+ * const [searchParams] = useSearchParams()
21
+ *
22
+ * const router = useMemo(() => createReactRouter({
23
+ * navigate,
24
+ * location,
25
+ * params,
26
+ * }), [navigate, location, params])
27
+ *
28
+ * useEffect(() => {
29
+ * setRouter(router)
30
+ * }, [router])
31
+ *
32
+ * return children
33
+ * }
34
+ * ```
35
+ * @param config - Configuration with React Router's `navigate` function, `location`, `params`, and optional `routes`.
36
+ * @returns A molecule `Router` with navigation, guards, query/hash management, and route matching.
37
+ */
38
+ export declare const createReactRouter: (config?: ReactRouterConfig) => Router;
39
+ /**
40
+ * Default React Router provider (basic, no hooks). For full functionality, use
41
+ * `createReactRouter` with `useNavigate`/`useLocation`/`useParams`.
42
+ */
43
+ export declare const provider: Router;
44
+ //# sourceMappingURL=provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EAIV,iBAAiB,EAMjB,MAAM,EACP,MAAM,YAAY,CAAA;AAGnB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,eAAO,MAAM,iBAAiB,GAAI,SAAQ,iBAAsB,KAAG,MA6QlE,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,QAAQ,QAAsB,CAAA"}
@@ -0,0 +1,270 @@
1
+ /**
2
+ * React Router (v7/v8) provider implementation.
3
+ *
4
+ * @module
5
+ */
6
+ import { I18nError } from '@molecule/app-i18n';
7
+ import { generatePath, matchPath, parseSearchParams, stringifyQuery } from './utilities.js';
8
+ /**
9
+ * Creates a React Router adapter that implements the molecule Router interface.
10
+ *
11
+ * @example
12
+ * ```tsx
13
+ * import { useNavigate, useLocation, useParams, useSearchParams } from 'react-router'
14
+ * import { createReactRouter, setRouter } from '@molecule/app-routing-react-router'
15
+ * import { useEffect, useMemo } from 'react'
16
+ *
17
+ * function RouterProvider({ children }) {
18
+ * const navigate = useNavigate()
19
+ * const location = useLocation()
20
+ * const params = useParams()
21
+ * const [searchParams] = useSearchParams()
22
+ *
23
+ * const router = useMemo(() => createReactRouter({
24
+ * navigate,
25
+ * location,
26
+ * params,
27
+ * }), [navigate, location, params])
28
+ *
29
+ * useEffect(() => {
30
+ * setRouter(router)
31
+ * }, [router])
32
+ *
33
+ * return children
34
+ * }
35
+ * ```
36
+ * @param config - Configuration with React Router's `navigate` function, `location`, `params`, and optional `routes`.
37
+ * @returns A molecule `Router` with navigation, guards, query/hash management, and route matching.
38
+ */
39
+ export const createReactRouter = (config = {}) => {
40
+ const { navigate: navigateFn, location: locationObj, params = {}, routes = [] } = config;
41
+ let registeredRoutes = [...routes];
42
+ const listeners = new Set();
43
+ const guards = [];
44
+ const namedRoutes = new Map();
45
+ // Index named routes
46
+ const indexRoutes = (routeList, prefix = '') => {
47
+ for (const route of routeList) {
48
+ const fullPath = prefix + route.path;
49
+ if (route.name) {
50
+ namedRoutes.set(route.name, { ...route, path: fullPath });
51
+ }
52
+ if (route.children) {
53
+ indexRoutes(route.children, fullPath);
54
+ }
55
+ }
56
+ };
57
+ indexRoutes(routes);
58
+ // Build current location
59
+ const getCurrentLocation = () => {
60
+ if (locationObj) {
61
+ return {
62
+ pathname: locationObj.pathname,
63
+ search: locationObj.search,
64
+ hash: locationObj.hash,
65
+ state: locationObj.state,
66
+ key: locationObj.key,
67
+ };
68
+ }
69
+ if (typeof window !== 'undefined') {
70
+ return {
71
+ pathname: window.location.pathname,
72
+ search: window.location.search,
73
+ hash: window.location.hash,
74
+ key: Math.random().toString(36).slice(2),
75
+ };
76
+ }
77
+ return {
78
+ pathname: '/',
79
+ search: '',
80
+ hash: '',
81
+ key: Math.random().toString(36).slice(2),
82
+ };
83
+ };
84
+ const normalizeParams = (p) => {
85
+ const result = {};
86
+ for (const [key, value] of Object.entries(p)) {
87
+ if (value !== undefined) {
88
+ result[key] = value;
89
+ }
90
+ }
91
+ return result;
92
+ };
93
+ const runGuards = async (to, from) => {
94
+ for (const guard of guards) {
95
+ const result = await guard(to, from);
96
+ if (result !== undefined && result !== true) {
97
+ return result;
98
+ }
99
+ }
100
+ return true;
101
+ };
102
+ const notify = (action) => {
103
+ const location = getCurrentLocation();
104
+ listeners.forEach((listener) => listener(location, action));
105
+ };
106
+ const doNavigate = async (path, options = {}) => {
107
+ const { replace = false, preserveQuery, preserveHash, state } = options;
108
+ const currentLocation = getCurrentLocation();
109
+ let fullPath = path;
110
+ if (preserveQuery && currentLocation.search) {
111
+ fullPath += currentLocation.search;
112
+ }
113
+ if (preserveHash && currentLocation.hash) {
114
+ fullPath += currentLocation.hash;
115
+ }
116
+ const newLocation = {
117
+ pathname: fullPath.split('?')[0].split('#')[0],
118
+ search: fullPath.includes('?') ? `?${fullPath.split('?')[1].split('#')[0]}` : '',
119
+ hash: fullPath.includes('#') ? `#${fullPath.split('#')[1]}` : '',
120
+ state,
121
+ key: Math.random().toString(36).slice(2),
122
+ };
123
+ const guardResult = await runGuards(newLocation, currentLocation);
124
+ if (guardResult === false)
125
+ return;
126
+ if (typeof guardResult === 'string') {
127
+ doNavigate(guardResult, { replace: true });
128
+ return;
129
+ }
130
+ if (typeof guardResult === 'object' && 'path' in guardResult) {
131
+ doNavigate(guardResult.path, { replace: guardResult.replace });
132
+ return;
133
+ }
134
+ if (navigateFn) {
135
+ navigateFn(fullPath, { replace, state });
136
+ }
137
+ else if (typeof window !== 'undefined') {
138
+ // Fallback to window.location
139
+ if (replace) {
140
+ window.location.replace(fullPath);
141
+ }
142
+ else {
143
+ window.location.href = fullPath;
144
+ }
145
+ }
146
+ notify(replace ? 'replace' : 'push');
147
+ };
148
+ const router = {
149
+ getLocation: getCurrentLocation,
150
+ getParams() {
151
+ return normalizeParams(params);
152
+ },
153
+ getQuery: () => {
154
+ const location = getCurrentLocation();
155
+ const searchParams = new URLSearchParams(location.search);
156
+ return parseSearchParams(searchParams);
157
+ },
158
+ getQueryParam: (key) => {
159
+ const location = getCurrentLocation();
160
+ const searchParams = new URLSearchParams(location.search);
161
+ return searchParams.get(key) ?? undefined;
162
+ },
163
+ getHash: () => getCurrentLocation().hash,
164
+ navigate: doNavigate,
165
+ navigateTo(name, routeParams, query, options) {
166
+ const route = namedRoutes.get(name);
167
+ if (!route) {
168
+ throw new I18nError('routing.error.routeNotFound', { name }, `Route "${name}" not found`);
169
+ }
170
+ let path = generatePath(route.path, routeParams);
171
+ if (query) {
172
+ path += stringifyQuery(query);
173
+ }
174
+ doNavigate(path, options);
175
+ },
176
+ back() {
177
+ if (navigateFn) {
178
+ navigateFn(-1);
179
+ }
180
+ else if (typeof window !== 'undefined') {
181
+ window.history.back();
182
+ }
183
+ },
184
+ forward() {
185
+ if (navigateFn) {
186
+ navigateFn(1);
187
+ }
188
+ else if (typeof window !== 'undefined') {
189
+ window.history.forward();
190
+ }
191
+ },
192
+ go(delta) {
193
+ if (navigateFn) {
194
+ navigateFn(delta);
195
+ }
196
+ else if (typeof window !== 'undefined') {
197
+ window.history.go(delta);
198
+ }
199
+ },
200
+ setQuery(query, options) {
201
+ const location = getCurrentLocation();
202
+ const path = location.pathname + stringifyQuery(query) + location.hash;
203
+ doNavigate(path, options);
204
+ },
205
+ setQueryParam(key, value, options) {
206
+ const query = this.getQuery();
207
+ if (value === undefined) {
208
+ delete query[key];
209
+ }
210
+ else {
211
+ query[key] = value;
212
+ }
213
+ this.setQuery(query, options);
214
+ },
215
+ setHash(hash, options) {
216
+ const location = getCurrentLocation();
217
+ const normalizedHash = hash.startsWith('#') ? hash : `#${hash}`;
218
+ const path = location.pathname + location.search + normalizedHash;
219
+ doNavigate(path, options);
220
+ },
221
+ isActive(path, exact) {
222
+ const location = getCurrentLocation();
223
+ const match = matchPath(path, location.pathname, exact);
224
+ return match !== null;
225
+ },
226
+ matchPath(pattern, pathToMatch) {
227
+ return matchPath(pattern, pathToMatch);
228
+ },
229
+ generatePath(name, routeParams, query) {
230
+ const route = namedRoutes.get(name);
231
+ if (!route) {
232
+ throw new I18nError('routing.error.routeNotFound', { name }, `Route "${name}" not found`);
233
+ }
234
+ let path = generatePath(route.path, routeParams);
235
+ if (query) {
236
+ path += stringifyQuery(query);
237
+ }
238
+ return path;
239
+ },
240
+ subscribe(listener) {
241
+ listeners.add(listener);
242
+ return () => listeners.delete(listener);
243
+ },
244
+ addGuard(guard) {
245
+ guards.push(guard);
246
+ return () => {
247
+ const index = guards.indexOf(guard);
248
+ if (index !== -1) {
249
+ guards.splice(index, 1);
250
+ }
251
+ };
252
+ },
253
+ registerRoutes(newRoutes) {
254
+ registeredRoutes = [...registeredRoutes, ...newRoutes];
255
+ indexRoutes(newRoutes);
256
+ },
257
+ getRoutes: () => registeredRoutes,
258
+ destroy() {
259
+ listeners.clear();
260
+ guards.length = 0;
261
+ },
262
+ };
263
+ return router;
264
+ };
265
+ /**
266
+ * Default React Router provider (basic, no hooks). For full functionality, use
267
+ * `createReactRouter` with `useNavigate`/`useLocation`/`useParams`.
268
+ */
269
+ export const provider = createReactRouter();
270
+ //# sourceMappingURL=provider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.js","sourceRoot":"","sources":["../src/prov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G,CAAC,QAAQ,CAAC,CAAA;YACvB,OAAO,GAAG,EAAE,CAAC,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAA;QACzC,CAAC;QAED,QAAQ,CAAC,KAAsB;YAC7B,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;YAClB,OAAO,GAAG,EAAE;gBACV,MAAM,KAAK,GAAG,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;gBACnC,IAAI,KAAK,KAAK,CAAC,CAAC,EAAE,CAAC;oBACjB,MAAM,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAA;gBACzB,CAAC;YACH,CAAC,CAAA;QACH,CAAC;QAED,cAAc,CAAC,SAA4B;YACzC,gBAAgB,GAAG,CAAC,GAAG,gBAAgB,EAAE,GAAG,SAAS,CAAC,CAAA;YACtD,WAAW,CAAC,SAAS,CAAC,CAAA;QACxB,CAAC;QAED,SAAS,EAAE,GAAG,EAAE,CAAC,gBAAgB;QAEjC,OAAO;YACL,SAAS,CAAC,KAAK,EAAE,CAAA;YACjB,MAAM,CAAC,MAAM,GAAG,CAAC,CAAA;QACnB,CAAC;KACF,CAAA;IAED,OAAO,MAAM,CAAA;AACf,CAAC,CAAA;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,iBAAiB,EAAE,CAAA"}
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Type definitions for React Router provider.
3
+ *
4
+ * @module
5
+ */
6
+ import type { NavigateFunction } from 'react-router';
7
+ import type { RouteDefinition } from '@molecule/app-routing';
8
+ export type { GuardResult, NavigateOptions, NavigationGuard, QueryParams, RouteChangeListener, RouteDefinition, RouteLocation, RouteMatch, RouteParams, Router, RouterConfig, } from '@molecule/app-routing';
9
+ /**
10
+ * React Router-specific configuration.
11
+ */
12
+ export interface ReactRouterConfig {
13
+ /**
14
+ * React Router navigate function (from useNavigate).
15
+ */
16
+ navigate?: NavigateFunction;
17
+ /**
18
+ * Current location (from useLocation).
19
+ */
20
+ location?: {
21
+ pathname: string;
22
+ search: string;
23
+ hash: string;
24
+ state?: unknown;
25
+ key?: string;
26
+ };
27
+ /**
28
+ * Current params (from useParams).
29
+ */
30
+ params?: Record<string, string | undefined>;
31
+ /**
32
+ * Initial route definitions.
33
+ */
34
+ routes?: RouteDefinition[];
35
+ }
36
+ /**
37
+ * React Router hooks adapter.
38
+ */
39
+ export interface ReactRouterHooks {
40
+ /**
41
+ * The navigate function from useNavigate.
42
+ */
43
+ navigate: NavigateFunction;
44
+ /**
45
+ * Location from useLocation.
46
+ */
47
+ location: {
48
+ pathname: string;
49
+ search: string;
50
+ hash: string;
51
+ state?: unknown;
52
+ key?: string;
53
+ };
54
+ /**
55
+ * Params from useParams.
56
+ */
57
+ params: Record<string, string | undefined>;
58
+ /**
59
+ * Search params from useSearchParams.
60
+ */
61
+ searchParams: URLSearchParams;
62
+ }
63
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAA;AAEpD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AAG5D,YAAY,EACV,WAAW,EACX,eAAe,EACf,eAAe,EACf,WAAW,EACX,mBAAmB,EACnB,eAAe,EACf,aAAa,EACb,UAAU,EACV,WAAW,EACX,MAAM,EACN,YAAY,GACb,MAAM,uBAAuB,CAAA;AAE9B;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAChC;;OAEG;IACH,QAAQ,CAAC,EAAE,gBAAgB,CAAA;IAE3B;;OAEG;IACH,QAAQ,CAAC,EAAE;QACT,QAAQ,EAAE,MAAM,CAAA;QAChB,MAAM,EAAE,MAAM,CAAA;QACd,IAAI,EAAE,MAAM,CAAA;QACZ,KAAK,CAAC,EAAE,OAAO,CAAA;QACf,GAAG,CAAC,EAAE,MAAM,CAAA;KACb,CAAA;IAED;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA;IAE3C;;OAEG;IACH,MAAM,CAAC,EAAE,eAAe,EAAE,CAAA;CAC3B;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;OAEG;IACH,QAAQ,EAAE,gBAAgB,CAAA;IAE1B;;OAEG;IACH,QAAQ,EAAE;QACR,QAAQ,EAAE,MAAM,CAAA;QAChB,MAAM,EAAE,MAAM,CAAA;QACd,IAAI,EAAE,MAAM,CAAA;QACZ,KAAK,CAAC,EAAE,OAAO,CAAA;QACf,GAAG,CAAC,EAAE,MAAM,CAAA;KACb,CAAA;IAED;;OAEG;IACH,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAA;IAE1C;;OAEG;IACH,YAAY,EAAE,eAAe,CAAA;CAC9B"}
package/dist/types.js ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Type definitions for React Router provider.
3
+ *
4
+ * @module
5
+ */
6
+ export {};
7
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG"}
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Utility functions for React Router provider.
3
+ *
4
+ * @module
5
+ */
6
+ import type { QueryParams, RouteMatch, RouteParams } from './types.js';
7
+ /**
8
+ * Parses `URLSearchParams` into a molecule `QueryParams` object. Duplicate keys
9
+ * are collected into arrays.
10
+ * @param searchParams - The `URLSearchParams` instance (e.g. from `useSearchParams()`).
11
+ * @returns A `QueryParams` map where duplicate keys become string arrays.
12
+ */
13
+ export declare function parseSearchParams(searchParams: URLSearchParams): QueryParams;
14
+ /**
15
+ * Converts a molecule `QueryParams` object to a URL search string (e.g. `?key=val&arr=1&arr=2`).
16
+ * Omits keys with `undefined` values. Returns empty string if no params.
17
+ * @param params - The query parameters to stringify.
18
+ * @returns A URL search string starting with `?`, or empty string if no params.
19
+ */
20
+ export declare function stringifyQuery(params: QueryParams): string;
21
+ /**
22
+ * Matches a path pattern (with `:param` segments and `*` wildcards) against a pathname.
23
+ * @param pattern - The route pattern (e.g. `'/users/:id'`).
24
+ * @param pathname - The actual URL pathname to test.
25
+ * @param exact - Whether to require an exact match (default `true`). Set to `false` for prefix matching.
26
+ * @returns A `RouteMatch` with extracted params, or `null` if no match.
27
+ */
28
+ export declare function matchPath<Params extends RouteParams = RouteParams>(pattern: string, pathname: string, exact?: boolean): RouteMatch<Params> | null;
29
+ /**
30
+ * Generates a concrete path from a route pattern by replacing `:param` segments with values.
31
+ * Throws if a required param is missing.
32
+ * @param pattern - The route pattern (e.g. `'/users/:id'`).
33
+ * @param params - A map of param names to values.
34
+ * @returns The resolved path string with params URL-encoded.
35
+ */
36
+ export declare function generatePath(pattern: string, params?: RouteParams): string;
37
+ /**
38
+ * Resolves a relative path against a base pathname. Absolute paths (starting with `/`) are
39
+ * returned as-is. Relative paths handle `..` (parent) and `.` (current) segments.
40
+ * @param to - The destination path (absolute or relative).
41
+ * @param fromPathname - The current pathname to resolve against.
42
+ * @returns The resolved absolute path.
43
+ */
44
+ export declare function resolvePath(to: string, fromPathname: string): string;
45
+ /**
46
+ * Removes trailing slashes from a path. Returns `'/'` for root/empty paths.
47
+ * @param path - The URL path to normalize.
48
+ * @returns The path without trailing slashes.
49
+ */
50
+ export declare function normalizePath(path: string): string;
51
+ //# sourceMappingURL=utilities.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utilities.d.ts","sourceRoot":"","sources":["../src/utilities.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAEtE;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,YAAY,EAAE,eAAe,GAAG,WAAW,CAe5E;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,EAAE,WAAW,GAAG,MAAM,CAW1D;AAED;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,MAAM,SAAS,WAAW,GAAG,WAAW,EAChE,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,KAAK,CAAC,EAAE,OAAO,GACd,UAAU,CAAC,MAAM,CAAC,GAAG,IAAI,CAgC3B;AAED;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,MAAM,CAc1E;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,EAAE,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,MAAM,CAiBpE;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAElD"}
@@ -0,0 +1,130 @@
1
+ /**
2
+ * Utility functions for React Router provider.
3
+ *
4
+ * @module
5
+ */
6
+ import { I18nError } from '@molecule/app-i18n';
7
+ /**
8
+ * Parses `URLSearchParams` into a molecule `QueryParams` object. Duplicate keys
9
+ * are collected into arrays.
10
+ * @param searchParams - The `URLSearchParams` instance (e.g. from `useSearchParams()`).
11
+ * @returns A `QueryParams` map where duplicate keys become string arrays.
12
+ */
13
+ export function parseSearchParams(searchParams) {
14
+ const result = {};
15
+ for (const [key, value] of searchParams.entries()) {
16
+ const existing = result[key];
17
+ if (existing === undefined) {
18
+ result[key] = value;
19
+ }
20
+ else if (Array.isArray(existing)) {
21
+ existing.push(value);
22
+ }
23
+ else {
24
+ result[key] = [existing, value];
25
+ }
26
+ }
27
+ return result;
28
+ }
29
+ /**
30
+ * Converts a molecule `QueryParams` object to a URL search string (e.g. `?key=val&arr=1&arr=2`).
31
+ * Omits keys with `undefined` values. Returns empty string if no params.
32
+ * @param params - The query parameters to stringify.
33
+ * @returns A URL search string starting with `?`, or empty string if no params.
34
+ */
35
+ export function stringifyQuery(params) {
36
+ const entries = Object.entries(params)
37
+ .filter(([, v]) => v !== undefined)
38
+ .flatMap(([k, v]) => {
39
+ if (Array.isArray(v)) {
40
+ return v.map((val) => `${encodeURIComponent(k)}=${encodeURIComponent(val)}`);
41
+ }
42
+ return `${encodeURIComponent(k)}=${encodeURIComponent(v)}`;
43
+ });
44
+ return entries.length ? `?${entries.join('&')}` : '';
45
+ }
46
+ /**
47
+ * Matches a path pattern (with `:param` segments and `*` wildcards) against a pathname.
48
+ * @param pattern - The route pattern (e.g. `'/users/:id'`).
49
+ * @param pathname - The actual URL pathname to test.
50
+ * @param exact - Whether to require an exact match (default `true`). Set to `false` for prefix matching.
51
+ * @returns A `RouteMatch` with extracted params, or `null` if no match.
52
+ */
53
+ export function matchPath(pattern, pathname, exact) {
54
+ // Convert pattern to regex
55
+ // :param -> named capture group
56
+ // * -> wildcard
57
+ const paramNames = [];
58
+ const regexPattern = pattern
59
+ .replace(/:([^/]+)/g, (_, name) => {
60
+ paramNames.push(name);
61
+ return '([^/]+)';
62
+ })
63
+ .replace(/\*/g, '.*');
64
+ const fullPattern = exact !== false ? `^${regexPattern}$` : `^${regexPattern}`;
65
+ const regex = new RegExp(fullPattern);
66
+ const match = pathname.match(regex);
67
+ if (!match) {
68
+ return null;
69
+ }
70
+ const params = {};
71
+ paramNames.forEach((name, index) => {
72
+ params[name] = match[index + 1] || '';
73
+ });
74
+ return {
75
+ path: pattern,
76
+ pathname: match[0],
77
+ params: params,
78
+ isExact: match[0] === pathname,
79
+ };
80
+ }
81
+ /**
82
+ * Generates a concrete path from a route pattern by replacing `:param` segments with values.
83
+ * Throws if a required param is missing.
84
+ * @param pattern - The route pattern (e.g. `'/users/:id'`).
85
+ * @param params - A map of param names to values.
86
+ * @returns The resolved path string with params URL-encoded.
87
+ */
88
+ export function generatePath(pattern, params) {
89
+ if (!params)
90
+ return pattern;
91
+ return pattern.replace(/:([^/]+)/g, (_, name) => {
92
+ const value = params[name];
93
+ if (value === undefined) {
94
+ throw new I18nError('routing.error.missingParam', { name, pattern }, `Missing required param "${name}" for path "${pattern}"`);
95
+ }
96
+ return encodeURIComponent(value);
97
+ });
98
+ }
99
+ /**
100
+ * Resolves a relative path against a base pathname. Absolute paths (starting with `/`) are
101
+ * returned as-is. Relative paths handle `..` (parent) and `.` (current) segments.
102
+ * @param to - The destination path (absolute or relative).
103
+ * @param fromPathname - The current pathname to resolve against.
104
+ * @returns The resolved absolute path.
105
+ */
106
+ export function resolvePath(to, fromPathname) {
107
+ if (to.startsWith('/')) {
108
+ return to;
109
+ }
110
+ const fromSegments = fromPathname.split('/').slice(0, -1);
111
+ const toSegments = to.split('/');
112
+ for (const segment of toSegments) {
113
+ if (segment === '..') {
114
+ fromSegments.pop();
115
+ }
116
+ else if (segment !== '.') {
117
+ fromSegments.push(segment);
118
+ }
119
+ }
120
+ return fromSegments.join('/') || '/';
121
+ }
122
+ /**
123
+ * Removes trailing slashes from a path. Returns `'/'` for root/empty paths.
124
+ * @param path - The URL path to normalize.
125
+ * @returns The path without trailing slashes.
126
+ */
127
+ export function normalizePath(path) {
128
+ return path.replace(/\/+$/, '') || '/';
129
+ }
130
+ //# sourceMappingURL=utilities.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utilities.js","sourceRoot":"","sources":["../src/utilities.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAA;AAI9C;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,YAA6B;IAC7D,MAAM,MAAM,GAAgB,EAAE,CAAA;IAE9B,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,YAAY,CAAC,OAAO,EAAE,EAAE,CAAC;QAClD,MAAM,QAAQ,GAAG,MAAM,CAAC,GAAG,CAAC,CAAA;QAC5B,IAAI,QAAQ,KAAK,SAAS,EAAE,CAAC;YAC3B,MAAM,CAAC,GAAG,CAAC,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AS,CACjB,4BAA4B,EAC5B,EAAE,IAAI,EAAE,OAAO,EAAE,EACjB,2BAA2B,IAAI,eAAe,OAAO,GAAG,CACzD,CAAA;QACH,CAAC;QACD,OAAO,kBAAkB,CAAC,KAAK,CAAC,CAAA;IAClC,CAAC,CAAC,CAAA;AACJ,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CAAC,EAAU,EAAE,YAAoB;IAC1D,IAAI,EAAE,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;QACvB,OAAO,EAAE,CAAA;IACX,CAAC;IAED,MAAM,YAAY,GAAG,YAAY,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAA;IACzD,MAAM,UAAU,GAAG,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAEhC,KAAK,MAAM,OAAO,IAAI,UAAU,EAAE,CAAC;QACjC,IAAI,OAAO,KAAK,IAAI,EAAE,CAAC;YACrB,YAAY,CAAC,GAAG,EAAE,CAAA;QACpB,CAAC;aAAM,IAAI,OAAO,KAAK,GAAG,EAAE,CAAC;YAC3B,YAAY,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;QAC5B,CAAC;IACH,CAAC;IAED,OAAO,YAAY,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,GAAG,CAAA;AACtC,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,IAAY;IACxC,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,IAAI,GAAG,CAAA;AACxC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@molecule/app-routing-react-router",
3
+ "version": "1.0.0",
4
+ "description": "React Router v7 provider for @molecule/app-routing",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist"
10
+ ],
11
+ "scripts": {
12
+ "build": "tsc",
13
+ "dev": "tsc --watch",
14
+ "test": "vitest run",
15
+ "test:watch": "vitest"
16
+ },
17
+ "author": "Molecule Dev, Inc. (https://molecule.dev)",
18
+ "license": "Apache-2.0",
19
+ "engines": {
20
+ "node": ">=20"
21
+ },
22
+ "peerDependencies": {
23
+ "@molecule/app-i18n": "^1.0.0",
24
+ "@molecule/app-routing": "^1.0.0",
25
+ "react": "^18.0.0 || ^19.0.0",
26
+ "react-router": "^7.0.0 || ^8.0.0"
27
+ },
28
+ "devDependencies": {
29
+ "@molecule/app-i18n": "1.0.0",
30
+ "@types/node": "26.1.2",
31
+ "@types/react": "19.2.17",
32
+ "react": "19.2.8",
33
+ "react-router": "8.3.0",
34
+ "typescript": "6.0.3",
35
+ "vitest": "4.1.10"
36
+ },
37
+ "repository": {
38
+ "type": "git",
39
+ "url": "https://github.com/molecule-dev/molecule.git",
40
+ "directory": "packages/app/bonds/routing/react-router"
41
+ },
42
+ "homepage": "https://github.com/molecule-dev/molecule/tree/main/packages/app/bonds/routing/react-router",
43
+ "bugs": "https://github.com/molecule-dev/molecule/issues",
44
+ "publishConfig": {
45
+ "access": "public"
46
+ }
47
+ }