cross-router-core 1.0.1
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.md +7 -0
- package/dist/index.d.ts +208 -0
- package/dist/index.js +1217 -0
- package/dist/index.js.map +1 -0
- package/package.json +41 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright (c) 2026 Bricklou
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { GenericSchema, InferOutput } from 'valibot';
|
|
2
|
+
|
|
3
|
+
interface RouterContext<T = unknown> {
|
|
4
|
+
defaultValue?: T;
|
|
5
|
+
}
|
|
6
|
+
declare function createContext<T>(defaultValue?: T): RouterContext<T>;
|
|
7
|
+
declare class RouterContextProvider {
|
|
8
|
+
private map;
|
|
9
|
+
/**
|
|
10
|
+
* Create a new `RouterContextProvider` instance
|
|
11
|
+
* @param init An optional initial context map to populate the provider with
|
|
12
|
+
*/
|
|
13
|
+
constructor(init?: Map<RouterContext, unknown>);
|
|
14
|
+
/**
|
|
15
|
+
* Access a value from the context. If no value has been set for the context,
|
|
16
|
+
* it will return the context's `defaultValue` if provided, or throw an error
|
|
17
|
+
* if no `defaultValue` was set.
|
|
18
|
+
* @param context The context to get the value for
|
|
19
|
+
* @returns The value for the context, or the context's `defaultValue` if no
|
|
20
|
+
* value was set
|
|
21
|
+
*/
|
|
22
|
+
get<T>(context: RouterContext<T>): T;
|
|
23
|
+
/**
|
|
24
|
+
* Set a value for the context. If the context already has a value set, this
|
|
25
|
+
* will overwrite it.
|
|
26
|
+
*
|
|
27
|
+
* @param context The context to set the value for
|
|
28
|
+
* @param value The value to set for the context
|
|
29
|
+
* @returns {void}
|
|
30
|
+
*/
|
|
31
|
+
set<C extends RouterContext>(context: C, value: C extends RouterContext<infer T> ? T : never): void;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
type ExtractParams<TPath extends string> = TPath extends `${infer _Start}:${infer Param}/${infer Rest}` ? Param | ExtractParams<`/${Rest}`> : TPath extends `${infer _Start}:${infer Param}` ? Param : never;
|
|
35
|
+
type ParamsFromPath<TPath extends string> = {
|
|
36
|
+
[K in ExtractParams<TPath>]: string;
|
|
37
|
+
};
|
|
38
|
+
type SearchSchema = GenericSchema<Record<string, unknown>>;
|
|
39
|
+
type InferSearch<TSchema extends SearchSchema> = InferOutput<TSchema>;
|
|
40
|
+
interface MiddlewareArgs {
|
|
41
|
+
context: RouterContextProvider;
|
|
42
|
+
request: NavigationRequest;
|
|
43
|
+
}
|
|
44
|
+
type NextFn = () => Promise<void>;
|
|
45
|
+
interface Middleware {
|
|
46
|
+
handler: (args: MiddlewareArgs, next: NextFn) => Promise<void>;
|
|
47
|
+
}
|
|
48
|
+
type AnyMiddleware = Middleware;
|
|
49
|
+
declare function defineMiddleware(handler: (args: MiddlewareArgs, next: NextFn) => Promise<void>): Middleware;
|
|
50
|
+
type MaybePromise<T> = Promise<T> | T;
|
|
51
|
+
interface LoaderArgs<TParams extends Record<string, string>, TSearch> {
|
|
52
|
+
context: RouterContextProvider;
|
|
53
|
+
params: TParams;
|
|
54
|
+
search: TSearch;
|
|
55
|
+
request: NavigationRequest;
|
|
56
|
+
}
|
|
57
|
+
interface ActionArgs<TParams extends Record<string, string>> {
|
|
58
|
+
context: RouterContextProvider;
|
|
59
|
+
params: TParams;
|
|
60
|
+
request: NavigationRequest;
|
|
61
|
+
formData: FormData;
|
|
62
|
+
}
|
|
63
|
+
type LoaderFn<TParams extends Record<string, string>, TSearch, TData> = (args: LoaderArgs<TParams, TSearch>) => MaybePromise<TData>;
|
|
64
|
+
type ActionFn<TParams extends Record<string, string>> = (args: ActionArgs<TParams>) => MaybePromise<unknown>;
|
|
65
|
+
interface RevalidateArgs<TParams extends Record<string, string>> {
|
|
66
|
+
currentParams: TParams;
|
|
67
|
+
nextParams: TParams;
|
|
68
|
+
currentSearch: Record<string, unknown>;
|
|
69
|
+
nextSearch: Record<string, unknown>;
|
|
70
|
+
formData?: FormData | undefined;
|
|
71
|
+
}
|
|
72
|
+
interface RouteDefinition<TPath extends string = string, TMiddlewares extends AnyMiddleware[] = AnyMiddleware[], TSearchSchema extends SearchSchema = SearchSchema, TLoaderData = unknown, TComponent = unknown> {
|
|
73
|
+
id: string;
|
|
74
|
+
path?: TPath;
|
|
75
|
+
index?: true;
|
|
76
|
+
middleware?: TMiddlewares;
|
|
77
|
+
search?: TSearchSchema;
|
|
78
|
+
loader?: LoaderFn<ParamsFromPath<TPath>, TSearchSchema extends SearchSchema ? InferSearch<TSearchSchema> : Record<string, unknown>, TLoaderData>;
|
|
79
|
+
action?: ActionFn<ParamsFromPath<TPath>>;
|
|
80
|
+
shouldRevalidate?: (args: RevalidateArgs<ParamsFromPath<TPath>>) => boolean;
|
|
81
|
+
component?: TComponent;
|
|
82
|
+
errorComponent?: TComponent;
|
|
83
|
+
lazy?: () => Promise<Partial<Pick<RouteDefinition<TPath, TMiddlewares, TSearchSchema, TLoaderData, TComponent>, 'loader' | 'action' | 'component' | 'errorComponent'>>>;
|
|
84
|
+
children?: AnyRouteDefinition[];
|
|
85
|
+
__renderer?: RouteRenderer;
|
|
86
|
+
}
|
|
87
|
+
type AnyRouteDefinition = RouteDefinition<any, any, any, any, any>;
|
|
88
|
+
interface RouteRenderer {
|
|
89
|
+
/** Mount component into target element. Returns cleanup fn. */
|
|
90
|
+
mount: (component: unknown, target: HTMLElement) => () => void;
|
|
91
|
+
}
|
|
92
|
+
interface RouteMatch<TLoaderData = unknown> {
|
|
93
|
+
route: AnyRouteDefinition;
|
|
94
|
+
params: Record<string, string>;
|
|
95
|
+
search: Record<string, unknown>;
|
|
96
|
+
pathname: string;
|
|
97
|
+
loaderData: TLoaderData;
|
|
98
|
+
status: 'idle' | 'loading' | 'success' | 'error';
|
|
99
|
+
error?: unknown;
|
|
100
|
+
}
|
|
101
|
+
type AnyRouteMatch = RouteMatch<unknown>;
|
|
102
|
+
interface NavigationRequest {
|
|
103
|
+
url: URL;
|
|
104
|
+
method: 'GET' | 'POST';
|
|
105
|
+
formData?: FormData;
|
|
106
|
+
}
|
|
107
|
+
type NavigationStatus = 'idle' | 'loading' | 'submitting' | 'redirecting' | 'error';
|
|
108
|
+
interface NavigationState {
|
|
109
|
+
location: Location;
|
|
110
|
+
status: NavigationStatus;
|
|
111
|
+
matches: AnyRouteMatch[];
|
|
112
|
+
loaderData: Record<string, unknown>;
|
|
113
|
+
actionData: unknown;
|
|
114
|
+
error: unknown;
|
|
115
|
+
isTransitioning: boolean;
|
|
116
|
+
viewTransition: boolean;
|
|
117
|
+
}
|
|
118
|
+
interface Location {
|
|
119
|
+
pathname: string;
|
|
120
|
+
search: string;
|
|
121
|
+
hash: string;
|
|
122
|
+
state: unknown;
|
|
123
|
+
key: string;
|
|
124
|
+
}
|
|
125
|
+
interface RouterOptions {
|
|
126
|
+
context?: () => RouterContextProvider;
|
|
127
|
+
middleware?: AnyMiddleware[];
|
|
128
|
+
notFoundComponent?: unknown;
|
|
129
|
+
errorComponent?: unknown;
|
|
130
|
+
}
|
|
131
|
+
declare class Redirect {
|
|
132
|
+
readonly to: string;
|
|
133
|
+
readonly status: 301 | 302 | 307 | 308;
|
|
134
|
+
readonly __type: "redirect";
|
|
135
|
+
constructor(to: string, status?: 301 | 302 | 307 | 308);
|
|
136
|
+
}
|
|
137
|
+
declare function redirect(to: string, status?: 301 | 302 | 307 | 308): never;
|
|
138
|
+
declare function isRedirect(value: unknown): value is Redirect;
|
|
139
|
+
interface RouterInstance {
|
|
140
|
+
/** Current state */
|
|
141
|
+
readonly state: NavigationState;
|
|
142
|
+
/** Subscribe to state changes — returns unsubscribe fn */
|
|
143
|
+
subscribe: (listener: (state: NavigationState) => void) => () => void;
|
|
144
|
+
navigate: (to: string, options?: NavigateOptions) => Promise<void>;
|
|
145
|
+
replace: (to: string, options?: NavigateOptions) => Promise<void>;
|
|
146
|
+
back: () => void;
|
|
147
|
+
forward: () => void;
|
|
148
|
+
/** Form submission trigger (called by <Form> component in adapters) */
|
|
149
|
+
submit: (formData: FormData, options: SubmitOptions) => Promise<void>;
|
|
150
|
+
/** Must be called once by the framework adapter before first render */
|
|
151
|
+
initialize: () => Promise<void>;
|
|
152
|
+
/** Add routes under a parent node (by id). Use 'root' for top level */
|
|
153
|
+
patch: (routes: AnyRouteDefinition[], parentId?: string) => void;
|
|
154
|
+
/** Remove a route subtree by id */
|
|
155
|
+
unpatch: (routeId: string) => void;
|
|
156
|
+
/** Cleanup — removes history listener */
|
|
157
|
+
destroy: () => void;
|
|
158
|
+
}
|
|
159
|
+
interface NavigateOptions {
|
|
160
|
+
state?: unknown;
|
|
161
|
+
replace?: boolean;
|
|
162
|
+
resetScroll?: boolean;
|
|
163
|
+
viewTransition?: boolean;
|
|
164
|
+
}
|
|
165
|
+
interface SubmitOptions {
|
|
166
|
+
action: string;
|
|
167
|
+
method?: 'POST';
|
|
168
|
+
}
|
|
169
|
+
interface RouteRegistry {
|
|
170
|
+
patch: (routes: AnyRouteDefinition[], parentId?: string) => void;
|
|
171
|
+
unpatch: (routeId: string) => void;
|
|
172
|
+
getTree: () => AnyRouteDefinition[];
|
|
173
|
+
subscribe: (listener: (tree: AnyRouteDefinition[]) => void) => () => void;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
type HistoryListener = (location: Location) => void;
|
|
177
|
+
interface History {
|
|
178
|
+
readonly location: Location;
|
|
179
|
+
push: (to: string, state?: unknown) => void;
|
|
180
|
+
replace: (to: string, state?: unknown) => void;
|
|
181
|
+
back: () => void;
|
|
182
|
+
forward: () => void;
|
|
183
|
+
go: (delta: number) => void;
|
|
184
|
+
listen: (listener: HistoryListener) => () => void;
|
|
185
|
+
destroy: () => void;
|
|
186
|
+
}
|
|
187
|
+
declare function createBrowserHistory(): History;
|
|
188
|
+
declare function createMemoryHistory(initialPath?: string): History;
|
|
189
|
+
|
|
190
|
+
declare class MiddlewareError extends Error {
|
|
191
|
+
readonly middlewareIndex: number;
|
|
192
|
+
constructor(message: string, middlewareIndex: number);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
declare class RegistryError extends Error {
|
|
196
|
+
constructor(message: string);
|
|
197
|
+
}
|
|
198
|
+
declare function createRegistry(initialRoutes?: AnyRouteDefinition[]): RouteRegistry;
|
|
199
|
+
|
|
200
|
+
declare function createRouter(history: History, options: RouterOptions, registry: RouteRegistry): RouterInstance;
|
|
201
|
+
declare function createBrowserRouter(options: RouterOptions, initialRoutes?: AnyRouteDefinition[]): RouterInstance;
|
|
202
|
+
/**
|
|
203
|
+
* Recursively tag all routes (and their children) with a renderer.
|
|
204
|
+
* This method is used by renderers
|
|
205
|
+
*/
|
|
206
|
+
declare function tag(renderer: RouteRenderer, defs: AnyRouteDefinition[]): AnyRouteDefinition[];
|
|
207
|
+
|
|
208
|
+
export { type ActionArgs, type ActionFn, type AnyMiddleware, type AnyRouteDefinition, type AnyRouteMatch, type ExtractParams, type History, type HistoryListener, type InferSearch, type LoaderArgs, type LoaderFn, type Location, type Middleware, type MiddlewareArgs, MiddlewareError, type NavigateOptions, type NavigationState, type NavigationStatus, type NextFn, type ParamsFromPath, Redirect, RegistryError, type RevalidateArgs, type RouteDefinition, type RouteMatch, type RouteRegistry, type RouteRenderer, RouterContextProvider, type RouterInstance, type RouterOptions, type SearchSchema, type SubmitOptions, createBrowserHistory, createBrowserRouter, createContext, createMemoryHistory, createRegistry, createRouter, defineMiddleware, isRedirect, redirect, tag };
|