@tanstack/react-router 0.0.1-beta.23 → 0.0.1-beta.231

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.
Files changed (108) hide show
  1. package/LICENSE +21 -0
  2. package/build/cjs/CatchBoundary.js +123 -0
  3. package/build/cjs/CatchBoundary.js.map +1 -0
  4. package/build/cjs/Matches.js +235 -0
  5. package/build/cjs/Matches.js.map +1 -0
  6. package/build/cjs/RouterProvider.js +159 -0
  7. package/build/cjs/RouterProvider.js.map +1 -0
  8. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +2 -22
  9. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +1 -1
  10. package/build/cjs/awaited.js +43 -0
  11. package/build/cjs/awaited.js.map +1 -0
  12. package/build/cjs/defer.js +37 -0
  13. package/build/cjs/defer.js.map +1 -0
  14. package/build/cjs/fileRoute.js +27 -0
  15. package/build/cjs/fileRoute.js.map +1 -0
  16. package/build/cjs/index.js +123 -0
  17. package/build/cjs/index.js.map +1 -0
  18. package/build/cjs/lazyRouteComponent.js +54 -0
  19. package/build/cjs/lazyRouteComponent.js.map +1 -0
  20. package/build/cjs/link.js +148 -0
  21. package/build/cjs/link.js.map +1 -0
  22. package/build/cjs/path.js +209 -0
  23. package/build/cjs/path.js.map +1 -0
  24. package/build/cjs/qss.js +63 -0
  25. package/build/cjs/qss.js.map +1 -0
  26. package/build/cjs/redirects.js +25 -0
  27. package/build/cjs/redirects.js.map +1 -0
  28. package/build/cjs/route.js +134 -0
  29. package/build/cjs/route.js.map +1 -0
  30. package/build/cjs/router.js +1101 -0
  31. package/build/cjs/router.js.map +1 -0
  32. package/build/cjs/scroll-restoration.js +202 -0
  33. package/build/cjs/scroll-restoration.js.map +1 -0
  34. package/build/cjs/searchParams.js +81 -0
  35. package/build/cjs/searchParams.js.map +1 -0
  36. package/build/cjs/useBlocker.js +61 -0
  37. package/build/cjs/useBlocker.js.map +1 -0
  38. package/build/cjs/useNavigate.js +75 -0
  39. package/build/cjs/useNavigate.js.map +1 -0
  40. package/build/cjs/useParams.js +26 -0
  41. package/build/cjs/useParams.js.map +1 -0
  42. package/build/cjs/useSearch.js +25 -0
  43. package/build/cjs/useSearch.js.map +1 -0
  44. package/build/cjs/utils.js +239 -0
  45. package/build/cjs/utils.js.map +1 -0
  46. package/build/esm/index.js +2174 -2557
  47. package/build/esm/index.js.map +1 -1
  48. package/build/stats-html.html +3498 -2694
  49. package/build/stats-react.json +927 -42
  50. package/build/types/CatchBoundary.d.ts +33 -0
  51. package/build/types/Matches.d.ts +57 -0
  52. package/build/types/RouterProvider.d.ts +41 -0
  53. package/build/types/awaited.d.ts +9 -0
  54. package/build/types/defer.d.ts +19 -0
  55. package/build/types/fileRoute.d.ts +35 -0
  56. package/build/types/history.d.ts +7 -0
  57. package/build/types/index.d.ts +27 -108
  58. package/build/types/injectHtml.d.ts +0 -0
  59. package/build/types/lazyRouteComponent.d.ts +2 -0
  60. package/build/types/link.d.ts +105 -0
  61. package/build/types/location.d.ts +14 -0
  62. package/build/types/path.d.ts +16 -0
  63. package/build/types/qss.d.ts +2 -0
  64. package/build/types/redirects.d.ts +10 -0
  65. package/build/types/route.d.ts +278 -0
  66. package/build/types/routeInfo.d.ts +22 -0
  67. package/build/types/router.d.ts +167 -0
  68. package/build/types/scroll-restoration.d.ts +18 -0
  69. package/build/types/searchParams.d.ts +7 -0
  70. package/build/types/useBlocker.d.ts +8 -0
  71. package/build/types/useNavigate.d.ts +20 -0
  72. package/build/types/useParams.d.ts +7 -0
  73. package/build/types/useSearch.d.ts +7 -0
  74. package/build/types/utils.d.ts +66 -0
  75. package/build/umd/index.development.js +2470 -2513
  76. package/build/umd/index.development.js.map +1 -1
  77. package/build/umd/index.production.js +4 -4
  78. package/build/umd/index.production.js.map +1 -1
  79. package/package.json +9 -10
  80. package/src/CatchBoundary.tsx +98 -0
  81. package/src/Matches.tsx +389 -0
  82. package/src/RouterProvider.tsx +226 -0
  83. package/src/awaited.tsx +40 -0
  84. package/src/defer.ts +55 -0
  85. package/src/fileRoute.ts +154 -0
  86. package/src/history.ts +8 -0
  87. package/src/index.tsx +28 -709
  88. package/src/injectHtml.ts +28 -0
  89. package/src/lazyRouteComponent.tsx +33 -0
  90. package/src/link.tsx +508 -0
  91. package/src/location.ts +15 -0
  92. package/src/path.ts +256 -0
  93. package/src/qss.ts +53 -0
  94. package/src/redirects.ts +31 -0
  95. package/src/route.ts +861 -0
  96. package/src/routeInfo.ts +68 -0
  97. package/src/router.ts +1664 -0
  98. package/src/scroll-restoration.tsx +230 -0
  99. package/src/searchParams.ts +79 -0
  100. package/src/useBlocker.tsx +34 -0
  101. package/src/useNavigate.tsx +109 -0
  102. package/src/useParams.tsx +25 -0
  103. package/src/useSearch.tsx +25 -0
  104. package/src/utils.ts +350 -0
  105. package/build/cjs/react-router/src/index.js +0 -473
  106. package/build/cjs/react-router/src/index.js.map +0 -1
  107. package/build/cjs/router-core/build/esm/index.js +0 -2527
  108. package/build/cjs/router-core/build/esm/index.js.map +0 -1
package/src/path.ts ADDED
@@ -0,0 +1,256 @@
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)] ?? ''
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 matchByPath(
165
+ basepath: string,
166
+ from: string,
167
+ matchLocation: Pick<MatchLocation, 'to' | 'caseSensitive' | 'fuzzy'>,
168
+ ): Record<string, string> | undefined {
169
+ // Remove the base path from the pathname
170
+ from = basepath != '/' ? from.substring(basepath.length) : from
171
+ // Default to to $ (wildcard)
172
+ const to = `${matchLocation.to ?? '$'}`
173
+ // Parse the from and to
174
+ const baseSegments = parsePathname(from)
175
+ const routeSegments = parsePathname(to)
176
+
177
+ if (!from.startsWith('/')) {
178
+ baseSegments.unshift({
179
+ type: 'pathname',
180
+ value: '/',
181
+ })
182
+ }
183
+
184
+ if (!to.startsWith('/')) {
185
+ routeSegments.unshift({
186
+ type: 'pathname',
187
+ value: '/',
188
+ })
189
+ }
190
+
191
+ const params: Record<string, string> = {}
192
+
193
+ let isMatch = (() => {
194
+ for (
195
+ let i = 0;
196
+ i < Math.max(baseSegments.length, routeSegments.length);
197
+ i++
198
+ ) {
199
+ const baseSegment = baseSegments[i]
200
+ const routeSegment = routeSegments[i]
201
+
202
+ const isLastBaseSegment = i >= baseSegments.length - 1
203
+ const isLastRouteSegment = i >= routeSegments.length - 1
204
+
205
+ if (routeSegment) {
206
+ if (routeSegment.type === 'wildcard') {
207
+ if (baseSegment?.value) {
208
+ params['*'] = joinPaths(baseSegments.slice(i).map((d) => d.value))
209
+ return true
210
+ }
211
+ return false
212
+ }
213
+
214
+ if (routeSegment.type === 'pathname') {
215
+ if (routeSegment.value === '/' && !baseSegment?.value) {
216
+ return true
217
+ }
218
+
219
+ if (baseSegment) {
220
+ if (matchLocation.caseSensitive) {
221
+ if (routeSegment.value !== baseSegment.value) {
222
+ return false
223
+ }
224
+ } else if (
225
+ routeSegment.value.toLowerCase() !==
226
+ baseSegment.value.toLowerCase()
227
+ ) {
228
+ return false
229
+ }
230
+ }
231
+ }
232
+
233
+ if (!baseSegment) {
234
+ return false
235
+ }
236
+
237
+ if (routeSegment.type === 'param') {
238
+ if (baseSegment?.value === '/') {
239
+ return false
240
+ }
241
+ if (baseSegment.value.charAt(0) !== '$') {
242
+ params[routeSegment.value.substring(1)] = baseSegment.value
243
+ }
244
+ }
245
+ }
246
+
247
+ if (!isLastBaseSegment && isLastRouteSegment) {
248
+ return !!matchLocation.fuzzy
249
+ }
250
+ }
251
+
252
+ return true
253
+ })()
254
+
255
+ return isMatch ? (params as Record<string, string>) : undefined
256
+ }
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
+ }
@@ -0,0 +1,31 @@
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>
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
+ > = NavigateOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo> & {
17
+ code?: number
18
+ }
19
+
20
+ export function redirect<
21
+ TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
22
+ TFrom extends RoutePaths<TRouteTree> = '/',
23
+ TTo extends string = '',
24
+ >(opts: Redirect<TRouteTree, TFrom, TTo>): Redirect<TRouteTree, TFrom, TTo> {
25
+ ;(opts as any).isRedirect = true
26
+ return opts
27
+ }
28
+
29
+ export function isRedirect(obj: any): obj is AnyRedirect {
30
+ return !!obj?.isRedirect
31
+ }