@rangojs/router 0.0.0-experimental.002d056c
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/AGENTS.md +9 -0
- package/README.md +899 -0
- package/dist/bin/rango.js +1606 -0
- package/dist/vite/index.js +5153 -0
- package/package.json +177 -0
- package/skills/breadcrumbs/SKILL.md +250 -0
- package/skills/cache-guide/SKILL.md +262 -0
- package/skills/caching/SKILL.md +253 -0
- package/skills/composability/SKILL.md +172 -0
- package/skills/debug-manifest/SKILL.md +112 -0
- package/skills/document-cache/SKILL.md +182 -0
- package/skills/fonts/SKILL.md +167 -0
- package/skills/hooks/SKILL.md +704 -0
- package/skills/host-router/SKILL.md +218 -0
- package/skills/intercept/SKILL.md +313 -0
- package/skills/layout/SKILL.md +310 -0
- package/skills/links/SKILL.md +239 -0
- package/skills/loader/SKILL.md +596 -0
- package/skills/middleware/SKILL.md +339 -0
- package/skills/mime-routes/SKILL.md +128 -0
- package/skills/parallel/SKILL.md +305 -0
- package/skills/prerender/SKILL.md +643 -0
- package/skills/rango/SKILL.md +118 -0
- package/skills/response-routes/SKILL.md +411 -0
- package/skills/route/SKILL.md +385 -0
- package/skills/router-setup/SKILL.md +439 -0
- package/skills/tailwind/SKILL.md +129 -0
- package/skills/theme/SKILL.md +79 -0
- package/skills/typesafety/SKILL.md +623 -0
- package/skills/use-cache/SKILL.md +324 -0
- package/src/__internal.ts +273 -0
- package/src/bin/rango.ts +321 -0
- package/src/browser/action-coordinator.ts +97 -0
- package/src/browser/action-response-classifier.ts +99 -0
- package/src/browser/event-controller.ts +899 -0
- package/src/browser/history-state.ts +80 -0
- package/src/browser/index.ts +18 -0
- package/src/browser/intercept-utils.ts +52 -0
- package/src/browser/link-interceptor.ts +141 -0
- package/src/browser/logging.ts +55 -0
- package/src/browser/merge-segment-loaders.ts +134 -0
- package/src/browser/navigation-bridge.ts +638 -0
- package/src/browser/navigation-client.ts +261 -0
- package/src/browser/navigation-store.ts +806 -0
- package/src/browser/navigation-transaction.ts +297 -0
- package/src/browser/network-error-handler.ts +61 -0
- package/src/browser/partial-update.ts +582 -0
- package/src/browser/prefetch/cache.ts +206 -0
- package/src/browser/prefetch/fetch.ts +145 -0
- package/src/browser/prefetch/observer.ts +65 -0
- package/src/browser/prefetch/policy.ts +48 -0
- package/src/browser/prefetch/queue.ts +128 -0
- package/src/browser/rango-state.ts +112 -0
- package/src/browser/react/Link.tsx +368 -0
- package/src/browser/react/NavigationProvider.tsx +413 -0
- package/src/browser/react/ScrollRestoration.tsx +94 -0
- package/src/browser/react/context.ts +59 -0
- package/src/browser/react/filter-segment-order.ts +11 -0
- package/src/browser/react/index.ts +52 -0
- package/src/browser/react/location-state-shared.ts +162 -0
- package/src/browser/react/location-state.ts +107 -0
- package/src/browser/react/mount-context.ts +37 -0
- package/src/browser/react/nonce-context.ts +23 -0
- package/src/browser/react/shallow-equal.ts +27 -0
- package/src/browser/react/use-action.ts +218 -0
- package/src/browser/react/use-client-cache.ts +58 -0
- package/src/browser/react/use-handle.ts +162 -0
- package/src/browser/react/use-href.tsx +40 -0
- package/src/browser/react/use-link-status.ts +135 -0
- package/src/browser/react/use-mount.ts +31 -0
- package/src/browser/react/use-navigation.ts +99 -0
- package/src/browser/react/use-params.ts +65 -0
- package/src/browser/react/use-pathname.ts +47 -0
- package/src/browser/react/use-router.ts +63 -0
- package/src/browser/react/use-search-params.ts +56 -0
- package/src/browser/react/use-segments.ts +171 -0
- package/src/browser/response-adapter.ts +73 -0
- package/src/browser/rsc-router.tsx +464 -0
- package/src/browser/scroll-restoration.ts +397 -0
- package/src/browser/segment-reconciler.ts +216 -0
- package/src/browser/segment-structure-assert.ts +83 -0
- package/src/browser/server-action-bridge.ts +667 -0
- package/src/browser/shallow.ts +40 -0
- package/src/browser/types.ts +547 -0
- package/src/browser/validate-redirect-origin.ts +29 -0
- package/src/build/generate-manifest.ts +438 -0
- package/src/build/generate-route-types.ts +36 -0
- package/src/build/index.ts +35 -0
- package/src/build/route-trie.ts +265 -0
- package/src/build/route-types/ast-helpers.ts +25 -0
- package/src/build/route-types/ast-route-extraction.ts +98 -0
- package/src/build/route-types/codegen.ts +102 -0
- package/src/build/route-types/include-resolution.ts +411 -0
- package/src/build/route-types/param-extraction.ts +48 -0
- package/src/build/route-types/per-module-writer.ts +128 -0
- package/src/build/route-types/router-processing.ts +479 -0
- package/src/build/route-types/scan-filter.ts +78 -0
- package/src/build/runtime-discovery.ts +231 -0
- package/src/cache/background-task.ts +34 -0
- package/src/cache/cache-key-utils.ts +44 -0
- package/src/cache/cache-policy.ts +125 -0
- package/src/cache/cache-runtime.ts +338 -0
- package/src/cache/cache-scope.ts +382 -0
- package/src/cache/cf/cf-cache-store.ts +982 -0
- package/src/cache/cf/index.ts +29 -0
- package/src/cache/document-cache.ts +369 -0
- package/src/cache/handle-capture.ts +81 -0
- package/src/cache/handle-snapshot.ts +41 -0
- package/src/cache/index.ts +44 -0
- package/src/cache/memory-segment-store.ts +328 -0
- package/src/cache/profile-registry.ts +73 -0
- package/src/cache/read-through-swr.ts +134 -0
- package/src/cache/segment-codec.ts +256 -0
- package/src/cache/taint.ts +98 -0
- package/src/cache/types.ts +342 -0
- package/src/client.rsc.tsx +85 -0
- package/src/client.tsx +601 -0
- package/src/component-utils.ts +76 -0
- package/src/components/DefaultDocument.tsx +27 -0
- package/src/context-var.ts +86 -0
- package/src/debug.ts +243 -0
- package/src/default-error-boundary.tsx +88 -0
- package/src/deps/browser.ts +8 -0
- package/src/deps/html-stream-client.ts +2 -0
- package/src/deps/html-stream-server.ts +2 -0
- package/src/deps/rsc.ts +10 -0
- package/src/deps/ssr.ts +2 -0
- package/src/errors.ts +365 -0
- package/src/handle.ts +135 -0
- package/src/handles/MetaTags.tsx +246 -0
- package/src/handles/breadcrumbs.ts +66 -0
- package/src/handles/index.ts +7 -0
- package/src/handles/meta.ts +264 -0
- package/src/host/cookie-handler.ts +165 -0
- package/src/host/errors.ts +97 -0
- package/src/host/index.ts +53 -0
- package/src/host/pattern-matcher.ts +214 -0
- package/src/host/router.ts +352 -0
- package/src/host/testing.ts +79 -0
- package/src/host/types.ts +146 -0
- package/src/host/utils.ts +25 -0
- package/src/href-client.ts +222 -0
- package/src/index.rsc.ts +233 -0
- package/src/index.ts +277 -0
- package/src/internal-debug.ts +11 -0
- package/src/loader.rsc.ts +89 -0
- package/src/loader.ts +64 -0
- package/src/network-error-thrower.tsx +23 -0
- package/src/outlet-context.ts +15 -0
- package/src/outlet-provider.tsx +45 -0
- package/src/prerender/param-hash.ts +37 -0
- package/src/prerender/store.ts +185 -0
- package/src/prerender.ts +463 -0
- package/src/reverse.ts +330 -0
- package/src/root-error-boundary.tsx +289 -0
- package/src/route-content-wrapper.tsx +196 -0
- package/src/route-definition/dsl-helpers.ts +934 -0
- package/src/route-definition/helper-factories.ts +200 -0
- package/src/route-definition/helpers-types.ts +430 -0
- package/src/route-definition/index.ts +52 -0
- package/src/route-definition/redirect.ts +93 -0
- package/src/route-definition.ts +1 -0
- package/src/route-map-builder.ts +281 -0
- package/src/route-name.ts +53 -0
- package/src/route-types.ts +259 -0
- package/src/router/content-negotiation.ts +116 -0
- package/src/router/debug-manifest.ts +72 -0
- package/src/router/error-handling.ts +287 -0
- package/src/router/find-match.ts +160 -0
- package/src/router/handler-context.ts +451 -0
- package/src/router/intercept-resolution.ts +397 -0
- package/src/router/lazy-includes.ts +236 -0
- package/src/router/loader-resolution.ts +420 -0
- package/src/router/logging.ts +251 -0
- package/src/router/manifest.ts +269 -0
- package/src/router/match-api.ts +620 -0
- package/src/router/match-context.ts +266 -0
- package/src/router/match-handlers.ts +440 -0
- package/src/router/match-middleware/background-revalidation.ts +223 -0
- package/src/router/match-middleware/cache-lookup.ts +634 -0
- package/src/router/match-middleware/cache-store.ts +295 -0
- package/src/router/match-middleware/index.ts +81 -0
- package/src/router/match-middleware/intercept-resolution.ts +306 -0
- package/src/router/match-middleware/segment-resolution.ts +193 -0
- package/src/router/match-pipelines.ts +179 -0
- package/src/router/match-result.ts +219 -0
- package/src/router/metrics.ts +282 -0
- package/src/router/middleware-cookies.ts +55 -0
- package/src/router/middleware-types.ts +222 -0
- package/src/router/middleware.ts +749 -0
- package/src/router/pattern-matching.ts +563 -0
- package/src/router/prerender-match.ts +402 -0
- package/src/router/preview-match.ts +170 -0
- package/src/router/revalidation.ts +289 -0
- package/src/router/router-context.ts +320 -0
- package/src/router/router-interfaces.ts +452 -0
- package/src/router/router-options.ts +592 -0
- package/src/router/router-registry.ts +24 -0
- package/src/router/segment-resolution/fresh.ts +570 -0
- package/src/router/segment-resolution/helpers.ts +263 -0
- package/src/router/segment-resolution/loader-cache.ts +198 -0
- package/src/router/segment-resolution/revalidation.ts +1242 -0
- package/src/router/segment-resolution/static-store.ts +67 -0
- package/src/router/segment-resolution.ts +21 -0
- package/src/router/segment-wrappers.ts +291 -0
- package/src/router/telemetry-otel.ts +299 -0
- package/src/router/telemetry.ts +300 -0
- package/src/router/timeout.ts +148 -0
- package/src/router/trie-matching.ts +239 -0
- package/src/router/types.ts +170 -0
- package/src/router.ts +1006 -0
- package/src/rsc/handler-context.ts +45 -0
- package/src/rsc/handler.ts +1089 -0
- package/src/rsc/helpers.ts +198 -0
- package/src/rsc/index.ts +36 -0
- package/src/rsc/loader-fetch.ts +209 -0
- package/src/rsc/manifest-init.ts +86 -0
- package/src/rsc/nonce.ts +32 -0
- package/src/rsc/origin-guard.ts +141 -0
- package/src/rsc/progressive-enhancement.ts +379 -0
- package/src/rsc/response-error.ts +37 -0
- package/src/rsc/response-route-handler.ts +347 -0
- package/src/rsc/rsc-rendering.ts +237 -0
- package/src/rsc/runtime-warnings.ts +42 -0
- package/src/rsc/server-action.ts +348 -0
- package/src/rsc/ssr-setup.ts +128 -0
- package/src/rsc/types.ts +263 -0
- package/src/search-params.ts +230 -0
- package/src/segment-system.tsx +454 -0
- package/src/server/context.ts +591 -0
- package/src/server/cookie-store.ts +190 -0
- package/src/server/fetchable-loader-store.ts +37 -0
- package/src/server/handle-store.ts +308 -0
- package/src/server/loader-registry.ts +133 -0
- package/src/server/request-context.ts +920 -0
- package/src/server/root-layout.tsx +10 -0
- package/src/server/tsconfig.json +14 -0
- package/src/server.ts +51 -0
- package/src/ssr/index.tsx +365 -0
- package/src/static-handler.ts +114 -0
- package/src/theme/ThemeProvider.tsx +297 -0
- package/src/theme/ThemeScript.tsx +61 -0
- package/src/theme/constants.ts +62 -0
- package/src/theme/index.ts +48 -0
- package/src/theme/theme-context.ts +44 -0
- package/src/theme/theme-script.ts +155 -0
- package/src/theme/types.ts +182 -0
- package/src/theme/use-theme.ts +44 -0
- package/src/types/boundaries.ts +158 -0
- package/src/types/cache-types.ts +198 -0
- package/src/types/error-types.ts +192 -0
- package/src/types/global-namespace.ts +100 -0
- package/src/types/handler-context.ts +687 -0
- package/src/types/index.ts +88 -0
- package/src/types/loader-types.ts +183 -0
- package/src/types/route-config.ts +170 -0
- package/src/types/route-entry.ts +109 -0
- package/src/types/segments.ts +148 -0
- package/src/types.ts +1 -0
- package/src/urls/include-helper.ts +197 -0
- package/src/urls/index.ts +53 -0
- package/src/urls/path-helper-types.ts +339 -0
- package/src/urls/path-helper.ts +329 -0
- package/src/urls/pattern-types.ts +95 -0
- package/src/urls/response-types.ts +106 -0
- package/src/urls/type-extraction.ts +372 -0
- package/src/urls/urls-function.ts +98 -0
- package/src/urls.ts +1 -0
- package/src/use-loader.tsx +354 -0
- package/src/vite/discovery/bundle-postprocess.ts +184 -0
- package/src/vite/discovery/discover-routers.ts +344 -0
- package/src/vite/discovery/prerender-collection.ts +385 -0
- package/src/vite/discovery/route-types-writer.ts +258 -0
- package/src/vite/discovery/self-gen-tracking.ts +47 -0
- package/src/vite/discovery/state.ts +108 -0
- package/src/vite/discovery/virtual-module-codegen.ts +203 -0
- package/src/vite/index.ts +16 -0
- package/src/vite/plugin-types.ts +48 -0
- package/src/vite/plugins/cjs-to-esm.ts +93 -0
- package/src/vite/plugins/client-ref-dedup.ts +115 -0
- package/src/vite/plugins/client-ref-hashing.ts +105 -0
- package/src/vite/plugins/expose-action-id.ts +363 -0
- package/src/vite/plugins/expose-id-utils.ts +287 -0
- package/src/vite/plugins/expose-ids/export-analysis.ts +296 -0
- package/src/vite/plugins/expose-ids/handler-transform.ts +179 -0
- package/src/vite/plugins/expose-ids/loader-transform.ts +74 -0
- package/src/vite/plugins/expose-ids/router-transform.ts +110 -0
- package/src/vite/plugins/expose-ids/types.ts +45 -0
- package/src/vite/plugins/expose-internal-ids.ts +569 -0
- package/src/vite/plugins/refresh-cmd.ts +65 -0
- package/src/vite/plugins/use-cache-transform.ts +323 -0
- package/src/vite/plugins/version-injector.ts +83 -0
- package/src/vite/plugins/version-plugin.ts +266 -0
- package/src/vite/plugins/version.d.ts +12 -0
- package/src/vite/plugins/virtual-entries.ts +123 -0
- package/src/vite/plugins/virtual-stub-plugin.ts +29 -0
- package/src/vite/rango.ts +445 -0
- package/src/vite/router-discovery.ts +777 -0
- package/src/vite/utils/ast-handler-extract.ts +517 -0
- package/src/vite/utils/banner.ts +36 -0
- package/src/vite/utils/bundle-analysis.ts +137 -0
- package/src/vite/utils/manifest-utils.ts +70 -0
- package/src/vite/utils/package-resolution.ts +121 -0
- package/src/vite/utils/prerender-utils.ts +189 -0
- package/src/vite/utils/shared-utils.ts +169 -0
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client-safe type-safe href function
|
|
3
|
+
*
|
|
4
|
+
* This is a compile-time only href that validates paths against registered routes.
|
|
5
|
+
* No runtime route map lookup - just an identity function with TypeScript validation.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { href } from "rsc-router/client";
|
|
10
|
+
*
|
|
11
|
+
* href("/blog/my-post"); // ✓ matches /blog/:slug
|
|
12
|
+
* href("/shop/product/widget"); // ✓ matches /shop/product/:slug
|
|
13
|
+
* href("/invalid"); // ✗ TypeScript error
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import type { GetRegisteredRoutes } from "./types.js";
|
|
18
|
+
import type { ResponseEnvelope } from "./urls.js";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Parse constraint values into a union type for paths
|
|
22
|
+
* "a|b|c" → "a" | "b" | "c"
|
|
23
|
+
*/
|
|
24
|
+
type ParseConstraintPath<T extends string> =
|
|
25
|
+
T extends `${infer First}|${infer Rest}`
|
|
26
|
+
? First | ParseConstraintPath<Rest>
|
|
27
|
+
: T;
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Convert a route pattern to a template literal type
|
|
31
|
+
*
|
|
32
|
+
* Supports:
|
|
33
|
+
* - Static: /about → "/about"
|
|
34
|
+
* - Dynamic: /blog/:slug → `/blog/${string}`
|
|
35
|
+
* - Optional: /:locale?/blog → "/blog" | `/${string}/blog`
|
|
36
|
+
* - Constrained: /:locale(en|gb)/blog → "/en/blog" | "/gb/blog"
|
|
37
|
+
* - Optional + Constrained: /:locale(en|gb)?/blog → "/blog" | "/en/blog" | "/gb/blog"
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* PatternToPath<"/blog/:slug"> = `/blog/${string}`
|
|
41
|
+
* PatternToPath<"/:locale?/blog"> = "/blog" | `/${string}/blog`
|
|
42
|
+
* PatternToPath<"/:locale(en|gb)/blog"> = "/en/blog" | "/gb/blog"
|
|
43
|
+
* PatternToPath<"/:locale(en|gb)?/blog"> = "/blog" | "/en/blog" | "/gb/blog"
|
|
44
|
+
*/
|
|
45
|
+
export type PatternToPath<T extends string> =
|
|
46
|
+
// Optional + constrained param in middle: /:param(a|b)?/rest
|
|
47
|
+
T extends `${infer Before}:${infer _Name}(${infer Constraint})?/${infer After}`
|
|
48
|
+
?
|
|
49
|
+
| PatternToPath<`${Before}${After}`>
|
|
50
|
+
| `${Before}${ParseConstraintPath<Constraint>}/${PatternToPath<After>}`
|
|
51
|
+
: // Optional + constrained param at end: /path/:param(a|b)?
|
|
52
|
+
T extends `${infer Before}:${infer _Name}(${infer Constraint})?`
|
|
53
|
+
? Before | `${Before}${ParseConstraintPath<Constraint>}`
|
|
54
|
+
: // Constrained param in middle: /:param(a|b)/rest
|
|
55
|
+
T extends `${infer Before}:${infer _Name}(${infer Constraint})/${infer After}`
|
|
56
|
+
? `${Before}${ParseConstraintPath<Constraint>}/${PatternToPath<After>}`
|
|
57
|
+
: // Constrained param at end: /path/:param(a|b)
|
|
58
|
+
T extends `${infer Before}:${infer _Name}(${infer Constraint})`
|
|
59
|
+
? `${Before}${ParseConstraintPath<Constraint>}`
|
|
60
|
+
: // Optional param in middle: /:param?/rest
|
|
61
|
+
T extends `${infer Before}:${infer _Param}?/${infer After}`
|
|
62
|
+
?
|
|
63
|
+
| PatternToPath<`${Before}${After}`>
|
|
64
|
+
| `${Before}${string}/${PatternToPath<After>}`
|
|
65
|
+
: // Optional param at end: /path/:param?
|
|
66
|
+
T extends `${infer Before}:${infer _Param}?`
|
|
67
|
+
? Before | `${Before}${string}`
|
|
68
|
+
: // Required param in middle: /:param/rest
|
|
69
|
+
T extends `${infer Before}:${infer _Param}/${infer After}`
|
|
70
|
+
? `${Before}${string}/${PatternToPath<After>}`
|
|
71
|
+
: // Required param at end: /path/:param
|
|
72
|
+
T extends `${infer Before}:${infer _Param}`
|
|
73
|
+
? `${Before}${string}`
|
|
74
|
+
: // Static path
|
|
75
|
+
T;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Allow optional query string (?...) and/or hash fragment (#...) suffix
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* WithSuffix<"/about"> = "/about" | "/about?..." | "/about#..." | "/about?...#..."
|
|
82
|
+
*/
|
|
83
|
+
type WithSuffix<T extends string> =
|
|
84
|
+
| T
|
|
85
|
+
| `${T}?${string}`
|
|
86
|
+
| `${T}#${string}`
|
|
87
|
+
| `${T}?${string}#${string}`;
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Helper type to get pattern from routes, handling string values and { path, response } objects
|
|
91
|
+
*/
|
|
92
|
+
type RoutePattern<TRoutes, K extends keyof TRoutes> = TRoutes[K] extends string
|
|
93
|
+
? TRoutes[K]
|
|
94
|
+
: TRoutes[K] extends { readonly path: infer P extends string }
|
|
95
|
+
? P
|
|
96
|
+
: string;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Reverse lookup: find route name where the pattern matches TPattern
|
|
100
|
+
*/
|
|
101
|
+
type NameForPattern<TPattern extends string, TRoutes = GetRegisteredRoutes> = {
|
|
102
|
+
[K in keyof TRoutes]: RoutePattern<TRoutes, K> extends TPattern ? K : never;
|
|
103
|
+
}[keyof TRoutes];
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Look up the response data type for a route pattern from RegisteredRoutes.
|
|
107
|
+
*
|
|
108
|
+
* Works by reverse-looking up the route name for the given pattern,
|
|
109
|
+
* then extracting the response type from the route entry.
|
|
110
|
+
*
|
|
111
|
+
* For static routes (no params), pattern === path:
|
|
112
|
+
* PathResponse<"/api/health"> → { status: string; timestamp: number }
|
|
113
|
+
*
|
|
114
|
+
* For dynamic routes, use the pattern:
|
|
115
|
+
* PathResponse<"/api/products/:id"> → Product
|
|
116
|
+
*/
|
|
117
|
+
export type PathResponse<
|
|
118
|
+
TPattern extends string,
|
|
119
|
+
TRoutes = GetRegisteredRoutes,
|
|
120
|
+
> = ResponseEnvelope<
|
|
121
|
+
{
|
|
122
|
+
[K in keyof TRoutes]: RoutePattern<TRoutes, K> extends TPattern
|
|
123
|
+
? TRoutes[K] extends { readonly response: infer R }
|
|
124
|
+
? Exclude<R, Response>
|
|
125
|
+
: never
|
|
126
|
+
: never;
|
|
127
|
+
}[keyof TRoutes]
|
|
128
|
+
>;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Strip trailing slash from a path (e.g., "/blog/" -> "/blog" | "/blog/")
|
|
132
|
+
* Allows navigation to include() prefixes without requiring trailing slash
|
|
133
|
+
*/
|
|
134
|
+
type OptionalTrailingSlash<T extends string> = T extends `${infer Base}/`
|
|
135
|
+
? Base extends ""
|
|
136
|
+
? T
|
|
137
|
+
: Base | T
|
|
138
|
+
: T;
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Union of all valid paths from registered routes
|
|
142
|
+
*
|
|
143
|
+
* Generated from RSCRouter.RegisteredRoutes via module augmentation.
|
|
144
|
+
* Allows optional query strings and hash fragments.
|
|
145
|
+
*/
|
|
146
|
+
export type ValidPaths<TRoutes = GetRegisteredRoutes> =
|
|
147
|
+
keyof TRoutes extends never
|
|
148
|
+
? `/${string}` // Fallback when no routes are registered
|
|
149
|
+
: WithSuffix<
|
|
150
|
+
{
|
|
151
|
+
[K in keyof TRoutes]: OptionalTrailingSlash<
|
|
152
|
+
PatternToPath<RoutePattern<TRoutes, K>>
|
|
153
|
+
>;
|
|
154
|
+
}[keyof TRoutes]
|
|
155
|
+
>;
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Type-safe href function for client-side use
|
|
159
|
+
*
|
|
160
|
+
* Without mount: identity function, validates absolute paths at compile time.
|
|
161
|
+
* With mount: prepends mount path, for use with useMount() inside include() scopes.
|
|
162
|
+
*
|
|
163
|
+
* @param path - A valid path matching one of the registered route patterns
|
|
164
|
+
* @param mount - Optional mount prefix from useMount() for include-scoped paths
|
|
165
|
+
* @returns The resolved path
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* ```typescript
|
|
169
|
+
* // Absolute paths (type-safe)
|
|
170
|
+
* href("/blog/hello"); // "/blog/hello"
|
|
171
|
+
* href("/shop/product/widget"); // "/shop/product/widget"
|
|
172
|
+
*
|
|
173
|
+
* // With mount (inside an include)
|
|
174
|
+
* const mount = useMount(); // "/articles"
|
|
175
|
+
* href("/", mount); // "/articles/"
|
|
176
|
+
* href("/my-post", mount); // "/articles/my-post"
|
|
177
|
+
*
|
|
178
|
+
* // Query strings and hashes pass through
|
|
179
|
+
* href("/blog/hello?page=1");
|
|
180
|
+
* href("/about#contact");
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
183
|
+
export function href<T extends ValidPaths>(path: T, mount?: string): string {
|
|
184
|
+
if (mount && mount !== "/") {
|
|
185
|
+
// Strip trailing slash from mount to avoid double-slash when joining
|
|
186
|
+
const normalizedMount = mount.endsWith("/") ? mount.slice(0, -1) : mount;
|
|
187
|
+
return normalizedMount + path;
|
|
188
|
+
}
|
|
189
|
+
return path;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Props shape returned by href.json() etc. for spreading on <Link>.
|
|
194
|
+
* Sets data-external to trigger hard navigation (skips RSC fetch).
|
|
195
|
+
*/
|
|
196
|
+
export interface ResponseHrefProps {
|
|
197
|
+
to: string;
|
|
198
|
+
"data-external": "";
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
type ResponseHrefFn = <T extends ValidPaths>(
|
|
202
|
+
path: T,
|
|
203
|
+
mount?: string,
|
|
204
|
+
) => ResponseHrefProps;
|
|
205
|
+
|
|
206
|
+
function createResponseHrefTag(): ResponseHrefFn {
|
|
207
|
+
return (path, mount) => ({
|
|
208
|
+
to: href(path, mount),
|
|
209
|
+
"data-external": "" as const,
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export namespace href {
|
|
214
|
+
export const json: ResponseHrefFn = createResponseHrefTag();
|
|
215
|
+
export const text: ResponseHrefFn = createResponseHrefTag();
|
|
216
|
+
export const html: ResponseHrefFn = createResponseHrefTag();
|
|
217
|
+
export const xml: ResponseHrefFn = createResponseHrefTag();
|
|
218
|
+
export const md: ResponseHrefFn = createResponseHrefTag();
|
|
219
|
+
export const image: ResponseHrefFn = createResponseHrefTag();
|
|
220
|
+
export const stream: ResponseHrefFn = createResponseHrefTag();
|
|
221
|
+
export const any: ResponseHrefFn = createResponseHrefTag();
|
|
222
|
+
}
|
package/src/index.rsc.ts
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @rangojs/router (react-server environment)
|
|
3
|
+
*
|
|
4
|
+
* This file is used when importing "@rangojs/router" from RSC (server components).
|
|
5
|
+
* It re-exports everything from the universal index.ts plus adds server-side
|
|
6
|
+
* implementations that replace the client-side error stubs.
|
|
7
|
+
*
|
|
8
|
+
* The bundler uses the "react-server" export condition to select this file
|
|
9
|
+
* in RSC context, while the regular index.ts is used in client components.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
// Re-export all universal exports from index.ts
|
|
13
|
+
export {
|
|
14
|
+
// Error classes
|
|
15
|
+
RouteNotFoundError,
|
|
16
|
+
DataNotFoundError,
|
|
17
|
+
notFound,
|
|
18
|
+
MiddlewareError,
|
|
19
|
+
HandlerError,
|
|
20
|
+
BuildError,
|
|
21
|
+
InvalidHandlerError,
|
|
22
|
+
RouterError,
|
|
23
|
+
Skip,
|
|
24
|
+
isSkip,
|
|
25
|
+
} from "./index.js";
|
|
26
|
+
|
|
27
|
+
// Re-export all types from types.ts (user-facing types only)
|
|
28
|
+
export type {
|
|
29
|
+
// Configuration types
|
|
30
|
+
DocumentProps,
|
|
31
|
+
DefaultEnv,
|
|
32
|
+
RouteDefinition,
|
|
33
|
+
RouteConfig,
|
|
34
|
+
RouteDefinitionOptions,
|
|
35
|
+
TrailingSlashMode,
|
|
36
|
+
// Handler types
|
|
37
|
+
Handler,
|
|
38
|
+
HandlerContext,
|
|
39
|
+
ExtractParams,
|
|
40
|
+
GenericParams,
|
|
41
|
+
// Middleware types
|
|
42
|
+
Middleware,
|
|
43
|
+
// Revalidation types
|
|
44
|
+
RevalidateParams,
|
|
45
|
+
Revalidate,
|
|
46
|
+
RouteKeys,
|
|
47
|
+
// Loader types
|
|
48
|
+
LoaderDefinition,
|
|
49
|
+
LoaderFn,
|
|
50
|
+
LoaderContext,
|
|
51
|
+
FetchableLoaderOptions,
|
|
52
|
+
LoadOptions,
|
|
53
|
+
// Error boundary types
|
|
54
|
+
ErrorInfo,
|
|
55
|
+
ErrorBoundaryFallbackProps,
|
|
56
|
+
ErrorBoundaryHandler,
|
|
57
|
+
ClientErrorBoundaryFallbackProps,
|
|
58
|
+
// NotFound boundary types
|
|
59
|
+
NotFoundInfo,
|
|
60
|
+
NotFoundBoundaryFallbackProps,
|
|
61
|
+
NotFoundBoundaryHandler,
|
|
62
|
+
// Error handling callback types
|
|
63
|
+
ErrorPhase,
|
|
64
|
+
OnErrorContext,
|
|
65
|
+
OnErrorCallback,
|
|
66
|
+
} from "./types.js";
|
|
67
|
+
|
|
68
|
+
// Router options type (server-only, so import directly)
|
|
69
|
+
export type {
|
|
70
|
+
RSCRouterOptions,
|
|
71
|
+
SSRStreamMode,
|
|
72
|
+
SSROptions,
|
|
73
|
+
ResolveStreamingContext,
|
|
74
|
+
} from "./router.js";
|
|
75
|
+
|
|
76
|
+
// Server-side createLoader and redirect
|
|
77
|
+
export {
|
|
78
|
+
createLoader,
|
|
79
|
+
redirect,
|
|
80
|
+
type RouteHelpers,
|
|
81
|
+
type RouteHandlers,
|
|
82
|
+
// Globally importable route helpers for composition
|
|
83
|
+
layout,
|
|
84
|
+
cache,
|
|
85
|
+
middleware,
|
|
86
|
+
revalidate,
|
|
87
|
+
loader,
|
|
88
|
+
loading,
|
|
89
|
+
parallel,
|
|
90
|
+
intercept,
|
|
91
|
+
when,
|
|
92
|
+
errorBoundary,
|
|
93
|
+
notFoundBoundary,
|
|
94
|
+
transition,
|
|
95
|
+
} from "./route-definition.js";
|
|
96
|
+
|
|
97
|
+
// Composition types for reusable callback factories
|
|
98
|
+
export type {
|
|
99
|
+
RouteUseItem,
|
|
100
|
+
LayoutUseItem,
|
|
101
|
+
AllUseItems,
|
|
102
|
+
UseItems,
|
|
103
|
+
} from "./route-types.js";
|
|
104
|
+
|
|
105
|
+
// Handle API
|
|
106
|
+
export { createHandle, isHandle, type Handle } from "./handle.js";
|
|
107
|
+
|
|
108
|
+
// Context variable API (typed ctx.set/ctx.get tokens)
|
|
109
|
+
export { createVar, type ContextVar } from "./context-var.js";
|
|
110
|
+
|
|
111
|
+
// CSP nonce token (use with ctx.get(nonce) in middleware/handlers)
|
|
112
|
+
export { nonce } from "./rsc/nonce.js";
|
|
113
|
+
|
|
114
|
+
// Pre-render handler API
|
|
115
|
+
export {
|
|
116
|
+
Prerender,
|
|
117
|
+
type PrerenderHandlerDefinition,
|
|
118
|
+
type PrerenderPassthroughContext,
|
|
119
|
+
type PrerenderOptions,
|
|
120
|
+
type BuildContext,
|
|
121
|
+
type StaticBuildContext,
|
|
122
|
+
type GetParamsContext,
|
|
123
|
+
} from "./prerender.js";
|
|
124
|
+
|
|
125
|
+
// Static handler API
|
|
126
|
+
export { Static, type StaticHandlerDefinition } from "./static-handler.js";
|
|
127
|
+
|
|
128
|
+
// Django-style URL patterns (RSC/server context)
|
|
129
|
+
export {
|
|
130
|
+
urls,
|
|
131
|
+
type PathHelpers,
|
|
132
|
+
type PathOptions,
|
|
133
|
+
type UrlPatterns,
|
|
134
|
+
type IncludeOptions,
|
|
135
|
+
type IncludeItem,
|
|
136
|
+
type RouteResponse,
|
|
137
|
+
type ResponseError,
|
|
138
|
+
type ResponseEnvelope,
|
|
139
|
+
type ResponseHandler,
|
|
140
|
+
type ResponseHandlerContext,
|
|
141
|
+
type JsonResponseHandler,
|
|
142
|
+
type TextResponseHandler,
|
|
143
|
+
type JsonValue,
|
|
144
|
+
type ResponsePathFn,
|
|
145
|
+
type JsonResponsePathFn,
|
|
146
|
+
type TextResponsePathFn,
|
|
147
|
+
} from "./urls.js";
|
|
148
|
+
|
|
149
|
+
// Core router (server-side)
|
|
150
|
+
export {
|
|
151
|
+
createRouter,
|
|
152
|
+
type RSCRouter,
|
|
153
|
+
type RootLayoutProps,
|
|
154
|
+
type RouterRequestInput,
|
|
155
|
+
} from "./router.js";
|
|
156
|
+
|
|
157
|
+
// RSC handler types (server-side)
|
|
158
|
+
export type { HandlerCacheConfig } from "./rsc/types.js";
|
|
159
|
+
|
|
160
|
+
// Built-in handles (server-side)
|
|
161
|
+
export { Meta } from "./handles/meta.js";
|
|
162
|
+
export { Breadcrumbs, type BreadcrumbItem } from "./handles/breadcrumbs.js";
|
|
163
|
+
|
|
164
|
+
// Request context (for accessing request data in server actions/components).
|
|
165
|
+
// Re-exported with a narrowed return type so that public consumers only see
|
|
166
|
+
// public members. Internal code imports from "./server/request-context.js"
|
|
167
|
+
// directly and gets the full type.
|
|
168
|
+
import { getRequestContext as _getRequestContextInternal } from "./server/request-context.js";
|
|
169
|
+
export type { PublicRequestContext as RequestContext } from "./server/request-context.js";
|
|
170
|
+
import type { PublicRequestContext } from "./server/request-context.js";
|
|
171
|
+
import type { DefaultEnv } from "./types/global-namespace.js";
|
|
172
|
+
|
|
173
|
+
export const getRequestContext: <
|
|
174
|
+
TEnv = DefaultEnv,
|
|
175
|
+
>() => PublicRequestContext<TEnv> = _getRequestContextInternal;
|
|
176
|
+
|
|
177
|
+
// Request-scoped shorthands
|
|
178
|
+
export {
|
|
179
|
+
cookies,
|
|
180
|
+
headers,
|
|
181
|
+
type CookieStore,
|
|
182
|
+
type Cookie,
|
|
183
|
+
type ReadonlyHeaders,
|
|
184
|
+
} from "./server/cookie-store.js";
|
|
185
|
+
|
|
186
|
+
// Meta types
|
|
187
|
+
export type { MetaDescriptor, MetaDescriptorBase } from "./router/types.js";
|
|
188
|
+
|
|
189
|
+
// Middleware context types
|
|
190
|
+
export type { MiddlewareContext, CookieOptions } from "./router/middleware.js";
|
|
191
|
+
|
|
192
|
+
// Reverse type utilities for type-safe URL generation (Django-style URL reversal)
|
|
193
|
+
export type {
|
|
194
|
+
ScopedReverseFunction,
|
|
195
|
+
ReverseFunction,
|
|
196
|
+
ExtractLocalRoutes,
|
|
197
|
+
ParamsFor,
|
|
198
|
+
} from "./reverse.js";
|
|
199
|
+
export { scopedReverse, createReverse } from "./reverse.js";
|
|
200
|
+
|
|
201
|
+
// Search params schema types
|
|
202
|
+
export type {
|
|
203
|
+
SearchSchema,
|
|
204
|
+
SearchSchemaValue,
|
|
205
|
+
ResolveSearchSchema,
|
|
206
|
+
RouteSearchParams,
|
|
207
|
+
RouteParams,
|
|
208
|
+
} from "./search-params.js";
|
|
209
|
+
|
|
210
|
+
// Location state (universal)
|
|
211
|
+
export {
|
|
212
|
+
createLocationState,
|
|
213
|
+
type LocationStateDefinition,
|
|
214
|
+
type LocationStateEntry,
|
|
215
|
+
type LocationStateOptions,
|
|
216
|
+
} from "./browser/react/location-state-shared.js";
|
|
217
|
+
|
|
218
|
+
// Path-based response type lookup from RegisteredRoutes
|
|
219
|
+
export type { PathResponse } from "./href-client.js";
|
|
220
|
+
|
|
221
|
+
// Telemetry sink
|
|
222
|
+
export { createConsoleSink } from "./router/telemetry.js";
|
|
223
|
+
export { createOTelSink } from "./router/telemetry-otel.js";
|
|
224
|
+
export type { OTelTracer, OTelSpan } from "./router/telemetry-otel.js";
|
|
225
|
+
export type { TelemetrySink, TelemetryEvent } from "./router/telemetry.js";
|
|
226
|
+
|
|
227
|
+
// Timeout types and error class
|
|
228
|
+
export { RouterTimeoutError } from "./router/timeout.js";
|
|
229
|
+
export type {
|
|
230
|
+
RouterTimeouts,
|
|
231
|
+
TimeoutPhase,
|
|
232
|
+
TimeoutContext,
|
|
233
|
+
} from "./router/timeout.js";
|