-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathMatches.ts
More file actions
292 lines (269 loc) · 8.4 KB
/
Matches.ts
File metadata and controls
292 lines (269 loc) · 8.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import type { AnyRoute, StaticDataRouteOption } from './route'
import type {
AllContext,
AllLoaderData,
AllParams,
FullSearchSchema,
ParseRoute,
RouteById,
RouteIds,
} from './routeInfo'
import type { AnyRouter, RegisteredRouter, SSROption } from './router'
import type { Constrain, ControlledPromise } from './utils'
export type AnyMatchAndValue = { match: any; value: any }
export type FindValueByIndex<
TKey,
TValue extends ReadonlyArray<any>,
> = TKey extends `${infer TIndex extends number}` ? TValue[TIndex] : never
export type FindValueByKey<TKey, TValue> =
TValue extends ReadonlyArray<any>
? FindValueByIndex<TKey, TValue>
: TValue[TKey & keyof TValue]
export type CreateMatchAndValue<TMatch, TValue> = TValue extends any
? {
match: TMatch
value: TValue
}
: never
export type NextMatchAndValue<
TKey,
TMatchAndValue extends AnyMatchAndValue,
> = TMatchAndValue extends any
? CreateMatchAndValue<
TMatchAndValue['match'],
FindValueByKey<TKey, TMatchAndValue['value']>
>
: never
export type IsMatchKeyOf<TValue> =
TValue extends ReadonlyArray<any>
? number extends TValue['length']
? `${number}`
: keyof TValue & `${number}`
: TValue extends object
? keyof TValue & string
: never
export type IsMatchPath<
TParentPath extends string,
TMatchAndValue extends AnyMatchAndValue,
> = `${TParentPath}${IsMatchKeyOf<TMatchAndValue['value']>}`
export type IsMatchResult<
TKey,
TMatchAndValue extends AnyMatchAndValue,
> = TMatchAndValue extends any
? TKey extends keyof TMatchAndValue['value']
? TMatchAndValue['match']
: never
: never
export type IsMatchParse<
TPath,
TMatchAndValue extends AnyMatchAndValue,
TParentPath extends string = '',
> = TPath extends `${string}.${string}`
? TPath extends `${infer TFirst}.${infer TRest}`
? IsMatchParse<
TRest,
NextMatchAndValue<TFirst, TMatchAndValue>,
`${TParentPath}${TFirst}.`
>
: never
: {
path: IsMatchPath<TParentPath, TMatchAndValue>
result: IsMatchResult<TPath, TMatchAndValue>
}
export type IsMatch<TMatch, TPath> = IsMatchParse<
TPath,
TMatch extends any ? { match: TMatch; value: TMatch } : never
>
/**
* Narrows matches based on a path
* @experimental
*/
export const isMatch = <TMatch, TPath extends string>(
match: TMatch,
path: Constrain<TPath, IsMatch<TMatch, TPath>['path']>,
): match is IsMatch<TMatch, TPath>['result'] => {
const parts = (path as string).split('.')
let part
let i = 0
let value: any = match
while ((part = parts[i++]) != null && value != null) {
value = value[part]
}
return value != null
}
export interface DefaultRouteMatchExtensions {
scripts?: unknown
links?: unknown
headScripts?: unknown
meta?: unknown
styles?: unknown
}
export interface RouteMatchExtensions extends DefaultRouteMatchExtensions {}
export interface RouteMatch<
out TRouteId,
out TFullPath,
out TAllParams,
out TFullSearchSchema,
out TLoaderData,
out TAllContext,
out TLoaderDeps,
> extends RouteMatchExtensions {
id: string
routeId: TRouteId
fullPath: TFullPath
index: number
pathname: string
params: TAllParams
_strictParams: TAllParams
status: 'pending' | 'success' | 'error' | 'redirected' | 'notFound'
isFetching: false | 'beforeLoad' | 'loader'
error: unknown
paramsError: unknown
searchError: unknown
updatedAt: number
_nonReactive: {
/** @internal */
beforeLoadPromise?: ControlledPromise<void>
/** @internal */
loaderPromise?: ControlledPromise<void>
/** @internal */
pendingTimeout?: ReturnType<typeof setTimeout>
loadPromise?: ControlledPromise<void>
pendingRenderPromise?: ControlledPromise<void>
displayPendingPromise?: Promise<void>
minPendingPromise?: ControlledPromise<void>
dehydrated?: boolean
/** @internal */
error?: unknown
}
loaderData?: TLoaderData
/** @internal */
__routeContext?: Record<string, unknown>
/** @internal */
__beforeLoadContext?: Record<string, unknown>
context: TAllContext
search: TFullSearchSchema
_strictSearch: TFullSearchSchema
fetchCount: number
abortController: AbortController
cause: 'preload' | 'enter' | 'stay'
loaderDeps: TLoaderDeps
preload: boolean
invalid: boolean
headers?: Record<string, string>
globalNotFound?: boolean
staticData: StaticDataRouteOption
/** This attribute is not reactive */
ssr?: SSROption
_forcePending?: boolean
_displayPending?: boolean
}
export interface PreValidationErrorHandlingRouteMatch<
TRouteId,
TFullPath,
TAllParams,
TFullSearchSchema,
> {
id: string
routeId: TRouteId
fullPath: TFullPath
index: number
pathname: string
search:
| { status: 'success'; value: TFullSearchSchema }
| { status: 'error'; error: unknown }
params:
| { status: 'success'; value: TAllParams }
| { status: 'error'; error: unknown }
staticData: StaticDataRouteOption
ssr?: boolean | 'data-only'
}
export type MakePreValidationErrorHandlingRouteMatchUnion<
TRouter extends AnyRouter = RegisteredRouter,
TRoute extends AnyRoute = ParseRoute<TRouter['routeTree']>,
> = TRoute extends any
? PreValidationErrorHandlingRouteMatch<
TRoute['id'],
TRoute['fullPath'],
TRoute['types']['allParams'],
TRoute['types']['fullSearchSchema']
>
: never
export type MakeRouteMatchFromRoute<TRoute extends AnyRoute> = RouteMatch<
TRoute['types']['id'],
TRoute['types']['fullPath'],
TRoute['types']['allParams'],
TRoute['types']['fullSearchSchema'],
TRoute['types']['loaderData'],
TRoute['types']['allContext'],
TRoute['types']['loaderDeps']
>
export type MakeRouteMatch<
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
TRouteId = RouteIds<TRouteTree>,
TStrict extends boolean = true,
> = RouteMatch<
TRouteId,
RouteById<TRouteTree, TRouteId>['types']['fullPath'],
TStrict extends false
? AllParams<TRouteTree>
: RouteById<TRouteTree, TRouteId>['types']['allParams'],
TStrict extends false
? FullSearchSchema<TRouteTree>
: RouteById<TRouteTree, TRouteId>['types']['fullSearchSchema'],
TStrict extends false
? AllLoaderData<TRouteTree>
: RouteById<TRouteTree, TRouteId>['types']['loaderData'],
TStrict extends false
? AllContext<TRouteTree>
: RouteById<TRouteTree, TRouteId>['types']['allContext'],
RouteById<TRouteTree, TRouteId>['types']['loaderDeps']
>
export type AnyRouteMatch = RouteMatch<any, any, any, any, any, any, any>
export type MakeRouteMatchUnion<
TRouter extends AnyRouter = RegisteredRouter,
TRoute extends AnyRoute = ParseRoute<TRouter['routeTree']>,
> = TRoute extends any
? RouteMatch<
TRoute['id'],
TRoute['fullPath'],
TRoute['types']['allParams'],
TRoute['types']['fullSearchSchema'],
TRoute['types']['loaderData'],
TRoute['types']['allContext'],
TRoute['types']['loaderDeps']
>
: never
/**
* The `MatchRouteOptions` type is used to describe the options that can be used when matching a route.
*
* @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/MatchRouteOptionsType#matchrouteoptions-type)
*/
export interface MatchRouteOptions {
/**
* If `true`, will match against pending location instead of the current location.
*
* @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/MatchRouteOptionsType#pending-property)
*/
pending?: boolean
/**
* If `true`, will match against the current location with case sensitivity.
*
* @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/MatchRouteOptionsType#casesensitive-property)
*
* @deprecated Declare case sensitivity in the route definition instead, or globally for all routes using the `caseSensitive` option on the router.
*/
caseSensitive?: boolean
/**
* If `true`, will match against the current location's search params using a deep inclusive check. e.g. `{ a: 1 }` will match for a current location of `{ a: 1, b: 2 }`.
*
* @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/MatchRouteOptionsType#includesearch-property)
*/
includeSearch?: boolean
/**
* If `true`, will match against the current location using a fuzzy match. e.g. `/posts` will match for a current location of `/posts/123`.
*
* @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/MatchRouteOptionsType#fuzzy-property)
*/
fuzzy?: boolean
}