@tanstack/router-core 0.0.1-beta.5 → 0.0.1-beta.50
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/build/cjs/actions.js +94 -0
- package/build/cjs/actions.js.map +1 -0
- package/build/cjs/history.js +163 -0
- package/build/cjs/history.js.map +1 -0
- package/build/cjs/{packages/router-core/src/index.js → index.js} +26 -11
- package/build/cjs/{packages/router-core/src/index.js.map → index.js.map} +1 -1
- package/build/cjs/interop.js +175 -0
- package/build/cjs/interop.js.map +1 -0
- package/build/cjs/{packages/router-core/src/path.js → path.js} +23 -48
- package/build/cjs/path.js.map +1 -0
- package/build/cjs/{packages/router-core/src/qss.js → qss.js} +8 -13
- package/build/cjs/qss.js.map +1 -0
- package/build/cjs/route.js +33 -0
- package/build/cjs/route.js.map +1 -0
- package/build/cjs/{packages/router-core/src/routeConfig.js → routeConfig.js} +13 -18
- package/build/cjs/routeConfig.js.map +1 -0
- package/build/cjs/routeMatch.js +237 -0
- package/build/cjs/routeMatch.js.map +1 -0
- package/build/cjs/router.js +821 -0
- package/build/cjs/router.js.map +1 -0
- package/build/cjs/{packages/router-core/src/searchParams.js → searchParams.js} +10 -12
- package/build/cjs/searchParams.js.map +1 -0
- package/build/cjs/store.js +54 -0
- package/build/cjs/store.js.map +1 -0
- package/build/cjs/utils.js +47 -0
- package/build/cjs/utils.js.map +1 -0
- package/build/esm/index.js +1384 -2059
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +59 -49
- package/build/stats-react.json +248 -193
- package/build/types/index.d.ts +385 -317
- package/build/umd/index.development.js +1486 -2142
- 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 +6 -4
- package/src/actions.ts +157 -0
- package/src/frameworks.ts +2 -2
- package/src/history.ts +199 -0
- package/src/index.ts +4 -7
- package/src/interop.ts +169 -0
- package/src/link.ts +87 -44
- package/src/path.ts +12 -8
- package/src/route.ts +36 -229
- package/src/routeConfig.ts +99 -102
- package/src/routeInfo.ts +28 -25
- package/src/routeMatch.ts +294 -322
- package/src/router.ts +1047 -884
- package/src/store.ts +52 -0
- package/src/utils.ts +14 -72
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +0 -33
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +0 -1
- package/build/cjs/node_modules/@babel/runtime/helpers/esm/extends.js +0 -33
- package/build/cjs/node_modules/@babel/runtime/helpers/esm/extends.js.map +0 -1
- package/build/cjs/node_modules/history/index.js +0 -815
- package/build/cjs/node_modules/history/index.js.map +0 -1
- package/build/cjs/node_modules/tiny-invariant/dist/esm/tiny-invariant.js +0 -30
- package/build/cjs/node_modules/tiny-invariant/dist/esm/tiny-invariant.js.map +0 -1
- package/build/cjs/packages/router-core/src/path.js.map +0 -1
- package/build/cjs/packages/router-core/src/qss.js.map +0 -1
- package/build/cjs/packages/router-core/src/route.js +0 -161
- package/build/cjs/packages/router-core/src/route.js.map +0 -1
- package/build/cjs/packages/router-core/src/routeConfig.js.map +0 -1
- package/build/cjs/packages/router-core/src/routeMatch.js +0 -266
- package/build/cjs/packages/router-core/src/routeMatch.js.map +0 -1
- package/build/cjs/packages/router-core/src/router.js +0 -797
- package/build/cjs/packages/router-core/src/router.js.map +0 -1
- package/build/cjs/packages/router-core/src/searchParams.js.map +0 -1
- package/build/cjs/packages/router-core/src/utils.js +0 -118
- package/build/cjs/packages/router-core/src/utils.js.map +0 -1
package/src/link.ts
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
|
-
import { AnyPathParams } from './routeConfig'
|
|
2
1
|
import {
|
|
3
2
|
AnyAllRouteInfo,
|
|
4
3
|
DefaultAllRouteInfo,
|
|
5
4
|
RouteInfoByPath,
|
|
6
5
|
} from './routeInfo'
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
6
|
+
import { ParsedLocation, LocationState } from './router'
|
|
7
|
+
import {
|
|
8
|
+
Expand,
|
|
9
|
+
NoInfer,
|
|
10
|
+
PickRequired,
|
|
11
|
+
UnionToIntersection,
|
|
12
|
+
Updater,
|
|
13
|
+
} from './utils'
|
|
9
14
|
|
|
10
15
|
export type LinkInfo =
|
|
11
16
|
| {
|
|
@@ -14,7 +19,7 @@ export type LinkInfo =
|
|
|
14
19
|
}
|
|
15
20
|
| {
|
|
16
21
|
type: 'internal'
|
|
17
|
-
next:
|
|
22
|
+
next: ParsedLocation
|
|
18
23
|
handleFocus: (e: any) => void
|
|
19
24
|
handleClick: (e: any) => void
|
|
20
25
|
handleEnter: (e: any) => void
|
|
@@ -58,7 +63,7 @@ export type Split<S, TIncludeTrailingSlash = true> = S extends unknown
|
|
|
58
63
|
: never
|
|
59
64
|
|
|
60
65
|
export type ParsePathParams<T extends string> = Split<T>[number] extends infer U
|
|
61
|
-
? U extends
|
|
66
|
+
? U extends `$${infer V}`
|
|
62
67
|
? V
|
|
63
68
|
: never
|
|
64
69
|
: never
|
|
@@ -106,11 +111,20 @@ export type RelativeToPathAutoComplete<
|
|
|
106
111
|
]
|
|
107
112
|
? `${TTo}${Join<RestPath>}`
|
|
108
113
|
: never
|
|
109
|
-
:
|
|
114
|
+
:
|
|
115
|
+
| (TFrom extends `/`
|
|
116
|
+
? never
|
|
117
|
+
: SplitPaths extends [...Split<TFrom, false>, ...infer RestPath]
|
|
118
|
+
? Join<RestPath> extends { length: 0 }
|
|
119
|
+
? never
|
|
120
|
+
: './'
|
|
121
|
+
: never)
|
|
122
|
+
| (TFrom extends `/` ? never : '../')
|
|
123
|
+
| AllPaths
|
|
110
124
|
|
|
111
|
-
export type
|
|
125
|
+
export type NavigateOptions<
|
|
112
126
|
TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo,
|
|
113
|
-
TFrom extends
|
|
127
|
+
TFrom extends TAllRouteInfo['routePaths'] = '/',
|
|
114
128
|
TTo extends string = '.',
|
|
115
129
|
> = ToOptions<TAllRouteInfo, TFrom, TTo> & {
|
|
116
130
|
// Whether to replace the current history stack instead of pushing a new one
|
|
@@ -119,13 +133,15 @@ export type NavigateOptionsAbsolute<
|
|
|
119
133
|
|
|
120
134
|
export type ToOptions<
|
|
121
135
|
TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo,
|
|
122
|
-
TFrom extends
|
|
136
|
+
TFrom extends TAllRouteInfo['routePaths'] = '/',
|
|
123
137
|
TTo extends string = '.',
|
|
124
138
|
TResolvedTo = ResolveRelativePath<TFrom, NoInfer<TTo>>,
|
|
125
139
|
> = {
|
|
126
140
|
to?: ToPathOption<TAllRouteInfo, TFrom, TTo>
|
|
127
141
|
// The new has string or a function to update it
|
|
128
142
|
hash?: Updater<string>
|
|
143
|
+
// State to pass to the history stack
|
|
144
|
+
state?: LocationState
|
|
129
145
|
// The source route path. This is automatically set when using route-level APIs, but for type-safe relative routing on the router itself, this is required
|
|
130
146
|
from?: TFrom
|
|
131
147
|
// // When using relative route paths, this option forces resolution from the current path, instead of the route API's path or `from` path
|
|
@@ -134,57 +150,84 @@ export type ToOptions<
|
|
|
134
150
|
SearchParamOptions<TAllRouteInfo, TFrom, TResolvedTo> &
|
|
135
151
|
PathParamOptions<TAllRouteInfo, TFrom, TResolvedTo>
|
|
136
152
|
|
|
137
|
-
type SearchParamOptions<
|
|
153
|
+
export type SearchParamOptions<
|
|
138
154
|
TAllRouteInfo extends AnyAllRouteInfo,
|
|
139
155
|
TFrom,
|
|
140
156
|
TTo,
|
|
141
|
-
TFromSchema =
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
157
|
+
TFromSchema = Expand<
|
|
158
|
+
UnionToIntersection<
|
|
159
|
+
TAllRouteInfo['fullSearchSchema'] &
|
|
160
|
+
RouteInfoByPath<TAllRouteInfo, TFrom> extends never
|
|
161
|
+
? {}
|
|
162
|
+
: RouteInfoByPath<TAllRouteInfo, TFrom>['fullSearchSchema']
|
|
163
|
+
>
|
|
164
|
+
>,
|
|
165
|
+
// Find the schema for the new path, and make optional any keys
|
|
166
|
+
// that are already defined in the current schema
|
|
167
|
+
TToSchema = Partial<
|
|
168
|
+
RouteInfoByPath<TAllRouteInfo, TFrom>['fullSearchSchema']
|
|
169
|
+
> &
|
|
170
|
+
Omit<
|
|
171
|
+
RouteInfoByPath<TAllRouteInfo, TTo>['fullSearchSchema'],
|
|
172
|
+
keyof PickRequired<
|
|
173
|
+
RouteInfoByPath<TAllRouteInfo, TFrom>['fullSearchSchema']
|
|
174
|
+
>
|
|
175
|
+
>,
|
|
176
|
+
TFromFullSchema = Expand<
|
|
177
|
+
UnionToIntersection<TAllRouteInfo['fullSearchSchema'] & TFromSchema>
|
|
178
|
+
>,
|
|
179
|
+
TToFullSchema = Expand<
|
|
180
|
+
UnionToIntersection<TAllRouteInfo['fullSearchSchema'] & TToSchema>
|
|
181
|
+
>,
|
|
182
|
+
> = keyof PickRequired<TToSchema> extends never
|
|
149
183
|
? {
|
|
150
|
-
search?: SearchReducer<
|
|
184
|
+
search?: true | SearchReducer<TFromFullSchema, TToFullSchema>
|
|
151
185
|
}
|
|
152
186
|
: {
|
|
153
|
-
|
|
154
|
-
search: SearchReducer<TFromSchema, TToSchema>
|
|
187
|
+
search: SearchReducer<TFromFullSchema, TToFullSchema>
|
|
155
188
|
}
|
|
156
189
|
|
|
157
190
|
type SearchReducer<TFrom, TTo> =
|
|
158
191
|
| { [TKey in keyof TTo]: TTo[TKey] }
|
|
159
192
|
| ((current: TFrom) => TTo)
|
|
160
193
|
|
|
161
|
-
type PathParamOptions<
|
|
194
|
+
export type PathParamOptions<
|
|
162
195
|
TAllRouteInfo extends AnyAllRouteInfo,
|
|
163
196
|
TFrom,
|
|
164
197
|
TTo,
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
>
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
198
|
+
TFromSchema = Expand<
|
|
199
|
+
UnionToIntersection<
|
|
200
|
+
RouteInfoByPath<TAllRouteInfo, TFrom> extends never
|
|
201
|
+
? {}
|
|
202
|
+
: RouteInfoByPath<TAllRouteInfo, TFrom>['allParams']
|
|
203
|
+
>
|
|
204
|
+
>,
|
|
205
|
+
// Find the schema for the new path, and make optional any keys
|
|
206
|
+
// that are already defined in the current schema
|
|
207
|
+
TToSchema = Partial<RouteInfoByPath<TAllRouteInfo, TFrom>['allParams']> &
|
|
208
|
+
Omit<
|
|
209
|
+
RouteInfoByPath<TAllRouteInfo, TTo>['allParams'],
|
|
210
|
+
keyof PickRequired<RouteInfoByPath<TAllRouteInfo, TFrom>['allParams']>
|
|
211
|
+
>,
|
|
212
|
+
TFromFullParams = Expand<
|
|
213
|
+
UnionToIntersection<TAllRouteInfo['allParams'] & TFromSchema>
|
|
214
|
+
>,
|
|
215
|
+
TToFullParams = Expand<
|
|
216
|
+
UnionToIntersection<TAllRouteInfo['allParams'] & TToSchema>
|
|
217
|
+
>,
|
|
218
|
+
> = keyof PickRequired<TToSchema> extends never
|
|
219
|
+
? {
|
|
220
|
+
params?: ParamsReducer<TFromFullParams, TToFullParams>
|
|
221
|
+
}
|
|
222
|
+
: {
|
|
223
|
+
params: ParamsReducer<TFromFullParams, TToFullParams>
|
|
224
|
+
}
|
|
182
225
|
|
|
183
226
|
type ParamsReducer<TFrom, TTo> = TTo | ((current: TFrom) => TTo)
|
|
184
227
|
|
|
185
228
|
export type ToPathOption<
|
|
186
229
|
TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo,
|
|
187
|
-
TFrom extends
|
|
230
|
+
TFrom extends TAllRouteInfo['routePaths'] = '/',
|
|
188
231
|
TTo extends string = '.',
|
|
189
232
|
> =
|
|
190
233
|
| TTo
|
|
@@ -196,7 +239,7 @@ export type ToPathOption<
|
|
|
196
239
|
|
|
197
240
|
export type ToIdOption<
|
|
198
241
|
TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo,
|
|
199
|
-
TFrom extends
|
|
242
|
+
TFrom extends TAllRouteInfo['routePaths'] = '/',
|
|
200
243
|
TTo extends string = '.',
|
|
201
244
|
> =
|
|
202
245
|
| TTo
|
|
@@ -206,16 +249,16 @@ export type ToIdOption<
|
|
|
206
249
|
NoInfer<TTo> & string
|
|
207
250
|
>
|
|
208
251
|
|
|
209
|
-
interface ActiveOptions {
|
|
252
|
+
export interface ActiveOptions {
|
|
210
253
|
exact?: boolean
|
|
211
254
|
includeHash?: boolean
|
|
212
255
|
}
|
|
213
256
|
|
|
214
257
|
export type LinkOptions<
|
|
215
258
|
TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo,
|
|
216
|
-
TFrom extends
|
|
259
|
+
TFrom extends TAllRouteInfo['routePaths'] = '/',
|
|
217
260
|
TTo extends string = '.',
|
|
218
|
-
> =
|
|
261
|
+
> = NavigateOptions<TAllRouteInfo, TFrom, TTo> & {
|
|
219
262
|
// The standard anchor tag target attribute
|
|
220
263
|
target?: HTMLAnchorElement['target']
|
|
221
264
|
// Defaults to `{ exact: false, includeHash: false }`
|
package/src/path.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import invariant from 'tiny-invariant'
|
|
1
2
|
import { AnyPathParams } from './routeConfig'
|
|
2
3
|
import { MatchLocation } from './router'
|
|
3
4
|
import { last } from './utils'
|
|
@@ -97,7 +98,7 @@ export function parsePathname(pathname?: string): Segment[] {
|
|
|
97
98
|
}
|
|
98
99
|
}
|
|
99
100
|
|
|
100
|
-
if (part.charAt(0) === '
|
|
101
|
+
if (part.charAt(0) === '$') {
|
|
101
102
|
return {
|
|
102
103
|
type: 'param',
|
|
103
104
|
value: part,
|
|
@@ -145,29 +146,32 @@ export function interpolatePath(
|
|
|
145
146
|
}
|
|
146
147
|
|
|
147
148
|
export function matchPathname(
|
|
149
|
+
basepath: string,
|
|
148
150
|
currentPathname: string,
|
|
149
151
|
matchLocation: Pick<MatchLocation, 'to' | 'fuzzy' | 'caseSensitive'>,
|
|
150
152
|
): AnyPathParams | undefined {
|
|
151
|
-
const pathParams = matchByPath(currentPathname, matchLocation)
|
|
153
|
+
const pathParams = matchByPath(basepath, currentPathname, matchLocation)
|
|
152
154
|
// const searchMatched = matchBySearch(currentLocation.search, matchLocation)
|
|
153
155
|
|
|
154
156
|
if (matchLocation.to && !pathParams) {
|
|
155
157
|
return
|
|
156
158
|
}
|
|
157
159
|
|
|
158
|
-
// if (matchLocation.search && !searchMatched) {
|
|
159
|
-
// return
|
|
160
|
-
// }
|
|
161
|
-
|
|
162
160
|
return pathParams ?? {}
|
|
163
161
|
}
|
|
164
162
|
|
|
165
163
|
export function matchByPath(
|
|
164
|
+
basepath: string,
|
|
166
165
|
from: string,
|
|
167
166
|
matchLocation: Pick<MatchLocation, 'to' | 'caseSensitive' | 'fuzzy'>,
|
|
168
167
|
): Record<string, string> | undefined {
|
|
168
|
+
if (!from.startsWith(basepath)) {
|
|
169
|
+
return undefined
|
|
170
|
+
}
|
|
171
|
+
from = basepath != '/' ? from.substring(basepath.length) : from
|
|
169
172
|
const baseSegments = parsePathname(from)
|
|
170
|
-
const
|
|
173
|
+
const to = `${matchLocation.to ?? '*'}`
|
|
174
|
+
const routeSegments = parsePathname(to)
|
|
171
175
|
|
|
172
176
|
const params: Record<string, string> = {}
|
|
173
177
|
|
|
@@ -219,7 +223,7 @@ export function matchByPath(
|
|
|
219
223
|
if (baseSegment?.value === '/') {
|
|
220
224
|
return false
|
|
221
225
|
}
|
|
222
|
-
if (
|
|
226
|
+
if (baseSegment.value.charAt(0) !== '$') {
|
|
223
227
|
params[routeSegment.value.substring(1)] = baseSegment.value
|
|
224
228
|
}
|
|
225
229
|
}
|
package/src/route.ts
CHANGED
|
@@ -1,243 +1,50 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
LinkInfo,
|
|
4
|
-
LinkOptions,
|
|
5
|
-
ResolveRelativePath,
|
|
6
|
-
ToOptions,
|
|
7
|
-
} from './link'
|
|
8
|
-
import { LoaderContext, RouteConfig, RouteOptions } from './routeConfig'
|
|
1
|
+
import { Action, ActionOptions } from './actions'
|
|
2
|
+
import { RouteConfig, RouteOptions } from './routeConfig'
|
|
9
3
|
import {
|
|
10
4
|
AnyAllRouteInfo,
|
|
11
5
|
AnyRouteInfo,
|
|
12
6
|
DefaultAllRouteInfo,
|
|
13
7
|
RouteInfo,
|
|
14
|
-
RouteInfoByPath,
|
|
15
8
|
} from './routeInfo'
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
18
|
-
Action,
|
|
19
|
-
ActionState,
|
|
20
|
-
Loader,
|
|
21
|
-
LoaderState,
|
|
22
|
-
MatchRouteOptions,
|
|
23
|
-
Router,
|
|
24
|
-
} from './router'
|
|
25
|
-
import { NoInfer, replaceEqualDeep } from './utils'
|
|
9
|
+
import { Router } from './router'
|
|
26
10
|
|
|
27
|
-
export interface AnyRoute extends Route<any, any> {}
|
|
11
|
+
export interface AnyRoute extends Route<any, any, any> {}
|
|
28
12
|
|
|
29
|
-
export
|
|
13
|
+
export class Route<
|
|
30
14
|
TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo,
|
|
31
15
|
TRouteInfo extends AnyRouteInfo = RouteInfo,
|
|
16
|
+
TRouterContext = unknown,
|
|
32
17
|
> {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
18
|
+
routeInfo!: TRouteInfo
|
|
19
|
+
id!: TRouteInfo['id']
|
|
20
|
+
customId!: TRouteInfo['customId']
|
|
21
|
+
path!: TRouteInfo['path']
|
|
22
|
+
fullPath!: TRouteInfo['fullPath']
|
|
23
|
+
getParentRoute!: () => undefined | AnyRoute
|
|
38
24
|
childRoutes?: AnyRoute[]
|
|
39
|
-
options
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
?
|
|
64
|
-
| Action<TRouteInfo['actionPayload'], TRouteInfo['actionResponse']>
|
|
65
|
-
| undefined
|
|
66
|
-
: Action<TRouteInfo['actionPayload'], TRouteInfo['actionResponse']>
|
|
67
|
-
loader: unknown extends TRouteInfo['routeLoaderData']
|
|
68
|
-
?
|
|
69
|
-
| Action<
|
|
70
|
-
LoaderContext<
|
|
71
|
-
TRouteInfo['fullSearchSchema'],
|
|
72
|
-
TRouteInfo['allParams']
|
|
73
|
-
>,
|
|
74
|
-
TRouteInfo['routeLoaderData']
|
|
75
|
-
>
|
|
76
|
-
| undefined
|
|
77
|
-
: Loader<
|
|
78
|
-
TRouteInfo['fullSearchSchema'],
|
|
79
|
-
TRouteInfo['allParams'],
|
|
80
|
-
TRouteInfo['routeLoaderData']
|
|
81
|
-
>
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export function createRoute<
|
|
85
|
-
TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo,
|
|
86
|
-
TRouteInfo extends AnyRouteInfo = RouteInfo,
|
|
87
|
-
>(
|
|
88
|
-
routeConfig: RouteConfig,
|
|
89
|
-
options: TRouteInfo['options'],
|
|
90
|
-
parent: undefined | Route<TAllRouteInfo, any>,
|
|
91
|
-
router: Router<TAllRouteInfo['routeConfig'], TAllRouteInfo>,
|
|
92
|
-
): Route<TAllRouteInfo, TRouteInfo> {
|
|
93
|
-
const { id, routeId, path: routePath, fullPath } = routeConfig
|
|
94
|
-
|
|
95
|
-
const action =
|
|
96
|
-
router.state.actions[id] ||
|
|
97
|
-
(() => {
|
|
98
|
-
router.state.actions[id] = {
|
|
99
|
-
pending: [],
|
|
100
|
-
submit: async <T, U>(
|
|
101
|
-
submission: T,
|
|
102
|
-
actionOpts?: { invalidate?: boolean },
|
|
103
|
-
) => {
|
|
104
|
-
if (!route) {
|
|
105
|
-
return
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
const invalidate = actionOpts?.invalidate ?? true
|
|
109
|
-
|
|
110
|
-
const actionState: ActionState<T, U> = {
|
|
111
|
-
submittedAt: Date.now(),
|
|
112
|
-
status: 'pending',
|
|
113
|
-
submission,
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
action.current = actionState
|
|
117
|
-
action.latest = actionState
|
|
118
|
-
action.pending.push(actionState)
|
|
119
|
-
|
|
120
|
-
router.state = {
|
|
121
|
-
...router.state,
|
|
122
|
-
currentAction: actionState,
|
|
123
|
-
latestAction: actionState,
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
router.notify()
|
|
127
|
-
|
|
128
|
-
try {
|
|
129
|
-
const res = await route.options.action?.(submission)
|
|
130
|
-
actionState.data = res as U
|
|
131
|
-
if (invalidate) {
|
|
132
|
-
router.invalidateRoute({ to: '.', fromCurrent: true })
|
|
133
|
-
await router.reload()
|
|
134
|
-
}
|
|
135
|
-
actionState.status = 'success'
|
|
136
|
-
return res
|
|
137
|
-
} catch (err) {
|
|
138
|
-
console.error(err)
|
|
139
|
-
actionState.error = err
|
|
140
|
-
actionState.status = 'error'
|
|
141
|
-
} finally {
|
|
142
|
-
action.pending = action.pending.filter((d) => d !== actionState)
|
|
143
|
-
router.removeActionQueue.push({ action, actionState })
|
|
144
|
-
router.notify()
|
|
145
|
-
}
|
|
146
|
-
},
|
|
147
|
-
}
|
|
148
|
-
return router.state.actions[id]!
|
|
149
|
-
})()
|
|
150
|
-
|
|
151
|
-
const loader =
|
|
152
|
-
router.state.loaders[id] ||
|
|
153
|
-
(() => {
|
|
154
|
-
router.state.loaders[id] = {
|
|
155
|
-
pending: [],
|
|
156
|
-
fetch: (async (loaderContext: LoaderContext<any, any>) => {
|
|
157
|
-
if (!route) {
|
|
158
|
-
return
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
const loaderState: LoaderState<any, any> = {
|
|
162
|
-
loadedAt: Date.now(),
|
|
163
|
-
loaderContext,
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
loader.current = loaderState
|
|
167
|
-
loader.latest = loaderState
|
|
168
|
-
loader.pending.push(loaderState)
|
|
169
|
-
|
|
170
|
-
// router.state = {
|
|
171
|
-
// ...router.state,
|
|
172
|
-
// currentAction: loaderState,
|
|
173
|
-
// latestAction: loaderState,
|
|
174
|
-
// }
|
|
175
|
-
|
|
176
|
-
router.notify()
|
|
177
|
-
|
|
178
|
-
try {
|
|
179
|
-
return await route.options.loader?.(loaderContext)
|
|
180
|
-
} finally {
|
|
181
|
-
loader.pending = loader.pending.filter((d) => d !== loaderState)
|
|
182
|
-
// router.removeActionQueue.push({ loader, loaderState })
|
|
183
|
-
router.notify()
|
|
184
|
-
}
|
|
185
|
-
}) as any,
|
|
186
|
-
}
|
|
187
|
-
return router.state.loaders[id]!
|
|
188
|
-
})()
|
|
189
|
-
|
|
190
|
-
let route: Route<TAllRouteInfo, TRouteInfo> = {
|
|
191
|
-
routeId: id,
|
|
192
|
-
routeRouteId: routeId,
|
|
193
|
-
routePath,
|
|
194
|
-
fullPath,
|
|
195
|
-
options,
|
|
196
|
-
router,
|
|
197
|
-
childRoutes: undefined!,
|
|
198
|
-
parentRoute: parent,
|
|
199
|
-
action,
|
|
200
|
-
loader: loader as any,
|
|
201
|
-
|
|
202
|
-
buildLink: (options) => {
|
|
203
|
-
return router.buildLink({
|
|
204
|
-
...options,
|
|
205
|
-
from: fullPath,
|
|
206
|
-
} as any) as any
|
|
207
|
-
},
|
|
208
|
-
|
|
209
|
-
navigate: (options) => {
|
|
210
|
-
return router.navigate({
|
|
211
|
-
...options,
|
|
212
|
-
from: fullPath,
|
|
213
|
-
} as any) as any
|
|
214
|
-
},
|
|
215
|
-
|
|
216
|
-
matchRoute: (matchLocation, opts) => {
|
|
217
|
-
return router.matchRoute(
|
|
218
|
-
{
|
|
219
|
-
...matchLocation,
|
|
220
|
-
from: fullPath,
|
|
221
|
-
} as any,
|
|
222
|
-
opts,
|
|
223
|
-
)
|
|
224
|
-
},
|
|
25
|
+
options!: RouteOptions
|
|
26
|
+
originalIndex!: number
|
|
27
|
+
getRouter!: () => Router<
|
|
28
|
+
TAllRouteInfo['routeConfig'],
|
|
29
|
+
TAllRouteInfo,
|
|
30
|
+
TRouterContext
|
|
31
|
+
>
|
|
32
|
+
constructor(
|
|
33
|
+
routeConfig: RouteConfig,
|
|
34
|
+
options: TRouteInfo['options'],
|
|
35
|
+
originalIndex: number,
|
|
36
|
+
parent: undefined | Route<TAllRouteInfo, any>,
|
|
37
|
+
router: Router<TAllRouteInfo['routeConfig'], TAllRouteInfo, TRouterContext>,
|
|
38
|
+
) {
|
|
39
|
+
Object.assign(this, {
|
|
40
|
+
...routeConfig,
|
|
41
|
+
originalIndex,
|
|
42
|
+
options,
|
|
43
|
+
getRouter: () => router,
|
|
44
|
+
childRoutes: undefined!,
|
|
45
|
+
getParentRoute: () => parent,
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
router.options.createRoute?.({ router, route: this })
|
|
225
49
|
}
|
|
226
|
-
|
|
227
|
-
router.options.createRoute?.({ router, route })
|
|
228
|
-
|
|
229
|
-
return route
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
export function cascadeLoaderData(matches: RouteMatch<any, any>[]) {
|
|
233
|
-
matches.forEach((match, index) => {
|
|
234
|
-
const parent = matches[index - 1]
|
|
235
|
-
|
|
236
|
-
if (parent) {
|
|
237
|
-
match.loaderData = replaceEqualDeep(match.loaderData, {
|
|
238
|
-
...parent.loaderData,
|
|
239
|
-
...match.routeLoaderData,
|
|
240
|
-
})
|
|
241
|
-
}
|
|
242
|
-
})
|
|
243
50
|
}
|