intentx-react-router 1.0.4 → 1.1.0-z10
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/README.md +456 -28
- package/build/core/env.d.ts +1 -0
- package/build/core/index.d.ts +1 -0
- package/build/core/intentCore.d.ts +27 -16
- package/build/core/setupIntentRouter.d.ts +9 -0
- package/build/index.esm.js +1 -1
- package/build/index.js +1 -1
- package/build/router/IntentLink.d.ts +1 -1
- package/build/router/IntentRoute.d.ts +1 -1
- package/build/router/RouterBinder.d.ts +4 -3
- package/build/router/index.d.ts +0 -1
- package/package.json +8 -2
- package/build/router/IntentRouter.d.ts +0 -5
package/README.md
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
# 🕹️ intentx-react-router
|
|
2
2
|
|
|
3
|
-
[](https://www.npmjs.com/package/intentx-react-router) 
|
|
3
|
+
[](https://www.npmjs.com/package/intentx-react-router)  
|
|
4
4
|
|
|
5
5
|
<a href="https://codesandbox.io/p/sandbox/q8gg5s" target="_blank">LIVE EXAMPLE</a>
|
|
6
6
|
|
|
7
|
-
Intent
|
|
7
|
+
Intent-based navigation layer for React applications.
|
|
8
8
|
|
|
9
|
-
Built on top of
|
|
10
|
-
event‑driven architectures like **eventbus-z**.
|
|
9
|
+
Built on top of React Router and designed for **domain-driven and event-driven navigation**.
|
|
11
10
|
|
|
12
11
|
> Navigate by **intent**, not by URL.
|
|
13
12
|
|
|
@@ -23,6 +22,7 @@ navigate("/users/42/edit")
|
|
|
23
22
|
Intent Router:
|
|
24
23
|
```ts
|
|
25
24
|
navigateIntent("edit-user", 42)
|
|
25
|
+
// → /users/42/edit
|
|
26
26
|
```
|
|
27
27
|
|
|
28
28
|
<b>Benefits:</b>
|
|
@@ -32,7 +32,6 @@ navigateIntent("edit-user", 42)
|
|
|
32
32
|
- 🔧 Easy URL refactor
|
|
33
33
|
- 🧩 Micro‑frontend friendly
|
|
34
34
|
- 📡 Event‑driven navigation support
|
|
35
|
-
- 🪶 Lightweight core (\~200 lines)
|
|
36
35
|
|
|
37
36
|
---
|
|
38
37
|
|
|
@@ -51,15 +50,41 @@ npm install intentx-react-router react-router@^7 react-router-dom@^7
|
|
|
51
50
|
|
|
52
51
|
---
|
|
53
52
|
|
|
53
|
+
# Quick Start
|
|
54
|
+
|
|
55
|
+
```tsx
|
|
56
|
+
import { createIntentRouter, navigateIntent } from "intentx-react-router"
|
|
57
|
+
|
|
58
|
+
createIntentRouter({
|
|
59
|
+
"view-user": "/users/:id",
|
|
60
|
+
"edit-user": "/users/:id/edit"
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
// navigate by intent
|
|
64
|
+
navigateIntent("view-user", 42)
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Result:
|
|
68
|
+
```ts
|
|
69
|
+
/users/42
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
54
74
|
# Basic Setup
|
|
55
75
|
|
|
56
|
-
## 1
|
|
76
|
+
## 1️⃣ Define intents
|
|
57
77
|
|
|
58
78
|
``` ts
|
|
59
79
|
import { createIntentRouter, NavigateFn } from "intentx-react-router"
|
|
60
80
|
|
|
61
81
|
createIntentRouter({
|
|
62
|
-
"view-user":
|
|
82
|
+
"view-user": {
|
|
83
|
+
path: "/users/:userId/name/:page",
|
|
84
|
+
meta: {
|
|
85
|
+
title: "User Profile"
|
|
86
|
+
}
|
|
87
|
+
},
|
|
63
88
|
"edit-user": "/users/:userId/edit",
|
|
64
89
|
"checkout": "/checkout"
|
|
65
90
|
})
|
|
@@ -73,12 +98,12 @@ export function bindRouterNavigate(navigate: NavigateFn) {
|
|
|
73
98
|
|
|
74
99
|
---
|
|
75
100
|
|
|
76
|
-
## 2
|
|
101
|
+
## 2️⃣ Bind React Router
|
|
77
102
|
|
|
78
103
|
``` ts
|
|
79
104
|
import React from "react"
|
|
80
105
|
import { useNavigate } from "react-router-dom"
|
|
81
|
-
import {
|
|
106
|
+
import { bindRouterNavigate } from "./router"
|
|
82
107
|
|
|
83
108
|
export function RouterBinder() {
|
|
84
109
|
const navigate = useNavigate();
|
|
@@ -93,13 +118,17 @@ export function RouterBinder() {
|
|
|
93
118
|
```
|
|
94
119
|
---
|
|
95
120
|
|
|
96
|
-
## 3
|
|
121
|
+
## 3️⃣ Use in App
|
|
97
122
|
|
|
98
123
|
```ts
|
|
99
124
|
import React from "react"
|
|
100
125
|
import { BrowserRouter, Routes } from "react-router-dom"
|
|
101
126
|
import { IntentLink, IntentRoute } from "intentx-react-router"
|
|
127
|
+
|
|
102
128
|
import { RouterBinder } from "./router"
|
|
129
|
+
// or
|
|
130
|
+
// import "./router"
|
|
131
|
+
// import { RouterBinder } from "intentx-react-router"
|
|
103
132
|
|
|
104
133
|
import { UserPage } from "./pages/UserPage"
|
|
105
134
|
import { EditUserPage } from "./pages/EditUserPage"
|
|
@@ -125,6 +154,124 @@ export default function App() {
|
|
|
125
154
|
)
|
|
126
155
|
}
|
|
127
156
|
```
|
|
157
|
+
|
|
158
|
+
IntentRoute automatically maps the intent path to a React Router route.
|
|
159
|
+
|
|
160
|
+
---
|
|
161
|
+
|
|
162
|
+
# Modular Router Setup
|
|
163
|
+
## 1️⃣ Define intents
|
|
164
|
+
|
|
165
|
+
```ts
|
|
166
|
+
// intents/user-intents.ts
|
|
167
|
+
export const userIntents = {
|
|
168
|
+
home: "/",
|
|
169
|
+
userProfile: "/users/:id",
|
|
170
|
+
settings: "/settings"
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// intents/cart-intents.ts
|
|
174
|
+
export const cartIntents = {
|
|
175
|
+
cart: "/cart",
|
|
176
|
+
checkout: "/checkout"
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
---
|
|
181
|
+
|
|
182
|
+
## 2️⃣ Setup router
|
|
183
|
+
|
|
184
|
+
```ts
|
|
185
|
+
// router/setupRouter.ts
|
|
186
|
+
|
|
187
|
+
import { setupIntentRouter } from "intentx-react-router"
|
|
188
|
+
|
|
189
|
+
import { userIntents } from "../intents/user-intents"
|
|
190
|
+
import { cartIntents } from "../intents/cart-intents"
|
|
191
|
+
|
|
192
|
+
export function setupRouter(navigate) {
|
|
193
|
+
setupIntentRouter({
|
|
194
|
+
intents: [
|
|
195
|
+
userIntents,
|
|
196
|
+
cartIntents
|
|
197
|
+
],
|
|
198
|
+
|
|
199
|
+
guards: [
|
|
200
|
+
(intent, params) => {
|
|
201
|
+
console.log("Navigate:", intent, params)
|
|
202
|
+
}
|
|
203
|
+
],
|
|
204
|
+
|
|
205
|
+
intentGuards: {
|
|
206
|
+
checkout() {
|
|
207
|
+
const cartReady = true
|
|
208
|
+
|
|
209
|
+
if (!cartReady) {
|
|
210
|
+
return "/cart"
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
|
|
215
|
+
navigate
|
|
216
|
+
})
|
|
217
|
+
}
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
---
|
|
221
|
+
|
|
222
|
+
## 3️⃣ Bind router to React Router
|
|
223
|
+
|
|
224
|
+
```ts
|
|
225
|
+
// App.tsx
|
|
226
|
+
|
|
227
|
+
import { RouterBinder } from "intentx-react-router"
|
|
228
|
+
import { setupRouter } from "./router/setupRouter"
|
|
229
|
+
|
|
230
|
+
export default function App() {
|
|
231
|
+
return (
|
|
232
|
+
<>
|
|
233
|
+
<RouterBinder initializeRouter={setupRouter} />
|
|
234
|
+
|
|
235
|
+
{/* your routes */}
|
|
236
|
+
</>
|
|
237
|
+
)
|
|
238
|
+
}
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
---
|
|
242
|
+
|
|
243
|
+
## 4️⃣ Navigate using intent
|
|
244
|
+
|
|
245
|
+
```ts
|
|
246
|
+
import { navigateIntent } from "intentx-react-router"
|
|
247
|
+
|
|
248
|
+
navigateIntent("userProfile", { id: 42 })
|
|
249
|
+
|
|
250
|
+
navigateIntent("checkout")
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
Generated URL:
|
|
254
|
+
|
|
255
|
+
```ts
|
|
256
|
+
/users/42
|
|
257
|
+
/checkout
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
---
|
|
261
|
+
|
|
262
|
+
## 5️⃣ Object navigation
|
|
263
|
+
```ts
|
|
264
|
+
navigateIntentObject("userProfile", {
|
|
265
|
+
params: { id: 10 },
|
|
266
|
+
query: { tab: "posts" }
|
|
267
|
+
})
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
Result:
|
|
271
|
+
```ts
|
|
272
|
+
/users/10?tab=posts
|
|
273
|
+
```
|
|
274
|
+
|
|
128
275
|
---
|
|
129
276
|
|
|
130
277
|
# Navigation
|
|
@@ -336,7 +483,7 @@ import { IntentRoute } from "intentx-react-router"
|
|
|
336
483
|
|
|
337
484
|
# Guards
|
|
338
485
|
|
|
339
|
-
Global guard
|
|
486
|
+
## Global guard
|
|
340
487
|
|
|
341
488
|
```ts
|
|
342
489
|
addIntentGuard((intent, params) => {
|
|
@@ -348,7 +495,7 @@ addIntentGuard((intent, params) => {
|
|
|
348
495
|
})
|
|
349
496
|
```
|
|
350
497
|
|
|
351
|
-
Per
|
|
498
|
+
## Per-intent guard
|
|
352
499
|
|
|
353
500
|
```ts
|
|
354
501
|
addIntentGuardFor("checkout", () => {
|
|
@@ -362,6 +509,38 @@ addIntentGuardFor("checkout", () => {
|
|
|
362
509
|
|
|
363
510
|
---
|
|
364
511
|
|
|
512
|
+
## Guard inside intent
|
|
513
|
+
|
|
514
|
+
You can attach guards directly to intents.
|
|
515
|
+
|
|
516
|
+
```ts
|
|
517
|
+
createIntentRouter({
|
|
518
|
+
|
|
519
|
+
"checkout": {
|
|
520
|
+
path: "/checkout",
|
|
521
|
+
guard: () => {
|
|
522
|
+
if (!isLoggedIn()) {
|
|
523
|
+
return "/login"
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
},
|
|
527
|
+
|
|
528
|
+
"admin": {
|
|
529
|
+
path: "/admin",
|
|
530
|
+
guard: () => {
|
|
531
|
+
if (!isAdmin()) {
|
|
532
|
+
return "/forbidden"
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
})
|
|
538
|
+
```
|
|
539
|
+
|
|
540
|
+
This keeps route logic close to its definition.
|
|
541
|
+
|
|
542
|
+
---
|
|
543
|
+
|
|
365
544
|
# Preload
|
|
366
545
|
|
|
367
546
|
Preload logic before navigation.
|
|
@@ -465,17 +644,257 @@ This allows:
|
|
|
465
644
|
|
|
466
645
|
---
|
|
467
646
|
|
|
647
|
+
# Why not just use React Router?
|
|
648
|
+
|
|
649
|
+
:contentReference[oaicite:1]{index=1} is an excellent routing library and intentx-react-router **does not replace it**.
|
|
650
|
+
|
|
651
|
+
Instead, it adds a **navigation abstraction layer** on top of React Router.
|
|
652
|
+
|
|
653
|
+
React Router focuses on **URL-driven navigation**, while intentx-react-router focuses on **intent-driven navigation**.
|
|
654
|
+
|
|
655
|
+
---
|
|
656
|
+
|
|
657
|
+
## React Router navigation
|
|
658
|
+
|
|
659
|
+
In React Router, navigation is usually done by URLs:
|
|
660
|
+
|
|
661
|
+
```ts
|
|
662
|
+
navigate("/users/42/edit")
|
|
663
|
+
```
|
|
664
|
+
|
|
665
|
+
The application logic becomes tied to the URL structure.
|
|
666
|
+
|
|
667
|
+
If the URL changes later:
|
|
668
|
+
|
|
669
|
+
```text
|
|
670
|
+
/users/:id/edit
|
|
671
|
+
→
|
|
672
|
+
/profile/:id/edit
|
|
673
|
+
```
|
|
674
|
+
|
|
675
|
+
You must update navigation code across the app.
|
|
676
|
+
|
|
677
|
+
---
|
|
678
|
+
|
|
679
|
+
## Intent navigation
|
|
680
|
+
|
|
681
|
+
intentx-react-router introduces intent-based navigation.
|
|
682
|
+
|
|
683
|
+
Instead of navigating by URL:
|
|
684
|
+
|
|
685
|
+
```ts
|
|
686
|
+
navigateIntent("edit-user", 42)
|
|
687
|
+
```
|
|
688
|
+
|
|
689
|
+
The router resolves the intent into a URL:
|
|
690
|
+
|
|
691
|
+
```text
|
|
692
|
+
/edit-user → /users/42/edit
|
|
693
|
+
```
|
|
694
|
+
|
|
695
|
+
If the URL changes later:
|
|
696
|
+
|
|
697
|
+
```text
|
|
698
|
+
/users/:id/edit
|
|
699
|
+
→
|
|
700
|
+
/profile/:id/edit
|
|
701
|
+
```
|
|
702
|
+
|
|
703
|
+
You only update the router definition:
|
|
704
|
+
|
|
705
|
+
```ts
|
|
706
|
+
createIntentRouter({
|
|
707
|
+
"edit-user": "/profile/:id/edit"
|
|
708
|
+
})
|
|
709
|
+
```
|
|
710
|
+
|
|
711
|
+
---
|
|
712
|
+
|
|
468
713
|
# Comparison
|
|
469
714
|
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
|
477
|
-
|
|
478
|
-
|
|
|
715
|
+
intentx-react-router focuses on **intent-driven navigation** rather than URL-driven navigation.
|
|
716
|
+
|
|
717
|
+
Instead of navigating by path strings, you navigate by **domain intent**.
|
|
718
|
+
|
|
719
|
+
## vs React Router
|
|
720
|
+
|
|
721
|
+
| Criteria | intentx-react-router | React Router |
|
|
722
|
+
|-------------------------|----------------------|--------------|
|
|
723
|
+
| Intent-based navigation | ✅ | ❌ |
|
|
724
|
+
| URL refactor safety | ✅ | ⚠️ |
|
|
725
|
+
| Event-driven navigation | ✅ | ❌ |
|
|
726
|
+
| Micro-frontend friendly | ✅ | ⚠️ |
|
|
727
|
+
| Reverse routing | ✅ | ❌ |
|
|
728
|
+
| Type-safe intent params | ✅ | ⚠️ |
|
|
729
|
+
| Query support | ✅ | ⚠️ |
|
|
730
|
+
|
|
731
|
+
|
|
732
|
+
<br />
|
|
733
|
+
|
|
734
|
+
```ts
|
|
735
|
+
// React Router:
|
|
736
|
+
navigate("/users/42/edit")
|
|
737
|
+
|
|
738
|
+
// intentx-react-router:
|
|
739
|
+
navigateIntent("edit-user", 42)
|
|
740
|
+
```
|
|
741
|
+
|
|
742
|
+
---
|
|
743
|
+
|
|
744
|
+
## vs TanStack Router
|
|
745
|
+
|
|
746
|
+
:contentReference[oaicite:4]{index=4} is a powerful type-safe router with a full route tree and data loading system.
|
|
747
|
+
|
|
748
|
+
| Criteria | intentx-react-router | TanStack Router |
|
|
749
|
+
| ----------------------- | -------------------- | --------------- |
|
|
750
|
+
| Intent navigation | ✅ | ❌ |
|
|
751
|
+
| Route tree system | ❌ | ✅ |
|
|
752
|
+
| Data loaders | ❌ | ✅ |
|
|
753
|
+
| Reverse routing | ✅ | ⚠️ |
|
|
754
|
+
| Guards | ✅ | ⚠️ |
|
|
755
|
+
| Event-driven navigation | ✅ | ❌ |
|
|
756
|
+
| Micro-frontend friendly | ✅ | ⚠️ |
|
|
757
|
+
|
|
758
|
+
<br />
|
|
759
|
+
|
|
760
|
+
Intent Router is designed to be minimal and flexible, especially for event-driven or micro-frontend architectures.
|
|
761
|
+
|
|
762
|
+
---
|
|
763
|
+
|
|
764
|
+
## vs Remix Router
|
|
765
|
+
|
|
766
|
+
:contentReference[oaicite:5]{index=5} provides a full-stack routing system with loaders and server rendering.
|
|
767
|
+
|
|
768
|
+
| Criteria | intentx-react-router | Remix |
|
|
769
|
+
| ------------------------- | --------------------- | -----------|
|
|
770
|
+
| Intent navigation | ✅ | ❌ |
|
|
771
|
+
| Reverse routing | ✅ | ❌ |
|
|
772
|
+
| Guards | ✅ | ⚠️ loaders |
|
|
773
|
+
| Event-driven navigation | ✅ | ❌ |
|
|
774
|
+
| Server loaders | ❌ | ✅ |
|
|
775
|
+
| Full-stack routing | ❌ | ✅ |
|
|
776
|
+
| Lightweight client router | ✅ | ❌ |
|
|
777
|
+
|
|
778
|
+
|
|
779
|
+
<br />
|
|
780
|
+
|
|
781
|
+
Remix focuses on server-first routing, while intentx-react-router focuses on client-side intent navigation.
|
|
782
|
+
|
|
783
|
+
---
|
|
784
|
+
|
|
785
|
+
# Route Meta
|
|
786
|
+
|
|
787
|
+
You can attach metadata to intents.
|
|
788
|
+
|
|
789
|
+
This is useful for:
|
|
790
|
+
|
|
791
|
+
- auth
|
|
792
|
+
- role permission
|
|
793
|
+
- page title
|
|
794
|
+
- layout selection
|
|
795
|
+
- analytics
|
|
796
|
+
|
|
797
|
+
---
|
|
798
|
+
|
|
799
|
+
## Define meta
|
|
800
|
+
|
|
801
|
+
```ts
|
|
802
|
+
createIntentRouter({
|
|
803
|
+
|
|
804
|
+
"home": "/",
|
|
805
|
+
|
|
806
|
+
"dashboard": {
|
|
807
|
+
path: "/dashboard",
|
|
808
|
+
meta: {
|
|
809
|
+
title: "Dashboard",
|
|
810
|
+
auth: true
|
|
811
|
+
}
|
|
812
|
+
},
|
|
813
|
+
|
|
814
|
+
"admin": {
|
|
815
|
+
path: "/admin",
|
|
816
|
+
meta: {
|
|
817
|
+
title: "Admin Panel",
|
|
818
|
+
auth: true,
|
|
819
|
+
role: "admin"
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
})
|
|
824
|
+
```
|
|
825
|
+
|
|
826
|
+
---
|
|
827
|
+
|
|
828
|
+
## Read meta
|
|
829
|
+
|
|
830
|
+
```ts
|
|
831
|
+
import { getIntentMeta } from "intentx-react-router"
|
|
832
|
+
|
|
833
|
+
const meta = getIntentMeta("admin")
|
|
834
|
+
|
|
835
|
+
console.log(meta)
|
|
836
|
+
```
|
|
837
|
+
|
|
838
|
+
Result:
|
|
839
|
+
|
|
840
|
+
```ts
|
|
841
|
+
{
|
|
842
|
+
title: "Admin Panel",
|
|
843
|
+
auth: true,
|
|
844
|
+
role: "admin"
|
|
845
|
+
}
|
|
846
|
+
```
|
|
847
|
+
|
|
848
|
+
---
|
|
849
|
+
|
|
850
|
+
## Meta with guards
|
|
851
|
+
|
|
852
|
+
Meta works perfectly with guards.
|
|
853
|
+
|
|
854
|
+
```ts
|
|
855
|
+
addIntentGuard((intent) => {
|
|
856
|
+
|
|
857
|
+
const meta = getIntentMeta(intent)
|
|
858
|
+
|
|
859
|
+
if (meta?.auth && !isLoggedIn()) {
|
|
860
|
+
return "/login"
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
})
|
|
864
|
+
```
|
|
865
|
+
|
|
866
|
+
Example with role protection:
|
|
867
|
+
|
|
868
|
+
```ts
|
|
869
|
+
addIntentGuard((intent) => {
|
|
870
|
+
|
|
871
|
+
const meta = getIntentMeta(intent)
|
|
872
|
+
|
|
873
|
+
if (meta?.role === "admin" && !isAdmin()) {
|
|
874
|
+
return "/forbidden"
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
})
|
|
878
|
+
```
|
|
879
|
+
|
|
880
|
+
---
|
|
881
|
+
|
|
882
|
+
## Meta in React
|
|
883
|
+
|
|
884
|
+
You can also read meta inside components.
|
|
885
|
+
|
|
886
|
+
```ts
|
|
887
|
+
import { useIntentRouter, getIntentMeta } from "intentx-react-router"
|
|
888
|
+
|
|
889
|
+
function PageHeader() {
|
|
890
|
+
|
|
891
|
+
const { intent } = useIntentRouter()
|
|
892
|
+
|
|
893
|
+
const meta = getIntentMeta(intent)
|
|
894
|
+
|
|
895
|
+
return <h1>{meta?.title}</h1>
|
|
896
|
+
}
|
|
897
|
+
```
|
|
479
898
|
|
|
480
899
|
---
|
|
481
900
|
|
|
@@ -483,20 +902,29 @@ This allows:
|
|
|
483
902
|
|
|
484
903
|
```text
|
|
485
904
|
Component
|
|
486
|
-
|
|
905
|
+
↓
|
|
487
906
|
navigateIntent()
|
|
488
|
-
|
|
489
|
-
eventbus-z
|
|
490
|
-
↓
|
|
907
|
+
↓
|
|
491
908
|
Intent Router
|
|
492
|
-
|
|
909
|
+
↓
|
|
910
|
+
(eventbus-z optional)
|
|
911
|
+
↓
|
|
493
912
|
React Router navigate()
|
|
494
|
-
|
|
495
|
-
URL
|
|
913
|
+
↓
|
|
914
|
+
URL
|
|
496
915
|
```
|
|
497
916
|
|
|
498
917
|
---
|
|
499
918
|
|
|
919
|
+
# Debug Mode
|
|
920
|
+
|
|
921
|
+
Enable debug logs:
|
|
922
|
+
|
|
923
|
+
```js
|
|
924
|
+
window.__INTENT_ROUTER_DEBUG__ = true
|
|
925
|
+
|
|
926
|
+
---
|
|
927
|
+
|
|
500
928
|
# License
|
|
501
929
|
|
|
502
930
|
MIT
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const __DEV__: boolean;
|
package/build/core/index.d.ts
CHANGED
|
@@ -1,38 +1,49 @@
|
|
|
1
1
|
import type { GlobalBus } from "eventbus-z";
|
|
2
|
-
export type
|
|
3
|
-
params?:
|
|
2
|
+
export type IntentSchema = Record<string, {
|
|
3
|
+
params?: Record<string, any>;
|
|
4
4
|
query?: Record<string, any>;
|
|
5
|
-
|
|
6
|
-
replace?: boolean;
|
|
7
|
-
scrollTop?: boolean;
|
|
8
|
-
};
|
|
9
|
-
};
|
|
5
|
+
}>;
|
|
10
6
|
export type NavigateOptions = {
|
|
11
7
|
replace?: boolean;
|
|
12
8
|
scrollTop?: boolean;
|
|
13
9
|
[key: string]: any;
|
|
14
10
|
};
|
|
15
|
-
type
|
|
16
|
-
|
|
11
|
+
export type NavigateIntentOptions<P = any> = {
|
|
12
|
+
params?: P | any[] | string | number;
|
|
13
|
+
query?: Record<string, any>;
|
|
14
|
+
options?: NavigateOptions;
|
|
15
|
+
};
|
|
17
16
|
export type NavigateFn = (path: string, options?: NavigateOptions) => void;
|
|
17
|
+
type GuardFn = ((intent: string, params: any) => boolean | string | void | Promise<boolean | string | void>);
|
|
18
18
|
type PreloadFn = () => Promise<any> | void;
|
|
19
|
-
|
|
19
|
+
type IntentMeta = Record<string, any> | ((params: any) => Record<string, any>);
|
|
20
|
+
type IntentConfig = {
|
|
21
|
+
path: string;
|
|
22
|
+
meta?: IntentMeta;
|
|
23
|
+
guard?: GuardFn;
|
|
24
|
+
preload?: PreloadFn;
|
|
25
|
+
};
|
|
26
|
+
type IntentMapInput = Record<string, string | IntentConfig>;
|
|
27
|
+
export declare function registerIntents(map: IntentMapInput): void;
|
|
28
|
+
export declare function bootstrapRouter(): void;
|
|
29
|
+
export declare function createIntentRouter(map: IntentMapInput): void;
|
|
30
|
+
export declare function getIntentMeta(intent: string, params?: any): any;
|
|
31
|
+
export declare function setIntentMeta(intent: string, meta: IntentMeta): void;
|
|
20
32
|
export declare function bindNavigate(fn: NavigateFn): void;
|
|
21
33
|
export declare function setFallbackPath(path: string): void;
|
|
22
|
-
export declare function
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}): void;
|
|
26
|
-
export declare function navigateIntentObject<P = any>(intent: string, navigateIntentOption?: NavigateIntentOptions<P>): void;
|
|
34
|
+
export declare function warnRouterNotReady(intent: string): void;
|
|
35
|
+
export declare function navigateIntent(intent: string, params?: any, query?: Record<string, any>, options?: NavigateOptions): void;
|
|
36
|
+
export declare function navigateIntentObject<P = any>(intent: string, args?: NavigateIntentOptions<P>): void;
|
|
27
37
|
export declare function addIntentGuard(fn: GuardFn): void;
|
|
28
38
|
export declare function addIntentGuardFor(intent: string, fn: GuardFn): void;
|
|
29
39
|
export declare function addIntentPreload(intent: string, fn: PreloadFn): void;
|
|
30
40
|
export declare function preloadIntent(intent: string): void;
|
|
31
|
-
export declare function generatePathFromIntent(intent: string, params?:
|
|
41
|
+
export declare function generatePathFromIntent(intent: string, params?: any, query?: Record<string, any>): string;
|
|
32
42
|
export declare function resolveIntentFromUrl(url: string): {
|
|
33
43
|
intent: string;
|
|
34
44
|
params: Record<string, any>;
|
|
35
45
|
query: Record<string, any>;
|
|
36
46
|
} | null;
|
|
47
|
+
export declare function syncIntentFromUrl(url: string): void;
|
|
37
48
|
export declare function setSharedBus(externalBus: GlobalBus): void;
|
|
38
49
|
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
type GuardFn = (intent: string, params: any) => any;
|
|
2
|
+
type SetupOptions = {
|
|
3
|
+
intents?: Record<string, any>[];
|
|
4
|
+
guards?: GuardFn[];
|
|
5
|
+
intentGuards?: Record<string, GuardFn>;
|
|
6
|
+
navigate?: (path: string, options?: any) => void;
|
|
7
|
+
};
|
|
8
|
+
export declare function setupIntentRouter({ intents, guards, intentGuards, navigate }?: SetupOptions): void;
|
|
9
|
+
export {};
|
package/build/index.esm.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{createEventBus as n}from"eventbus-z";import{jsx as t,Fragment as
|
|
1
|
+
import{createEventBus as n}from"eventbus-z";import{jsx as t,Fragment as e}from"react/jsx-runtime";import{Link as o,useLocation as r,useNavigate as i}from"react-router-dom";import{useEffect as c,useRef as a}from"react";const u="undefined"!=typeof window&&!0===window.__INTENT_ROUTER_DEBUG__,s=n();let f=null,l=null;const p="INTENT_NAVIGATE_DELPIK";let d={},h={},m={},y={},w=!1;const g=[],R={},E={};let _=null;const I=[];let v=!1;function T(...n){u&&console.log("[IntentRouter]",...n)}function $(n){if(!n)return"";const t=new URLSearchParams;for(const e in n){const o=n[e];null!=o&&(Array.isArray(o)?o.forEach(n=>t.append(e,String(n))):t.append(e,String(o)))}const e=t.toString();return e?`?${e}`:""}function A(n){for(const t in n){u&&d[t]&&console.warn(`[IntentRouter] duplicate intent "${t}"`);const e=n[t];"string"!=typeof e?(d[t]=e.path,e.meta&&(h[t]=e.meta),e.guard&&(m[t]=e.guard),e.preload&&(y[t]=e.preload)):d[t]=e}}function k(n){I.push(n),v&&A(n)}function U(){if(v)return;v=!0;const n={};I.length||console.warn("[IntentRouter] no intents registered");for(const t of I)Object.assign(n,t);A(n),T("intent map",d)}function P(n){k(n),U()}function S(n,t){const e=h[n];return e?"function"==typeof e?e(t):e:{}}function b(n,t){h[n]=t}function q(n){l&&l!==n&&u&&console.warn("IntentRouter navigate replaced"),l=n,w||(s.$onMultiple(p,D),w=!0)}function z(n){_=n}function M(n){u&&(l||console.warn(`[IntentRouter] navigateIntent("${n}") called before router initialized.\nMake sure <RouterBinder /> is mounted.`))}function x(n,t,e,o){M(n);const r={intent:n,params:t,query:e,options:o};T("emit",r),s.$emit(p,r),f&&f!==s&&f.$emit(p,r)}function K(n,t){const{params:e,query:o,options:r}=t||{};x(n,e,o,r)}function N(n){g.push(n)}function j(n,t){R[n]||(R[n]=[]),R[n].push(t)}function C(n,t){E[n]||(E[n]=[]),E[n].push(t)}function L(n){G(n)}async function D(n){if(n.__handled)return;n.__handled=!0;const t=d[n.intent];if(!t)return console.warn("Unknown intent:",n.intent),void(_&&l?.(_));const e=Z(t,n.params),o=S(n.intent,e);for(const t of g){const o=await t(n.intent,e);if(!1===o)return;if("string"==typeof o)return void l?.(o)}const r=R[n.intent]||[];for(const t of r){const o=await t(n.intent,e);if(!1===o)return;if("string"==typeof o)return void l?.(o)}const i=m[n.intent];if(i){const t=await i(n.intent,e);if(!1===t)return;if("string"==typeof t)return void l?.(t)}try{const t=y[n.intent]?.();t instanceof Promise&&t.catch(()=>console.warn(`Preload failed for ${n.intent}`))}catch{}G(n.intent);const c=B(t,e)+$(n.query);T("navigate",{intent:n.intent,params:e,meta:o,url:c});const{scrollTop:a,...u}=n.options||{};("undefined"!=typeof window?window.location.pathname+window.location.search:null)!==c||u?.replace?(l?.(c,u),a&&"undefined"!=typeof window&&window.scrollTo(0,0)):T("skip navigation",c)}function G(n){const t=E[n];if(t)for(const e of t)try{const t=e();t instanceof Promise&&t.catch(()=>console.warn(`Preload failed for ${n}`))}catch{}}const O={};function Z(n,t){const e=function(n){if(O[n])return O[n];const t=[];return n.replace(/:([A-Za-z0-9_]+)/g,(n,e)=>(t.push(e),"")),O[n]=t,t}(n);if(!t)return{};if(Array.isArray(t)){const n={};return e.forEach((e,o)=>{n[e]=t[o]}),n}return"object"==typeof t?t:1===e.length?{[e[0]]:t}:{}}function B(n,t){return n.replace(/:([A-Za-z0-9_]+)/g,(n,e)=>{const o=t[e];return void 0===o?(console.warn(`Missing param "${e}"`),""):encodeURIComponent(o)})}function V(n,t,e){const o=d[n];if(!o)return"";return B(o,Z(o,t))+$(e)}const F={};function H(n){if(F[n])return F[n];const t=[],e=new RegExp("^"+n.replace(/:([A-Za-z0-9_]+)/g,(n,e)=>(t.push(e),"([^/]+)"))+"$");return F[n]={regex:e,keys:t},F[n]}function J(n){const[t,e]=n.split("?"),o=decodeURI(t),r=function(n){if(!n)return{};const t=new URLSearchParams(n),e={};return t.forEach((n,t)=>{e[t]?Array.isArray(e[t])?e[t].push(n):e[t]=[e[t],n]:e[t]=n}),e}(e);for(const n in d){const t=d[n],{regex:e,keys:i}=H(t),c=o.match(e);if(!c)continue;const a={};return i.forEach((n,t)=>{a[n]=decodeURIComponent(c[t+1])}),{intent:n,params:a,query:r}}return null}function Q(n){const t=J(n);t&&x(t.intent,t.params,t.query)}function W(n){f=n,f!==s&&f.$onMultiple(p,D)}let X=!1;function Y({intents:n=[],guards:t=[],intentGuards:e={},navigate:o}={}){if(X)return;X=!0;const r={};for(const t of n)for(const n in t)u&&r[n]&&console.warn(`[IntentRouter] duplicate intent "${n}"`),r[n]=t[n];Object.keys(r).length&&P(r),t.forEach(N);for(const n in e)j(n,e[n]);o&&q(o)}function nn({intent:n,params:r,query:i,scrollTop:c,prefetch:a,onClick:u,onMouseEnter:s,onTouchStart:f,...l}){const p=V(n,r,i);return p?t(o,{to:p,onClick:t=>{t.metaKey||t.ctrlKey||t.shiftKey||t.altKey||0!==t.button||(t.preventDefault(),x(n,r,i,{scrollTop:c}),u?.(t))},onMouseEnter:t=>{a&&L(n),s?.(t)},onTouchStart:t=>{a&&L(n),f?.(t)},...l}):(M(n),t(e,{children:l.children}))}function tn(){const n=r(),t=J(n.pathname),e=new URLSearchParams(n.search),o={};return e.forEach((n,t)=>{o[t]=n}),{intent:t?.intent??null,params:t?.params??{},query:o}}function en({intent:n,component:o,fallback:r=null,loading:i,guard:c}){const a=tn();return a?a.intent!==n?null:c&&!c(a)?t(e,{children:r}):t(o,{...a}):i?t(e,{children:i}):null}function on(n){const t=i();return c(()=>{q(n??t)},[n,t]),x}function rn({initializeRouter:n}){const t=i(),e=r(),o=a(!1);return on(),c(()=>{n&&(o.current||(o.current=!0,n(t)))},[t,n]),c(()=>{Q(e.pathname+e.search)},[e.pathname,e.search]),null}export{nn as IntentLink,en as IntentRoute,rn as RouterBinder,N as addIntentGuard,j as addIntentGuardFor,C as addIntentPreload,q as bindNavigate,U as bootstrapRouter,P as createIntentRouter,V as generatePathFromIntent,S as getIntentMeta,x as navigateIntent,K as navigateIntentObject,L as preloadIntent,k as registerIntents,J as resolveIntentFromUrl,z as setFallbackPath,b as setIntentMeta,W as setSharedBus,Y as setupIntentRouter,Q as syncIntentFromUrl,tn as useIntentRouter,on as useNavigateIntent,M as warnRouterNotReady};
|
package/build/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var n=require("eventbus-z"),t=require("react/jsx-runtime"),e=require("react-router-dom"),
|
|
1
|
+
"use strict";var n=require("eventbus-z"),t=require("react/jsx-runtime"),e=require("react-router-dom"),o=require("react");const r="undefined"!=typeof window&&!0===window.__INTENT_ROUTER_DEBUG__,i=n.createEventBus();let s=null,a=null;const c="INTENT_NAVIGATE_DELPIK";let u={},f={},l={},p={},d=!1;const h=[],g={},m={};let y=null;const x=[];let w=!1;function I(...n){r&&console.log("[IntentRouter]",...n)}function R(n){if(!n)return"";const t=new URLSearchParams;for(const e in n){const o=n[e];null!=o&&(Array.isArray(o)?o.forEach(n=>t.append(e,String(n))):t.append(e,String(o)))}const e=t.toString();return e?`?${e}`:""}function v(n){for(const t in n){r&&u[t]&&console.warn(`[IntentRouter] duplicate intent "${t}"`);const e=n[t];"string"!=typeof e?(u[t]=e.path,e.meta&&(f[t]=e.meta),e.guard&&(l[t]=e.guard),e.preload&&(p[t]=e.preload)):u[t]=e}}function E(n){x.push(n),w&&v(n)}function _(){if(w)return;w=!0;const n={};x.length||console.warn("[IntentRouter] no intents registered");for(const t of x)Object.assign(n,t);v(n),I("intent map",u)}function k(n){E(n),_()}function T(n,t){const e=f[n];return e?"function"==typeof e?e(t):e:{}}function $(n){a&&a!==n&&r&&console.warn("IntentRouter navigate replaced"),a=n,d||(i.$onMultiple(c,j),d=!0)}function b(n){r&&(a||console.warn(`[IntentRouter] navigateIntent("${n}") called before router initialized.\nMake sure <RouterBinder /> is mounted.`))}function q(n,t,e,o){b(n);const r={intent:n,params:t,query:e,options:o};I("emit",r),i.$emit(c,r),s&&s!==i&&s.$emit(c,r)}function A(n){h.push(n)}function P(n,t){g[n]||(g[n]=[]),g[n].push(t)}function U(n){N(n)}async function j(n){if(n.__handled)return;n.__handled=!0;const t=u[n.intent];if(!t)return console.warn("Unknown intent:",n.intent),void(y&&a?.(y));const e=F(t,n.params),o=T(n.intent,e);for(const t of h){const o=await t(n.intent,e);if(!1===o)return;if("string"==typeof o)return void a?.(o)}const r=g[n.intent]||[];for(const t of r){const o=await t(n.intent,e);if(!1===o)return;if("string"==typeof o)return void a?.(o)}const i=l[n.intent];if(i){const t=await i(n.intent,e);if(!1===t)return;if("string"==typeof t)return void a?.(t)}try{const t=p[n.intent]?.();t instanceof Promise&&t.catch(()=>console.warn(`Preload failed for ${n.intent}`))}catch{}N(n.intent);const s=L(t,e)+R(n.query);I("navigate",{intent:n.intent,params:e,meta:o,url:s});const{scrollTop:c,...f}=n.options||{};("undefined"!=typeof window?window.location.pathname+window.location.search:null)!==s||f?.replace?(a?.(s,f),c&&"undefined"!=typeof window&&window.scrollTo(0,0)):I("skip navigation",s)}function N(n){const t=m[n];if(t)for(const e of t)try{const t=e();t instanceof Promise&&t.catch(()=>console.warn(`Preload failed for ${n}`))}catch{}}const S={};function F(n,t){const e=function(n){if(S[n])return S[n];const t=[];return n.replace(/:([A-Za-z0-9_]+)/g,(n,e)=>(t.push(e),"")),S[n]=t,t}(n);if(!t)return{};if(Array.isArray(t)){const n={};return e.forEach((e,o)=>{n[e]=t[o]}),n}return"object"==typeof t?t:1===e.length?{[e[0]]:t}:{}}function L(n,t){return n.replace(/:([A-Za-z0-9_]+)/g,(n,e)=>{const o=t[e];return void 0===o?(console.warn(`Missing param "${e}"`),""):encodeURIComponent(o)})}function M(n,t,e){const o=u[n];if(!o)return"";return L(o,F(o,t))+R(e)}const z={};function B(n){if(z[n])return z[n];const t=[],e=new RegExp("^"+n.replace(/:([A-Za-z0-9_]+)/g,(n,e)=>(t.push(e),"([^/]+)"))+"$");return z[n]={regex:e,keys:t},z[n]}function G(n){const[t,e]=n.split("?"),o=decodeURI(t),r=function(n){if(!n)return{};const t=new URLSearchParams(n),e={};return t.forEach((n,t)=>{e[t]?Array.isArray(e[t])?e[t].push(n):e[t]=[e[t],n]:e[t]=n}),e}(e);for(const n in u){const t=u[n],{regex:e,keys:i}=B(t),s=o.match(e);if(!s)continue;const a={};return i.forEach((n,t)=>{a[n]=decodeURIComponent(s[t+1])}),{intent:n,params:a,query:r}}return null}function K(n){const t=G(n);t&&q(t.intent,t.params,t.query)}let C=!1;function O(){const n=e.useLocation(),t=G(n.pathname),o=new URLSearchParams(n.search),r={};return o.forEach((n,t)=>{r[t]=n}),{intent:t?.intent??null,params:t?.params??{},query:r}}function D(n){const t=e.useNavigate();return o.useEffect(()=>{$(n??t)},[n,t]),q}exports.IntentLink=function({intent:n,params:o,query:r,scrollTop:i,prefetch:s,onClick:a,onMouseEnter:c,onTouchStart:u,...f}){const l=M(n,o,r);return l?t.jsx(e.Link,{to:l,onClick:t=>{t.metaKey||t.ctrlKey||t.shiftKey||t.altKey||0!==t.button||(t.preventDefault(),q(n,o,r,{scrollTop:i}),a?.(t))},onMouseEnter:t=>{s&&U(n),c?.(t)},onTouchStart:t=>{s&&U(n),u?.(t)},...f}):(b(n),t.jsx(t.Fragment,{children:f.children}))},exports.IntentRoute=function({intent:n,component:e,fallback:o=null,loading:r,guard:i}){const s=O();return s?s.intent!==n?null:i&&!i(s)?t.jsx(t.Fragment,{children:o}):t.jsx(e,{...s}):r?t.jsx(t.Fragment,{children:r}):null},exports.RouterBinder=function({initializeRouter:n}){const t=e.useNavigate(),r=e.useLocation(),i=o.useRef(!1);return D(),o.useEffect(()=>{n&&(i.current||(i.current=!0,n(t)))},[t,n]),o.useEffect(()=>{K(r.pathname+r.search)},[r.pathname,r.search]),null},exports.addIntentGuard=A,exports.addIntentGuardFor=P,exports.addIntentPreload=function(n,t){m[n]||(m[n]=[]),m[n].push(t)},exports.bindNavigate=$,exports.bootstrapRouter=_,exports.createIntentRouter=k,exports.generatePathFromIntent=M,exports.getIntentMeta=T,exports.navigateIntent=q,exports.navigateIntentObject=function(n,t){const{params:e,query:o,options:r}=t||{};q(n,e,o,r)},exports.preloadIntent=U,exports.registerIntents=E,exports.resolveIntentFromUrl=G,exports.setFallbackPath=function(n){y=n},exports.setIntentMeta=function(n,t){f[n]=t},exports.setSharedBus=function(n){s=n,s!==i&&s.$onMultiple(c,j)},exports.setupIntentRouter=function({intents:n=[],guards:t=[],intentGuards:e={},navigate:o}={}){if(C)return;C=!0;const i={};for(const t of n)for(const n in t)r&&i[n]&&console.warn(`[IntentRouter] duplicate intent "${n}"`),i[n]=t[n];Object.keys(i).length&&k(i),t.forEach(A);for(const n in e)P(n,e[n]);o&&$(o)},exports.syncIntentFromUrl=K,exports.useIntentRouter=O,exports.useNavigateIntent=D,exports.warnRouterNotReady=b;
|
|
@@ -5,6 +5,6 @@ type Props = {
|
|
|
5
5
|
query?: Record<string, any>;
|
|
6
6
|
scrollTop?: boolean;
|
|
7
7
|
prefetch?: boolean;
|
|
8
|
-
} & Omit<LinkProps, "to"
|
|
8
|
+
} & Omit<LinkProps, "to">;
|
|
9
9
|
export declare function IntentLink({ intent, params, query, scrollTop, prefetch, onClick, onMouseEnter, onTouchStart, ...props }: Props): JSX.Element;
|
|
10
10
|
export {};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
type
|
|
2
|
-
|
|
1
|
+
import type { NavigateFn } from "../core/intentCore";
|
|
2
|
+
type RouterBinderProps = {
|
|
3
|
+
initializeRouter?: (navigate: NavigateFn) => void;
|
|
3
4
|
};
|
|
4
|
-
export declare function RouterBinder({
|
|
5
|
+
export declare function RouterBinder({ initializeRouter }: RouterBinderProps): null;
|
|
5
6
|
export {};
|
package/build/router/index.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "intentx-react-router",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0-z10",
|
|
4
4
|
"description": "Intent-based routing for React. Navigate by intent instead of URLs.",
|
|
5
5
|
"author": "Delpi.Kye",
|
|
6
6
|
"license": "MIT",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
|
|
7
9
|
"type": "module",
|
|
8
10
|
"main": "./build/index.js",
|
|
9
11
|
"module": "./build/index.esm.js",
|
|
10
12
|
"types": "./build/index.d.ts",
|
|
13
|
+
|
|
11
14
|
"exports": {
|
|
12
15
|
".": {
|
|
13
16
|
"types": "./build/index.d.ts",
|
|
@@ -18,13 +21,14 @@
|
|
|
18
21
|
"files": [
|
|
19
22
|
"build"
|
|
20
23
|
],
|
|
21
|
-
|
|
24
|
+
|
|
22
25
|
"scripts": {
|
|
23
26
|
"clean": "rimraf build",
|
|
24
27
|
"build": "rimraf build && rollup -c",
|
|
25
28
|
"watch": "rollup -c -w",
|
|
26
29
|
"typecheck": "tsc --noEmit"
|
|
27
30
|
},
|
|
31
|
+
|
|
28
32
|
"engines": {
|
|
29
33
|
"node": ">=16"
|
|
30
34
|
},
|
|
@@ -36,6 +40,7 @@
|
|
|
36
40
|
"bugs": {
|
|
37
41
|
"url": "https://github.com/delpikye-v/intentx-react-router/issues"
|
|
38
42
|
},
|
|
43
|
+
|
|
39
44
|
"keywords": [
|
|
40
45
|
"react",
|
|
41
46
|
"react-router",
|
|
@@ -48,6 +53,7 @@
|
|
|
48
53
|
"spa",
|
|
49
54
|
"routing"
|
|
50
55
|
],
|
|
56
|
+
|
|
51
57
|
"peerDependencies": {
|
|
52
58
|
"react": ">=18",
|
|
53
59
|
"react-dom": ">=18",
|