@vobs/router 1.1.0 → 1.2.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.
@@ -0,0 +1,215 @@
1
+ import { Signal } from '@vobs/reactivity';
2
+ import { VobsNode, InjectionKey, VobsPlugin } from '@vobs/vobs';
3
+ export { RouterDebugEvent, RouterDebugEventType, createRouterDebugId, emitRouterDebug, subscribeRouterDebug } from './debug.cjs';
4
+ import '@vobs/runtime';
5
+
6
+ type RouteParams = Readonly<Record<string, string>>;
7
+ type RouteQueryValue = string | readonly string[];
8
+ type RouteQuery = Readonly<Record<string, RouteQueryValue>>;
9
+ type RouteMeta = Readonly<Record<string, unknown>>;
10
+ interface RouteLocation {
11
+ readonly path: string;
12
+ readonly fullPath: string;
13
+ readonly params: RouteParams;
14
+ readonly query: RouteQuery;
15
+ readonly hash: string;
16
+ readonly name: string | undefined;
17
+ readonly meta: RouteMeta;
18
+ readonly record: RouteRecord | null;
19
+ readonly matched: readonly RouteRecord[];
20
+ readonly state?: unknown;
21
+ }
22
+ interface RouteComponentProps {
23
+ readonly route: RouteLocation;
24
+ readonly params: RouteParams;
25
+ readonly query: RouteQuery;
26
+ readonly children?: VobsNode;
27
+ }
28
+ type RouteComponent = (props: RouteComponentProps) => VobsNode;
29
+ type RouteComponentModule = RouteComponent | {
30
+ default: RouteComponent;
31
+ };
32
+ type RouteComponentLoader = () => PromiseLike<RouteComponentModule>;
33
+ interface LazyRouteComponent {
34
+ readonly kind: 'vobs-lazy-route';
35
+ readonly load: RouteComponentLoader;
36
+ }
37
+ type RouteComponentDefinition = RouteComponent | LazyRouteComponent;
38
+ interface RouteLoaderContext {
39
+ readonly route: RouteLocation;
40
+ readonly navigationId?: number;
41
+ readonly dataRequestId?: number;
42
+ }
43
+ type RouteLoader = (context: RouteLoaderContext) => unknown | PromiseLike<unknown>;
44
+ interface RouteRecord {
45
+ readonly path?: string;
46
+ readonly component?: RouteComponentDefinition;
47
+ readonly source?: string;
48
+ readonly name?: string;
49
+ readonly meta?: Record<string, unknown>;
50
+ readonly loader?: RouteLoader;
51
+ readonly action?: RouteLoader;
52
+ readonly children?: readonly RouteRecord[];
53
+ }
54
+ type RouteQueryInput = Record<string, unknown> | URLSearchParams;
55
+ interface RouteLocationRaw {
56
+ readonly path?: string;
57
+ readonly name?: string;
58
+ readonly params?: Record<string, unknown>;
59
+ readonly query?: RouteQueryInput;
60
+ readonly hash?: string;
61
+ readonly state?: unknown;
62
+ }
63
+ type RouteTarget = string | RouteLocationRaw;
64
+ type NavigationGuardResult = void | boolean | RouteTarget;
65
+ type NavigationGuard = (to: RouteLocation, from: RouteLocation) => NavigationGuardResult | PromiseLike<NavigationGuardResult>;
66
+ declare class NavigationCancelledError extends Error {
67
+ readonly code = "NAVIGATION_CANCELLED";
68
+ constructor();
69
+ }
70
+ declare class NavigationRedirectError extends Error {
71
+ readonly code = "NAVIGATION_REDIRECT_LIMIT";
72
+ constructor();
73
+ }
74
+ interface RouterHistory {
75
+ readonly location: string;
76
+ /** 当前 history 条目携带的导航 state(push/replace 时写入,popstate/初始启动时回读)。 */
77
+ readonly state?: unknown;
78
+ push(path: string, state?: unknown): void;
79
+ replace(path: string, state?: unknown): void;
80
+ back(): void;
81
+ listen(listener: (path: string, state: unknown) => void): () => void;
82
+ }
83
+ interface RouterOptions {
84
+ readonly routes: readonly RouteRecord[];
85
+ readonly history?: RouterHistory;
86
+ }
87
+ interface RouterViewState {
88
+ readonly status: 'ready' | 'loading' | 'error' | 'not-found';
89
+ readonly component?: RouteComponent;
90
+ readonly layouts?: readonly RouteComponent[];
91
+ readonly error?: Error;
92
+ readonly retry: () => void;
93
+ }
94
+ interface RouteDebugNode {
95
+ readonly id: string;
96
+ readonly path: string;
97
+ readonly name?: string;
98
+ readonly component: string;
99
+ readonly lazy: boolean;
100
+ readonly loader: boolean;
101
+ readonly action: boolean;
102
+ readonly status: 'ready' | 'loading' | 'error';
103
+ readonly meta: RouteMeta;
104
+ readonly source?: string;
105
+ readonly children: readonly RouteDebugNode[];
106
+ }
107
+ interface RouteErrorTrace {
108
+ readonly id: number;
109
+ readonly phase: 'navigation' | 'render' | 'lazy' | 'loader' | 'action' | 'fetcher';
110
+ readonly route: string;
111
+ readonly message: string;
112
+ readonly stack?: string;
113
+ readonly timestamp: number;
114
+ readonly requestId?: number;
115
+ readonly navigationId?: number;
116
+ }
117
+ interface NavigationTrace {
118
+ readonly id: number;
119
+ readonly from: string;
120
+ readonly to: string;
121
+ readonly status: 'success' | 'redirected' | 'cancelled' | 'error';
122
+ readonly source: 'push' | 'replace' | 'history';
123
+ readonly startedAt: number;
124
+ readonly endedAt: number;
125
+ readonly duration: number;
126
+ readonly redirect?: string;
127
+ readonly error?: string;
128
+ }
129
+ interface NavigationState {
130
+ readonly status: 'idle' | 'loading' | 'error';
131
+ readonly from: string;
132
+ readonly to: string;
133
+ readonly traceId?: number;
134
+ readonly error?: string;
135
+ }
136
+ interface RouterPerformanceMetrics {
137
+ readonly navigationCount: number;
138
+ readonly averageNavigationDuration: number;
139
+ readonly slowNavigationCount: number;
140
+ }
141
+ type RouterDataRequestKind = 'loader' | 'action' | 'fetcher';
142
+ interface RouterDataRequestTrace {
143
+ readonly id: number;
144
+ readonly kind: RouterDataRequestKind;
145
+ readonly key: string;
146
+ readonly route?: string;
147
+ readonly status: 'loading' | 'success' | 'error' | 'cancelled';
148
+ readonly startedAt: number;
149
+ readonly endedAt?: number;
150
+ readonly duration?: number;
151
+ readonly result?: unknown;
152
+ readonly error?: string;
153
+ readonly navigationId?: number;
154
+ readonly trigger?: 'navigation' | 'revalidate' | 'manual' | 'resource';
155
+ readonly environment?: 'client' | 'server';
156
+ }
157
+ interface RouterDataRequestOptions {
158
+ readonly route?: string;
159
+ readonly navigationId?: number;
160
+ readonly trigger?: 'navigation' | 'revalidate' | 'manual' | 'resource';
161
+ readonly environment?: 'client' | 'server';
162
+ }
163
+ type RouterDevToolsEvent = 'navigation:start' | 'navigation:end' | 'route:update' | 'data-request' | 'error';
164
+ interface RouterDevToolsAPI {
165
+ getRouteTree(): readonly RouteDebugNode[];
166
+ getCurrentRoute(): RouteLocation;
167
+ getNavigationState(): NavigationState;
168
+ getNavigationHistory(): readonly NavigationTrace[];
169
+ getPerformanceMetrics(): RouterPerformanceMetrics;
170
+ getDataRequests(): readonly RouterDataRequestTrace[];
171
+ getErrors(): readonly RouteErrorTrace[];
172
+ trackDataRequest<T>(kind: RouterDataRequestKind, key: string, task: () => T | PromiseLike<T>, options?: RouterDataRequestOptions | string): Promise<T>;
173
+ runAction<T>(key: string, task: () => T | PromiseLike<T>): Promise<T>;
174
+ runFetcher<T>(key: string, task: () => T | PromiseLike<T>): Promise<T>;
175
+ reportError(phase: RouteErrorTrace['phase'], error: unknown, route?: string, context?: {
176
+ readonly requestId?: number;
177
+ readonly navigationId?: number;
178
+ }): void;
179
+ revalidate(route?: string): Promise<void>;
180
+ subscribe(event: RouterDevToolsEvent, callback: (payload: unknown) => void): () => void;
181
+ }
182
+ interface Router {
183
+ readonly currentRoute: Signal<RouteLocation>;
184
+ readonly history: RouterHistory;
185
+ resolve(to: RouteTarget): RouteLocation;
186
+ push(to: RouteTarget): Promise<RouteLocation | false>;
187
+ replace(to: RouteTarget): Promise<RouteLocation | false>;
188
+ back(): void;
189
+ beforeEach(guard: NavigationGuard): () => void;
190
+ getViewState(route: RouteLocation): RouterViewState;
191
+ readonly devtools: RouterDevToolsAPI;
192
+ destroy(): void;
193
+ }
194
+ interface RouterViewProps {
195
+ readonly router?: Router;
196
+ readonly loading?: () => VobsNode | null | undefined;
197
+ readonly notFound?: (route: RouteLocation) => VobsNode | null | undefined;
198
+ readonly error?: (error: Error, retry: () => void) => VobsNode | null | undefined;
199
+ }
200
+ interface RouterPluginOptions {
201
+ readonly router?: Router;
202
+ readonly routes?: readonly RouteRecord[];
203
+ readonly history?: RouterHistory;
204
+ }
205
+ declare const ROUTER_KEY: InjectionKey<Router>;
206
+ declare function lazy(loader: RouteComponentLoader): LazyRouteComponent;
207
+ declare function createMemoryHistory(initial?: string): RouterHistory;
208
+ declare function createBrowserHistory(base?: string): RouterHistory;
209
+ declare function createRouter(options: RouterOptions): Router;
210
+ declare function RouterView(props?: RouterViewProps): VobsNode;
211
+ declare function useRouter(): Router;
212
+ declare function useRoute(): Signal<RouteLocation>;
213
+ declare function routerPlugin(options?: RouterPluginOptions): VobsPlugin;
214
+
215
+ export { type LazyRouteComponent, NavigationCancelledError, type NavigationGuard, type NavigationGuardResult, NavigationRedirectError, type NavigationState, type NavigationTrace, ROUTER_KEY, type RouteComponent, type RouteComponentDefinition, type RouteComponentLoader, type RouteComponentModule, type RouteComponentProps, type RouteDebugNode, type RouteErrorTrace, type RouteLoader, type RouteLoaderContext, type RouteLocation, type RouteLocationRaw, type RouteMeta, type RouteParams, type RouteQuery, type RouteQueryInput, type RouteQueryValue, type RouteRecord, type RouteTarget, type Router, type RouterDataRequestKind, type RouterDataRequestOptions, type RouterDataRequestTrace, type RouterDevToolsAPI, type RouterDevToolsEvent, type RouterHistory, type RouterOptions, type RouterPerformanceMetrics, type RouterPluginOptions, RouterView, type RouterViewProps, type RouterViewState, createBrowserHistory, createMemoryHistory, createRouter, lazy, routerPlugin, useRoute, useRouter };
@@ -0,0 +1,215 @@
1
+ import { Signal } from '@vobs/reactivity';
2
+ import { VobsNode, InjectionKey, VobsPlugin } from '@vobs/vobs';
3
+ export { RouterDebugEvent, RouterDebugEventType, createRouterDebugId, emitRouterDebug, subscribeRouterDebug } from './debug.js';
4
+ import '@vobs/runtime';
5
+
6
+ type RouteParams = Readonly<Record<string, string>>;
7
+ type RouteQueryValue = string | readonly string[];
8
+ type RouteQuery = Readonly<Record<string, RouteQueryValue>>;
9
+ type RouteMeta = Readonly<Record<string, unknown>>;
10
+ interface RouteLocation {
11
+ readonly path: string;
12
+ readonly fullPath: string;
13
+ readonly params: RouteParams;
14
+ readonly query: RouteQuery;
15
+ readonly hash: string;
16
+ readonly name: string | undefined;
17
+ readonly meta: RouteMeta;
18
+ readonly record: RouteRecord | null;
19
+ readonly matched: readonly RouteRecord[];
20
+ readonly state?: unknown;
21
+ }
22
+ interface RouteComponentProps {
23
+ readonly route: RouteLocation;
24
+ readonly params: RouteParams;
25
+ readonly query: RouteQuery;
26
+ readonly children?: VobsNode;
27
+ }
28
+ type RouteComponent = (props: RouteComponentProps) => VobsNode;
29
+ type RouteComponentModule = RouteComponent | {
30
+ default: RouteComponent;
31
+ };
32
+ type RouteComponentLoader = () => PromiseLike<RouteComponentModule>;
33
+ interface LazyRouteComponent {
34
+ readonly kind: 'vobs-lazy-route';
35
+ readonly load: RouteComponentLoader;
36
+ }
37
+ type RouteComponentDefinition = RouteComponent | LazyRouteComponent;
38
+ interface RouteLoaderContext {
39
+ readonly route: RouteLocation;
40
+ readonly navigationId?: number;
41
+ readonly dataRequestId?: number;
42
+ }
43
+ type RouteLoader = (context: RouteLoaderContext) => unknown | PromiseLike<unknown>;
44
+ interface RouteRecord {
45
+ readonly path?: string;
46
+ readonly component?: RouteComponentDefinition;
47
+ readonly source?: string;
48
+ readonly name?: string;
49
+ readonly meta?: Record<string, unknown>;
50
+ readonly loader?: RouteLoader;
51
+ readonly action?: RouteLoader;
52
+ readonly children?: readonly RouteRecord[];
53
+ }
54
+ type RouteQueryInput = Record<string, unknown> | URLSearchParams;
55
+ interface RouteLocationRaw {
56
+ readonly path?: string;
57
+ readonly name?: string;
58
+ readonly params?: Record<string, unknown>;
59
+ readonly query?: RouteQueryInput;
60
+ readonly hash?: string;
61
+ readonly state?: unknown;
62
+ }
63
+ type RouteTarget = string | RouteLocationRaw;
64
+ type NavigationGuardResult = void | boolean | RouteTarget;
65
+ type NavigationGuard = (to: RouteLocation, from: RouteLocation) => NavigationGuardResult | PromiseLike<NavigationGuardResult>;
66
+ declare class NavigationCancelledError extends Error {
67
+ readonly code = "NAVIGATION_CANCELLED";
68
+ constructor();
69
+ }
70
+ declare class NavigationRedirectError extends Error {
71
+ readonly code = "NAVIGATION_REDIRECT_LIMIT";
72
+ constructor();
73
+ }
74
+ interface RouterHistory {
75
+ readonly location: string;
76
+ /** 当前 history 条目携带的导航 state(push/replace 时写入,popstate/初始启动时回读)。 */
77
+ readonly state?: unknown;
78
+ push(path: string, state?: unknown): void;
79
+ replace(path: string, state?: unknown): void;
80
+ back(): void;
81
+ listen(listener: (path: string, state: unknown) => void): () => void;
82
+ }
83
+ interface RouterOptions {
84
+ readonly routes: readonly RouteRecord[];
85
+ readonly history?: RouterHistory;
86
+ }
87
+ interface RouterViewState {
88
+ readonly status: 'ready' | 'loading' | 'error' | 'not-found';
89
+ readonly component?: RouteComponent;
90
+ readonly layouts?: readonly RouteComponent[];
91
+ readonly error?: Error;
92
+ readonly retry: () => void;
93
+ }
94
+ interface RouteDebugNode {
95
+ readonly id: string;
96
+ readonly path: string;
97
+ readonly name?: string;
98
+ readonly component: string;
99
+ readonly lazy: boolean;
100
+ readonly loader: boolean;
101
+ readonly action: boolean;
102
+ readonly status: 'ready' | 'loading' | 'error';
103
+ readonly meta: RouteMeta;
104
+ readonly source?: string;
105
+ readonly children: readonly RouteDebugNode[];
106
+ }
107
+ interface RouteErrorTrace {
108
+ readonly id: number;
109
+ readonly phase: 'navigation' | 'render' | 'lazy' | 'loader' | 'action' | 'fetcher';
110
+ readonly route: string;
111
+ readonly message: string;
112
+ readonly stack?: string;
113
+ readonly timestamp: number;
114
+ readonly requestId?: number;
115
+ readonly navigationId?: number;
116
+ }
117
+ interface NavigationTrace {
118
+ readonly id: number;
119
+ readonly from: string;
120
+ readonly to: string;
121
+ readonly status: 'success' | 'redirected' | 'cancelled' | 'error';
122
+ readonly source: 'push' | 'replace' | 'history';
123
+ readonly startedAt: number;
124
+ readonly endedAt: number;
125
+ readonly duration: number;
126
+ readonly redirect?: string;
127
+ readonly error?: string;
128
+ }
129
+ interface NavigationState {
130
+ readonly status: 'idle' | 'loading' | 'error';
131
+ readonly from: string;
132
+ readonly to: string;
133
+ readonly traceId?: number;
134
+ readonly error?: string;
135
+ }
136
+ interface RouterPerformanceMetrics {
137
+ readonly navigationCount: number;
138
+ readonly averageNavigationDuration: number;
139
+ readonly slowNavigationCount: number;
140
+ }
141
+ type RouterDataRequestKind = 'loader' | 'action' | 'fetcher';
142
+ interface RouterDataRequestTrace {
143
+ readonly id: number;
144
+ readonly kind: RouterDataRequestKind;
145
+ readonly key: string;
146
+ readonly route?: string;
147
+ readonly status: 'loading' | 'success' | 'error' | 'cancelled';
148
+ readonly startedAt: number;
149
+ readonly endedAt?: number;
150
+ readonly duration?: number;
151
+ readonly result?: unknown;
152
+ readonly error?: string;
153
+ readonly navigationId?: number;
154
+ readonly trigger?: 'navigation' | 'revalidate' | 'manual' | 'resource';
155
+ readonly environment?: 'client' | 'server';
156
+ }
157
+ interface RouterDataRequestOptions {
158
+ readonly route?: string;
159
+ readonly navigationId?: number;
160
+ readonly trigger?: 'navigation' | 'revalidate' | 'manual' | 'resource';
161
+ readonly environment?: 'client' | 'server';
162
+ }
163
+ type RouterDevToolsEvent = 'navigation:start' | 'navigation:end' | 'route:update' | 'data-request' | 'error';
164
+ interface RouterDevToolsAPI {
165
+ getRouteTree(): readonly RouteDebugNode[];
166
+ getCurrentRoute(): RouteLocation;
167
+ getNavigationState(): NavigationState;
168
+ getNavigationHistory(): readonly NavigationTrace[];
169
+ getPerformanceMetrics(): RouterPerformanceMetrics;
170
+ getDataRequests(): readonly RouterDataRequestTrace[];
171
+ getErrors(): readonly RouteErrorTrace[];
172
+ trackDataRequest<T>(kind: RouterDataRequestKind, key: string, task: () => T | PromiseLike<T>, options?: RouterDataRequestOptions | string): Promise<T>;
173
+ runAction<T>(key: string, task: () => T | PromiseLike<T>): Promise<T>;
174
+ runFetcher<T>(key: string, task: () => T | PromiseLike<T>): Promise<T>;
175
+ reportError(phase: RouteErrorTrace['phase'], error: unknown, route?: string, context?: {
176
+ readonly requestId?: number;
177
+ readonly navigationId?: number;
178
+ }): void;
179
+ revalidate(route?: string): Promise<void>;
180
+ subscribe(event: RouterDevToolsEvent, callback: (payload: unknown) => void): () => void;
181
+ }
182
+ interface Router {
183
+ readonly currentRoute: Signal<RouteLocation>;
184
+ readonly history: RouterHistory;
185
+ resolve(to: RouteTarget): RouteLocation;
186
+ push(to: RouteTarget): Promise<RouteLocation | false>;
187
+ replace(to: RouteTarget): Promise<RouteLocation | false>;
188
+ back(): void;
189
+ beforeEach(guard: NavigationGuard): () => void;
190
+ getViewState(route: RouteLocation): RouterViewState;
191
+ readonly devtools: RouterDevToolsAPI;
192
+ destroy(): void;
193
+ }
194
+ interface RouterViewProps {
195
+ readonly router?: Router;
196
+ readonly loading?: () => VobsNode | null | undefined;
197
+ readonly notFound?: (route: RouteLocation) => VobsNode | null | undefined;
198
+ readonly error?: (error: Error, retry: () => void) => VobsNode | null | undefined;
199
+ }
200
+ interface RouterPluginOptions {
201
+ readonly router?: Router;
202
+ readonly routes?: readonly RouteRecord[];
203
+ readonly history?: RouterHistory;
204
+ }
205
+ declare const ROUTER_KEY: InjectionKey<Router>;
206
+ declare function lazy(loader: RouteComponentLoader): LazyRouteComponent;
207
+ declare function createMemoryHistory(initial?: string): RouterHistory;
208
+ declare function createBrowserHistory(base?: string): RouterHistory;
209
+ declare function createRouter(options: RouterOptions): Router;
210
+ declare function RouterView(props?: RouterViewProps): VobsNode;
211
+ declare function useRouter(): Router;
212
+ declare function useRoute(): Signal<RouteLocation>;
213
+ declare function routerPlugin(options?: RouterPluginOptions): VobsPlugin;
214
+
215
+ export { type LazyRouteComponent, NavigationCancelledError, type NavigationGuard, type NavigationGuardResult, NavigationRedirectError, type NavigationState, type NavigationTrace, ROUTER_KEY, type RouteComponent, type RouteComponentDefinition, type RouteComponentLoader, type RouteComponentModule, type RouteComponentProps, type RouteDebugNode, type RouteErrorTrace, type RouteLoader, type RouteLoaderContext, type RouteLocation, type RouteLocationRaw, type RouteMeta, type RouteParams, type RouteQuery, type RouteQueryInput, type RouteQueryValue, type RouteRecord, type RouteTarget, type Router, type RouterDataRequestKind, type RouterDataRequestOptions, type RouterDataRequestTrace, type RouterDevToolsAPI, type RouterDevToolsEvent, type RouterHistory, type RouterOptions, type RouterPerformanceMetrics, type RouterPluginOptions, RouterView, type RouterViewProps, type RouterViewState, createBrowserHistory, createMemoryHistory, createRouter, lazy, routerPlugin, useRoute, useRouter };