@stealthscale/provider-router 0.1.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,489 @@
1
+ import { ComponentProps, FunctionComponent, ReactNode } from "react";
2
+ export * from "@tanstack/react-router";
3
+ //#region src/declaration.d.ts
4
+ /**
5
+ * Loads a page on first navigation, rather than with the bundle that declared it.
6
+ *
7
+ * @remarks
8
+ * A wrapper rather than a bare function, because a React component is a function too and nothing
9
+ * at run time separates one from an importer.
10
+ */
11
+ interface LazyPage {
12
+ /**
13
+ * The export the page is published under. The module's default export where it states none.
14
+ */
15
+ readonly export?: string | undefined;
16
+ /**
17
+ * Imports the module holding the page.
18
+ */
19
+ readonly load: () => Promise<Readonly<Record<string, FunctionComponent>>>;
20
+ }
21
+ /**
22
+ * Reports one fault a schema found.
23
+ */
24
+ interface SearchIssue {
25
+ /**
26
+ * The fault, in words a person can read.
27
+ */
28
+ readonly message: string;
29
+ }
30
+ /**
31
+ * Reports that a value was refused, and why.
32
+ */
33
+ interface SearchRefused {
34
+ /**
35
+ * Each fault the schema found.
36
+ */
37
+ readonly issues: readonly SearchIssue[];
38
+ }
39
+ /**
40
+ * Reports that a value was accepted, and what it read as.
41
+ */
42
+ interface SearchRead {
43
+ /**
44
+ * Nothing, which is how a reader tells an acceptance from a refusal.
45
+ */
46
+ readonly issues?: undefined;
47
+ /**
48
+ * The value, as the schema read it.
49
+ */
50
+ readonly value: unknown;
51
+ }
52
+ /**
53
+ * Checks a value against a schema, in the shape Standard Schema settled on.
54
+ */
55
+ interface StandardSurface {
56
+ /**
57
+ * Checks a value and returns what it read, or why it refused.
58
+ */
59
+ readonly validate: (value: unknown) => Promise<SearchRead | SearchRefused> | SearchRead | SearchRefused;
60
+ }
61
+ /**
62
+ * Reads a search string into the values a route draws with.
63
+ *
64
+ * @remarks
65
+ * Stated structurally rather than by the library's own name, so a declaration carries a validator
66
+ * from any library that implements Standard Schema without this package depending on any of them.
67
+ */
68
+ interface SearchValidator {
69
+ /**
70
+ * The Standard Schema surface, which is the whole of what the library reads.
71
+ */
72
+ readonly "~standard": StandardSurface;
73
+ }
74
+ /**
75
+ * Draws a frame around whatever a route below it draws.
76
+ */
77
+ interface LayoutProps {
78
+ /**
79
+ * The route drawn inside the frame, which React supplies.
80
+ */
81
+ readonly children?: ReactNode | undefined;
82
+ /**
83
+ * The options the declaration stated for this layout, passed through untouched.
84
+ */
85
+ readonly options?: Readonly<Record<string, unknown>> | undefined;
86
+ }
87
+ /**
88
+ * Describes one route a host declares, in the form the compiler takes.
89
+ *
90
+ * @remarks
91
+ * The condition is generic because this package cannot read a session and does not know the
92
+ * language conditions are written in. A host states its own and supplies the evaluator that reads
93
+ * it.
94
+ */
95
+ interface RouteDeclaration<Condition = unknown> {
96
+ /**
97
+ * Draws the page, either directly or loaded on first navigation.
98
+ */
99
+ readonly component: FunctionComponent | LazyPage;
100
+ /**
101
+ * The id a host and a plugin refer to it by, which resolves to a path through the map.
102
+ */
103
+ readonly id: string;
104
+ /**
105
+ * The layouts it is drawn in, outermost first. Drawn bare where it names none.
106
+ */
107
+ readonly layout?: readonly string[] | undefined;
108
+ /**
109
+ * Passed to each layout untouched, for whatever the layout reads.
110
+ */
111
+ readonly layoutOptions?: Readonly<Record<string, unknown>> | undefined;
112
+ /**
113
+ * The menu entry a menu reads, which the compiler writes onto the route without reading.
114
+ */
115
+ readonly navigation?: unknown;
116
+ /**
117
+ * The pane it is drawn in, which this package refuses because it draws no panes.
118
+ */
119
+ readonly outlet?: string | undefined;
120
+ /**
121
+ * Another declaration it nests under, by id. The compiler's own parent where it names none.
122
+ */
123
+ readonly parent?: string | undefined;
124
+ /**
125
+ * The path pattern, relative to the parent, in the library's `$id` form.
126
+ */
127
+ readonly path: string;
128
+ /**
129
+ * A sample of the parameters, which a plugin's own tests open the page at.
130
+ */
131
+ readonly sample?: Readonly<Record<string, string>> | undefined;
132
+ /**
133
+ * Reads the search string this route draws with.
134
+ */
135
+ readonly search?: SearchValidator | undefined;
136
+ /**
137
+ * When it is routed at all. Routed always where it states none.
138
+ */
139
+ readonly when?: Condition | undefined;
140
+ }
141
+ /**
142
+ * Carries the name a route goes by, and whatever a menu reads off it.
143
+ *
144
+ * @remarks
145
+ * Written onto the route's `staticData`, which the library hands back on every match. A menu, a
146
+ * breadcrumb or a telemetry hook therefore reads the name off the page it is drawing rather than
147
+ * holding a second copy of the list.
148
+ */
149
+ interface DeclaredRoute {
150
+ /**
151
+ * The id the route is named under.
152
+ */
153
+ readonly id: string;
154
+ /**
155
+ * The menu entry the declaration carried, passed through untouched.
156
+ */
157
+ readonly navigation?: unknown;
158
+ }
159
+ /**
160
+ * Reports whether a route's condition holds for whoever is asking.
161
+ *
162
+ * @remarks
163
+ * Returning false makes the route a 404, because a route nobody may reach does not exist. An
164
+ * evaluator wanting anything else, such as sending an unauthenticated person to sign in, throws
165
+ * the library's own `redirect` instead.
166
+ */
167
+ type Evaluate<Condition = unknown> = (when: Condition) => boolean;
168
+ import * as import__tanstack_react_router from "@tanstack/react-router";
169
+ //#endregion
170
+ //#region src/compile.d.ts
171
+ /**
172
+ * Describes what one compilation needs beyond the declarations themselves.
173
+ */
174
+ interface CompileOptions<Condition = unknown> {
175
+ /**
176
+ * Draws a page whose own component threw or failed to load, named per declaration.
177
+ */
178
+ readonly errorComponent?: ((declaration: RouteDeclaration<Condition>) => tanstack_d_exports.ErrorRouteComponent) | undefined;
179
+ /**
180
+ * Reports whether a route's condition holds. Required where any declaration states one.
181
+ */
182
+ readonly evaluate?: Evaluate<Condition> | undefined;
183
+ /**
184
+ * The layouts a declaration may name.
185
+ */
186
+ readonly layouts?: Readonly<Record<string, FunctionComponent<LayoutProps>>> | undefined;
187
+ /**
188
+ * The route every compiled route hangs under, which this compilation never changes.
189
+ */
190
+ readonly parent: tanstack_d_exports.AnyRoute;
191
+ }
192
+ /**
193
+ * Compiles declarations into routes under the parent a caller states.
194
+ *
195
+ * @remarks
196
+ * Pure with respect to the caller. It creates routes and never mutates the parent, so a second
197
+ * call returns a second set of routes sharing no object with the first. That is what keeps two
198
+ * routers in one process from reading each other's tree through the library's process-wide cache.
199
+ * Compile every contributor's declarations in one call. Two calls under one parent cannot see
200
+ * each other's paths, and `routeMap` reports the collision once the tree is assembled.
201
+ * @param declarations - The routes to compile, in any order.
202
+ * @param options - The parent, and what the declarations may name.
203
+ * @returns The routes to place in the parent's own `addChildren` call.
204
+ * @throws {@link Error} Where two declarations share an id, where one names a parent or a layout
205
+ * nothing provides, where one names a layout a route above it already draws, where parents form a
206
+ * cycle, where one states a condition and no evaluator was given, or where one states an outlet.
207
+ */
208
+ export declare function compileRoutes<Condition = unknown>(declarations: ReadonlyArray<RouteDeclaration<Condition>>, options: CompileOptions<Condition>): readonly tanstack_d_exports.AnyRoute[];
209
+ //#endregion
210
+ //#region src/reference.d.ts
211
+ /**
212
+ * Points at a route without naming a path, a module or an address.
213
+ */
214
+ /**
215
+ * The parameters a route names where its reference states none.
216
+ */
217
+ type AnyParams = Readonly<Record<string, string>>;
218
+ /**
219
+ * Points at a route by the id it was declared under, carrying the parameters its path names.
220
+ *
221
+ * @remarks
222
+ * Stated structurally rather than imported, so a plugin SDK's own reference type satisfies it
223
+ * without this package depending on that SDK. A reference carries no path, because the path is
224
+ * whoever composed the application's to choose and a link written against one would move when
225
+ * they re-placed the route.
226
+ * The parameters live in the type alone. Nothing reads `~types` at run time, and it is there so
227
+ * a link to a route filling `id` is refused where the route names `invoice`.
228
+ */
229
+ interface RouteRef<Params extends AnyParams = AnyParams> {
230
+ /**
231
+ * The parameters, in the type alone.
232
+ */
233
+ readonly "~types"?: {
234
+ /**
235
+ * The parameters the route's path names, which a link fills.
236
+ */
237
+ readonly params: Params;
238
+ };
239
+ /**
240
+ * The full id the route was declared under.
241
+ */
242
+ readonly id: string;
243
+ }
244
+ /**
245
+ * Points at a route, either by a reference or by the bare id one carries.
246
+ */
247
+ type RouteTarget<Params extends AnyParams = AnyParams> = RouteRef<Params> | string;
248
+ //#endregion
249
+ //#region src/declared.d.ts
250
+ /**
251
+ * Describes the part of a match this package reads.
252
+ */
253
+ interface MatchedRoute {
254
+ /**
255
+ * The data the route was given to carry, which the library hands back untouched.
256
+ */
257
+ readonly staticData: object;
258
+ }
259
+ /**
260
+ * Reads the id and the menu entry a matched route carries, or nothing where it carries neither.
261
+ *
262
+ * @remarks
263
+ * A route carries its id in `staticData`, which is where the library keeps whatever a route wants
264
+ * to carry. The compiler writes it for a route built from a declaration and `namedRoute` writes
265
+ * it for one written in code, so a menu, a breadcrumb or a telemetry hook reads what it needs off
266
+ * the match either way. The shape is checked rather than trusted, because `staticData` is untyped
267
+ * by design and a route may carry anything under the same name. The checked object itself is
268
+ * returned, so every read of one route gives one reference.
269
+ * @param match - A match, as `useMatches` returns one.
270
+ * @returns The id and the menu entry it carries, or nothing.
271
+ */
272
+ export declare function declaredOf(match: MatchedRoute): DeclaredRoute | undefined;
273
+ /**
274
+ * Reads the name the deepest named route the page is on goes by.
275
+ *
276
+ * @remarks
277
+ * The deepest match rather than the first, because a named route nested under another is the page
278
+ * a person is looking at. A page drawn entirely from unnamed routes returns nothing.
279
+ * The selector returns the object the route carries rather than a copy of it. A route's static
280
+ * data is fixed once the tree is built, so the reference is the same one until a different route
281
+ * matches, and a navigation that left the page alone re-renders nothing.
282
+ * @returns The id and the menu entry, or nothing where no matched route carries either.
283
+ */
284
+ export declare function useDeclaredRoute(): DeclaredRoute | undefined;
285
+ /**
286
+ * Reads the parameters of the page being drawn, typed by the reference the caller holds.
287
+ *
288
+ * @remarks
289
+ * A compiled route is outside the tree an application registered, so `useParams` types against a
290
+ * tree the route is not in. The parameters are read off the match instead, and the reference
291
+ * carries their names. This checks at run time that the page really is the route the reference
292
+ * names before it makes the claim.
293
+ * The selector returns the id and the parameters alone, which are strings. Structural sharing
294
+ * compares them, so a navigation that changed neither re-renders nothing.
295
+ * @param to - A reference to the route the calling component draws.
296
+ * @returns The parameters the route's path named.
297
+ * @throws {@link Error} Where the page being drawn is not the route the reference names.
298
+ */
299
+ export declare function useRouteParams<Params extends AnyParams>(to: RouteRef<Params>): Params;
300
+ //#endregion
301
+ //#region src/defaults.d.ts
302
+ /**
303
+ * How a router in this design system behaves before an application says otherwise.
304
+ */
305
+ /**
306
+ * The options every application's router starts from, spread before its own.
307
+ *
308
+ * @remarks
309
+ * A route preloads on intent, which is when the pointer rests on a link or the link takes focus,
310
+ * so a page is usually loaded before it is asked for.
311
+ * A preloaded route never counts as fresh on its own. The query client decides freshness for
312
+ * everything it loads, and a router keeping its own staleness would serve a copy the client had
313
+ * already replaced.
314
+ * Scroll position is restored on the way back, because a person returning to a list expects to be
315
+ * where they left it rather than at the top.
316
+ */
317
+ export declare const routerDefaults: {
318
+ readonly defaultPreload: "intent";
319
+ readonly defaultPreloadStaleTime: 0;
320
+ readonly scrollRestoration: true;
321
+ };
322
+ //#endregion
323
+ //#region src/map.d.ts
324
+ /**
325
+ * Every id anything links by, against the route that draws it.
326
+ */
327
+ type RouteMap = ReadonlyMap<string, tanstack_d_exports.AnyRoute>;
328
+ /**
329
+ * Carries a route's name where the library hands it back on every match.
330
+ */
331
+ interface StaticName {
332
+ /**
333
+ * The static data the route carries, which `declaredOf` reads the name out of.
334
+ */
335
+ readonly staticData: Readonly<Record<"declared", DeclaredRoute>>;
336
+ }
337
+ /**
338
+ * Gives a route written in code the id everything else links to it by.
339
+ *
340
+ * @remarks
341
+ * A route carries its id in `staticData`, which is where the compiler writes the id of a route
342
+ * built from a declaration. One mechanism names both, so a link does not need to know which of
343
+ * the two it is pointing at.
344
+ * @param id - The id to name the route under.
345
+ * @returns The static data to spread into `createRoute`.
346
+ */
347
+ export declare function namedRoute(id: string): StaticName;
348
+ /**
349
+ * Reads every id a tree names, against the route that draws it.
350
+ *
351
+ * @remarks
352
+ * Derived from the tree rather than collected beside it, so a route written in code and a route
353
+ * built from a declaration reach the map the same way. The walk also reports two routes that
354
+ * serve one URL, which a development build reports as a duplicate route and a production build
355
+ * resolves by keeping the first and dropping the rest in silence.
356
+ * @param tree - The assembled tree, as `createRouter` takes it.
357
+ * @returns Every id, against its route.
358
+ * @throws {@link Error} Where two routes carry one id, or where two serve one path under a shared
359
+ * parent.
360
+ */
361
+ export declare function routeMap(tree: tanstack_d_exports.AnyRoute): RouteMap;
362
+ //#endregion
363
+ //#region src/href.d.ts
364
+ /**
365
+ * Resolves a declared id to a path, with the parameters the path names filled in.
366
+ *
367
+ * @remarks
368
+ * Throwing is deliberate in all three cases. Interpolation otherwise returns a path with a
369
+ * literal `$id` still in it, and a route the library has not worked a full path out for yet
370
+ * returns the site root. A link that navigates somewhere wrong is worse than one that fails where
371
+ * somebody wrote it.
372
+ * @param map - The map every route is named in.
373
+ * @param to - A reference to the route, or the bare id one carries.
374
+ * @param params - The parameters the path names, typed by the reference where one was given.
375
+ * @returns The path, ready to navigate to.
376
+ * @throws {@link Error} Where the map holds no such id, where no router has processed the tree the
377
+ * route was placed in, or where a parameter the path names is missing.
378
+ */
379
+ export declare function routeHref<Params extends AnyParams>(map: RouteMap, to: RouteTarget<Params>, params?: Params): string;
380
+ /**
381
+ * Reads the map every route is named in, from the router's own context.
382
+ *
383
+ * @remarks
384
+ * The map reaches a component through the router's own context rather than a provider of this
385
+ * package's own, so an application mounts nothing extra to make a link by id work.
386
+ * Take the map rather than a resolved path where the route is chosen after the render: a hook
387
+ * cannot run inside an event handler, and `routeHref` can.
388
+ * @returns Every id the router knows, against the route that serves it.
389
+ * @throws {@link Error} Where the router was built without one.
390
+ */
391
+ export declare function useRouteMap(): RouteMap;
392
+ /**
393
+ * Resolves a reference to a path, through the map the router context holds.
394
+ *
395
+ * @remarks
396
+ * A hook, so the route has to be known while the component renders. Use `useRouteMap` where the
397
+ * route is chosen later, such as in the handler of a row a person clicked.
398
+ * @param to - A reference to the route, or the bare id one carries.
399
+ * @param params - The parameters the path names, typed by the reference where one was given.
400
+ * @returns The path, ready to navigate to.
401
+ * @throws {@link Error} Where no map is in the router context, where it holds no such id, or where
402
+ * a parameter the path names is missing.
403
+ */
404
+ export declare function useRouteHref<Params extends AnyParams>(to: RouteTarget<Params>, params?: Params): string;
405
+ //#endregion
406
+ //#region src/link.d.ts
407
+ /**
408
+ * Describes a link to a declared route.
409
+ *
410
+ * @remarks
411
+ * Everything the library's own `Link` takes passes through, so a declared link is styled,
412
+ * labelled and given an active state the way any other link is. Only `to` and `params` are
413
+ * restated, because a declared id is not a path the compiler knows.
414
+ */
415
+ type RouteLinkProps<Params extends AnyParams = AnyParams> = {
416
+ /**
417
+ * The parameters the route's path names, typed by the reference where one was given.
418
+ */
419
+ readonly params?: Params | undefined;
420
+ /**
421
+ * A reference to the route, or the bare id one carries.
422
+ */
423
+ readonly to: RouteTarget<Params>;
424
+ } & Omit<ComponentProps<typeof tanstack_d_exports.Link>, "params" | "to">;
425
+ /**
426
+ * Links to a route by the id a manifest declared it under.
427
+ *
428
+ * @remarks
429
+ * A declared route is outside the tree an application registered, so `Link` refuses its path at
430
+ * compile time. This resolves the id at run time and hands the library a plain string, which is
431
+ * the one place in this package where a path is not checked by the compiler.
432
+ * @param props - The declared id, its parameters, and whatever else the library's `Link` takes.
433
+ * @returns An anchor to the resolved path.
434
+ */
435
+ export declare function RouteLink<Params extends AnyParams>({ params, to, ...rest }: RouteLinkProps<Params>): ReactNode;
436
+ //#endregion
437
+ //#region src/options.d.ts
438
+ /**
439
+ * Describes the one member this package expects a router's context to carry.
440
+ *
441
+ * @remarks
442
+ * A component resolves a declared id through the router's own context rather than through a
443
+ * provider of this package's own, so an application mounts nothing extra to make a declared link
444
+ * work.
445
+ */
446
+ interface RoutesContext {
447
+ /**
448
+ * Every declared id, against the route compiled for it.
449
+ */
450
+ readonly routes?: RouteMap | undefined;
451
+ }
452
+ /**
453
+ * Describes the options a caller spreads into `createRouter`.
454
+ */
455
+ type AppRouterOptions<Context extends object> = {
456
+ /**
457
+ * The context the router hands every loader and every route component.
458
+ */
459
+ readonly context: Context;
460
+ } & typeof routerDefaults;
461
+ /**
462
+ * Collects the options every router in this design system starts from.
463
+ *
464
+ * @remarks
465
+ * An object to spread rather than a factory that calls `createRouter`. The library states
466
+ * forty-eight router options, and a factory would expose the few it thought of and hide the rest.
467
+ * Spreading leaves every one of them the caller's to set, the four stated here included.
468
+ * @param context - The context the router hands a loader, holding the route map where a caller compiled
469
+ * declarations.
470
+ * @returns The options, to spread into `createRouter` before the caller's own.
471
+ */
472
+ export declare function routerOptions<Context extends object>(context: Context): AppRouterOptions<Context>;
473
+ //#endregion
474
+ //#region src/root.d.ts
475
+ /**
476
+ * Builds a root route whose context holds whatever the application states and the route map.
477
+ *
478
+ * @remarks
479
+ * Calling the library's `createRootRoute` instead leaves the context typed as empty, and the
480
+ * first sign of it is `createRouter` refusing the options `routerOptions` returned. The error
481
+ * then names the router rather than the root, which is the file that has to change.
482
+ * @returns The root route factory, to call with the route's own options.
483
+ */
484
+ export declare function createAppRootRoute<Context extends object = RoutesContext>(): ReturnType<typeof tanstack_d_exports.createRootRouteWithContext<Context>>;
485
+ declare namespace index_d_exports {
486
+ export { AnyParams, AppRouterOptions, CompileOptions, DeclaredRoute, Evaluate, LayoutProps, LazyPage, MatchedRoute, RouteDeclaration, RouteLink, RouteLinkProps, RouteMap, RouteRef, RouteTarget, RoutesContext, SearchValidator, StaticName, compileRoutes, createAppRootRoute, declaredOf, namedRoute, routeHref, routeMap, routerDefaults, routerOptions, useDeclaredRoute, useRouteHref, useRouteMap, useRouteParams };
487
+ }
488
+ //#endregion
489
+ export type { AnyParams, AppRouterOptions, CompileOptions, DeclaredRoute, Evaluate, LayoutProps, LazyPage, MatchedRoute, RouteDeclaration, RouteLinkProps, RouteMap, RouteRef, RouteTarget, RoutesContext, SearchValidator, StaticName };