@tanstack/react-router 0.0.1-beta.9 → 1.0.1
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/CatchBoundary.js +128 -0
- package/build/cjs/CatchBoundary.js.map +1 -0
- package/build/cjs/Matches.js +233 -0
- package/build/cjs/Matches.js.map +1 -0
- package/build/cjs/RouterProvider.js +172 -0
- package/build/cjs/RouterProvider.js.map +1 -0
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +2 -22
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +1 -1
- package/build/cjs/awaited.js +43 -0
- package/build/cjs/awaited.js.map +1 -0
- package/build/cjs/defer.js +37 -0
- package/build/cjs/defer.js.map +1 -0
- package/build/cjs/fileRoute.js +27 -0
- package/build/cjs/fileRoute.js.map +1 -0
- package/build/cjs/index.js +130 -0
- package/build/cjs/index.js.map +1 -0
- package/build/cjs/lazyRouteComponent.js +54 -0
- package/build/cjs/lazyRouteComponent.js.map +1 -0
- package/build/cjs/link.js +223 -0
- package/build/cjs/link.js.map +1 -0
- package/build/cjs/path.js +214 -0
- package/build/cjs/path.js.map +1 -0
- package/build/cjs/qss.js +63 -0
- package/build/cjs/qss.js.map +1 -0
- package/build/cjs/redirects.js +28 -0
- package/build/cjs/redirects.js.map +1 -0
- package/build/cjs/route.js +191 -0
- package/build/cjs/route.js.map +1 -0
- package/build/cjs/router.js +1085 -0
- package/build/cjs/router.js.map +1 -0
- package/build/cjs/scroll-restoration.js +202 -0
- package/build/cjs/scroll-restoration.js.map +1 -0
- package/build/cjs/searchParams.js +81 -0
- package/build/cjs/searchParams.js.map +1 -0
- package/build/cjs/useBlocker.js +55 -0
- package/build/cjs/useBlocker.js.map +1 -0
- package/build/cjs/useNavigate.js +86 -0
- package/build/cjs/useNavigate.js.map +1 -0
- package/build/cjs/useParams.js +26 -0
- package/build/cjs/useParams.js.map +1 -0
- package/build/cjs/useSearch.js +25 -0
- package/build/cjs/useSearch.js.map +1 -0
- package/build/cjs/utils.js +241 -0
- package/build/cjs/utils.js.map +1 -0
- package/build/esm/index.js +2302 -2534
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +3498 -2694
- package/build/stats-react.json +1204 -44
- package/build/types/CatchBoundary.d.ts +36 -0
- package/build/types/Matches.d.ts +64 -0
- package/build/types/RouterProvider.d.ts +35 -0
- package/build/types/awaited.d.ts +9 -0
- package/build/types/defer.d.ts +19 -0
- package/build/types/fileRoute.d.ts +38 -0
- package/build/types/history.d.ts +7 -0
- package/build/types/index.d.ts +27 -74
- package/build/types/lazyRouteComponent.d.ts +2 -0
- package/build/types/link.d.ts +93 -0
- package/build/types/location.d.ts +12 -0
- package/build/types/path.d.ts +17 -0
- package/build/types/qss.d.ts +2 -0
- package/build/types/redirects.d.ts +11 -0
- package/build/types/route.d.ts +283 -0
- package/build/types/routeInfo.d.ts +31 -0
- package/build/types/router.d.ts +186 -0
- package/build/types/scroll-restoration.d.ts +18 -0
- package/build/types/searchParams.d.ts +7 -0
- package/build/types/useBlocker.d.ts +9 -0
- package/build/types/useNavigate.d.ts +19 -0
- package/build/types/useParams.d.ts +7 -0
- package/build/types/useSearch.d.ts +7 -0
- package/build/types/utils.d.ts +69 -0
- package/build/umd/index.development.js +2899 -2493
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +4 -4
- package/build/umd/index.production.js.map +1 -1
- package/package.json +12 -10
- package/src/CatchBoundary.tsx +101 -0
- package/src/Matches.tsx +423 -0
- package/src/RouterProvider.tsx +254 -0
- package/src/awaited.tsx +40 -0
- package/src/defer.ts +55 -0
- package/src/fileRoute.ts +152 -0
- package/src/history.ts +8 -0
- package/src/index.tsx +28 -619
- package/src/lazyRouteComponent.tsx +33 -0
- package/src/link.tsx +603 -0
- package/src/location.ts +13 -0
- package/src/path.ts +261 -0
- package/src/qss.ts +53 -0
- package/src/redirects.ts +39 -0
- package/src/route.ts +882 -0
- package/src/routeInfo.ts +84 -0
- package/src/router.ts +1671 -0
- package/src/scroll-restoration.tsx +230 -0
- package/src/searchParams.ts +79 -0
- package/src/useBlocker.tsx +27 -0
- package/src/useNavigate.tsx +111 -0
- package/src/useParams.tsx +25 -0
- package/src/useSearch.tsx +25 -0
- package/src/utils.ts +360 -0
- package/build/cjs/react-router/src/index.js +0 -458
- package/build/cjs/react-router/src/index.js.map +0 -1
- package/build/cjs/router-core/build/esm/index.js +0 -2524
- package/build/cjs/router-core/build/esm/index.js.map +0 -1
package/src/path.ts
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import { MatchLocation } from './RouterProvider'
|
|
2
|
+
import { AnyPathParams } from './route'
|
|
3
|
+
import { last } from './utils'
|
|
4
|
+
|
|
5
|
+
export interface Segment {
|
|
6
|
+
type: 'pathname' | 'param' | 'wildcard'
|
|
7
|
+
value: string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function joinPaths(paths: (string | undefined)[]) {
|
|
11
|
+
return cleanPath(paths.filter(Boolean).join('/'))
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function cleanPath(path: string) {
|
|
15
|
+
// remove double slashes
|
|
16
|
+
return path.replace(/\/{2,}/g, '/')
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function trimPathLeft(path: string) {
|
|
20
|
+
return path === '/' ? path : path.replace(/^\/{1,}/, '')
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function trimPathRight(path: string) {
|
|
24
|
+
return path === '/' ? path : path.replace(/\/{1,}$/, '')
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function trimPath(path: string) {
|
|
28
|
+
return trimPathRight(trimPathLeft(path))
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function resolvePath(basepath: string, base: string, to: string) {
|
|
32
|
+
base = base.replace(new RegExp(`^${basepath}`), '/')
|
|
33
|
+
to = to.replace(new RegExp(`^${basepath}`), '/')
|
|
34
|
+
|
|
35
|
+
let baseSegments = parsePathname(base)
|
|
36
|
+
const toSegments = parsePathname(to)
|
|
37
|
+
|
|
38
|
+
toSegments.forEach((toSegment, index) => {
|
|
39
|
+
if (toSegment.value === '/') {
|
|
40
|
+
if (!index) {
|
|
41
|
+
// Leading slash
|
|
42
|
+
baseSegments = [toSegment]
|
|
43
|
+
} else if (index === toSegments.length - 1) {
|
|
44
|
+
// Trailing Slash
|
|
45
|
+
baseSegments.push(toSegment)
|
|
46
|
+
} else {
|
|
47
|
+
// ignore inter-slashes
|
|
48
|
+
}
|
|
49
|
+
} else if (toSegment.value === '..') {
|
|
50
|
+
// Extra trailing slash? pop it off
|
|
51
|
+
if (baseSegments.length > 1 && last(baseSegments)?.value === '/') {
|
|
52
|
+
baseSegments.pop()
|
|
53
|
+
}
|
|
54
|
+
baseSegments.pop()
|
|
55
|
+
} else if (toSegment.value === '.') {
|
|
56
|
+
return
|
|
57
|
+
} else {
|
|
58
|
+
baseSegments.push(toSegment)
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
const joined = joinPaths([basepath, ...baseSegments.map((d) => d.value)])
|
|
63
|
+
|
|
64
|
+
return cleanPath(joined)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function parsePathname(pathname?: string): Segment[] {
|
|
68
|
+
if (!pathname) {
|
|
69
|
+
return []
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
pathname = cleanPath(pathname)
|
|
73
|
+
|
|
74
|
+
const segments: Segment[] = []
|
|
75
|
+
|
|
76
|
+
if (pathname.slice(0, 1) === '/') {
|
|
77
|
+
pathname = pathname.substring(1)
|
|
78
|
+
segments.push({
|
|
79
|
+
type: 'pathname',
|
|
80
|
+
value: '/',
|
|
81
|
+
})
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (!pathname) {
|
|
85
|
+
return segments
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Remove empty segments and '.' segments
|
|
89
|
+
const split = pathname.split('/').filter(Boolean)
|
|
90
|
+
|
|
91
|
+
segments.push(
|
|
92
|
+
...split.map((part): Segment => {
|
|
93
|
+
if (part === '$' || part === '*') {
|
|
94
|
+
return {
|
|
95
|
+
type: 'wildcard',
|
|
96
|
+
value: part,
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (part.charAt(0) === '$') {
|
|
101
|
+
return {
|
|
102
|
+
type: 'param',
|
|
103
|
+
value: part,
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
type: 'pathname',
|
|
109
|
+
value: part,
|
|
110
|
+
}
|
|
111
|
+
}),
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
if (pathname.slice(-1) === '/') {
|
|
115
|
+
pathname = pathname.substring(1)
|
|
116
|
+
segments.push({
|
|
117
|
+
type: 'pathname',
|
|
118
|
+
value: '/',
|
|
119
|
+
})
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return segments
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export function interpolatePath(
|
|
126
|
+
path: string | undefined,
|
|
127
|
+
params: any,
|
|
128
|
+
leaveWildcards: boolean = false,
|
|
129
|
+
) {
|
|
130
|
+
const interpolatedPathSegments = parsePathname(path)
|
|
131
|
+
|
|
132
|
+
return joinPaths(
|
|
133
|
+
interpolatedPathSegments.map((segment) => {
|
|
134
|
+
if (segment.type === 'wildcard') {
|
|
135
|
+
const value = params[segment.value]
|
|
136
|
+
if (leaveWildcards) return `${segment.value}${value ?? ''}`
|
|
137
|
+
return value
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (segment.type === 'param') {
|
|
141
|
+
return params![segment.value.substring(1)] ?? 'undefined'
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
return segment.value
|
|
145
|
+
}),
|
|
146
|
+
)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function matchPathname(
|
|
150
|
+
basepath: string,
|
|
151
|
+
currentPathname: string,
|
|
152
|
+
matchLocation: Pick<MatchLocation, 'to' | 'fuzzy' | 'caseSensitive'>,
|
|
153
|
+
): AnyPathParams | undefined {
|
|
154
|
+
const pathParams = matchByPath(basepath, currentPathname, matchLocation)
|
|
155
|
+
// const searchMatched = matchBySearch(location.search, matchLocation)
|
|
156
|
+
|
|
157
|
+
if (matchLocation.to && !pathParams) {
|
|
158
|
+
return
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return pathParams ?? {}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export function removeBasepath(basepath: string, pathname: string) {
|
|
165
|
+
return basepath != '/' ? pathname.substring(basepath.length) : pathname
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function matchByPath(
|
|
169
|
+
basepath: string,
|
|
170
|
+
from: string,
|
|
171
|
+
matchLocation: Pick<MatchLocation, 'to' | 'caseSensitive' | 'fuzzy'>,
|
|
172
|
+
): Record<string, string> | undefined {
|
|
173
|
+
// Remove the base path from the pathname
|
|
174
|
+
from = removeBasepath(basepath, from)
|
|
175
|
+
// Default to to $ (wildcard)
|
|
176
|
+
const to = `${matchLocation.to ?? '$'}`
|
|
177
|
+
// Parse the from and to
|
|
178
|
+
const baseSegments = parsePathname(from)
|
|
179
|
+
const routeSegments = parsePathname(to)
|
|
180
|
+
|
|
181
|
+
if (!from.startsWith('/')) {
|
|
182
|
+
baseSegments.unshift({
|
|
183
|
+
type: 'pathname',
|
|
184
|
+
value: '/',
|
|
185
|
+
})
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (!to.startsWith('/')) {
|
|
189
|
+
routeSegments.unshift({
|
|
190
|
+
type: 'pathname',
|
|
191
|
+
value: '/',
|
|
192
|
+
})
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const params: Record<string, string> = {}
|
|
196
|
+
|
|
197
|
+
let isMatch = (() => {
|
|
198
|
+
for (
|
|
199
|
+
let i = 0;
|
|
200
|
+
i < Math.max(baseSegments.length, routeSegments.length);
|
|
201
|
+
i++
|
|
202
|
+
) {
|
|
203
|
+
const baseSegment = baseSegments[i]
|
|
204
|
+
const routeSegment = routeSegments[i]
|
|
205
|
+
|
|
206
|
+
const isLastBaseSegment = i >= baseSegments.length - 1
|
|
207
|
+
const isLastRouteSegment = i >= routeSegments.length - 1
|
|
208
|
+
|
|
209
|
+
if (routeSegment) {
|
|
210
|
+
if (routeSegment.type === 'wildcard') {
|
|
211
|
+
if (baseSegment?.value) {
|
|
212
|
+
params['*'] = joinPaths(baseSegments.slice(i).map((d) => d.value))
|
|
213
|
+
return true
|
|
214
|
+
}
|
|
215
|
+
return false
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (routeSegment.type === 'pathname') {
|
|
219
|
+
if (routeSegment.value === '/' && !baseSegment?.value) {
|
|
220
|
+
return true
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (baseSegment) {
|
|
224
|
+
if (matchLocation.caseSensitive) {
|
|
225
|
+
if (routeSegment.value !== baseSegment.value) {
|
|
226
|
+
return false
|
|
227
|
+
}
|
|
228
|
+
} else if (
|
|
229
|
+
routeSegment.value.toLowerCase() !==
|
|
230
|
+
baseSegment.value.toLowerCase()
|
|
231
|
+
) {
|
|
232
|
+
return false
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
if (!baseSegment) {
|
|
238
|
+
return false
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (routeSegment.type === 'param') {
|
|
242
|
+
if (baseSegment?.value === '/') {
|
|
243
|
+
return false
|
|
244
|
+
}
|
|
245
|
+
if (baseSegment.value.charAt(0) !== '$') {
|
|
246
|
+
params[routeSegment.value.substring(1)] = baseSegment.value
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (!isLastBaseSegment && isLastRouteSegment) {
|
|
252
|
+
params['**'] = joinPaths(baseSegments.slice(i + 1).map((d) => d.value))
|
|
253
|
+
return !!matchLocation.fuzzy && routeSegment?.value !== '/'
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return true
|
|
258
|
+
})()
|
|
259
|
+
|
|
260
|
+
return isMatch ? (params as Record<string, string>) : undefined
|
|
261
|
+
}
|
package/src/qss.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
|
|
3
|
+
// qss has been slightly modified and inlined here for our use cases (and compression's sake). We've included it as a hard dependency for MIT license attribution.
|
|
4
|
+
|
|
5
|
+
export function encode(obj, pfx?: string) {
|
|
6
|
+
var k,
|
|
7
|
+
i,
|
|
8
|
+
tmp,
|
|
9
|
+
str = ''
|
|
10
|
+
|
|
11
|
+
for (k in obj) {
|
|
12
|
+
if ((tmp = obj[k]) !== void 0) {
|
|
13
|
+
if (Array.isArray(tmp)) {
|
|
14
|
+
for (i = 0; i < tmp.length; i++) {
|
|
15
|
+
str && (str += '&')
|
|
16
|
+
str += encodeURIComponent(k) + '=' + encodeURIComponent(tmp[i])
|
|
17
|
+
}
|
|
18
|
+
} else {
|
|
19
|
+
str && (str += '&')
|
|
20
|
+
str += encodeURIComponent(k) + '=' + encodeURIComponent(tmp)
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return (pfx || '') + str
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function toValue(mix) {
|
|
29
|
+
if (!mix) return ''
|
|
30
|
+
var str = decodeURIComponent(mix)
|
|
31
|
+
if (str === 'false') return false
|
|
32
|
+
if (str === 'true') return true
|
|
33
|
+
return +str * 0 === 0 && +str + '' === str ? +str : str
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function decode(str) {
|
|
37
|
+
var tmp,
|
|
38
|
+
k,
|
|
39
|
+
out = {},
|
|
40
|
+
arr = str.split('&')
|
|
41
|
+
|
|
42
|
+
while ((tmp = arr.shift())) {
|
|
43
|
+
tmp = tmp.split('=')
|
|
44
|
+
k = tmp.shift()
|
|
45
|
+
if (out[k] !== void 0) {
|
|
46
|
+
out[k] = [].concat(out[k], toValue(tmp.shift()))
|
|
47
|
+
} else {
|
|
48
|
+
out[k] = toValue(tmp.shift())
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return out
|
|
53
|
+
}
|
package/src/redirects.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { NavigateOptions } from './link'
|
|
2
|
+
import { AnyRoute } from './route'
|
|
3
|
+
import { RoutePaths } from './routeInfo'
|
|
4
|
+
import { RegisteredRouter } from './router'
|
|
5
|
+
|
|
6
|
+
// Detect if we're in the DOM
|
|
7
|
+
|
|
8
|
+
export type AnyRedirect = Redirect<any, any, any, any, any>
|
|
9
|
+
|
|
10
|
+
export type Redirect<
|
|
11
|
+
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
|
|
12
|
+
TFrom extends RoutePaths<TRouteTree> = '/',
|
|
13
|
+
TTo extends string = '',
|
|
14
|
+
TMaskFrom extends RoutePaths<TRouteTree> = TFrom,
|
|
15
|
+
TMaskTo extends string = '',
|
|
16
|
+
> = {
|
|
17
|
+
code?: number
|
|
18
|
+
throw?: any
|
|
19
|
+
} & NavigateOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>
|
|
20
|
+
|
|
21
|
+
export function redirect<
|
|
22
|
+
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
|
|
23
|
+
TFrom extends RoutePaths<TRouteTree> = '/',
|
|
24
|
+
TTo extends string = '',
|
|
25
|
+
TMaskFrom extends RoutePaths<TRouteTree> = TFrom,
|
|
26
|
+
TMaskTo extends string = '',
|
|
27
|
+
>(
|
|
28
|
+
opts: Redirect<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>,
|
|
29
|
+
): Redirect<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo> {
|
|
30
|
+
;(opts as any).isRedirect = true
|
|
31
|
+
if (opts.throw) {
|
|
32
|
+
throw opts
|
|
33
|
+
}
|
|
34
|
+
return opts
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function isRedirect(obj: any): obj is AnyRedirect {
|
|
38
|
+
return !!obj?.isRedirect
|
|
39
|
+
}
|