@surgijs/router 0.0.0-stage → 0.0.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 +21 -0
- package/README.md +41 -2
- package/dist/angular.d.ts +45 -0
- package/dist/angular.js +52 -0
- package/dist/angular.js.map +1 -0
- package/dist/chunk-FRJIJ3K7.js +104 -0
- package/dist/chunk-FRJIJ3K7.js.map +1 -0
- package/dist/chunk-LY6JNBHA.js +20 -0
- package/dist/chunk-LY6JNBHA.js.map +1 -0
- package/dist/chunk-OQUB6NVM.js +53 -0
- package/dist/chunk-OQUB6NVM.js.map +1 -0
- package/dist/chunk-W2SIXU6O.js +44 -0
- package/dist/chunk-W2SIXU6O.js.map +1 -0
- package/dist/core.d.ts +73 -0
- package/dist/core.js +4 -0
- package/dist/core.js.map +1 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +1123 -0
- package/dist/index.js.map +1 -0
- package/dist/react.d.ts +80 -0
- package/dist/react.js +120 -0
- package/dist/react.js.map +1 -0
- package/dist/router-ClmQESkC.d.ts +497 -0
- package/dist/server.d.ts +21 -0
- package/dist/server.js +25 -0
- package/dist/server.js.map +1 -0
- package/dist/svelte.d.ts +50 -0
- package/dist/svelte.js +60 -0
- package/dist/svelte.js.map +1 -0
- package/dist/vue.d.ts +80 -0
- package/dist/vue.js +115 -0
- package/dist/vue.js.map +1 -0
- package/package.json +112 -3
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
interface HistoryLocation {
|
|
2
|
+
/** Path, query and hash as the router sees them: `/users/42?tab=posts#bio`. */
|
|
3
|
+
url: string;
|
|
4
|
+
state: unknown;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Where the router keeps its location. A driver maps the router's own URL (`/users/42?x=1`) to a
|
|
8
|
+
* place in the browser: the path, the hash or memory.
|
|
9
|
+
*/
|
|
10
|
+
interface HistoryDriver {
|
|
11
|
+
/** `memory` drivers work without a DOM; the router leaves `<a>` clicks to the application for them. */
|
|
12
|
+
readonly kind: 'browser' | 'memory';
|
|
13
|
+
read(): HistoryLocation;
|
|
14
|
+
push(url: string, state?: unknown): void;
|
|
15
|
+
replace(url: string, state?: unknown): void;
|
|
16
|
+
go(delta: number): void;
|
|
17
|
+
/** `delta` is the number of entries moved: negative for back. */
|
|
18
|
+
listen(callback: (location: HistoryLocation, delta: number) => void): () => void;
|
|
19
|
+
/** The `href` for an anchor that navigates to `url`. */
|
|
20
|
+
href(url: string): string;
|
|
21
|
+
/** The router URL an absolute `href` stands for, or `null` when it leaves this router. */
|
|
22
|
+
fromHref(url: URL): string | null;
|
|
23
|
+
}
|
|
24
|
+
interface SlashHistoryOptions {
|
|
25
|
+
/** A path prefix the application lives under, such as `/admin`. */
|
|
26
|
+
base?: string;
|
|
27
|
+
}
|
|
28
|
+
/** Real paths through the History API: `/users/42?tab=posts#bio`. */
|
|
29
|
+
declare function slashHistory(options?: SlashHistoryOptions): HistoryDriver;
|
|
30
|
+
/** How a hash holds the router URL. */
|
|
31
|
+
interface HashCodec {
|
|
32
|
+
/** The router URL in `hash` (which starts with `#`, or is empty), or `null` when it holds none. */
|
|
33
|
+
parse(hash: string): string | null;
|
|
34
|
+
/** The next hash for `url`, given the current one. */
|
|
35
|
+
format(url: string, hash: string): string;
|
|
36
|
+
}
|
|
37
|
+
interface HashHistoryOptions {
|
|
38
|
+
/** What comes before the URL in the hash: `#` (default), `#!` or `#/app`. */
|
|
39
|
+
prefix?: string;
|
|
40
|
+
/**
|
|
41
|
+
* The router owns one `key=value` pair of the hash and leaves the others alone, so several routers
|
|
42
|
+
* and the host page can share it: `#checkout=%2Fstep%2F2`. Ignores `prefix`.
|
|
43
|
+
*/
|
|
44
|
+
key?: string;
|
|
45
|
+
codec?: HashCodec;
|
|
46
|
+
}
|
|
47
|
+
/** The router URL lives in `location.hash`, so a static host needs no rewrites. */
|
|
48
|
+
declare function hashHistory(options?: HashHistoryOptions): HistoryDriver;
|
|
49
|
+
/** History in memory, for servers and tests. `go` notifies listeners at once. */
|
|
50
|
+
declare function memoryHistory(initial?: string): HistoryDriver;
|
|
51
|
+
|
|
52
|
+
/** A query or path value as it comes out of the URL: one string, repeated keys, or absent. */
|
|
53
|
+
type Raw = string | string[] | undefined;
|
|
54
|
+
type Parsed<T> = {
|
|
55
|
+
ok: true;
|
|
56
|
+
value: T;
|
|
57
|
+
} | {
|
|
58
|
+
ok: false;
|
|
59
|
+
error: string;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* One typed value of a path or query. `D` is `true` when the field has a default, so reading it
|
|
63
|
+
* never yields `undefined`.
|
|
64
|
+
*/
|
|
65
|
+
interface Field<T = unknown, D extends boolean = boolean> {
|
|
66
|
+
readonly hasDefault: D;
|
|
67
|
+
readonly fallback: T | undefined;
|
|
68
|
+
/** An absent value gives the default; a present one that does not fit gives `ok: false`. */
|
|
69
|
+
parse(raw: Raw): Parsed<T | undefined>;
|
|
70
|
+
format(value: T): Raw;
|
|
71
|
+
}
|
|
72
|
+
/** The part of the Standard Schema v1 contract (zod, valibot, ArkType, ...) the router reads. */
|
|
73
|
+
interface StandardSchemaV1<Input = unknown, Output = Input> {
|
|
74
|
+
readonly '~standard': {
|
|
75
|
+
readonly version: 1;
|
|
76
|
+
readonly vendor: string;
|
|
77
|
+
readonly validate: (value: unknown) => StandardResult<Output> | Promise<StandardResult<Output>>;
|
|
78
|
+
readonly types?: {
|
|
79
|
+
readonly input: Input;
|
|
80
|
+
readonly output: Output;
|
|
81
|
+
} | undefined;
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
type StandardResult<Output> = {
|
|
85
|
+
readonly value: Output;
|
|
86
|
+
readonly issues?: undefined;
|
|
87
|
+
} | {
|
|
88
|
+
readonly issues: ReadonlyArray<{
|
|
89
|
+
readonly message: string;
|
|
90
|
+
}>;
|
|
91
|
+
};
|
|
92
|
+
/** What `params` and `query` accept: a record of `p.*` fields, a Standard Schema, or a function. */
|
|
93
|
+
type SchemaSpec = Record<string, Field<any, any>> | StandardSchemaV1<any, any> | ((raw: Record<string, Raw>) => unknown);
|
|
94
|
+
type Simplify$1<T> = {
|
|
95
|
+
[K in keyof T]: T[K];
|
|
96
|
+
} & {};
|
|
97
|
+
type FieldValue<F> = F extends Field<infer T, any> ? T : never;
|
|
98
|
+
type FieldOut<F> = F extends Field<infer T, infer D> ? ([D] extends [true] ? T : T | undefined) : never;
|
|
99
|
+
/** The value type a spec produces. */
|
|
100
|
+
type SpecOut<S> = S extends undefined ? {} : S extends StandardSchemaV1<any, infer O> ? O : S extends (raw: any) => infer O ? O : S extends Record<string, Field<any, any>> ? Simplify$1<{
|
|
101
|
+
[K in keyof S]: FieldOut<S[K]>;
|
|
102
|
+
}> : {};
|
|
103
|
+
type Has<O, K extends string> = O extends {
|
|
104
|
+
[P in K]: infer V;
|
|
105
|
+
} ? ([V] extends [undefined] ? false : true) : false;
|
|
106
|
+
interface StringOptions {
|
|
107
|
+
default?: string;
|
|
108
|
+
minLength?: number;
|
|
109
|
+
maxLength?: number;
|
|
110
|
+
pattern?: RegExp;
|
|
111
|
+
}
|
|
112
|
+
interface NumberOptions {
|
|
113
|
+
default?: number;
|
|
114
|
+
min?: number;
|
|
115
|
+
max?: number;
|
|
116
|
+
}
|
|
117
|
+
declare function string<O extends StringOptions>(o?: O): Field<string, Has<O, 'default'>>;
|
|
118
|
+
declare function number<O extends NumberOptions>(o?: O): Field<number, Has<O, 'default'>>;
|
|
119
|
+
declare function int<O extends NumberOptions>(o?: O): Field<number, Has<O, 'default'>>;
|
|
120
|
+
declare function boolean<O extends {
|
|
121
|
+
default?: boolean;
|
|
122
|
+
}>(o?: O): Field<boolean, Has<O, 'default'>>;
|
|
123
|
+
declare function oneOf<const V extends readonly string[], O extends {
|
|
124
|
+
default?: V[number];
|
|
125
|
+
}>(values: V, o?: O): Field<V[number], Has<O, 'default'>>;
|
|
126
|
+
declare function json<T = unknown, O extends {
|
|
127
|
+
default?: T;
|
|
128
|
+
} = {}>(o?: O): Field<T, Has<O, 'default'>>;
|
|
129
|
+
/** A value of your own type: `decode` throws or returns `undefined` for input that does not fit. */
|
|
130
|
+
declare function custom<T, O extends {
|
|
131
|
+
default?: T;
|
|
132
|
+
}>(decode: (s: string) => T | undefined, encode: (v: T) => string, o?: O): Field<T, Has<O, 'default'>>;
|
|
133
|
+
interface ArrayOptions<T> {
|
|
134
|
+
default?: T[];
|
|
135
|
+
/** `repeat`: `?tag=a&tag=b` (default). `comma`: `?tag=a,b`. */
|
|
136
|
+
format?: 'repeat' | 'comma';
|
|
137
|
+
}
|
|
138
|
+
declare function array<T>(item: Field<T, any>, o?: ArrayOptions<T>): Field<T[], true>;
|
|
139
|
+
/** Typed path and query fields. Each gives a value of its type, or the default when the URL has none. */
|
|
140
|
+
declare const p: {
|
|
141
|
+
string: typeof string;
|
|
142
|
+
number: typeof number;
|
|
143
|
+
int: typeof int;
|
|
144
|
+
boolean: typeof boolean;
|
|
145
|
+
enum: typeof oneOf;
|
|
146
|
+
array: typeof array;
|
|
147
|
+
json: typeof json;
|
|
148
|
+
custom: typeof custom;
|
|
149
|
+
};
|
|
150
|
+
/** A compiled `params` or `query` spec. */
|
|
151
|
+
interface Compiled {
|
|
152
|
+
/** Names of the fields of a record spec; `null` for a Standard Schema or a function. */
|
|
153
|
+
readonly keys: readonly string[] | null;
|
|
154
|
+
parse(raw: Record<string, Raw>): {
|
|
155
|
+
ok: boolean;
|
|
156
|
+
value: Record<string, unknown>;
|
|
157
|
+
};
|
|
158
|
+
/** Raw values for `values`. With `strip`, a field equal to its default is left out. */
|
|
159
|
+
format(values: Record<string, unknown>, strip: boolean): Record<string, Raw>;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
type Params = Record<string, string>;
|
|
163
|
+
interface Segment {
|
|
164
|
+
kind: 0 | 1 | 2 | 3;
|
|
165
|
+
/** The literal for a static segment, the name otherwise (`'*'` for an unnamed splat). */
|
|
166
|
+
name: string;
|
|
167
|
+
}
|
|
168
|
+
/** Matches `pathname` against one pattern, outside of any route table. */
|
|
169
|
+
declare function matchPath(pattern: string, pathname: string, options?: {
|
|
170
|
+
caseSensitive?: boolean;
|
|
171
|
+
}): Params | null;
|
|
172
|
+
declare function parseQuery(search: string): Record<string, string | string[]>;
|
|
173
|
+
/** `?a=1&b=2&b=3`, or `''` when nothing is left. */
|
|
174
|
+
declare function stringifyQuery(query: Record<string, Raw>): string;
|
|
175
|
+
|
|
176
|
+
type Simplify<T> = {
|
|
177
|
+
[K in keyof T]: T[K];
|
|
178
|
+
} & {};
|
|
179
|
+
/** A redirect from a guard, a `load` function or a route's `redirect`. Build one with `redirect()`. */
|
|
180
|
+
declare class Redirect {
|
|
181
|
+
readonly to: string | AnyRoute;
|
|
182
|
+
readonly status: number;
|
|
183
|
+
readonly options: {
|
|
184
|
+
replace?: boolean;
|
|
185
|
+
params?: Record<string, unknown>;
|
|
186
|
+
query?: Record<string, unknown>;
|
|
187
|
+
hash?: string;
|
|
188
|
+
};
|
|
189
|
+
constructor(to: string | AnyRoute, status: number, options?: {
|
|
190
|
+
replace?: boolean;
|
|
191
|
+
params?: Record<string, unknown>;
|
|
192
|
+
query?: Record<string, unknown>;
|
|
193
|
+
hash?: string;
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* `return redirect('/login')` from a guard, or `throw redirect('/login')` from `load`.
|
|
198
|
+
* The status (302 by default) is what a server answers with.
|
|
199
|
+
*/
|
|
200
|
+
declare function redirect(to: string | AnyRoute, init?: number | {
|
|
201
|
+
status?: number;
|
|
202
|
+
replace?: boolean;
|
|
203
|
+
params?: Record<string, unknown>;
|
|
204
|
+
query?: Record<string, unknown>;
|
|
205
|
+
hash?: string;
|
|
206
|
+
}): Redirect;
|
|
207
|
+
interface RouteContext {
|
|
208
|
+
/** The location being entered, matched and validated. */
|
|
209
|
+
to: RouteLocation;
|
|
210
|
+
/** The current location; `null` for the first navigation. */
|
|
211
|
+
from: RouterState | null;
|
|
212
|
+
params: Record<string, any>;
|
|
213
|
+
query: Record<string, any>;
|
|
214
|
+
/** Aborted when another navigation starts or the router is destroyed. */
|
|
215
|
+
signal: AbortSignal;
|
|
216
|
+
store: StoreLike | undefined;
|
|
217
|
+
router: Router;
|
|
218
|
+
}
|
|
219
|
+
/** `false` cancels, a string or `redirect()` redirects, anything else continues. */
|
|
220
|
+
type GuardResult = void | undefined | boolean | string | Redirect;
|
|
221
|
+
type Guard = (ctx: RouteContext) => GuardResult | PromiseLike<GuardResult>;
|
|
222
|
+
interface RouteDef {
|
|
223
|
+
/**
|
|
224
|
+
* `users`, `:id`, `:id?` (last only), `*rest` (last only) or `''` for an index route. Relative to the
|
|
225
|
+
* parent; a path that starts with `/` is absolute. A route with children is not matched itself:
|
|
226
|
+
* give it an index child.
|
|
227
|
+
*/
|
|
228
|
+
path: string;
|
|
229
|
+
/** The component to render, whatever the UI library takes. */
|
|
230
|
+
component?: unknown;
|
|
231
|
+
/** Loads the component before the route is entered; the module's `default` export, or the module itself. */
|
|
232
|
+
lazy?: () => Promise<unknown>;
|
|
233
|
+
/** Rendered in place of the route when `lazy` or `load` fails. */
|
|
234
|
+
errorComponent?: unknown;
|
|
235
|
+
children?: readonly RouteDef[];
|
|
236
|
+
/** Attaches this route under `parent` without listing it in `parent.children`. */
|
|
237
|
+
parent?: RouteDef;
|
|
238
|
+
/** Typed path parameters: `p.*` fields, a Standard Schema or a function. Invalid values do not match. */
|
|
239
|
+
params?: SchemaSpec;
|
|
240
|
+
/** Typed query: invalid values fall back to the field default, unless `strictQuery` is set. */
|
|
241
|
+
query?: SchemaSpec;
|
|
242
|
+
/** An invalid query does not match the route. */
|
|
243
|
+
strictQuery?: boolean;
|
|
244
|
+
/** Where to go instead; a function receives the entering context. */
|
|
245
|
+
redirect?: string | ((ctx: RouteContext) => string | Redirect);
|
|
246
|
+
beforeEnter?: Guard | readonly Guard[];
|
|
247
|
+
/** Data for the route, fetched in parallel with the other routes and `lazy`; read it with `useLoaderData`. */
|
|
248
|
+
load?: (ctx: RouteContext) => unknown;
|
|
249
|
+
/** Keys of store remotes to load before the route is entered, in parallel with `lazy` and `load`. */
|
|
250
|
+
preload?: readonly string[];
|
|
251
|
+
/** HTTP status of the page, 200 by default. */
|
|
252
|
+
status?: number;
|
|
253
|
+
meta?: Record<string, unknown>;
|
|
254
|
+
}
|
|
255
|
+
/** A route definition that carries the types of its parameters and query. */
|
|
256
|
+
interface Route<P extends string = string, Pr = any, Q = any> extends RouteDef {
|
|
257
|
+
readonly path: P;
|
|
258
|
+
readonly '~params'?: Pr;
|
|
259
|
+
readonly '~query'?: Q;
|
|
260
|
+
}
|
|
261
|
+
type AnyRoute = Route<string, any, any>;
|
|
262
|
+
type Segments<P extends string> = P extends `${infer A}/${infer B}` ? A | Segments<B> : P;
|
|
263
|
+
/** `:id` gives `{ id: string }`, `:id?` an optional one, `*rest` a `rest` string. */
|
|
264
|
+
type PathParams<P extends string> = Simplify<{
|
|
265
|
+
[S in Segments<P> as S extends `:${string}?` ? never : S extends `:${infer N}` ? N : never]: string;
|
|
266
|
+
} & {
|
|
267
|
+
[S in Segments<P> as S extends `:${infer N}?` ? N : never]?: string;
|
|
268
|
+
} & {
|
|
269
|
+
[S in Segments<P> as S extends `*${infer N}` ? (N extends '' ? '*' : N) : never]: string;
|
|
270
|
+
}>;
|
|
271
|
+
type ParamsWithFields<Pa, PS> = Simplify<{
|
|
272
|
+
[K in keyof Pa]: K extends keyof PS ? (undefined extends Pa[K] ? FieldValue<PS[K]> | undefined : FieldValue<PS[K]>) : Pa[K];
|
|
273
|
+
} & {
|
|
274
|
+
[K in Exclude<keyof PS, keyof Pa>]: FieldValue<PS[K]>;
|
|
275
|
+
}>;
|
|
276
|
+
type ParamsOf<P extends string, PS> = [PS] extends [undefined] ? PathParams<P> : PS extends Record<string, Field<any, any>> ? ParamsWithFields<PathParams<P>, PS> : Simplify<PathParams<P> & SpecOut<PS>>;
|
|
277
|
+
type ParentParams<Par> = Par extends Route<string, infer Pr, any> ? Pr : {};
|
|
278
|
+
type ParentQuery<Par> = Par extends Route<string, any, infer Q> ? Q : {};
|
|
279
|
+
/**
|
|
280
|
+
* Declares a route with the types of its path parameters (inferred from `path`, refined by `params`) and
|
|
281
|
+
* its query. Pass the result to `router.navigate`, `router.href` and the hooks to get them checked.
|
|
282
|
+
* A route with `parent` takes the parent's parameters and query as well.
|
|
283
|
+
*/
|
|
284
|
+
declare function defineRoute<const P extends string, PS extends SchemaSpec | undefined = undefined, QS extends SchemaSpec | undefined = undefined, Par extends AnyRoute | undefined = undefined>(def: Omit<RouteDef, 'path' | 'params' | 'query' | 'parent'> & {
|
|
285
|
+
path: P;
|
|
286
|
+
params?: PS;
|
|
287
|
+
query?: QS;
|
|
288
|
+
parent?: Par;
|
|
289
|
+
}): Route<P, Simplify<ParentParams<Par> & ParamsOf<P, PS>>, Simplify<ParentQuery<Par> & SpecOut<QS>>>;
|
|
290
|
+
/** The list a router takes. It exists for the types: a plain array works as well. */
|
|
291
|
+
declare function defineRoutes<const R extends readonly RouteDef[]>(routes: R): R;
|
|
292
|
+
type ParamsIn<Pr> = {
|
|
293
|
+
[K in keyof Pr]: Pr[K] extends string ? Pr[K] | number : Pr[K];
|
|
294
|
+
};
|
|
295
|
+
type NeedsParams<T> = {} extends T ? false : true;
|
|
296
|
+
/** The parameters of a route declared with `defineRoute`. */
|
|
297
|
+
type RouteParams<R> = R extends Route<string, infer Pr, any> ? Pr : Record<string, any>;
|
|
298
|
+
/** The query of a route declared with `defineRoute`. */
|
|
299
|
+
type RouteQuery<R> = R extends Route<string, any, infer Q> ? Q : Record<string, any>;
|
|
300
|
+
/** Options of `navigate` and `href` for a route: its required parameters, its query and a hash. */
|
|
301
|
+
type RouteArgs<R> = R extends Route<string, infer Pr, infer Q> ? (NeedsParams<Pr> extends true ? {
|
|
302
|
+
params: ParamsIn<Pr>;
|
|
303
|
+
} : {
|
|
304
|
+
params?: ParamsIn<Pr>;
|
|
305
|
+
}) & {
|
|
306
|
+
query?: Partial<Q>;
|
|
307
|
+
hash?: string;
|
|
308
|
+
} : never;
|
|
309
|
+
declare class RouteRecord {
|
|
310
|
+
readonly id: number;
|
|
311
|
+
readonly def: RouteDef;
|
|
312
|
+
readonly parent: RouteRecord | null;
|
|
313
|
+
readonly segs: Segment[];
|
|
314
|
+
component: unknown;
|
|
315
|
+
lazy: (() => Promise<unknown>) | undefined;
|
|
316
|
+
loading: Promise<void> | undefined;
|
|
317
|
+
readonly errorComponent: unknown;
|
|
318
|
+
readonly meta: Record<string, unknown>;
|
|
319
|
+
readonly params: Compiled | null;
|
|
320
|
+
readonly query: Compiled | null;
|
|
321
|
+
readonly pattern: string;
|
|
322
|
+
readonly score: number[];
|
|
323
|
+
readonly children: RouteRecord[];
|
|
324
|
+
constructor(id: number, def: RouteDef, parent: RouteRecord | null, segs: Segment[]);
|
|
325
|
+
/** The records from the outermost route down to this one. */
|
|
326
|
+
get chain(): RouteRecord[];
|
|
327
|
+
}
|
|
328
|
+
interface Match {
|
|
329
|
+
route: RouteRecord;
|
|
330
|
+
params: Record<string, any>;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** What the router needs of `@surgijs/store`; any `Store` fits. */
|
|
334
|
+
interface StoreLike {
|
|
335
|
+
get(): Record<string, any>;
|
|
336
|
+
set(key: any, value: any): void;
|
|
337
|
+
subscribe(selector: (state: any) => unknown, listener: (value: any, prev: any) => void, options?: {
|
|
338
|
+
equals?: (a: any, b: any) => boolean;
|
|
339
|
+
}): () => void;
|
|
340
|
+
status(key: any): {
|
|
341
|
+
isFetching: boolean;
|
|
342
|
+
};
|
|
343
|
+
hydrate?(snapshot: any): void;
|
|
344
|
+
dehydrate?(options?: any): any;
|
|
345
|
+
}
|
|
346
|
+
/** The part of the location a store keeps, under `storeKey` (`route` by default). */
|
|
347
|
+
interface RouteSlice {
|
|
348
|
+
path: string;
|
|
349
|
+
params: Record<string, any>;
|
|
350
|
+
query: Record<string, any>;
|
|
351
|
+
hash: string;
|
|
352
|
+
}
|
|
353
|
+
interface RouteLocation {
|
|
354
|
+
/** Path, query and hash: `/users/42?tab=posts#bio`. */
|
|
355
|
+
url: string;
|
|
356
|
+
path: string;
|
|
357
|
+
/** With the leading `?`, or empty. */
|
|
358
|
+
search: string;
|
|
359
|
+
/** With the leading `#`, or empty. */
|
|
360
|
+
hash: string;
|
|
361
|
+
/** Path parameters after validation: `p.int()` gives a number. */
|
|
362
|
+
params: Record<string, any>;
|
|
363
|
+
query: Record<string, any>;
|
|
364
|
+
/** The routes from the outermost layout to the matched one, empty when nothing matches. */
|
|
365
|
+
matches: Match[];
|
|
366
|
+
route: RouteRecord | null;
|
|
367
|
+
}
|
|
368
|
+
interface RouteError {
|
|
369
|
+
error: unknown;
|
|
370
|
+
/** The depth of the route that failed. */
|
|
371
|
+
level: number;
|
|
372
|
+
/** The depth of the outlet that shows the failure: the nearest route with an `errorComponent`, else `level`. */
|
|
373
|
+
at: number;
|
|
374
|
+
}
|
|
375
|
+
interface RouterState extends RouteLocation {
|
|
376
|
+
/** 200, the `status` of the route, 404 when nothing matches or 500 when `lazy` or `load` failed. */
|
|
377
|
+
status: number;
|
|
378
|
+
/** What `load` returned, by depth. */
|
|
379
|
+
data: unknown[];
|
|
380
|
+
/** The `state` the entry was navigated with. */
|
|
381
|
+
state: unknown;
|
|
382
|
+
error: RouteError | null;
|
|
383
|
+
/** Set when the location is the result of a redirect: where the request started and the status to answer with. */
|
|
384
|
+
redirect: {
|
|
385
|
+
from: string;
|
|
386
|
+
status: number;
|
|
387
|
+
} | null;
|
|
388
|
+
/** A navigation is waiting for `lazy`, `load`, `preload` or an asynchronous guard. */
|
|
389
|
+
isPending: boolean;
|
|
390
|
+
/** The URL being navigated to while `isPending`. */
|
|
391
|
+
pending: string | null;
|
|
392
|
+
}
|
|
393
|
+
interface NavigationResult {
|
|
394
|
+
/** `done`: committed; `blocked`: a guard said no; `cancelled`: another navigation took over. */
|
|
395
|
+
type: 'done' | 'blocked' | 'cancelled';
|
|
396
|
+
state: RouterState;
|
|
397
|
+
}
|
|
398
|
+
interface NavigateOptions {
|
|
399
|
+
/** Rewrite the current history entry instead of adding one. */
|
|
400
|
+
replace?: boolean;
|
|
401
|
+
/** Kept with the history entry; read it from `state.state`. */
|
|
402
|
+
state?: unknown;
|
|
403
|
+
}
|
|
404
|
+
interface UrlOptions {
|
|
405
|
+
query?: Record<string, unknown>;
|
|
406
|
+
hash?: string;
|
|
407
|
+
}
|
|
408
|
+
interface LeaveContext {
|
|
409
|
+
to: string;
|
|
410
|
+
from: RouterState;
|
|
411
|
+
}
|
|
412
|
+
type LeaveGuard = (ctx: LeaveContext) => boolean | void | PromiseLike<boolean | void>;
|
|
413
|
+
interface NavFn<T> {
|
|
414
|
+
<R extends AnyRoute>(to: R, options: NavigateOptions & RouteArgs<R>): T;
|
|
415
|
+
(to: string, options?: NavigateOptions & UrlOptions): T;
|
|
416
|
+
}
|
|
417
|
+
interface SubscribeOptions<T> {
|
|
418
|
+
equals?: (a: T, b: T) => boolean;
|
|
419
|
+
fireImmediately?: boolean;
|
|
420
|
+
}
|
|
421
|
+
interface RouterSnapshot {
|
|
422
|
+
/** The URL (path and query) the data belongs to. */
|
|
423
|
+
url: string;
|
|
424
|
+
/** What `load` returned, by depth. */
|
|
425
|
+
data: unknown[];
|
|
426
|
+
store?: unknown;
|
|
427
|
+
}
|
|
428
|
+
interface RouterOptions {
|
|
429
|
+
routes: readonly RouteDef[];
|
|
430
|
+
/** Where the location lives. Default: `slashHistory({ base })` in a browser, `memoryHistory(url)` elsewhere or when `url` is set. */
|
|
431
|
+
history?: HistoryDriver;
|
|
432
|
+
/** The path the app is served under, for the default `slashHistory`. */
|
|
433
|
+
base?: string;
|
|
434
|
+
/** The URL to start from without a browser: the request URL on a server. */
|
|
435
|
+
url?: string;
|
|
436
|
+
/** A `@surgijs/store`: the router keeps `{ path, params, query, hash }` in the `storeKey` field and loads `preload` remotes. */
|
|
437
|
+
store?: StoreLike;
|
|
438
|
+
storeKey?: string;
|
|
439
|
+
/** An invalid query does not match the route. Default: it falls back to the field defaults. */
|
|
440
|
+
strictQuery?: boolean;
|
|
441
|
+
caseSensitive?: boolean;
|
|
442
|
+
/** After a navigation, rewrite the URL with `replace` to its canonical form: defaults and invalid query values removed. */
|
|
443
|
+
canonical?: boolean;
|
|
444
|
+
/** Navigate on clicks of `<a>` elements, through shadow roots too. Default: on for browser histories. */
|
|
445
|
+
intercept?: boolean;
|
|
446
|
+
/** Called after each navigation but the first. `autoScroll` is a ready-made one. */
|
|
447
|
+
scroll?: (to: RouterState, from: RouterState | null, info: {
|
|
448
|
+
pop: boolean;
|
|
449
|
+
}) => void;
|
|
450
|
+
/** What the server sent (`readSnapshot()`): store data and `load` results the first navigation reuses. */
|
|
451
|
+
snapshot?: RouterSnapshot;
|
|
452
|
+
}
|
|
453
|
+
interface Router {
|
|
454
|
+
readonly history: HistoryDriver;
|
|
455
|
+
readonly store: StoreLike | undefined;
|
|
456
|
+
readonly routes: readonly RouteRecord[];
|
|
457
|
+
get(): RouterState;
|
|
458
|
+
get<T>(selector: (state: RouterState) => T): T;
|
|
459
|
+
/** Calls `listener` when the selected value changes by deep equality. */
|
|
460
|
+
subscribe<T>(selector: (state: RouterState) => T, listener: (value: T, prev: T) => void, options?: SubscribeOptions<T>): () => void;
|
|
461
|
+
navigate: NavFn<Promise<NavigationResult>>;
|
|
462
|
+
push: NavFn<Promise<NavigationResult>>;
|
|
463
|
+
replace: NavFn<Promise<NavigationResult>>;
|
|
464
|
+
back(): void;
|
|
465
|
+
forward(): void;
|
|
466
|
+
go(delta: number): void;
|
|
467
|
+
/** Merges `patch` into the query (`undefined` removes a key) and navigates; other query keys stay. */
|
|
468
|
+
setQuery(patch: Record<string, unknown> | ((query: Record<string, any>) => Record<string, unknown>), options?: NavigateOptions): Promise<NavigationResult>;
|
|
469
|
+
/** The `href` for an anchor. */
|
|
470
|
+
href: NavFn<string>;
|
|
471
|
+
/** The router URL a target stands for. */
|
|
472
|
+
resolve: NavFn<string>;
|
|
473
|
+
/** Whether the current path is `to` (`exact`) or below it. */
|
|
474
|
+
isActive(to: string | AnyRoute, options?: {
|
|
475
|
+
exact?: boolean;
|
|
476
|
+
params?: Record<string, unknown>;
|
|
477
|
+
}): boolean;
|
|
478
|
+
/** Loads the `lazy` components of the routes `to` leads to. */
|
|
479
|
+
prefetch(to: string | AnyRoute, options?: UrlOptions & {
|
|
480
|
+
params?: Record<string, unknown>;
|
|
481
|
+
}): Promise<void>;
|
|
482
|
+
/** Runs before every navigation, ahead of the route's own guards. */
|
|
483
|
+
beforeEach(guard: Guard): () => void;
|
|
484
|
+
afterEach(hook: (to: RouterState, from: RouterState | null) => void): () => void;
|
|
485
|
+
/** Runs when leaving the current location; `false` stays. */
|
|
486
|
+
beforeLeave(guard: LeaveGuard): () => void;
|
|
487
|
+
/** Settles when the first navigation has. */
|
|
488
|
+
ready(): Promise<void>;
|
|
489
|
+
/** What a server sends to the browser so the first navigation needs neither `load` nor `preload` again. */
|
|
490
|
+
snapshot(options?: {
|
|
491
|
+
sync?: boolean | string[];
|
|
492
|
+
}): RouterSnapshot;
|
|
493
|
+
destroy(): void;
|
|
494
|
+
}
|
|
495
|
+
declare function createRouter(options: RouterOptions): Router;
|
|
496
|
+
|
|
497
|
+
export { stringifyQuery as $, type AnyRoute as A, type SlashHistoryOptions as B, type SpecOut as C, type StandardSchemaV1 as D, type StoreLike as E, type Field as F, type Guard as G, type HashCodec as H, type StringOptions as I, type SubscribeOptions as J, createRouter as K, type LeaveGuard as L, type Match as M, type NavFn as N, defineRoute as O, type Params as P, defineRoutes as Q, type Router as R, type SchemaSpec as S, hashHistory as T, type UrlOptions as U, matchPath as V, memoryHistory as W, p as X, parseQuery as Y, redirect as Z, slashHistory as _, type RouterState as a, type RouteParams as b, type RouteQuery as c, type RouterSnapshot as d, type ArrayOptions as e, type GuardResult as f, type HashHistoryOptions as g, type HistoryDriver as h, type HistoryLocation as i, type LeaveContext as j, type NavigateOptions as k, type NavigationResult as l, type NumberOptions as m, type Parsed as n, type PathParams as o, type Raw as p, Redirect as q, type Route as r, type RouteArgs as s, type RouteContext as t, type RouteDef as u, type RouteError as v, type RouteLocation as w, RouteRecord as x, type RouteSlice as y, type RouterOptions as z };
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { JSX } from '@surgijs/core';
|
|
2
|
+
import { R as Router } from './router-ClmQESkC.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Wraps a server view so the routed components under it can read `router`:
|
|
6
|
+
* `renderToString('x-app', withRouter(router, App))`. A router is made per request, so it
|
|
7
|
+
* cannot be a module value the server view imports.
|
|
8
|
+
*/
|
|
9
|
+
declare function withRouter<P>(router: Router, view: (props: P) => unknown): (props: P) => JSX.Element;
|
|
10
|
+
/** The `<script type="application/json">` that carries the router's data to `readSnapshot()` in the browser. */
|
|
11
|
+
declare function stateScript(router: Router, options?: {
|
|
12
|
+
sync?: boolean | string[];
|
|
13
|
+
id?: string;
|
|
14
|
+
}): string;
|
|
15
|
+
/**
|
|
16
|
+
* A `Response` for a rendered page: the status of the matched route, or a redirect with the status
|
|
17
|
+
* and location a guard, `redirect` or `load` chose. Wait for `router.ready()` before rendering.
|
|
18
|
+
*/
|
|
19
|
+
declare function routerResponse(router: Router, html: string | ReadableStream<Uint8Array>, init?: ResponseInit): Response;
|
|
20
|
+
|
|
21
|
+
export { routerResponse, stateScript, withRouter };
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { serializeSnapshot } from './chunk-LY6JNBHA.js';
|
|
2
|
+
import { RouterProvider } from './chunk-FRJIJ3K7.js';
|
|
3
|
+
import './chunk-W2SIXU6O.js';
|
|
4
|
+
|
|
5
|
+
// src/server.ts
|
|
6
|
+
function withRouter(router, view) {
|
|
7
|
+
return (props) => RouterProvider({ router, children: () => view(props) });
|
|
8
|
+
}
|
|
9
|
+
function stateScript(router, options = {}) {
|
|
10
|
+
return serializeSnapshot(router.snapshot({ sync: options.sync }), options.id);
|
|
11
|
+
}
|
|
12
|
+
function routerResponse(router, html, init = {}) {
|
|
13
|
+
const state = router.get();
|
|
14
|
+
const headers = new Headers(init.headers);
|
|
15
|
+
if (state.redirect) {
|
|
16
|
+
headers.set("location", router.href(state.url));
|
|
17
|
+
return new Response(null, { ...init, status: state.redirect.status, headers });
|
|
18
|
+
}
|
|
19
|
+
if (!headers.has("content-type")) headers.set("content-type", "text/html; charset=utf-8");
|
|
20
|
+
return new Response(html, { ...init, status: state.status, headers });
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { routerResponse, stateScript, withRouter };
|
|
24
|
+
//# sourceMappingURL=server.js.map
|
|
25
|
+
//# sourceMappingURL=server.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/server.ts"],"names":[],"mappings":";;;;;AAUO,SAAS,UAAA,CAAc,QAAgB,IAAA,EAAwD;AACpG,EAAA,OAAO,CAAC,KAAA,KAAU,cAAA,CAAe,EAAE,MAAA,EAAQ,UAAU,MAAM,IAAA,CAAK,KAAK,CAAA,EAAkB,CAAA;AACzF;AAGO,SAAS,WAAA,CAAY,MAAA,EAAgB,OAAA,GAAsD,EAAC,EAAW;AAC5G,EAAA,OAAO,iBAAA,CAAkB,MAAA,CAAO,QAAA,CAAS,EAAE,IAAA,EAAM,QAAQ,IAAA,EAAM,CAAA,EAAqB,OAAA,CAAQ,EAAE,CAAA;AAChG;AAMO,SAAS,cAAA,CAAe,MAAA,EAAgB,IAAA,EAA2C,IAAA,GAAqB,EAAC,EAAa;AAC3H,EAAA,MAAM,KAAA,GAAQ,OAAO,GAAA,EAAI;AACzB,EAAA,MAAM,OAAA,GAAU,IAAI,OAAA,CAAQ,IAAA,CAAK,OAAO,CAAA;AACxC,EAAA,IAAI,MAAM,QAAA,EAAU;AAClB,IAAA,OAAA,CAAQ,IAAI,UAAA,EAAY,MAAA,CAAO,IAAA,CAAK,KAAA,CAAM,GAAG,CAAC,CAAA;AAC9C,IAAA,OAAO,IAAI,QAAA,CAAS,IAAA,EAAM,EAAE,GAAG,IAAA,EAAM,MAAA,EAAQ,KAAA,CAAM,QAAA,CAAS,MAAA,EAAQ,OAAA,EAAS,CAAA;AAAA,EAC/E;AACA,EAAA,IAAI,CAAC,QAAQ,GAAA,CAAI,cAAc,GAAG,OAAA,CAAQ,GAAA,CAAI,gBAAgB,0BAA0B,CAAA;AACxF,EAAA,OAAO,IAAI,QAAA,CAAS,IAAA,EAAM,EAAE,GAAG,MAAM,MAAA,EAAQ,KAAA,CAAM,MAAA,EAAQ,OAAA,EAAS,CAAA;AACtE","file":"server.js","sourcesContent":["import type { JSX } from '@surgijs/core';\nimport { RouterProvider } from './core';\nimport type { Router, RouterSnapshot } from './router';\nimport { serializeSnapshot } from './snapshot';\n\n/**\n * Wraps a server view so the routed components under it can read `router`:\n * `renderToString('x-app', withRouter(router, App))`. A router is made per request, so it\n * cannot be a module value the server view imports.\n */\nexport function withRouter<P>(router: Router, view: (props: P) => unknown): (props: P) => JSX.Element {\n return (props) => RouterProvider({ router, children: () => view(props) as JSX.Element });\n}\n\n/** The `<script type=\"application/json\">` that carries the router's data to `readSnapshot()` in the browser. */\nexport function stateScript(router: Router, options: { sync?: boolean | string[]; id?: string } = {}): string {\n return serializeSnapshot(router.snapshot({ sync: options.sync }) as RouterSnapshot, options.id);\n}\n\n/**\n * A `Response` for a rendered page: the status of the matched route, or a redirect with the status\n * and location a guard, `redirect` or `load` chose. Wait for `router.ready()` before rendering.\n */\nexport function routerResponse(router: Router, html: string | ReadableStream<Uint8Array>, init: ResponseInit = {}): Response {\n const state = router.get();\n const headers = new Headers(init.headers);\n if (state.redirect) {\n headers.set('location', router.href(state.url));\n return new Response(null, { ...init, status: state.redirect.status, headers });\n }\n if (!headers.has('content-type')) headers.set('content-type', 'text/html; charset=utf-8');\n return new Response(html, { ...init, status: state.status, headers });\n}\n"]}
|
package/dist/svelte.d.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { k as NavigateOptions, R as Router, A as AnyRoute, b as RouteParams, c as RouteQuery, a as RouterState } from './router-ClmQESkC.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The Svelte store contract: `subscribe(run)` calls `run` with the current value and returns an
|
|
5
|
+
* unsubscribe function. Compatible with `svelte/store`'s `Readable`, so `$value` and `derived()` work.
|
|
6
|
+
*/
|
|
7
|
+
interface RouterReadable<T> {
|
|
8
|
+
subscribe(run: (value: T) => void, invalidate?: () => void): () => void;
|
|
9
|
+
}
|
|
10
|
+
/** A router selector as a Svelte store. Each subscriber opens its own router subscription. */
|
|
11
|
+
declare function fromRouter<T>(router: Router, selector: (state: RouterState) => T, equals?: (a: T, b: T) => boolean): RouterReadable<T>;
|
|
12
|
+
declare const fromLocation: (router: Router) => RouterReadable<{
|
|
13
|
+
url: string;
|
|
14
|
+
path: string;
|
|
15
|
+
search: string;
|
|
16
|
+
hash: string;
|
|
17
|
+
}>;
|
|
18
|
+
declare const fromParams: <R extends AnyRoute = AnyRoute>(router: Router, _route?: R) => RouterReadable<RouteParams<R>>;
|
|
19
|
+
declare const fromQuery: <R extends AnyRoute = AnyRoute>(router: Router, _route?: R) => RouterReadable<RouteQuery<R>>;
|
|
20
|
+
declare const fromPending: (router: Router) => RouterReadable<boolean>;
|
|
21
|
+
/** What `load` returned for the route at `level` (the leaf when omitted). */
|
|
22
|
+
declare const fromLoaderData: <T = unknown>(router: Router, level?: number) => RouterReadable<T | undefined>;
|
|
23
|
+
interface OutletView {
|
|
24
|
+
/** The component to render: the route's, or its `errorComponent` after a failure. */
|
|
25
|
+
component: unknown;
|
|
26
|
+
/** Props for it: `{ error }` for an error component. */
|
|
27
|
+
props: Record<string, unknown>;
|
|
28
|
+
/** The depth for the outlet inside it. */
|
|
29
|
+
next: number;
|
|
30
|
+
/** The failure this outlet shows; `component` is `null` when the route has no error component. */
|
|
31
|
+
error?: unknown;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* What the outlet at `level` renders: the component of the route at that depth, or its error component.
|
|
35
|
+
* On a failure without an error component `component` is `null` and `error` is set. `null` when no route matches there.
|
|
36
|
+
*/
|
|
37
|
+
declare function fromOutlet(router: Router, level?: number): RouterReadable<OutletView | null>;
|
|
38
|
+
interface LinkActionOptions extends NavigateOptions {
|
|
39
|
+
router: Router;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* `use:link={{ router }}` on an `<a href>`: a plain click navigates through the router. Not needed
|
|
43
|
+
* while the router intercepts clicks itself (the default for browser histories).
|
|
44
|
+
*/
|
|
45
|
+
declare function link(node: HTMLAnchorElement, options: LinkActionOptions): {
|
|
46
|
+
update(next: LinkActionOptions): void;
|
|
47
|
+
destroy(): void;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export { type LinkActionOptions, type OutletView, type RouterReadable, fromLoaderData, fromLocation, fromOutlet, fromParams, fromPending, fromQuery, fromRouter, link };
|
package/dist/svelte.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { isPlainClick, isNativeAnchor } from './chunk-OQUB6NVM.js';
|
|
2
|
+
import { deepEqual } from './chunk-W2SIXU6O.js';
|
|
3
|
+
|
|
4
|
+
// src/svelte.ts
|
|
5
|
+
function fromRouter(router, selector, equals = deepEqual) {
|
|
6
|
+
return {
|
|
7
|
+
subscribe(run) {
|
|
8
|
+
const stop = router.subscribe(selector, (value) => run(value), { equals });
|
|
9
|
+
try {
|
|
10
|
+
run(router.get(selector));
|
|
11
|
+
} catch (error) {
|
|
12
|
+
stop();
|
|
13
|
+
throw error;
|
|
14
|
+
}
|
|
15
|
+
return stop;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
var fromLocation = (router) => fromRouter(router, (s) => ({ url: s.url, path: s.path, search: s.search, hash: s.hash }));
|
|
20
|
+
var fromParams = (router, _route) => fromRouter(router, (s) => s.params);
|
|
21
|
+
var fromQuery = (router, _route) => fromRouter(router, (s) => s.query);
|
|
22
|
+
var fromPending = (router) => fromRouter(router, (s) => s.isPending);
|
|
23
|
+
var fromLoaderData = (router, level) => fromRouter(router, (s) => s.data[level ?? s.data.length - 1]);
|
|
24
|
+
function fromOutlet(router, level = 0) {
|
|
25
|
+
return fromRouter(
|
|
26
|
+
router,
|
|
27
|
+
(s) => {
|
|
28
|
+
const rec = s.matches[level]?.route;
|
|
29
|
+
if (!rec) return null;
|
|
30
|
+
const err = s.error && s.error.at === level ? s.error : null;
|
|
31
|
+
return {
|
|
32
|
+
component: (err ? rec.errorComponent : rec.component) ?? null,
|
|
33
|
+
props: err ? { error: err.error } : {},
|
|
34
|
+
next: level + 1,
|
|
35
|
+
...err && { error: err.error }
|
|
36
|
+
};
|
|
37
|
+
},
|
|
38
|
+
(a, b) => a === null || b === null ? a === b : a.component === b.component && a.next === b.next && a.props.error === b.props.error
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
function link(node, options) {
|
|
42
|
+
let current = options;
|
|
43
|
+
const onClick = (e) => {
|
|
44
|
+
if (e.defaultPrevented || !isPlainClick(e) || isNativeAnchor(node)) return;
|
|
45
|
+
const url = current.router.history.fromHref(new URL(node.href, location.href));
|
|
46
|
+
if (url === null) return;
|
|
47
|
+
e.preventDefault();
|
|
48
|
+
const { router, ...rest } = current;
|
|
49
|
+
router.navigate(url, rest).catch((err) => console.error("[@surgijs/router]", err));
|
|
50
|
+
};
|
|
51
|
+
node.addEventListener("click", onClick);
|
|
52
|
+
return {
|
|
53
|
+
update: (next) => void (current = next),
|
|
54
|
+
destroy: () => node.removeEventListener("click", onClick)
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export { fromLoaderData, fromLocation, fromOutlet, fromParams, fromPending, fromQuery, fromRouter, link };
|
|
59
|
+
//# sourceMappingURL=svelte.js.map
|
|
60
|
+
//# sourceMappingURL=svelte.js.map
|