@iyulab/router 0.7.6 → 0.9.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/CHANGELOG.md +27 -0
- package/README.md +79 -213
- package/dist/index.d.ts +75 -7
- package/dist/index.js +124 -63
- package/dist/react.d.ts +11 -3
- package/dist/react.js +1 -1
- package/dist/{share-sbAElOI7.js → share-CUGwxZKa.js} +12 -7
- package/package.json +1 -1
- package/skills/iyulab-router/SKILL.md +28 -6
- package/skills/iyulab-router/references/components.md +43 -0
- package/skills/iyulab-router/references/events-and-errors.md +39 -0
- package/skills/iyulab-router/references/guards-and-metadata.md +50 -0
- package/skills/iyulab-router/references/routing-basics.md +40 -0
- package/skills/iyulab-router/references/url-pattern.md +32 -0
- package/skills/iyulab-router/references/REFERENCE.md +0 -175
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# URL Pattern
|
|
2
|
+
|
|
3
|
+
## Supported Patterns
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
// Required
|
|
7
|
+
{ path: '/user/:id' }
|
|
8
|
+
|
|
9
|
+
// Optional
|
|
10
|
+
{ path: '/posts/:category?' }
|
|
11
|
+
|
|
12
|
+
// Wildcard
|
|
13
|
+
{ path: '/docs/:path*' }
|
|
14
|
+
|
|
15
|
+
// Multiple params
|
|
16
|
+
{ path: '/org/:orgId/repo/:repoId' }
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Access params via `ctx.params`.
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
ctx.params.id;
|
|
23
|
+
ctx.params.category;
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Query and Hash
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
ctx.query.get('q');
|
|
30
|
+
ctx.query.get('page');
|
|
31
|
+
ctx.hash;
|
|
32
|
+
```
|
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
# @iyulab/router — Reference
|
|
2
|
-
|
|
3
|
-
## URL Parameter Patterns
|
|
4
|
-
|
|
5
|
-
```ts
|
|
6
|
-
// Required
|
|
7
|
-
{ path: '/user/:id' }
|
|
8
|
-
|
|
9
|
-
// Optional
|
|
10
|
-
{ path: '/posts/:category?' }
|
|
11
|
-
|
|
12
|
-
// Wildcard (catch-all)
|
|
13
|
-
{ path: '/docs/:path*' }
|
|
14
|
-
|
|
15
|
-
// Multiple
|
|
16
|
-
{ path: '/org/:orgId/repo/:repoId' }
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
Access via `ctx.params.id`, `ctx.params.category`, etc.
|
|
20
|
-
|
|
21
|
-
## Query String
|
|
22
|
-
|
|
23
|
-
```ts
|
|
24
|
-
// URL: /search?q=hello&page=2
|
|
25
|
-
ctx.query.get('q') // 'hello'
|
|
26
|
-
ctx.query.get('page') // '2'
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
## Route Meta
|
|
30
|
-
|
|
31
|
-
Meta from the full matched chain is merged (parent → child, child overrides):
|
|
32
|
-
|
|
33
|
-
```ts
|
|
34
|
-
{
|
|
35
|
-
path: '/admin',
|
|
36
|
-
meta: { requiresAuth: true, layout: 'admin' },
|
|
37
|
-
render: (ctx) => {
|
|
38
|
-
// ctx.meta === { requiresAuth: true, layout: 'admin' }
|
|
39
|
-
return html`<admin-layout><u-outlet></u-outlet></admin-layout>`;
|
|
40
|
-
},
|
|
41
|
-
children: [
|
|
42
|
-
{
|
|
43
|
-
path: 'settings',
|
|
44
|
-
meta: { role: 'superadmin' },
|
|
45
|
-
render: (ctx) => {
|
|
46
|
-
// ctx.meta === { requiresAuth: true, layout: 'admin', role: 'superadmin' }
|
|
47
|
-
return html`<admin-settings></admin-settings>`;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
]
|
|
51
|
-
}
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
## Async Render with Progress
|
|
55
|
-
|
|
56
|
-
```ts
|
|
57
|
-
{
|
|
58
|
-
path: '/user/:id',
|
|
59
|
-
render: async (ctx) => {
|
|
60
|
-
ctx.progress(20);
|
|
61
|
-
const user = await fetchUser(ctx.params.id);
|
|
62
|
-
ctx.progress(80);
|
|
63
|
-
return html`<user-profile .data=${user}></user-profile>`;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
## Route Events
|
|
69
|
-
|
|
70
|
-
```ts
|
|
71
|
-
window.addEventListener('route-begin', (e) => {
|
|
72
|
-
console.log('navigating to:', e.context.pathname);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
window.addEventListener('route-progress', (e) => {
|
|
76
|
-
progressBar.value = e.progress; // 0–100
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
window.addEventListener('route-done', (e) => {
|
|
80
|
-
analytics.track(e.context.pathname);
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
window.addEventListener('route-error', (e) => {
|
|
84
|
-
errorTracker.report(e.error);
|
|
85
|
-
});
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
## Error Fallback
|
|
89
|
-
|
|
90
|
-
```ts
|
|
91
|
-
fallback: {
|
|
92
|
-
render: (ctx) => {
|
|
93
|
-
const { code, message } = ctx.error;
|
|
94
|
-
if (code === 'NOT_FOUND') return html`<not-found-page></not-found-page>`;
|
|
95
|
-
return html`<error-page .message=${message}></error-page>`;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
## React Usage
|
|
101
|
-
|
|
102
|
-
```tsx
|
|
103
|
-
import { UOutlet, ULink } from '@iyulab/router/react';
|
|
104
|
-
|
|
105
|
-
export function AppRoot() {
|
|
106
|
-
return (
|
|
107
|
-
<div>
|
|
108
|
-
<nav>
|
|
109
|
-
<ULink href="/">Home</ULink>
|
|
110
|
-
<ULink href="/about">About</ULink>
|
|
111
|
-
</nav>
|
|
112
|
-
<main>
|
|
113
|
-
<UOutlet />
|
|
114
|
-
</main>
|
|
115
|
-
</div>
|
|
116
|
-
);
|
|
117
|
-
}
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
Mixed Lit + React routes:
|
|
121
|
-
|
|
122
|
-
```ts
|
|
123
|
-
const routes = [
|
|
124
|
-
{
|
|
125
|
-
path: '/lit-page',
|
|
126
|
-
render: () => html`<my-lit-component></my-lit-component>`
|
|
127
|
-
},
|
|
128
|
-
{
|
|
129
|
-
path: '/react-page',
|
|
130
|
-
render: () => <MyReactComponent />
|
|
131
|
-
},
|
|
132
|
-
{
|
|
133
|
-
path: '/raw-element',
|
|
134
|
-
render: (ctx) => {
|
|
135
|
-
const el = document.createElement('my-element');
|
|
136
|
-
el.data = ctx.params;
|
|
137
|
-
return el;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
];
|
|
141
|
-
```
|
|
142
|
-
|
|
143
|
-
## RouterConfig Options
|
|
144
|
-
|
|
145
|
-
| Option | Default | Description |
|
|
146
|
-
|---|---|---|
|
|
147
|
-
| `root` | — | Mount element (required) |
|
|
148
|
-
| `basepath` | `'/'` | URL base path |
|
|
149
|
-
| `routes` | `[]` | Route definitions |
|
|
150
|
-
| `fallback` | built-in error page | Error/404 handler |
|
|
151
|
-
| `useIntercept` | `true` | Intercept `<a>` clicks for client routing |
|
|
152
|
-
| `initialLoad` | `true` | Auto-navigate to current URL on init |
|
|
153
|
-
|
|
154
|
-
## Lit Element Integration
|
|
155
|
-
|
|
156
|
-
```ts
|
|
157
|
-
import "@iyulab/router"; // registers <u-outlet> and <u-link>
|
|
158
|
-
import { LitElement, html } from 'lit';
|
|
159
|
-
import { customElement } from 'lit/decorators.js';
|
|
160
|
-
|
|
161
|
-
@customElement('app-root')
|
|
162
|
-
export class AppRoot extends LitElement {
|
|
163
|
-
render() {
|
|
164
|
-
return html`
|
|
165
|
-
<nav>
|
|
166
|
-
<u-link href="/">Home</u-link>
|
|
167
|
-
<u-link href="/about">About</u-link>
|
|
168
|
-
</nav>
|
|
169
|
-
<main>
|
|
170
|
-
<u-outlet></u-outlet>
|
|
171
|
-
</main>
|
|
172
|
-
`;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
```
|