@tanstack/router-core 0.0.1-beta.8 → 1.20.3-alpha.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.
Files changed (194) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +5 -0
  3. package/dist/cjs/Matches.cjs +13 -0
  4. package/dist/cjs/Matches.cjs.map +1 -0
  5. package/dist/cjs/Matches.d.cts +109 -0
  6. package/dist/cjs/RouterProvider.d.cts +26 -0
  7. package/dist/cjs/defer.cjs +25 -0
  8. package/dist/cjs/defer.cjs.map +1 -0
  9. package/dist/cjs/defer.d.cts +20 -0
  10. package/dist/cjs/fileRoute.d.cts +23 -0
  11. package/dist/cjs/history.d.cts +8 -0
  12. package/dist/cjs/index.cjs +80 -0
  13. package/dist/cjs/index.cjs.map +1 -0
  14. package/dist/cjs/index.d.cts +41 -0
  15. package/dist/cjs/link.cjs +5 -0
  16. package/dist/cjs/link.cjs.map +1 -0
  17. package/dist/cjs/link.d.cts +200 -0
  18. package/dist/cjs/location.d.cts +12 -0
  19. package/dist/cjs/manifest.d.cts +24 -0
  20. package/dist/cjs/not-found.cjs +13 -0
  21. package/dist/cjs/not-found.cjs.map +1 -0
  22. package/dist/cjs/not-found.d.cts +20 -0
  23. package/dist/cjs/path.cjs +412 -0
  24. package/dist/cjs/path.cjs.map +1 -0
  25. package/dist/cjs/path.d.cts +56 -0
  26. package/dist/cjs/qss.cjs +38 -0
  27. package/dist/cjs/qss.cjs.map +1 -0
  28. package/dist/cjs/qss.d.cts +22 -0
  29. package/dist/cjs/redirect.cjs +34 -0
  30. package/dist/cjs/redirect.cjs.map +1 -0
  31. package/dist/cjs/redirect.d.cts +38 -0
  32. package/dist/cjs/root.cjs +5 -0
  33. package/dist/cjs/root.cjs.map +1 -0
  34. package/dist/cjs/root.d.cts +2 -0
  35. package/dist/cjs/route.cjs +119 -0
  36. package/dist/cjs/route.cjs.map +1 -0
  37. package/dist/cjs/route.d.cts +422 -0
  38. package/dist/cjs/routeInfo.d.cts +54 -0
  39. package/dist/cjs/router.cjs +1800 -0
  40. package/dist/cjs/router.cjs.map +1 -0
  41. package/dist/cjs/router.d.cts +630 -0
  42. package/dist/cjs/scroll-restoration.cjs +196 -0
  43. package/dist/cjs/scroll-restoration.cjs.map +1 -0
  44. package/dist/cjs/scroll-restoration.d.cts +38 -0
  45. package/dist/cjs/searchMiddleware.cjs +42 -0
  46. package/dist/cjs/searchMiddleware.cjs.map +1 -0
  47. package/dist/cjs/searchMiddleware.d.cts +5 -0
  48. package/dist/cjs/searchParams.cjs +61 -0
  49. package/dist/cjs/searchParams.cjs.map +1 -0
  50. package/dist/cjs/searchParams.d.cts +7 -0
  51. package/dist/cjs/serializer.d.cts +22 -0
  52. package/dist/cjs/structuralSharing.d.cts +4 -0
  53. package/dist/cjs/typePrimitives.d.cts +65 -0
  54. package/dist/cjs/useLoaderData.d.cts +5 -0
  55. package/dist/cjs/useLoaderDeps.d.cts +5 -0
  56. package/dist/cjs/useNavigate.d.cts +3 -0
  57. package/dist/cjs/useParams.d.cts +5 -0
  58. package/dist/cjs/useRouteContext.d.cts +9 -0
  59. package/dist/cjs/useSearch.d.cts +5 -0
  60. package/dist/cjs/utils.cjs +160 -0
  61. package/dist/cjs/utils.cjs.map +1 -0
  62. package/dist/cjs/utils.d.cts +105 -0
  63. package/dist/cjs/validators.d.cts +51 -0
  64. package/dist/esm/Matches.d.ts +109 -0
  65. package/dist/esm/Matches.js +13 -0
  66. package/dist/esm/Matches.js.map +1 -0
  67. package/dist/esm/RouterProvider.d.ts +26 -0
  68. package/dist/esm/defer.d.ts +20 -0
  69. package/dist/esm/defer.js +25 -0
  70. package/dist/esm/defer.js.map +1 -0
  71. package/dist/esm/fileRoute.d.ts +23 -0
  72. package/dist/esm/history.d.ts +8 -0
  73. package/dist/esm/index.d.ts +41 -0
  74. package/dist/esm/index.js +80 -0
  75. package/dist/esm/index.js.map +1 -0
  76. package/dist/esm/link.d.ts +200 -0
  77. package/dist/esm/link.js +5 -0
  78. package/dist/esm/link.js.map +1 -0
  79. package/dist/esm/location.d.ts +12 -0
  80. package/dist/esm/manifest.d.ts +24 -0
  81. package/dist/esm/not-found.d.ts +20 -0
  82. package/dist/esm/not-found.js +13 -0
  83. package/dist/esm/not-found.js.map +1 -0
  84. package/dist/esm/path.d.ts +56 -0
  85. package/dist/esm/path.js +412 -0
  86. package/dist/esm/path.js.map +1 -0
  87. package/dist/esm/qss.d.ts +22 -0
  88. package/dist/esm/qss.js +38 -0
  89. package/dist/esm/qss.js.map +1 -0
  90. package/dist/esm/redirect.d.ts +38 -0
  91. package/dist/esm/redirect.js +34 -0
  92. package/dist/esm/redirect.js.map +1 -0
  93. package/dist/esm/root.d.ts +2 -0
  94. package/dist/esm/root.js +5 -0
  95. package/dist/esm/root.js.map +1 -0
  96. package/dist/esm/route.d.ts +422 -0
  97. package/dist/esm/route.js +119 -0
  98. package/dist/esm/route.js.map +1 -0
  99. package/dist/esm/routeInfo.d.ts +54 -0
  100. package/dist/esm/router.d.ts +630 -0
  101. package/dist/esm/router.js +1800 -0
  102. package/dist/esm/router.js.map +1 -0
  103. package/dist/esm/scroll-restoration.d.ts +38 -0
  104. package/dist/esm/scroll-restoration.js +196 -0
  105. package/dist/esm/scroll-restoration.js.map +1 -0
  106. package/dist/esm/searchMiddleware.d.ts +5 -0
  107. package/dist/esm/searchMiddleware.js +42 -0
  108. package/dist/esm/searchMiddleware.js.map +1 -0
  109. package/dist/esm/searchParams.d.ts +7 -0
  110. package/dist/esm/searchParams.js +61 -0
  111. package/dist/esm/searchParams.js.map +1 -0
  112. package/dist/esm/serializer.d.ts +22 -0
  113. package/dist/esm/structuralSharing.d.ts +4 -0
  114. package/dist/esm/typePrimitives.d.ts +65 -0
  115. package/dist/esm/useLoaderData.d.ts +5 -0
  116. package/dist/esm/useLoaderDeps.d.ts +5 -0
  117. package/dist/esm/useNavigate.d.ts +3 -0
  118. package/dist/esm/useParams.d.ts +5 -0
  119. package/dist/esm/useRouteContext.d.ts +9 -0
  120. package/dist/esm/useSearch.d.ts +5 -0
  121. package/dist/esm/utils.d.ts +105 -0
  122. package/dist/esm/utils.js +160 -0
  123. package/dist/esm/utils.js.map +1 -0
  124. package/dist/esm/validators.d.ts +51 -0
  125. package/package.json +36 -32
  126. package/src/Matches.ts +239 -0
  127. package/src/RouterProvider.ts +50 -0
  128. package/src/defer.ts +52 -0
  129. package/src/fileRoute.ts +140 -0
  130. package/src/history.ts +9 -0
  131. package/src/index.ts +421 -19
  132. package/src/link.ts +580 -286
  133. package/src/location.ts +13 -0
  134. package/src/manifest.ts +32 -0
  135. package/src/not-found.ts +29 -0
  136. package/src/path.ts +425 -49
  137. package/src/qss.ts +70 -41
  138. package/src/redirect.ts +100 -0
  139. package/src/root.ts +2 -0
  140. package/src/route.ts +1679 -228
  141. package/src/routeInfo.ts +224 -217
  142. package/src/router.ts +3073 -1033
  143. package/src/scroll-restoration.ts +340 -0
  144. package/src/searchMiddleware.ts +54 -0
  145. package/src/searchParams.ts +43 -20
  146. package/src/serializer.ts +32 -0
  147. package/src/structuralSharing.ts +7 -0
  148. package/src/typePrimitives.ts +181 -0
  149. package/src/useLoaderData.ts +20 -0
  150. package/src/useLoaderDeps.ts +13 -0
  151. package/src/useNavigate.ts +13 -0
  152. package/src/useParams.ts +20 -0
  153. package/src/useRouteContext.ts +39 -0
  154. package/src/useSearch.ts +20 -0
  155. package/src/utils.ts +369 -75
  156. package/src/validators.ts +121 -0
  157. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +0 -33
  158. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +0 -1
  159. package/build/cjs/node_modules/@babel/runtime/helpers/esm/extends.js +0 -33
  160. package/build/cjs/node_modules/@babel/runtime/helpers/esm/extends.js.map +0 -1
  161. package/build/cjs/node_modules/history/index.js +0 -815
  162. package/build/cjs/node_modules/history/index.js.map +0 -1
  163. package/build/cjs/node_modules/tiny-invariant/dist/esm/tiny-invariant.js +0 -30
  164. package/build/cjs/node_modules/tiny-invariant/dist/esm/tiny-invariant.js.map +0 -1
  165. package/build/cjs/packages/router-core/src/index.js +0 -59
  166. package/build/cjs/packages/router-core/src/index.js.map +0 -1
  167. package/build/cjs/packages/router-core/src/path.js +0 -222
  168. package/build/cjs/packages/router-core/src/path.js.map +0 -1
  169. package/build/cjs/packages/router-core/src/qss.js +0 -71
  170. package/build/cjs/packages/router-core/src/qss.js.map +0 -1
  171. package/build/cjs/packages/router-core/src/route.js +0 -161
  172. package/build/cjs/packages/router-core/src/route.js.map +0 -1
  173. package/build/cjs/packages/router-core/src/routeConfig.js +0 -69
  174. package/build/cjs/packages/router-core/src/routeConfig.js.map +0 -1
  175. package/build/cjs/packages/router-core/src/routeMatch.js +0 -266
  176. package/build/cjs/packages/router-core/src/routeMatch.js.map +0 -1
  177. package/build/cjs/packages/router-core/src/router.js +0 -812
  178. package/build/cjs/packages/router-core/src/router.js.map +0 -1
  179. package/build/cjs/packages/router-core/src/searchParams.js +0 -70
  180. package/build/cjs/packages/router-core/src/searchParams.js.map +0 -1
  181. package/build/cjs/packages/router-core/src/utils.js +0 -125
  182. package/build/cjs/packages/router-core/src/utils.js.map +0 -1
  183. package/build/esm/index.js +0 -2480
  184. package/build/esm/index.js.map +0 -1
  185. package/build/stats-html.html +0 -4034
  186. package/build/stats-react.json +0 -499
  187. package/build/types/index.d.ts +0 -619
  188. package/build/umd/index.development.js +0 -2514
  189. package/build/umd/index.development.js.map +0 -1
  190. package/build/umd/index.production.js +0 -12
  191. package/build/umd/index.production.js.map +0 -1
  192. package/src/frameworks.ts +0 -12
  193. package/src/routeConfig.ts +0 -495
  194. package/src/routeMatch.ts +0 -374
package/src/qss.ts CHANGED
@@ -1,54 +1,83 @@
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
- }
1
+ /**
2
+ * Program is a reimplementation of the `qss` package:
3
+ * Copyright (c) Luke Edwards luke.edwards05@gmail.com, MIT License
4
+ * https://github.com/lukeed/qss/blob/master/license.md
5
+ *
6
+ * This reimplementation uses modern browser APIs
7
+ * (namely URLSearchParams) and TypeScript while still
8
+ * maintaining the original functionality and interface.
9
+ */
10
+ import { hasUriEncodedChars } from './utils'
11
+
12
+ /**
13
+ * Encodes an object into a query string.
14
+ * @param obj - The object to encode into a query string.
15
+ * @param [pfx] - An optional prefix to add before the query string.
16
+ * @returns The encoded query string.
17
+ * @example
18
+ * ```
19
+ * // Example input: encode({ token: 'foo', key: 'value' })
20
+ * // Expected output: "token=foo&key=value"
21
+ * ```
22
+ */
23
+ export function encode(obj: any, pfx?: string) {
24
+ const normalizedObject = Object.entries(obj).flatMap(([key, value]) => {
25
+ if (Array.isArray(value)) {
26
+ return value.map((v) => [key, String(v)])
27
+ } else {
28
+ return [[key, String(value)]]
22
29
  }
23
- }
30
+ })
31
+
32
+ const searchParams = new URLSearchParams(normalizedObject)
24
33
 
25
- return (pfx || '') + str
34
+ return (pfx || '') + searchParams.toString()
26
35
  }
27
36
 
28
- function toValue(mix) {
37
+ /**
38
+ * Converts a string value to its appropriate type (string, number, boolean).
39
+ * @param mix - The string value to convert.
40
+ * @returns The converted value.
41
+ * @example
42
+ * // Example input: toValue("123")
43
+ * // Expected output: 123
44
+ */
45
+ function toValue(mix: any) {
29
46
  if (!mix) return ''
30
- var str = decodeURIComponent(mix)
47
+ const str = hasUriEncodedChars(mix)
48
+ ? decodeURIComponent(mix)
49
+ : decodeURIComponent(encodeURIComponent(mix))
50
+
31
51
  if (str === 'false') return false
32
52
  if (str === 'true') return true
33
- if (str.charAt(0) === '0') return str
34
- return +str * 0 === 0 ? +str : str
53
+ return +str * 0 === 0 && +str + '' === str ? +str : str
35
54
  }
36
55
 
37
- export function decode(str) {
38
- var tmp,
39
- k,
40
- out = {},
41
- arr = str.split('&')
42
-
43
- while ((tmp = arr.shift())) {
44
- tmp = tmp.split('=')
45
- k = tmp.shift()
46
- if (out[k] !== void 0) {
47
- out[k] = [].concat(out[k], toValue(tmp.shift()))
56
+ /**
57
+ * Decodes a query string into an object.
58
+ * @param str - The query string to decode.
59
+ * @param [pfx] - An optional prefix to filter out from the query string.
60
+ * @returns The decoded key-value pairs in an object format.
61
+ * @example
62
+ * // Example input: decode("token=foo&key=value")
63
+ * // Expected output: { "token": "foo", "key": "value" }
64
+ */
65
+ export function decode(str: any, pfx?: string): any {
66
+ const searchParamsPart = pfx ? str.slice(pfx.length) : str
67
+ const searchParams = new URLSearchParams(searchParamsPart)
68
+
69
+ const entries = [...searchParams.entries()]
70
+
71
+ return entries.reduce<Record<string, unknown>>((acc, [key, value]) => {
72
+ const previousValue = acc[key]
73
+ if (previousValue == null) {
74
+ acc[key] = toValue(value)
48
75
  } else {
49
- out[k] = toValue(tmp.shift())
76
+ acc[key] = Array.isArray(previousValue)
77
+ ? [...previousValue, toValue(value)]
78
+ : [previousValue, toValue(value)]
50
79
  }
51
- }
52
80
 
53
- return out
81
+ return acc
82
+ }, {})
54
83
  }
@@ -0,0 +1,100 @@
1
+ import type { NavigateOptions } from './link'
2
+ import type { AnyRouter, RegisteredRouter } from './router'
3
+
4
+ export const tsrRedirectHeaderKey = 'X-Tanstack-Router-Redirect-Options'
5
+
6
+ export type AnyRedirect = Redirect<any, any, any, any, any>
7
+
8
+ /**
9
+ * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RedirectType)
10
+ */
11
+ export type Redirect<
12
+ TRouter extends AnyRouter = RegisteredRouter,
13
+ TFrom extends string = string,
14
+ TTo extends string | undefined = undefined,
15
+ TMaskFrom extends string = TFrom,
16
+ TMaskTo extends string = '.',
17
+ > = Response & {
18
+ options: NavigateOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo>
19
+ redirectHandled?: boolean
20
+ }
21
+
22
+ export type RedirectOptions<
23
+ TRouter extends AnyRouter = RegisteredRouter,
24
+ TFrom extends string = string,
25
+ TTo extends string | undefined = undefined,
26
+ TMaskFrom extends string = TFrom,
27
+ TMaskTo extends string = '.',
28
+ > = {
29
+ href?: string
30
+ /**
31
+ * @deprecated Use `statusCode` instead
32
+ **/
33
+ code?: number
34
+ /**
35
+ * The HTTP status code to use when redirecting.
36
+ * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RedirectType#statuscode-property)
37
+ */
38
+ statusCode?: number
39
+ /**
40
+ * If provided, will throw the redirect object instead of returning it. This can be useful in places where `throwing` in a function might cause it to have a return type of `never`. In that case, you can use `redirect({ throw: true })` to throw the redirect object instead of returning it.
41
+ * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RedirectType#throw-property)
42
+ */
43
+ throw?: any
44
+ /**
45
+ * The HTTP headers to use when redirecting.
46
+ * @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/RedirectType#headers-property)
47
+ */
48
+ headers?: HeadersInit
49
+ } & NavigateOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo>
50
+
51
+ export type ResolvedRedirect<
52
+ TRouter extends AnyRouter = RegisteredRouter,
53
+ TFrom extends string = string,
54
+ TTo extends string = '',
55
+ TMaskFrom extends string = TFrom,
56
+ TMaskTo extends string = '',
57
+ > = Redirect<TRouter, TFrom, TTo, TMaskFrom, TMaskTo>
58
+
59
+ export function redirect<
60
+ TRouter extends AnyRouter = RegisteredRouter,
61
+ const TTo extends string | undefined = '.',
62
+ const TFrom extends string = string,
63
+ const TMaskFrom extends string = TFrom,
64
+ const TMaskTo extends string = '',
65
+ >(
66
+ opts: RedirectOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo>,
67
+ ): Redirect<TRouter, TFrom, TTo, TMaskFrom, TMaskTo> {
68
+ opts.statusCode = opts.statusCode || opts.code || 307
69
+ const headers = new Headers(opts.headers || {})
70
+
71
+ const response = new Response(null, {
72
+ status: opts.statusCode,
73
+ headers,
74
+ })
75
+
76
+ ;(response as Redirect<TRouter, TFrom, TTo, TMaskFrom, TMaskTo>).options =
77
+ opts
78
+
79
+ if (opts.throw) {
80
+ throw response
81
+ }
82
+
83
+ return response as Redirect<TRouter, TFrom, TTo, TMaskFrom, TMaskTo>
84
+ }
85
+
86
+ export function isRedirect(obj: any): obj is AnyRedirect {
87
+ return obj instanceof Response && !!(obj as any).options
88
+ }
89
+
90
+ export function isResolvedRedirect(obj: any): obj is AnyRedirect {
91
+ return isRedirect(obj) && !!obj.options.href
92
+ }
93
+
94
+ export function parseRedirect(obj: any) {
95
+ if (typeof obj === 'object' && obj.isSerializedRedirect) {
96
+ return redirect(obj)
97
+ }
98
+
99
+ return undefined
100
+ }
package/src/root.ts ADDED
@@ -0,0 +1,2 @@
1
+ export const rootRouteId = '__root__'
2
+ export type RootRouteId = typeof rootRouteId