@benjavicente/router-core 1.168.9
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/README.md +5 -0
- package/bin/intent.js +25 -0
- package/dist/cjs/Matches.cjs +17 -0
- package/dist/cjs/Matches.cjs.map +1 -0
- package/dist/cjs/Matches.d.cts +139 -0
- package/dist/cjs/RouterProvider.d.cts +27 -0
- package/dist/cjs/config.cjs +11 -0
- package/dist/cjs/config.cjs.map +1 -0
- package/dist/cjs/config.d.cts +17 -0
- package/dist/cjs/defer.cjs +41 -0
- package/dist/cjs/defer.cjs.map +1 -0
- package/dist/cjs/defer.d.cts +37 -0
- package/dist/cjs/fileRoute.d.cts +24 -0
- package/dist/cjs/global.d.cts +7 -0
- package/dist/cjs/hash-scroll.cjs +20 -0
- package/dist/cjs/hash-scroll.cjs.map +1 -0
- package/dist/cjs/hash-scroll.d.cts +7 -0
- package/dist/cjs/history.d.cts +8 -0
- package/dist/cjs/index.cjs +96 -0
- package/dist/cjs/index.d.cts +53 -0
- package/dist/cjs/invariant.cjs +8 -0
- package/dist/cjs/invariant.cjs.map +1 -0
- package/dist/cjs/invariant.d.cts +1 -0
- package/dist/cjs/isServer/client.cjs +7 -0
- package/dist/cjs/isServer/client.cjs.map +1 -0
- package/dist/cjs/isServer/client.d.cts +1 -0
- package/dist/cjs/isServer/development.cjs +7 -0
- package/dist/cjs/isServer/development.cjs.map +1 -0
- package/dist/cjs/isServer/development.d.cts +1 -0
- package/dist/cjs/isServer/server.cjs +7 -0
- package/dist/cjs/isServer/server.cjs.map +1 -0
- package/dist/cjs/isServer/server.d.cts +1 -0
- package/dist/cjs/link.cjs +6 -0
- package/dist/cjs/link.cjs.map +1 -0
- package/dist/cjs/link.d.cts +221 -0
- package/dist/cjs/load-matches.cjs +659 -0
- package/dist/cjs/load-matches.cjs.map +1 -0
- package/dist/cjs/load-matches.d.cts +18 -0
- package/dist/cjs/location.d.cts +50 -0
- package/dist/cjs/lru-cache.cjs +70 -0
- package/dist/cjs/lru-cache.cjs.map +1 -0
- package/dist/cjs/lru-cache.d.cts +6 -0
- package/dist/cjs/manifest.cjs +18 -0
- package/dist/cjs/manifest.cjs.map +1 -0
- package/dist/cjs/manifest.d.cts +35 -0
- package/dist/cjs/new-process-route-tree.cjs +754 -0
- package/dist/cjs/new-process-route-tree.cjs.map +1 -0
- package/dist/cjs/new-process-route-tree.d.cts +236 -0
- package/dist/cjs/not-found.cjs +26 -0
- package/dist/cjs/not-found.cjs.map +1 -0
- package/dist/cjs/not-found.d.cts +32 -0
- package/dist/cjs/path.cjs +252 -0
- package/dist/cjs/path.cjs.map +1 -0
- package/dist/cjs/path.d.cts +56 -0
- package/dist/cjs/qss.cjs +70 -0
- package/dist/cjs/qss.cjs.map +1 -0
- package/dist/cjs/qss.d.cts +33 -0
- package/dist/cjs/redirect.cjs +56 -0
- package/dist/cjs/redirect.cjs.map +1 -0
- package/dist/cjs/redirect.d.cts +77 -0
- package/dist/cjs/rewrite.cjs +68 -0
- package/dist/cjs/rewrite.cjs.map +1 -0
- package/dist/cjs/rewrite.d.cts +30 -0
- package/dist/cjs/root.cjs +7 -0
- package/dist/cjs/root.cjs.map +1 -0
- package/dist/cjs/root.d.cts +3 -0
- package/dist/cjs/route.cjs +100 -0
- package/dist/cjs/route.cjs.map +1 -0
- package/dist/cjs/route.d.cts +552 -0
- package/dist/cjs/routeInfo.d.cts +54 -0
- package/dist/cjs/router.cjs +1173 -0
- package/dist/cjs/router.cjs.map +1 -0
- package/dist/cjs/router.d.cts +734 -0
- package/dist/cjs/scroll-restoration-inline.cjs +6 -0
- package/dist/cjs/scroll-restoration-inline.cjs.map +1 -0
- package/dist/cjs/scroll-restoration-inline.d.cts +6 -0
- package/dist/cjs/scroll-restoration-script/client.cjs +9 -0
- package/dist/cjs/scroll-restoration-script/client.cjs.map +1 -0
- package/dist/cjs/scroll-restoration-script/client.d.cts +2 -0
- package/dist/cjs/scroll-restoration-script/server.cjs +30 -0
- package/dist/cjs/scroll-restoration-script/server.cjs.map +1 -0
- package/dist/cjs/scroll-restoration-script/server.d.cts +2 -0
- package/dist/cjs/scroll-restoration.cjs +191 -0
- package/dist/cjs/scroll-restoration.cjs.map +1 -0
- package/dist/cjs/scroll-restoration.d.cts +38 -0
- package/dist/cjs/searchMiddleware.cjs +55 -0
- package/dist/cjs/searchMiddleware.cjs.map +1 -0
- package/dist/cjs/searchMiddleware.d.cts +25 -0
- package/dist/cjs/searchParams.cjs +65 -0
- package/dist/cjs/searchParams.cjs.map +1 -0
- package/dist/cjs/searchParams.d.cts +31 -0
- package/dist/cjs/ssr/client.cjs +7 -0
- package/dist/cjs/ssr/client.d.cts +6 -0
- package/dist/cjs/ssr/constants.cjs +8 -0
- package/dist/cjs/ssr/constants.cjs.map +1 -0
- package/dist/cjs/ssr/constants.d.cts +3 -0
- package/dist/cjs/ssr/createRequestHandler.cjs +44 -0
- package/dist/cjs/ssr/createRequestHandler.cjs.map +1 -0
- package/dist/cjs/ssr/createRequestHandler.d.cts +9 -0
- package/dist/cjs/ssr/handlerCallback.cjs +8 -0
- package/dist/cjs/ssr/handlerCallback.cjs.map +1 -0
- package/dist/cjs/ssr/handlerCallback.d.cts +9 -0
- package/dist/cjs/ssr/headers.cjs +21 -0
- package/dist/cjs/ssr/headers.cjs.map +1 -0
- package/dist/cjs/ssr/headers.d.cts +3 -0
- package/dist/cjs/ssr/json.cjs +11 -0
- package/dist/cjs/ssr/json.cjs.map +1 -0
- package/dist/cjs/ssr/json.d.cts +10 -0
- package/dist/cjs/ssr/serializer/RawStream.cjs +287 -0
- package/dist/cjs/ssr/serializer/RawStream.cjs.map +1 -0
- package/dist/cjs/ssr/serializer/RawStream.d.cts +64 -0
- package/dist/cjs/ssr/serializer/ShallowErrorPlugin.cjs +32 -0
- package/dist/cjs/ssr/serializer/ShallowErrorPlugin.cjs.map +1 -0
- package/dist/cjs/ssr/serializer/ShallowErrorPlugin.d.cts +9 -0
- package/dist/cjs/ssr/serializer/seroval-plugins.cjs +13 -0
- package/dist/cjs/ssr/serializer/seroval-plugins.cjs.map +1 -0
- package/dist/cjs/ssr/serializer/seroval-plugins.d.cts +2 -0
- package/dist/cjs/ssr/serializer/transformer.cjs +53 -0
- package/dist/cjs/ssr/serializer/transformer.cjs.map +1 -0
- package/dist/cjs/ssr/serializer/transformer.d.cts +91 -0
- package/dist/cjs/ssr/server.cjs +13 -0
- package/dist/cjs/ssr/server.d.cts +6 -0
- package/dist/cjs/ssr/ssr-client.cjs +183 -0
- package/dist/cjs/ssr/ssr-client.cjs.map +1 -0
- package/dist/cjs/ssr/ssr-client.d.cts +10 -0
- package/dist/cjs/ssr/ssr-match-id.cjs +12 -0
- package/dist/cjs/ssr/ssr-match-id.cjs.map +1 -0
- package/dist/cjs/ssr/ssr-match-id.d.cts +2 -0
- package/dist/cjs/ssr/ssr-server.cjs +279 -0
- package/dist/cjs/ssr/ssr-server.cjs.map +1 -0
- package/dist/cjs/ssr/ssr-server.d.cts +42 -0
- package/dist/cjs/ssr/transformStreamWithRouter.cjs +327 -0
- package/dist/cjs/ssr/transformStreamWithRouter.cjs.map +1 -0
- package/dist/cjs/ssr/transformStreamWithRouter.d.cts +11 -0
- package/dist/cjs/ssr/tsrScript.cjs +6 -0
- package/dist/cjs/ssr/tsrScript.cjs.map +1 -0
- package/dist/cjs/ssr/tsrScript.d.cts +1 -0
- package/dist/cjs/ssr/types.d.cts +30 -0
- package/dist/cjs/stores.cjs +148 -0
- package/dist/cjs/stores.cjs.map +1 -0
- package/dist/cjs/stores.d.cts +70 -0
- package/dist/cjs/structuralSharing.d.cts +4 -0
- package/dist/cjs/typePrimitives.d.cts +65 -0
- package/dist/cjs/useLoaderData.d.cts +5 -0
- package/dist/cjs/useLoaderDeps.d.cts +5 -0
- package/dist/cjs/useNavigate.d.cts +3 -0
- package/dist/cjs/useParams.d.cts +5 -0
- package/dist/cjs/useRouteContext.d.cts +9 -0
- package/dist/cjs/useSearch.d.cts +5 -0
- package/dist/cjs/utils.cjs +339 -0
- package/dist/cjs/utils.cjs.map +1 -0
- package/dist/cjs/utils.d.cts +178 -0
- package/dist/cjs/validators.d.cts +51 -0
- package/dist/esm/Matches.d.ts +139 -0
- package/dist/esm/Matches.js +17 -0
- package/dist/esm/Matches.js.map +1 -0
- package/dist/esm/RouterProvider.d.ts +27 -0
- package/dist/esm/config.d.ts +17 -0
- package/dist/esm/config.js +11 -0
- package/dist/esm/config.js.map +1 -0
- package/dist/esm/defer.d.ts +37 -0
- package/dist/esm/defer.js +40 -0
- package/dist/esm/defer.js.map +1 -0
- package/dist/esm/fileRoute.d.ts +24 -0
- package/dist/esm/global.d.ts +7 -0
- package/dist/esm/hash-scroll.d.ts +7 -0
- package/dist/esm/hash-scroll.js +20 -0
- package/dist/esm/hash-scroll.js.map +1 -0
- package/dist/esm/history.d.ts +8 -0
- package/dist/esm/index.d.ts +53 -0
- package/dist/esm/index.js +24 -0
- package/dist/esm/invariant.d.ts +1 -0
- package/dist/esm/invariant.js +8 -0
- package/dist/esm/invariant.js.map +1 -0
- package/dist/esm/isServer/client.d.ts +1 -0
- package/dist/esm/isServer/client.js +6 -0
- package/dist/esm/isServer/client.js.map +1 -0
- package/dist/esm/isServer/development.d.ts +1 -0
- package/dist/esm/isServer/development.js +6 -0
- package/dist/esm/isServer/development.js.map +1 -0
- package/dist/esm/isServer/server.d.ts +1 -0
- package/dist/esm/isServer/server.js +6 -0
- package/dist/esm/isServer/server.js.map +1 -0
- package/dist/esm/link.d.ts +221 -0
- package/dist/esm/link.js +6 -0
- package/dist/esm/link.js.map +1 -0
- package/dist/esm/load-matches.d.ts +18 -0
- package/dist/esm/load-matches.js +657 -0
- package/dist/esm/load-matches.js.map +1 -0
- package/dist/esm/location.d.ts +50 -0
- package/dist/esm/lru-cache.d.ts +6 -0
- package/dist/esm/lru-cache.js +70 -0
- package/dist/esm/lru-cache.js.map +1 -0
- package/dist/esm/manifest.d.ts +35 -0
- package/dist/esm/manifest.js +17 -0
- package/dist/esm/manifest.js.map +1 -0
- package/dist/esm/new-process-route-tree.d.ts +236 -0
- package/dist/esm/new-process-route-tree.js +749 -0
- package/dist/esm/new-process-route-tree.js.map +1 -0
- package/dist/esm/not-found.d.ts +32 -0
- package/dist/esm/not-found.js +25 -0
- package/dist/esm/not-found.js.map +1 -0
- package/dist/esm/path.d.ts +56 -0
- package/dist/esm/path.js +243 -0
- package/dist/esm/path.js.map +1 -0
- package/dist/esm/qss.d.ts +33 -0
- package/dist/esm/qss.js +69 -0
- package/dist/esm/qss.js.map +1 -0
- package/dist/esm/redirect.d.ts +77 -0
- package/dist/esm/redirect.js +53 -0
- package/dist/esm/redirect.js.map +1 -0
- package/dist/esm/rewrite.d.ts +30 -0
- package/dist/esm/rewrite.js +65 -0
- package/dist/esm/rewrite.js.map +1 -0
- package/dist/esm/root.d.ts +3 -0
- package/dist/esm/root.js +7 -0
- package/dist/esm/root.js.map +1 -0
- package/dist/esm/route.d.ts +552 -0
- package/dist/esm/route.js +98 -0
- package/dist/esm/route.js.map +1 -0
- package/dist/esm/routeInfo.d.ts +54 -0
- package/dist/esm/router.d.ts +734 -0
- package/dist/esm/router.js +1165 -0
- package/dist/esm/router.js.map +1 -0
- package/dist/esm/scroll-restoration-inline.d.ts +6 -0
- package/dist/esm/scroll-restoration-inline.js +6 -0
- package/dist/esm/scroll-restoration-inline.js.map +1 -0
- package/dist/esm/scroll-restoration-script/client.d.ts +2 -0
- package/dist/esm/scroll-restoration-script/client.js +8 -0
- package/dist/esm/scroll-restoration-script/client.js.map +1 -0
- package/dist/esm/scroll-restoration-script/server.d.ts +2 -0
- package/dist/esm/scroll-restoration-script/server.js +29 -0
- package/dist/esm/scroll-restoration-script/server.js.map +1 -0
- package/dist/esm/scroll-restoration.d.ts +38 -0
- package/dist/esm/scroll-restoration.js +187 -0
- package/dist/esm/scroll-restoration.js.map +1 -0
- package/dist/esm/searchMiddleware.d.ts +25 -0
- package/dist/esm/searchMiddleware.js +54 -0
- package/dist/esm/searchMiddleware.js.map +1 -0
- package/dist/esm/searchParams.d.ts +31 -0
- package/dist/esm/searchParams.js +62 -0
- package/dist/esm/searchParams.js.map +1 -0
- package/dist/esm/ssr/client.d.ts +6 -0
- package/dist/esm/ssr/client.js +4 -0
- package/dist/esm/ssr/constants.d.ts +3 -0
- package/dist/esm/ssr/constants.js +7 -0
- package/dist/esm/ssr/constants.js.map +1 -0
- package/dist/esm/ssr/createRequestHandler.d.ts +9 -0
- package/dist/esm/ssr/createRequestHandler.js +44 -0
- package/dist/esm/ssr/createRequestHandler.js.map +1 -0
- package/dist/esm/ssr/handlerCallback.d.ts +9 -0
- package/dist/esm/ssr/handlerCallback.js +8 -0
- package/dist/esm/ssr/handlerCallback.js.map +1 -0
- package/dist/esm/ssr/headers.d.ts +3 -0
- package/dist/esm/ssr/headers.js +21 -0
- package/dist/esm/ssr/headers.js.map +1 -0
- package/dist/esm/ssr/json.d.ts +10 -0
- package/dist/esm/ssr/json.js +11 -0
- package/dist/esm/ssr/json.js.map +1 -0
- package/dist/esm/ssr/serializer/RawStream.d.ts +64 -0
- package/dist/esm/ssr/serializer/RawStream.js +282 -0
- package/dist/esm/ssr/serializer/RawStream.js.map +1 -0
- package/dist/esm/ssr/serializer/ShallowErrorPlugin.d.ts +9 -0
- package/dist/esm/ssr/serializer/ShallowErrorPlugin.js +33 -0
- package/dist/esm/ssr/serializer/ShallowErrorPlugin.js.map +1 -0
- package/dist/esm/ssr/serializer/seroval-plugins.d.ts +2 -0
- package/dist/esm/ssr/serializer/seroval-plugins.js +13 -0
- package/dist/esm/ssr/serializer/seroval-plugins.js.map +1 -0
- package/dist/esm/ssr/serializer/transformer.d.ts +91 -0
- package/dist/esm/ssr/serializer/transformer.js +51 -0
- package/dist/esm/ssr/serializer/transformer.js.map +1 -0
- package/dist/esm/ssr/server.d.ts +6 -0
- package/dist/esm/ssr/server.js +5 -0
- package/dist/esm/ssr/ssr-client.d.ts +10 -0
- package/dist/esm/ssr/ssr-client.js +183 -0
- package/dist/esm/ssr/ssr-client.js.map +1 -0
- package/dist/esm/ssr/ssr-match-id.d.ts +2 -0
- package/dist/esm/ssr/ssr-match-id.js +11 -0
- package/dist/esm/ssr/ssr-match-id.js.map +1 -0
- package/dist/esm/ssr/ssr-server.d.ts +42 -0
- package/dist/esm/ssr/ssr-server.js +277 -0
- package/dist/esm/ssr/ssr-server.js.map +1 -0
- package/dist/esm/ssr/transformStreamWithRouter.d.ts +11 -0
- package/dist/esm/ssr/transformStreamWithRouter.js +325 -0
- package/dist/esm/ssr/transformStreamWithRouter.js.map +1 -0
- package/dist/esm/ssr/tsrScript.d.ts +0 -0
- package/dist/esm/ssr/tsrScript.js +6 -0
- package/dist/esm/ssr/tsrScript.js.map +1 -0
- package/dist/esm/ssr/types.d.ts +30 -0
- package/dist/esm/stores.d.ts +70 -0
- package/dist/esm/stores.js +146 -0
- package/dist/esm/stores.js.map +1 -0
- package/dist/esm/structuralSharing.d.ts +4 -0
- package/dist/esm/typePrimitives.d.ts +65 -0
- package/dist/esm/useLoaderData.d.ts +5 -0
- package/dist/esm/useLoaderDeps.d.ts +5 -0
- package/dist/esm/useNavigate.d.ts +3 -0
- package/dist/esm/useParams.d.ts +5 -0
- package/dist/esm/useRouteContext.d.ts +9 -0
- package/dist/esm/useSearch.d.ts +5 -0
- package/dist/esm/utils.d.ts +178 -0
- package/dist/esm/utils.js +322 -0
- package/dist/esm/utils.js.map +1 -0
- package/dist/esm/validators.d.ts +51 -0
- package/package.json +200 -0
- package/skills/router-core/SKILL.md +139 -0
- package/skills/router-core/auth-and-guards/SKILL.md +458 -0
- package/skills/router-core/code-splitting/SKILL.md +322 -0
- package/skills/router-core/data-loading/SKILL.md +485 -0
- package/skills/router-core/navigation/SKILL.md +448 -0
- package/skills/router-core/not-found-and-errors/SKILL.md +435 -0
- package/skills/router-core/path-params/SKILL.md +382 -0
- package/skills/router-core/search-params/SKILL.md +349 -0
- package/skills/router-core/search-params/references/validation-patterns.md +379 -0
- package/skills/router-core/ssr/SKILL.md +437 -0
- package/skills/router-core/type-safety/SKILL.md +497 -0
- package/src/Matches.ts +291 -0
- package/src/RouterProvider.ts +47 -0
- package/src/config.ts +42 -0
- package/src/defer.ts +69 -0
- package/src/fileRoute.ts +164 -0
- package/src/global.ts +9 -0
- package/src/hash-scroll.ts +21 -0
- package/src/history.ts +9 -0
- package/src/index.ts +471 -0
- package/src/invariant.ts +3 -0
- package/src/isServer/client.ts +1 -0
- package/src/isServer/development.ts +2 -0
- package/src/isServer/server.ts +1 -0
- package/src/link.ts +704 -0
- package/src/load-matches.ts +1281 -0
- package/src/location.ts +51 -0
- package/src/lru-cache.ts +74 -0
- package/src/manifest.ts +68 -0
- package/src/new-process-route-tree.ts +1387 -0
- package/src/not-found.ts +41 -0
- package/src/path.ts +436 -0
- package/src/qss.ts +81 -0
- package/src/redirect.ts +179 -0
- package/src/rewrite.ts +93 -0
- package/src/root.ts +3 -0
- package/src/route.ts +2235 -0
- package/src/routeInfo.ts +235 -0
- package/src/router.ts +3207 -0
- package/src/scroll-restoration-inline.ts +81 -0
- package/src/scroll-restoration-script/client.ts +5 -0
- package/src/scroll-restoration-script/server.ts +64 -0
- package/src/scroll-restoration.ts +357 -0
- package/src/searchMiddleware.ts +76 -0
- package/src/searchParams.ts +90 -0
- package/src/ssr/client.ts +6 -0
- package/src/ssr/constants.ts +3 -0
- package/src/ssr/createRequestHandler.ts +98 -0
- package/src/ssr/handlerCallback.ts +15 -0
- package/src/ssr/headers.ts +40 -0
- package/src/ssr/json.ts +16 -0
- package/src/ssr/serializer/RawStream.ts +464 -0
- package/src/ssr/serializer/ShallowErrorPlugin.ts +43 -0
- package/src/ssr/serializer/seroval-plugins.ts +12 -0
- package/src/ssr/serializer/transformer.ts +312 -0
- package/src/ssr/server.ts +14 -0
- package/src/ssr/ssr-client.ts +313 -0
- package/src/ssr/ssr-match-id.ts +7 -0
- package/src/ssr/ssr-server.ts +425 -0
- package/src/ssr/transformStreamWithRouter.ts +493 -0
- package/src/ssr/tsrScript.ts +20 -0
- package/src/ssr/types.ts +41 -0
- package/src/stores.ts +342 -0
- package/src/structuralSharing.ts +7 -0
- package/src/typePrimitives.ts +181 -0
- package/src/useLoaderData.ts +20 -0
- package/src/useLoaderDeps.ts +13 -0
- package/src/useNavigate.ts +13 -0
- package/src/useParams.ts +20 -0
- package/src/useRouteContext.ts +39 -0
- package/src/useSearch.ts +20 -0
- package/src/utils.ts +708 -0
- package/src/validators.ts +121 -0
- package/src/vite-env.d.ts +4 -0
package/src/not-found.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { RouteIds } from './routeInfo'
|
|
2
|
+
import type { RegisteredRouter } from './router'
|
|
3
|
+
|
|
4
|
+
export type NotFoundError = {
|
|
5
|
+
/**
|
|
6
|
+
@deprecated
|
|
7
|
+
Use `routeId: rootRouteId` instead
|
|
8
|
+
*/
|
|
9
|
+
global?: boolean
|
|
10
|
+
/**
|
|
11
|
+
@private
|
|
12
|
+
Do not use this. It's used internally to indicate a path matching error
|
|
13
|
+
*/
|
|
14
|
+
_global?: boolean
|
|
15
|
+
data?: any
|
|
16
|
+
throw?: boolean
|
|
17
|
+
routeId?: RouteIds<RegisteredRouter['routeTree']>
|
|
18
|
+
headers?: HeadersInit
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Create a not-found error object recognized by TanStack Router.
|
|
23
|
+
*
|
|
24
|
+
* Throw this from loaders/actions to trigger the nearest `notFoundComponent`.
|
|
25
|
+
* Use `routeId` to target a specific route's not-found boundary. If `throw`
|
|
26
|
+
* is true, the error is thrown instead of returned.
|
|
27
|
+
*
|
|
28
|
+
* @param options Optional settings including `routeId`, `headers`, and `throw`.
|
|
29
|
+
* @returns A not-found error object that can be thrown or returned.
|
|
30
|
+
* @link https://tanstack.com/router/latest/docs/router/framework/react/api/router/notFoundFunction
|
|
31
|
+
*/
|
|
32
|
+
export function notFound(options: NotFoundError = {}) {
|
|
33
|
+
;(options as any).isNotFound = true
|
|
34
|
+
if (options.throw) throw options
|
|
35
|
+
return options
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Determine if a value is a TanStack Router not-found error. */
|
|
39
|
+
export function isNotFound(obj: any): obj is NotFoundError {
|
|
40
|
+
return !!obj?.isNotFound
|
|
41
|
+
}
|
package/src/path.ts
ADDED
|
@@ -0,0 +1,436 @@
|
|
|
1
|
+
import { isServer } from '@benjavicente/router-core/isServer'
|
|
2
|
+
import { last } from './utils'
|
|
3
|
+
import {
|
|
4
|
+
SEGMENT_TYPE_OPTIONAL_PARAM,
|
|
5
|
+
SEGMENT_TYPE_PARAM,
|
|
6
|
+
SEGMENT_TYPE_PATHNAME,
|
|
7
|
+
SEGMENT_TYPE_WILDCARD,
|
|
8
|
+
parseSegment,
|
|
9
|
+
} from './new-process-route-tree'
|
|
10
|
+
import type { LRUCache } from './lru-cache'
|
|
11
|
+
|
|
12
|
+
/** Join path segments, cleaning duplicate slashes between parts. */
|
|
13
|
+
export function joinPaths(paths: Array<string | undefined>) {
|
|
14
|
+
return cleanPath(
|
|
15
|
+
paths
|
|
16
|
+
.filter((val) => {
|
|
17
|
+
return val !== undefined
|
|
18
|
+
})
|
|
19
|
+
.join('/'),
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Remove repeated slashes from a path string. */
|
|
24
|
+
export function cleanPath(path: string) {
|
|
25
|
+
// remove double slashes
|
|
26
|
+
return path.replace(/\/{2,}/g, '/')
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Trim leading slashes (except preserving root '/'). */
|
|
30
|
+
export function trimPathLeft(path: string) {
|
|
31
|
+
return path === '/' ? path : path.replace(/^\/{1,}/, '')
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Trim trailing slashes (except preserving root '/'). */
|
|
35
|
+
export function trimPathRight(path: string) {
|
|
36
|
+
const len = path.length
|
|
37
|
+
return len > 1 && path[len - 1] === '/' ? path.replace(/\/{1,}$/, '') : path
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Trim both leading and trailing slashes. */
|
|
41
|
+
export function trimPath(path: string) {
|
|
42
|
+
return trimPathRight(trimPathLeft(path))
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Remove a trailing slash from value when appropriate for comparisons. */
|
|
46
|
+
export function removeTrailingSlash(value: string, basepath: string): string {
|
|
47
|
+
if (value?.endsWith('/') && value !== '/' && value !== `${basepath}/`) {
|
|
48
|
+
return value.slice(0, -1)
|
|
49
|
+
}
|
|
50
|
+
return value
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// intended to only compare path name
|
|
54
|
+
// see the usage in the isActive under useLinkProps
|
|
55
|
+
// /sample/path1 = /sample/path1/
|
|
56
|
+
// /sample/path1/some <> /sample/path1
|
|
57
|
+
/**
|
|
58
|
+
* Compare two pathnames for exact equality after normalizing trailing slashes
|
|
59
|
+
* relative to the provided `basepath`.
|
|
60
|
+
*/
|
|
61
|
+
export function exactPathTest(
|
|
62
|
+
pathName1: string,
|
|
63
|
+
pathName2: string,
|
|
64
|
+
basepath: string,
|
|
65
|
+
): boolean {
|
|
66
|
+
return (
|
|
67
|
+
removeTrailingSlash(pathName1, basepath) ===
|
|
68
|
+
removeTrailingSlash(pathName2, basepath)
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// When resolving relative paths, we treat all paths as if they are trailing slash
|
|
73
|
+
// documents. All trailing slashes are removed after the path is resolved.
|
|
74
|
+
// Here are a few examples:
|
|
75
|
+
//
|
|
76
|
+
// /a/b/c + ./d = /a/b/c/d
|
|
77
|
+
// /a/b/c + ../d = /a/b/d
|
|
78
|
+
// /a/b/c + ./d/ = /a/b/c/d
|
|
79
|
+
// /a/b/c + ../d/ = /a/b/d
|
|
80
|
+
// /a/b/c + ./ = /a/b/c
|
|
81
|
+
//
|
|
82
|
+
// Absolute paths that start with `/` short circuit the resolution process to the root
|
|
83
|
+
// path.
|
|
84
|
+
//
|
|
85
|
+
// Here are some examples:
|
|
86
|
+
//
|
|
87
|
+
// /a/b/c + /d = /d
|
|
88
|
+
// /a/b/c + /d/ = /d
|
|
89
|
+
// /a/b/c + / = /
|
|
90
|
+
//
|
|
91
|
+
// Non-.-prefixed paths are still treated as relative paths, resolved like `./`
|
|
92
|
+
//
|
|
93
|
+
// Here are some examples:
|
|
94
|
+
//
|
|
95
|
+
// /a/b/c + d = /a/b/c/d
|
|
96
|
+
// /a/b/c + d/ = /a/b/c/d
|
|
97
|
+
// /a/b/c + d/e = /a/b/c/d/e
|
|
98
|
+
interface ResolvePathOptions {
|
|
99
|
+
base: string
|
|
100
|
+
to: string
|
|
101
|
+
trailingSlash?: 'always' | 'never' | 'preserve'
|
|
102
|
+
cache?: LRUCache<string, string>
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Resolve a destination path against a base, honoring trailing-slash policy
|
|
107
|
+
* and supporting relative segments (`.`/`..`) and absolute `to` values.
|
|
108
|
+
*/
|
|
109
|
+
export function resolvePath({
|
|
110
|
+
base,
|
|
111
|
+
to,
|
|
112
|
+
trailingSlash = 'never',
|
|
113
|
+
cache,
|
|
114
|
+
}: ResolvePathOptions) {
|
|
115
|
+
const isAbsolute = to.startsWith('/')
|
|
116
|
+
const isBase = !isAbsolute && to === '.'
|
|
117
|
+
|
|
118
|
+
let key
|
|
119
|
+
if (cache) {
|
|
120
|
+
// `trailingSlash` is static per router, so it doesn't need to be part of the cache key
|
|
121
|
+
key = isAbsolute ? to : isBase ? base : base + '\0' + to
|
|
122
|
+
const cached = cache.get(key)
|
|
123
|
+
if (cached) return cached
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
let baseSegments: Array<string>
|
|
127
|
+
if (isBase) {
|
|
128
|
+
baseSegments = base.split('/')
|
|
129
|
+
} else if (isAbsolute) {
|
|
130
|
+
baseSegments = to.split('/')
|
|
131
|
+
} else {
|
|
132
|
+
baseSegments = base.split('/')
|
|
133
|
+
while (baseSegments.length > 1 && last(baseSegments) === '') {
|
|
134
|
+
baseSegments.pop()
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const toSegments = to.split('/')
|
|
138
|
+
for (let index = 0, length = toSegments.length; index < length; index++) {
|
|
139
|
+
const value = toSegments[index]!
|
|
140
|
+
if (value === '') {
|
|
141
|
+
if (!index) {
|
|
142
|
+
// Leading slash
|
|
143
|
+
baseSegments = [value]
|
|
144
|
+
} else if (index === length - 1) {
|
|
145
|
+
// Trailing Slash
|
|
146
|
+
baseSegments.push(value)
|
|
147
|
+
} else {
|
|
148
|
+
// ignore inter-slashes
|
|
149
|
+
}
|
|
150
|
+
} else if (value === '..') {
|
|
151
|
+
baseSegments.pop()
|
|
152
|
+
} else if (value === '.') {
|
|
153
|
+
// ignore
|
|
154
|
+
} else {
|
|
155
|
+
baseSegments.push(value)
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (baseSegments.length > 1) {
|
|
161
|
+
if (last(baseSegments) === '') {
|
|
162
|
+
if (trailingSlash === 'never') {
|
|
163
|
+
baseSegments.pop()
|
|
164
|
+
}
|
|
165
|
+
} else if (trailingSlash === 'always') {
|
|
166
|
+
baseSegments.push('')
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
let segment
|
|
171
|
+
let joined = ''
|
|
172
|
+
for (let i = 0; i < baseSegments.length; i++) {
|
|
173
|
+
if (i > 0) joined += '/'
|
|
174
|
+
const part = baseSegments[i]!
|
|
175
|
+
if (!part) continue
|
|
176
|
+
segment = parseSegment(part, 0, segment)
|
|
177
|
+
const kind = segment[0]
|
|
178
|
+
if (kind === SEGMENT_TYPE_PATHNAME) {
|
|
179
|
+
joined += part
|
|
180
|
+
continue
|
|
181
|
+
}
|
|
182
|
+
const end = segment[5]
|
|
183
|
+
const prefix = part.substring(0, segment[1])
|
|
184
|
+
const suffix = part.substring(segment[4], end)
|
|
185
|
+
const value = part.substring(segment[2], segment[3])
|
|
186
|
+
if (kind === SEGMENT_TYPE_PARAM) {
|
|
187
|
+
joined += prefix || suffix ? `${prefix}{$${value}}${suffix}` : `$${value}`
|
|
188
|
+
} else if (kind === SEGMENT_TYPE_WILDCARD) {
|
|
189
|
+
joined += prefix || suffix ? `${prefix}{$}${suffix}` : '$'
|
|
190
|
+
} else {
|
|
191
|
+
// SEGMENT_TYPE_OPTIONAL_PARAM
|
|
192
|
+
joined += `${prefix}{-$${value}}${suffix}`
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
joined = cleanPath(joined)
|
|
196
|
+
const result = joined || '/'
|
|
197
|
+
if (key && cache) cache.set(key, result)
|
|
198
|
+
return result
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Create a pre-compiled decode config from allowed characters.
|
|
203
|
+
* This should be called once at router initialization.
|
|
204
|
+
*/
|
|
205
|
+
export function compileDecodeCharMap(
|
|
206
|
+
pathParamsAllowedCharacters: ReadonlyArray<string>,
|
|
207
|
+
) {
|
|
208
|
+
const charMap = new Map(
|
|
209
|
+
pathParamsAllowedCharacters.map((char) => [encodeURIComponent(char), char]),
|
|
210
|
+
)
|
|
211
|
+
// Escape special regex characters and join with |
|
|
212
|
+
const pattern = Array.from(charMap.keys())
|
|
213
|
+
.map((key) => key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
|
|
214
|
+
.join('|')
|
|
215
|
+
const regex = new RegExp(pattern, 'g')
|
|
216
|
+
return (encoded: string) =>
|
|
217
|
+
encoded.replace(regex, (match) => charMap.get(match) ?? match)
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
interface InterpolatePathOptions {
|
|
221
|
+
path?: string
|
|
222
|
+
params: Record<string, unknown>
|
|
223
|
+
/**
|
|
224
|
+
* A function that decodes a path parameter value.
|
|
225
|
+
* Obtained from `compileDecodeCharMap(pathParamsAllowedCharacters)`.
|
|
226
|
+
*/
|
|
227
|
+
decoder?: (encoded: string) => string
|
|
228
|
+
/**
|
|
229
|
+
* @internal
|
|
230
|
+
* For testing only, in development mode we use the router.isServer value
|
|
231
|
+
*/
|
|
232
|
+
server?: boolean
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
type InterPolatePathResult = {
|
|
236
|
+
interpolatedPath: string
|
|
237
|
+
usedParams: Record<string, unknown>
|
|
238
|
+
isMissingParams: boolean // true if any params were not available when being looked up in the params object
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function encodeParam(
|
|
242
|
+
key: string,
|
|
243
|
+
params: InterpolatePathOptions['params'],
|
|
244
|
+
decoder: InterpolatePathOptions['decoder'],
|
|
245
|
+
): any {
|
|
246
|
+
const value = params[key]
|
|
247
|
+
if (typeof value !== 'string') return value
|
|
248
|
+
|
|
249
|
+
if (key === '_splat') {
|
|
250
|
+
// Early return if value only contains URL-safe characters (performance optimization)
|
|
251
|
+
if (/^[a-zA-Z0-9\-._~!/]*$/.test(value)) return value
|
|
252
|
+
// the splat/catch-all routes shouldn't have the '/' encoded out
|
|
253
|
+
// Use encodeURIComponent for each segment to properly encode spaces,
|
|
254
|
+
// plus signs, and other special characters that encodeURI leaves unencoded
|
|
255
|
+
return value
|
|
256
|
+
.split('/')
|
|
257
|
+
.map((segment) => encodePathParam(segment, decoder))
|
|
258
|
+
.join('/')
|
|
259
|
+
} else {
|
|
260
|
+
return encodePathParam(value, decoder)
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Interpolate params and wildcards into a route path template.
|
|
266
|
+
*
|
|
267
|
+
* - Encodes params safely (configurable allowed characters)
|
|
268
|
+
* - Supports `{-$optional}` segments, `{prefix{$id}suffix}` and `{$}` wildcards
|
|
269
|
+
*/
|
|
270
|
+
export function interpolatePath({
|
|
271
|
+
path,
|
|
272
|
+
params,
|
|
273
|
+
decoder,
|
|
274
|
+
// `server` is marked @internal and stripped from .d.ts by `stripInternal`.
|
|
275
|
+
// We avoid destructuring it in the function signature so the emitted
|
|
276
|
+
// declaration doesn't reference a property that no longer exists.
|
|
277
|
+
...rest
|
|
278
|
+
}: InterpolatePathOptions): InterPolatePathResult {
|
|
279
|
+
// Tracking if any params are missing in the `params` object
|
|
280
|
+
// when interpolating the path
|
|
281
|
+
let isMissingParams = false
|
|
282
|
+
const usedParams: Record<string, unknown> = Object.create(null)
|
|
283
|
+
|
|
284
|
+
if (!path || path === '/')
|
|
285
|
+
return { interpolatedPath: '/', usedParams, isMissingParams }
|
|
286
|
+
if (!path.includes('$'))
|
|
287
|
+
return { interpolatedPath: path, usedParams, isMissingParams }
|
|
288
|
+
|
|
289
|
+
if (isServer ?? rest.server) {
|
|
290
|
+
// Fast path for common templates like `/posts/$id` or `/files/$`.
|
|
291
|
+
// Braced segments (`{...}`) are more complex (prefix/suffix/optional) and are
|
|
292
|
+
// handled by the general parser below.
|
|
293
|
+
if (path.indexOf('{') === -1) {
|
|
294
|
+
const length = path.length
|
|
295
|
+
let cursor = 0
|
|
296
|
+
let joined = ''
|
|
297
|
+
|
|
298
|
+
while (cursor < length) {
|
|
299
|
+
// Skip slashes between segments. '/' code is 47
|
|
300
|
+
while (cursor < length && path.charCodeAt(cursor) === 47) cursor++
|
|
301
|
+
if (cursor >= length) break
|
|
302
|
+
|
|
303
|
+
const start = cursor
|
|
304
|
+
let end = path.indexOf('/', cursor)
|
|
305
|
+
if (end === -1) end = length
|
|
306
|
+
cursor = end
|
|
307
|
+
|
|
308
|
+
const part = path.substring(start, end)
|
|
309
|
+
if (!part) continue
|
|
310
|
+
|
|
311
|
+
// `$id` or `$` (splat). '$' code is 36
|
|
312
|
+
if (part.charCodeAt(0) === 36) {
|
|
313
|
+
if (part.length === 1) {
|
|
314
|
+
const splat = params._splat
|
|
315
|
+
usedParams._splat = splat
|
|
316
|
+
// TODO: Deprecate *
|
|
317
|
+
usedParams['*'] = splat
|
|
318
|
+
|
|
319
|
+
if (!splat) {
|
|
320
|
+
isMissingParams = true
|
|
321
|
+
continue
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const value = encodeParam('_splat', params, decoder)
|
|
325
|
+
joined += '/' + value
|
|
326
|
+
} else {
|
|
327
|
+
const key = part.substring(1)
|
|
328
|
+
if (!isMissingParams && !(key in params)) {
|
|
329
|
+
isMissingParams = true
|
|
330
|
+
}
|
|
331
|
+
usedParams[key] = params[key]
|
|
332
|
+
|
|
333
|
+
const value = encodeParam(key, params, decoder) ?? 'undefined'
|
|
334
|
+
joined += '/' + value
|
|
335
|
+
}
|
|
336
|
+
} else {
|
|
337
|
+
joined += '/' + part
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
if (path.endsWith('/')) joined += '/'
|
|
342
|
+
|
|
343
|
+
const interpolatedPath = joined || '/'
|
|
344
|
+
return { usedParams, interpolatedPath, isMissingParams }
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const length = path.length
|
|
349
|
+
let cursor = 0
|
|
350
|
+
let segment
|
|
351
|
+
let joined = ''
|
|
352
|
+
while (cursor < length) {
|
|
353
|
+
const start = cursor
|
|
354
|
+
segment = parseSegment(path, start, segment)
|
|
355
|
+
const end = segment[5]
|
|
356
|
+
cursor = end + 1
|
|
357
|
+
|
|
358
|
+
if (start === end) continue
|
|
359
|
+
|
|
360
|
+
const kind = segment[0]
|
|
361
|
+
|
|
362
|
+
if (kind === SEGMENT_TYPE_PATHNAME) {
|
|
363
|
+
joined += '/' + path.substring(start, end)
|
|
364
|
+
continue
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
if (kind === SEGMENT_TYPE_WILDCARD) {
|
|
368
|
+
const splat = params._splat
|
|
369
|
+
usedParams._splat = splat
|
|
370
|
+
// TODO: Deprecate *
|
|
371
|
+
usedParams['*'] = splat
|
|
372
|
+
|
|
373
|
+
const prefix = path.substring(start, segment[1])
|
|
374
|
+
const suffix = path.substring(segment[4], end)
|
|
375
|
+
|
|
376
|
+
// Check if _splat parameter is missing. _splat could be missing if undefined or an empty string or some other falsy value.
|
|
377
|
+
if (!splat) {
|
|
378
|
+
isMissingParams = true
|
|
379
|
+
// For missing splat parameters, just return the prefix and suffix without the wildcard
|
|
380
|
+
// If there is a prefix or suffix, return them joined, otherwise omit the segment
|
|
381
|
+
if (prefix || suffix) {
|
|
382
|
+
joined += '/' + prefix + suffix
|
|
383
|
+
}
|
|
384
|
+
continue
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
const value = encodeParam('_splat', params, decoder)
|
|
388
|
+
joined += '/' + prefix + value + suffix
|
|
389
|
+
continue
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
if (kind === SEGMENT_TYPE_PARAM) {
|
|
393
|
+
const key = path.substring(segment[2], segment[3])
|
|
394
|
+
if (!isMissingParams && !(key in params)) {
|
|
395
|
+
isMissingParams = true
|
|
396
|
+
}
|
|
397
|
+
usedParams[key] = params[key]
|
|
398
|
+
|
|
399
|
+
const prefix = path.substring(start, segment[1])
|
|
400
|
+
const suffix = path.substring(segment[4], end)
|
|
401
|
+
const value = encodeParam(key, params, decoder) ?? 'undefined'
|
|
402
|
+
joined += '/' + prefix + value + suffix
|
|
403
|
+
continue
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
if (kind === SEGMENT_TYPE_OPTIONAL_PARAM) {
|
|
407
|
+
const key = path.substring(segment[2], segment[3])
|
|
408
|
+
const valueRaw = params[key]
|
|
409
|
+
|
|
410
|
+
// Check if optional parameter is missing or undefined
|
|
411
|
+
if (valueRaw == null) continue
|
|
412
|
+
|
|
413
|
+
usedParams[key] = valueRaw
|
|
414
|
+
|
|
415
|
+
const prefix = path.substring(start, segment[1])
|
|
416
|
+
const suffix = path.substring(segment[4], end)
|
|
417
|
+
const value = encodeParam(key, params, decoder) ?? ''
|
|
418
|
+
joined += '/' + prefix + value + suffix
|
|
419
|
+
continue
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
if (path.endsWith('/')) joined += '/'
|
|
424
|
+
|
|
425
|
+
const interpolatedPath = joined || '/'
|
|
426
|
+
|
|
427
|
+
return { usedParams, interpolatedPath, isMissingParams }
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function encodePathParam(
|
|
431
|
+
value: string,
|
|
432
|
+
decoder?: InterpolatePathOptions['decoder'],
|
|
433
|
+
) {
|
|
434
|
+
const encoded = encodeURIComponent(value)
|
|
435
|
+
return decoder?.(encoded) ?? encoded
|
|
436
|
+
}
|
package/src/qss.ts
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
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
|
+
* Update: this implementation has also been mangled to
|
|
11
|
+
* fit exactly our use-case (single value per key in encoding).
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Encodes an object into a query string.
|
|
16
|
+
* @param obj - The object to encode into a query string.
|
|
17
|
+
* @param stringify - An optional custom stringify function.
|
|
18
|
+
* @returns The encoded query string.
|
|
19
|
+
* @example
|
|
20
|
+
* ```
|
|
21
|
+
* // Example input: encode({ token: 'foo', key: 'value' })
|
|
22
|
+
* // Expected output: "token=foo&key=value"
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export function encode(
|
|
26
|
+
obj: Record<string, any>,
|
|
27
|
+
stringify: (value: any) => string = String,
|
|
28
|
+
): string {
|
|
29
|
+
const result = new URLSearchParams()
|
|
30
|
+
|
|
31
|
+
for (const key in obj) {
|
|
32
|
+
const val = obj[key]
|
|
33
|
+
if (val !== undefined) {
|
|
34
|
+
result.set(key, stringify(val))
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return result.toString()
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Converts a string value to its appropriate type (string, number, boolean).
|
|
43
|
+
* @param mix - The string value to convert.
|
|
44
|
+
* @returns The converted value.
|
|
45
|
+
* @example
|
|
46
|
+
* // Example input: toValue("123")
|
|
47
|
+
* // Expected output: 123
|
|
48
|
+
*/
|
|
49
|
+
function toValue(str: unknown) {
|
|
50
|
+
if (!str) return ''
|
|
51
|
+
|
|
52
|
+
if (str === 'false') return false
|
|
53
|
+
if (str === 'true') return true
|
|
54
|
+
return +str * 0 === 0 && +str + '' === str ? +str : str
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Decodes a query string into an object.
|
|
58
|
+
* @param str - The query string to decode.
|
|
59
|
+
* @returns The decoded key-value pairs in an object format.
|
|
60
|
+
* @example
|
|
61
|
+
* // Example input: decode("token=foo&key=value")
|
|
62
|
+
* // Expected output: { "token": "foo", "key": "value" }
|
|
63
|
+
*/
|
|
64
|
+
export function decode(str: any): any {
|
|
65
|
+
const searchParams = new URLSearchParams(str)
|
|
66
|
+
|
|
67
|
+
const result: Record<string, unknown> = Object.create(null)
|
|
68
|
+
|
|
69
|
+
for (const [key, value] of searchParams.entries()) {
|
|
70
|
+
const previousValue = result[key]
|
|
71
|
+
if (previousValue == null) {
|
|
72
|
+
result[key] = toValue(value)
|
|
73
|
+
} else if (Array.isArray(previousValue)) {
|
|
74
|
+
previousValue.push(toValue(value))
|
|
75
|
+
} else {
|
|
76
|
+
result[key] = [previousValue, toValue(value)]
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return result
|
|
81
|
+
}
|