@subako-ai/react-router 0.1.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 +202 -0
- package/README.md +98 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +77 -0
- package/dist/index.js.map +1 -0
- package/package.json +46 -0
- package/src/index.ts +108 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright 2026 Kikuvi, Inc.
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
# @subako-ai/react-router
|
|
2
|
+
|
|
3
|
+
[React Router](https://reactrouter.com) as a `navigateTool` spec for
|
|
4
|
+
`@subako-ai/tools`: the app names the routes, `useNavigate()` is the move, and
|
|
5
|
+
`useLocation()` is the read back.
|
|
6
|
+
|
|
7
|
+
Requires **React Router 6.4 or newer** and **Node 22 or newer** for the build.
|
|
8
|
+
The package is **ESM only**.
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
pnpm add @subako-ai/react-router @subako-ai/tools
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## `fromReactRouter`
|
|
15
|
+
|
|
16
|
+
```tsx
|
|
17
|
+
import { useTool } from "@subako-ai/react";
|
|
18
|
+
import { fromReactRouter } from "@subako-ai/react-router";
|
|
19
|
+
import { navigateTool } from "@subako-ai/tools";
|
|
20
|
+
import { useRef } from "react";
|
|
21
|
+
import { useLocation, useNavigate } from "react-router";
|
|
22
|
+
|
|
23
|
+
function App() {
|
|
24
|
+
const navigate = useNavigate();
|
|
25
|
+
const { pathname } = useLocation();
|
|
26
|
+
|
|
27
|
+
// Where the app is now, read when the model asks rather than when the tool was built.
|
|
28
|
+
const here = useRef(pathname);
|
|
29
|
+
here.current = pathname;
|
|
30
|
+
|
|
31
|
+
useTool(
|
|
32
|
+
client,
|
|
33
|
+
"navigate",
|
|
34
|
+
navigateTool(
|
|
35
|
+
fromReactRouter({
|
|
36
|
+
navigate,
|
|
37
|
+
location: () => here.current,
|
|
38
|
+
routes: [
|
|
39
|
+
{ to: "/documents", description: "Every document" },
|
|
40
|
+
{ to: "/documents/:id", description: "One document" },
|
|
41
|
+
],
|
|
42
|
+
}),
|
|
43
|
+
),
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
**The app names its own routes.** Neither the `<Routes>` style nor the
|
|
51
|
+
file-based one keeps a registry to read at runtime, so there is nothing to
|
|
52
|
+
enumerate — and naming them is worth doing anyway, because it is the allowlist:
|
|
53
|
+
the model can only reach a page you listed, with the description you gave it.
|
|
54
|
+
|
|
55
|
+
A route's path parameters come from its path, so `/documents/:id` needs no
|
|
56
|
+
`params` of its own. Declare them only to say one is a number.
|
|
57
|
+
|
|
58
|
+
The binding builds the path it navigates to: React Router's `navigate()` takes
|
|
59
|
+
`/documents/7`, not `/documents/:id` and `{ id: "7" }`. Values are escaped on the
|
|
60
|
+
way in. A splat is not a destination a model can name, and neither is an
|
|
61
|
+
optional segment (`:id?`) — the binding does not elide it, so both are refused
|
|
62
|
+
when the binding is built; list the concrete variants as separate routes
|
|
63
|
+
instead.
|
|
64
|
+
|
|
65
|
+
`location` is optional. Without it the answer is the path the binding just
|
|
66
|
+
built, which is still a place — unlike the pattern `navigateTool` would
|
|
67
|
+
otherwise report.
|
|
68
|
+
|
|
69
|
+
**Read it live.** `location` is called when the model asks, which can be many
|
|
70
|
+
renders after the tool was built, and `useLocation()` answers with a new object
|
|
71
|
+
every render. Close over one — `location: () => location.pathname` — and the
|
|
72
|
+
binding reports the page the app was on when the tool was declared: the model
|
|
73
|
+
navigates, is told it is still where it started, and navigates again. The ref
|
|
74
|
+
above always holds the current render's path, so the read is live whether or not
|
|
75
|
+
the tool is memoised. `window.location.pathname` works too, minus the router's
|
|
76
|
+
`basename` if it has one.
|
|
77
|
+
|
|
78
|
+
## `routesOf`
|
|
79
|
+
|
|
80
|
+
For a data router, where the route objects do exist:
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
import { createBrowserRouter } from "react-router";
|
|
84
|
+
import { fromReactRouter, routesOf } from "@subako-ai/react-router";
|
|
85
|
+
|
|
86
|
+
const router = createBrowserRouter(routeObjects);
|
|
87
|
+
fromReactRouter({
|
|
88
|
+
navigate: (to) => router.navigate(to),
|
|
89
|
+
routes: routesOf(routeObjects),
|
|
90
|
+
});
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
It flattens the tree, joining a child's path onto its parent's unless the child
|
|
94
|
+
opens with a slash. A pathless layout route is not a destination, and neither
|
|
95
|
+
is a splat or an optional segment: all three are dropped, and what sits under
|
|
96
|
+
them is kept. An `index: true` route has no path of its own either, and needs
|
|
97
|
+
none — the parent's path already reaches it. Descriptions are yours to add
|
|
98
|
+
afterwards — the route objects carry none.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { NavigateSource, RouteSpec } from "@subako-ai/tools";
|
|
2
|
+
/** The shape of a data router's route objects, as much of it as this needs. */
|
|
3
|
+
export interface RouteObjectLike {
|
|
4
|
+
readonly path?: string;
|
|
5
|
+
readonly index?: boolean;
|
|
6
|
+
readonly children?: readonly RouteObjectLike[];
|
|
7
|
+
}
|
|
8
|
+
/** What an app hands the binding: how to go, where it may go, and where it is. */
|
|
9
|
+
export interface ReactRouterSource {
|
|
10
|
+
/** What `useNavigate()` answers with. */
|
|
11
|
+
navigate(to: string): void | Promise<unknown>;
|
|
12
|
+
/** The routes the model may name, as the app spells them: `/documents/:id`. */
|
|
13
|
+
readonly routes: readonly RouteSpec[];
|
|
14
|
+
/**
|
|
15
|
+
* Where the app is now. It is called when the model asks, not when the tool is
|
|
16
|
+
* built, so it has to read the live value: `useLocation()` answers with a new
|
|
17
|
+
* object every render, and closing over one leaves the binding reporting the
|
|
18
|
+
* page the app was on when the tool was declared.
|
|
19
|
+
*/
|
|
20
|
+
location?(): string;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* React Router as a `navigateTool` spec. Neither the `<Routes>` style nor the file-based one
|
|
24
|
+
* keeps a route registry to read at runtime, so the routes are the ones the app names here.
|
|
25
|
+
*/
|
|
26
|
+
export declare function fromReactRouter(source: ReactRouterSource): NavigateSource;
|
|
27
|
+
/**
|
|
28
|
+
* A `createBrowserRouter` route tree, flattened into the specs `navigateTool` takes. A pathless
|
|
29
|
+
* layout is not a destination, and neither is a splat or an optional segment, so all three are
|
|
30
|
+
* dropped and what is under them is kept. An `index: true` route has no `path` of its own
|
|
31
|
+
* either, and needs none: the parent's path already reaches it.
|
|
32
|
+
*/
|
|
33
|
+
export declare function routesOf(routes: readonly RouteObjectLike[]): RouteSpec[];
|
|
34
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAkB,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAyBlF,+EAA+E;AAC/E,MAAM,WAAW,eAAe;IAC/B,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,QAAQ,CAAC,EAAE,SAAS,eAAe,EAAE,CAAC;CAC/C;AAED,kFAAkF;AAClF,MAAM,WAAW,iBAAiB;IACjC,yCAAyC;IACzC,QAAQ,CAAC,EAAE,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IAC9C,+EAA+E;IAC/E,QAAQ,CAAC,MAAM,EAAE,SAAS,SAAS,EAAE,CAAC;IACtC;;;;;OAKG;IACH,QAAQ,CAAC,IAAI,MAAM,CAAC;CACpB;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,iBAAiB,GAAG,cAAc,CA4BzE;AAMD;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,MAAM,EAAE,SAAS,eAAe,EAAE,GAAG,SAAS,EAAE,CAgBxE"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
/** React Router spells a path parameter `:name`, and every one of them is a string. */
|
|
2
|
+
const PARAM = /:([A-Za-z0-9_]+)/g;
|
|
3
|
+
/** What a path says it takes; a route that declared its own parameters keeps them. */
|
|
4
|
+
function paramsOf(path) {
|
|
5
|
+
const params = {};
|
|
6
|
+
for (const match of path.matchAll(PARAM)) {
|
|
7
|
+
const name = match[1];
|
|
8
|
+
if (name !== undefined) {
|
|
9
|
+
params[name] = "string";
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
return Object.keys(params).length === 0 ? undefined : params;
|
|
13
|
+
}
|
|
14
|
+
/** `/documents/:id` and `{ id: "7" }` make `/documents/7`; React Router navigates to a built path. */
|
|
15
|
+
function pathOf(to, params) {
|
|
16
|
+
return to.replace(PARAM, (whole, name) => {
|
|
17
|
+
const value = params[name];
|
|
18
|
+
return value === undefined ? whole : encodeURIComponent(String(value));
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* React Router as a `navigateTool` spec. Neither the `<Routes>` style nor the file-based one
|
|
23
|
+
* keeps a route registry to read at runtime, so the routes are the ones the app names here.
|
|
24
|
+
*/
|
|
25
|
+
export function fromReactRouter(source) {
|
|
26
|
+
for (const route of source.routes) {
|
|
27
|
+
if (route.to.includes("*")) {
|
|
28
|
+
throw new TypeError(`fromReactRouter: ${route.to} is a splat, which is not a destination a model can name`);
|
|
29
|
+
}
|
|
30
|
+
if (route.to.includes("?")) {
|
|
31
|
+
throw new TypeError(`fromReactRouter: ${route.to} has an optional segment, which is not a destination this binding builds; list the concrete variants instead`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const routes = source.routes.map((route) => {
|
|
35
|
+
if (route.params !== undefined) {
|
|
36
|
+
return route;
|
|
37
|
+
}
|
|
38
|
+
const params = paramsOf(route.to);
|
|
39
|
+
return params === undefined ? route : { ...route, params };
|
|
40
|
+
});
|
|
41
|
+
// Where the last navigation went, so the answer is a path even when the app reads nothing back.
|
|
42
|
+
let went = "";
|
|
43
|
+
return {
|
|
44
|
+
routes,
|
|
45
|
+
navigate: async (to, params) => {
|
|
46
|
+
went = pathOf(to, params ?? {});
|
|
47
|
+
await source.navigate(went);
|
|
48
|
+
},
|
|
49
|
+
read: () => ({ location: source.location?.() ?? went }),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/** A child's path is relative unless it opens with a slash, which is React Router's own rule. */
|
|
53
|
+
const join = (base, path) => path.startsWith("/") ? path : `${base.endsWith("/") ? base.slice(0, -1) : base}/${path}`;
|
|
54
|
+
/**
|
|
55
|
+
* A `createBrowserRouter` route tree, flattened into the specs `navigateTool` takes. A pathless
|
|
56
|
+
* layout is not a destination, and neither is a splat or an optional segment, so all three are
|
|
57
|
+
* dropped and what is under them is kept. An `index: true` route has no `path` of its own
|
|
58
|
+
* either, and needs none: the parent's path already reaches it.
|
|
59
|
+
*/
|
|
60
|
+
export function routesOf(routes) {
|
|
61
|
+
const found = new Map();
|
|
62
|
+
const walk = (list, base) => {
|
|
63
|
+
for (const route of list) {
|
|
64
|
+
const here = route.path === undefined ? base : join(base, route.path);
|
|
65
|
+
if (route.path !== undefined && !here.includes("*") && !here.includes("?")) {
|
|
66
|
+
const params = paramsOf(here);
|
|
67
|
+
found.set(here, params === undefined ? { to: here } : { to: here, params });
|
|
68
|
+
}
|
|
69
|
+
if (route.children !== undefined) {
|
|
70
|
+
walk(route.children, here);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
walk(routes, "");
|
|
75
|
+
return [...found.values()].sort((left, right) => left.to.localeCompare(right.to));
|
|
76
|
+
}
|
|
77
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,uFAAuF;AACvF,MAAM,KAAK,GAAG,mBAAmB,CAAC;AAElC,sFAAsF;AACtF,SAAS,QAAQ,CAAC,IAAY;IAC7B,MAAM,MAAM,GAAmC,EAAE,CAAC;IAClD,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QAC1C,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;QACtB,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACxB,MAAM,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC;QACzB,CAAC;IACF,CAAC;IACD,OAAO,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC;AAC9D,CAAC;AAED,sGAAsG;AACtG,SAAS,MAAM,CAAC,EAAU,EAAE,MAAuC;IAClE,OAAO,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC,KAAK,EAAE,IAAY,EAAE,EAAE;QAChD,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;QAC3B,OAAO,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,kBAAkB,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IACxE,CAAC,CAAC,CAAC;AACJ,CAAC;AAwBD;;;GAGG;AACH,MAAM,UAAU,eAAe,CAAC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AAC,MAAM,EAAE,EAAE,CAAC,CAAC;IACjB,OAAO,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC,aAAa,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC;AACnF,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@subako-ai/react-router",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "React Router as a navigate tool for Subako sessions",
|
|
5
|
+
"homepage": "https://github.com/Kikuvi-Inc/subako-sdk-typescript/tree/main/packages/react-router#readme",
|
|
6
|
+
"bugs": {
|
|
7
|
+
"url": "https://github.com/Kikuvi-Inc/subako-sdk-typescript/issues"
|
|
8
|
+
},
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"author": "Kikuvi, Inc.",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/Kikuvi-Inc/subako-sdk-typescript.git",
|
|
14
|
+
"directory": "packages/react-router"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"src"
|
|
19
|
+
],
|
|
20
|
+
"type": "module",
|
|
21
|
+
"sideEffects": false,
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"default": "./dist/index.js"
|
|
26
|
+
},
|
|
27
|
+
"./package.json": "./package.json"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"react-router": "^7.12.0",
|
|
34
|
+
"typescript": "^5.9.3",
|
|
35
|
+
"@subako-ai/sdk": "0.1.0",
|
|
36
|
+
"@subako-ai/tools": "0.1.0"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"react-router": ">=6.4.0",
|
|
40
|
+
"@subako-ai/tools": "^0.1.0"
|
|
41
|
+
},
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=22"
|
|
44
|
+
},
|
|
45
|
+
"scripts": {}
|
|
46
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type { NavigateSource, RouteParamType, RouteSpec } from "@subako-ai/tools";
|
|
2
|
+
|
|
3
|
+
/** React Router spells a path parameter `:name`, and every one of them is a string. */
|
|
4
|
+
const PARAM = /:([A-Za-z0-9_]+)/g;
|
|
5
|
+
|
|
6
|
+
/** What a path says it takes; a route that declared its own parameters keeps them. */
|
|
7
|
+
function paramsOf(path: string): Readonly<Record<string, RouteParamType>> | undefined {
|
|
8
|
+
const params: Record<string, RouteParamType> = {};
|
|
9
|
+
for (const match of path.matchAll(PARAM)) {
|
|
10
|
+
const name = match[1];
|
|
11
|
+
if (name !== undefined) {
|
|
12
|
+
params[name] = "string";
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return Object.keys(params).length === 0 ? undefined : params;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** `/documents/:id` and `{ id: "7" }` make `/documents/7`; React Router navigates to a built path. */
|
|
19
|
+
function pathOf(to: string, params: Record<string, string | number>): string {
|
|
20
|
+
return to.replace(PARAM, (whole, name: string) => {
|
|
21
|
+
const value = params[name];
|
|
22
|
+
return value === undefined ? whole : encodeURIComponent(String(value));
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The shape of a data router's route objects, as much of it as this needs. */
|
|
27
|
+
export interface RouteObjectLike {
|
|
28
|
+
readonly path?: string;
|
|
29
|
+
readonly index?: boolean;
|
|
30
|
+
readonly children?: readonly RouteObjectLike[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** What an app hands the binding: how to go, where it may go, and where it is. */
|
|
34
|
+
export interface ReactRouterSource {
|
|
35
|
+
/** What `useNavigate()` answers with. */
|
|
36
|
+
navigate(to: string): void | Promise<unknown>;
|
|
37
|
+
/** The routes the model may name, as the app spells them: `/documents/:id`. */
|
|
38
|
+
readonly routes: readonly RouteSpec[];
|
|
39
|
+
/**
|
|
40
|
+
* Where the app is now. It is called when the model asks, not when the tool is
|
|
41
|
+
* built, so it has to read the live value: `useLocation()` answers with a new
|
|
42
|
+
* object every render, and closing over one leaves the binding reporting the
|
|
43
|
+
* page the app was on when the tool was declared.
|
|
44
|
+
*/
|
|
45
|
+
location?(): string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* React Router as a `navigateTool` spec. Neither the `<Routes>` style nor the file-based one
|
|
50
|
+
* keeps a route registry to read at runtime, so the routes are the ones the app names here.
|
|
51
|
+
*/
|
|
52
|
+
export function fromReactRouter(source: ReactRouterSource): NavigateSource {
|
|
53
|
+
for (const route of source.routes) {
|
|
54
|
+
if (route.to.includes("*")) {
|
|
55
|
+
throw new TypeError(`fromReactRouter: ${route.to} is a splat, which is not a destination a model can name`);
|
|
56
|
+
}
|
|
57
|
+
if (route.to.includes("?")) {
|
|
58
|
+
throw new TypeError(
|
|
59
|
+
`fromReactRouter: ${route.to} has an optional segment, which is not a destination this binding builds; list the concrete variants instead`,
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const routes = source.routes.map((route) => {
|
|
64
|
+
if (route.params !== undefined) {
|
|
65
|
+
return route;
|
|
66
|
+
}
|
|
67
|
+
const params = paramsOf(route.to);
|
|
68
|
+
return params === undefined ? route : { ...route, params };
|
|
69
|
+
});
|
|
70
|
+
// Where the last navigation went, so the answer is a path even when the app reads nothing back.
|
|
71
|
+
let went = "";
|
|
72
|
+
return {
|
|
73
|
+
routes,
|
|
74
|
+
navigate: async (to, params) => {
|
|
75
|
+
went = pathOf(to, params ?? {});
|
|
76
|
+
await source.navigate(went);
|
|
77
|
+
},
|
|
78
|
+
read: () => ({ location: source.location?.() ?? went }),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** A child's path is relative unless it opens with a slash, which is React Router's own rule. */
|
|
83
|
+
const join = (base: string, path: string): string =>
|
|
84
|
+
path.startsWith("/") ? path : `${base.endsWith("/") ? base.slice(0, -1) : base}/${path}`;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* A `createBrowserRouter` route tree, flattened into the specs `navigateTool` takes. A pathless
|
|
88
|
+
* layout is not a destination, and neither is a splat or an optional segment, so all three are
|
|
89
|
+
* dropped and what is under them is kept. An `index: true` route has no `path` of its own
|
|
90
|
+
* either, and needs none: the parent's path already reaches it.
|
|
91
|
+
*/
|
|
92
|
+
export function routesOf(routes: readonly RouteObjectLike[]): RouteSpec[] {
|
|
93
|
+
const found = new Map<string, RouteSpec>();
|
|
94
|
+
const walk = (list: readonly RouteObjectLike[], base: string): void => {
|
|
95
|
+
for (const route of list) {
|
|
96
|
+
const here = route.path === undefined ? base : join(base, route.path);
|
|
97
|
+
if (route.path !== undefined && !here.includes("*") && !here.includes("?")) {
|
|
98
|
+
const params = paramsOf(here);
|
|
99
|
+
found.set(here, params === undefined ? { to: here } : { to: here, params });
|
|
100
|
+
}
|
|
101
|
+
if (route.children !== undefined) {
|
|
102
|
+
walk(route.children, here);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
walk(routes, "");
|
|
107
|
+
return [...found.values()].sort((left, right) => left.to.localeCompare(right.to));
|
|
108
|
+
}
|