@tanstack/router-core 0.0.1-beta.21 → 0.0.1-beta.24
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/build/cjs/packages/router-core/src/path.js +1 -4
- package/build/cjs/packages/router-core/src/path.js.map +1 -1
- package/build/cjs/packages/router-core/src/route.js +1 -0
- package/build/cjs/packages/router-core/src/route.js.map +1 -1
- package/build/cjs/packages/router-core/src/routeConfig.js.map +1 -1
- package/build/cjs/packages/router-core/src/router.js +15 -2
- package/build/cjs/packages/router-core/src/router.js.map +1 -1
- package/build/cjs/packages/router-core/src/utils.js +0 -6
- package/build/cjs/packages/router-core/src/utils.js.map +1 -1
- package/build/esm/index.js +17 -12
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +134 -134
- package/build/types/index.d.ts +18 -7
- package/build/umd/index.development.js +17 -12
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +1 -1
- package/src/link.ts +3 -2
- package/src/path.ts +0 -4
- package/src/route.ts +6 -1
- package/src/routeConfig.ts +5 -1
- package/src/routeInfo.ts +4 -1
- package/src/router.ts +28 -5
- package/src/utils.ts +5 -5
package/package.json
CHANGED
package/src/link.ts
CHANGED
|
@@ -142,8 +142,9 @@ type SearchParamOptions<
|
|
|
142
142
|
TTo,
|
|
143
143
|
TFromSchema = RouteInfoByPath<TAllRouteInfo, TFrom>['fullSearchSchema'],
|
|
144
144
|
TToSchema = RouteInfoByPath<TAllRouteInfo, TTo>['fullSearchSchema'],
|
|
145
|
-
> = StartsWith<TFrom, TTo> extends true
|
|
146
|
-
?
|
|
145
|
+
> = StartsWith<TFrom, TTo> extends true
|
|
146
|
+
? // If the next route search extend or cover the from route, params will be optional
|
|
147
|
+
{
|
|
147
148
|
search?: SearchReducer<TFromSchema, TToSchema>
|
|
148
149
|
}
|
|
149
150
|
: // Optional search params? Allow it
|
package/src/path.ts
CHANGED
package/src/route.ts
CHANGED
|
@@ -29,6 +29,7 @@ export interface Route<
|
|
|
29
29
|
TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo,
|
|
30
30
|
TRouteInfo extends AnyRouteInfo = RouteInfo,
|
|
31
31
|
> {
|
|
32
|
+
routeInfo: TRouteInfo
|
|
32
33
|
routeId: TRouteInfo['id']
|
|
33
34
|
routeRouteId: TRouteInfo['routeId']
|
|
34
35
|
routePath: TRouteInfo['path']
|
|
@@ -56,7 +57,10 @@ export interface Route<
|
|
|
56
57
|
opts?: MatchRouteOptions,
|
|
57
58
|
) => RouteInfoByPath<TAllRouteInfo, TResolved>['allParams']
|
|
58
59
|
navigate: <TTo extends string = '.'>(
|
|
59
|
-
options: Omit<
|
|
60
|
+
options: Omit<
|
|
61
|
+
LinkOptions<TAllRouteInfo, TRouteInfo['fullPath'], TTo>,
|
|
62
|
+
'from'
|
|
63
|
+
>,
|
|
60
64
|
) => Promise<void>
|
|
61
65
|
action: unknown extends TRouteInfo['actionResponse']
|
|
62
66
|
?
|
|
@@ -185,6 +189,7 @@ export function createRoute<
|
|
|
185
189
|
})()
|
|
186
190
|
|
|
187
191
|
let route: Route<TAllRouteInfo, TRouteInfo> = {
|
|
192
|
+
routeInfo: undefined!,
|
|
188
193
|
routeId: id,
|
|
189
194
|
routeRouteId: routeId,
|
|
190
195
|
routePath,
|
package/src/routeConfig.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { ParsePathParams } from './link'
|
|
|
3
3
|
import { joinPaths, trimPath, trimPathRight } from './path'
|
|
4
4
|
import { RouteInfo } from './routeInfo'
|
|
5
5
|
import { RouteMatch } from './routeMatch'
|
|
6
|
+
import { RouterContext } from './router'
|
|
6
7
|
import {
|
|
7
8
|
DeepAwaited,
|
|
8
9
|
Expand,
|
|
@@ -127,10 +128,13 @@ export type RouteOptions<
|
|
|
127
128
|
// An asynchronous function made available to the route for performing asynchronous or mutative actions that
|
|
128
129
|
// might invalidate the route's data.
|
|
129
130
|
action?: ActionFn<TActionPayload, TActionResponse>
|
|
131
|
+
// This async function is called before a route is loaded. If an error is thrown, the navigation is cancelled.
|
|
132
|
+
// If you want to redirect instead, throw a call to the `router.navigate()` function
|
|
133
|
+
beforeLoad?: (opts: { context: RouterContext }) => Promise<void> | void
|
|
130
134
|
// This function is called
|
|
131
135
|
// when moving from an inactive state to an active one. Likewise, when moving from
|
|
132
136
|
// an active to an inactive state, the return function (if provided) is called.
|
|
133
|
-
|
|
137
|
+
onLoaded?: (matchContext: {
|
|
134
138
|
params: TAllParams
|
|
135
139
|
search: TFullSearchSchema
|
|
136
140
|
}) =>
|
package/src/routeInfo.ts
CHANGED
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
RouteConfig,
|
|
11
11
|
RouteOptions,
|
|
12
12
|
} from './routeConfig'
|
|
13
|
-
import { IsAny, Values } from './utils'
|
|
13
|
+
import { IsAny, UnionToIntersection, Values } from './utils'
|
|
14
14
|
|
|
15
15
|
export interface AnyAllRouteInfo {
|
|
16
16
|
routeConfig: AnyRouteConfig
|
|
@@ -19,6 +19,7 @@ export interface AnyAllRouteInfo {
|
|
|
19
19
|
routeInfoByFullPath: Record<string, AnyRouteInfo>
|
|
20
20
|
routeIds: any
|
|
21
21
|
routePaths: any
|
|
22
|
+
fullSearchSchema: Record<string, any>
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
export interface DefaultAllRouteInfo {
|
|
@@ -28,6 +29,7 @@ export interface DefaultAllRouteInfo {
|
|
|
28
29
|
routeInfoByFullPath: Record<string, RouteInfo>
|
|
29
30
|
routeIds: string
|
|
30
31
|
routePaths: string
|
|
32
|
+
fullSearchSchema: AnySearchSchema
|
|
31
33
|
}
|
|
32
34
|
|
|
33
35
|
export interface AllRouteInfo<TRouteConfig extends AnyRouteConfig = RouteConfig>
|
|
@@ -133,6 +135,7 @@ export interface RoutesInfoInner<
|
|
|
133
135
|
routeInfoByFullPath: TRouteInfoByFullPath
|
|
134
136
|
routeIds: keyof TRouteInfoById
|
|
135
137
|
routePaths: keyof TRouteInfoByFullPath
|
|
138
|
+
fullSearchSchema: Partial<UnionToIntersection<TRouteInfo['fullSearchSchema']>>
|
|
136
139
|
}
|
|
137
140
|
|
|
138
141
|
export interface AnyRouteInfo
|
package/src/router.ts
CHANGED
|
@@ -172,9 +172,12 @@ export interface LoaderState<
|
|
|
172
172
|
loaderContext: LoaderContext<TFullSearchSchema, TAllParams>
|
|
173
173
|
}
|
|
174
174
|
|
|
175
|
-
export interface RouterState
|
|
175
|
+
export interface RouterState<
|
|
176
|
+
TSearchObj extends AnySearchSchema = {},
|
|
177
|
+
TState extends LocationState = LocationState,
|
|
178
|
+
> {
|
|
176
179
|
status: 'idle' | 'loading'
|
|
177
|
-
location: Location
|
|
180
|
+
location: Location<TSearchObj, TState>
|
|
178
181
|
matches: RouteMatch[]
|
|
179
182
|
lastUpdated: number
|
|
180
183
|
actions: Record<string, Action>
|
|
@@ -244,6 +247,8 @@ interface DehydratedRouteMatch
|
|
|
244
247
|
| 'invalidAt'
|
|
245
248
|
> {}
|
|
246
249
|
|
|
250
|
+
export interface RouterContext {}
|
|
251
|
+
|
|
247
252
|
export interface Router<
|
|
248
253
|
TRouteConfig extends AnyRouteConfig = RouteConfig,
|
|
249
254
|
TAllRouteInfo extends AnyAllRouteInfo = AllRouteInfo<TRouteConfig>,
|
|
@@ -263,11 +268,12 @@ export interface Router<
|
|
|
263
268
|
// Computed in this.update()
|
|
264
269
|
basepath: string
|
|
265
270
|
// Internal:
|
|
271
|
+
context: RouterContext
|
|
266
272
|
listeners: Listener[]
|
|
267
|
-
location: Location
|
|
273
|
+
location: Location<TAllRouteInfo['fullSearchSchema']>
|
|
268
274
|
navigateTimeout?: Timeout
|
|
269
275
|
nextAction?: 'push' | 'replace'
|
|
270
|
-
state: RouterState
|
|
276
|
+
state: RouterState<TAllRouteInfo['fullSearchSchema']>
|
|
271
277
|
routeTree: Route<TAllRouteInfo, RouteInfo>
|
|
272
278
|
routesById: RoutesById<TAllRouteInfo>
|
|
273
279
|
navigationPromise: Promise<void>
|
|
@@ -395,6 +401,7 @@ export function createRouter<
|
|
|
395
401
|
options: originalOptions,
|
|
396
402
|
listeners: [],
|
|
397
403
|
// Resolved after construction
|
|
404
|
+
context: {},
|
|
398
405
|
basepath: '',
|
|
399
406
|
routeTree: undefined!,
|
|
400
407
|
routesById: {} as any,
|
|
@@ -577,6 +584,22 @@ export function createRouter<
|
|
|
577
584
|
strictParseParams: true,
|
|
578
585
|
})
|
|
579
586
|
|
|
587
|
+
// Check if each match middleware to see if the route can be accessed
|
|
588
|
+
try {
|
|
589
|
+
await Promise.all(
|
|
590
|
+
matches.map((match) =>
|
|
591
|
+
match.options.beforeLoad?.({
|
|
592
|
+
context: router.context,
|
|
593
|
+
}),
|
|
594
|
+
),
|
|
595
|
+
)
|
|
596
|
+
} catch (err: any) {
|
|
597
|
+
if (err?.then) {
|
|
598
|
+
await new Promise(() => {})
|
|
599
|
+
}
|
|
600
|
+
throw err
|
|
601
|
+
}
|
|
602
|
+
|
|
580
603
|
if (typeof document !== 'undefined') {
|
|
581
604
|
router.state = {
|
|
582
605
|
...router.state,
|
|
@@ -655,7 +678,7 @@ export function createRouter<
|
|
|
655
678
|
})
|
|
656
679
|
|
|
657
680
|
entering.forEach((d) => {
|
|
658
|
-
d.__.onExit = d.options.
|
|
681
|
+
d.__.onExit = d.options.onLoaded?.({
|
|
659
682
|
params: d.params,
|
|
660
683
|
search: d.search,
|
|
661
684
|
})
|
package/src/utils.ts
CHANGED
|
@@ -24,11 +24,11 @@ export type Expand<T> = T extends object
|
|
|
24
24
|
: never
|
|
25
25
|
: T
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
27
|
+
export type UnionToIntersection<U> = (
|
|
28
|
+
U extends any ? (k: U) => void : never
|
|
29
|
+
) extends (k: infer I) => any
|
|
30
|
+
? I
|
|
31
|
+
: never
|
|
32
32
|
|
|
33
33
|
export type Values<O> = O[ValueKeys<O>]
|
|
34
34
|
export type ValueKeys<O> = Extract<keyof O, PropertyKey>
|