Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

Matches.ts

287 lines8.2 KB

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