@rangojs/router 0.0.0-experimental.14 → 0.0.0-experimental.140
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 +17 -0
- package/README.md +432 -7
- package/dist/bin/rango.js +2073 -213
- package/dist/testing/vitest.js +82 -0
- package/dist/vite/index.js +7258 -2714
- package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/package.json +140 -67
- package/skills/api-client/SKILL.md +211 -0
- package/skills/breadcrumbs/SKILL.md +329 -0
- package/skills/bundle-analysis/SKILL.md +159 -0
- package/skills/cache-guide/SKILL.md +487 -0
- package/skills/caching/SKILL.md +357 -25
- package/skills/comparison/SKILL.md +50 -0
- package/skills/comparison/agents/openai.yaml +4 -0
- package/skills/comparison/references/framework-comparison.md +837 -0
- package/skills/composability/SKILL.md +246 -0
- package/skills/css/SKILL.md +76 -0
- package/skills/debug-manifest/SKILL.md +16 -10
- package/skills/document-cache/SKILL.md +87 -62
- package/skills/fonts/SKILL.md +6 -4
- package/skills/handler-use/SKILL.md +364 -0
- package/skills/hooks/SKILL.md +557 -79
- package/skills/host-router/SKILL.md +320 -0
- package/skills/i18n/SKILL.md +276 -0
- package/skills/intercept/SKILL.md +207 -15
- package/skills/layout/SKILL.md +146 -6
- package/skills/links/SKILL.md +304 -25
- package/skills/loader/SKILL.md +616 -54
- package/skills/middleware/SKILL.md +217 -37
- package/skills/migrate-nextjs/SKILL.md +611 -0
- package/skills/migrate-react-router/SKILL.md +927 -0
- package/skills/mime-routes/SKILL.md +42 -11
- package/skills/observability/SKILL.md +194 -0
- package/skills/parallel/SKILL.md +284 -3
- package/skills/ppr/SKILL.md +426 -0
- package/skills/prerender/SKILL.md +437 -52
- package/skills/rango/SKILL.md +369 -22
- package/skills/react-compiler/SKILL.md +168 -0
- package/skills/response-routes/SKILL.md +263 -121
- package/skills/route/SKILL.md +350 -21
- package/skills/router-setup/SKILL.md +246 -33
- package/skills/scripts/SKILL.md +179 -0
- package/skills/server-actions/SKILL.md +775 -0
- package/skills/shell-manifest/SKILL.md +185 -0
- package/skills/streams-and-websockets/SKILL.md +283 -0
- package/skills/tailwind/SKILL.md +27 -3
- package/skills/testing/SKILL.md +126 -222
- package/skills/testing/bindings.md +103 -0
- package/skills/testing/cache-prerender.md +127 -0
- package/skills/testing/client-components.md +124 -0
- package/skills/testing/e2e-parity.md +125 -0
- package/skills/testing/flight.md +91 -0
- package/skills/testing/handles.md +131 -0
- package/skills/testing/loader.md +128 -0
- package/skills/testing/middleware.md +99 -0
- package/skills/testing/render-handler.md +122 -0
- package/skills/testing/response-routes.md +95 -0
- package/skills/testing/reverse-and-types.md +85 -0
- package/skills/testing/server-actions.md +107 -0
- package/skills/testing/server-tree.md +128 -0
- package/skills/testing/setup.md +123 -0
- package/skills/theme/SKILL.md +9 -8
- package/skills/typesafety/SKILL.md +532 -103
- package/skills/use-cache/SKILL.md +367 -0
- package/skills/vercel/SKILL.md +128 -0
- package/skills/view-transitions/SKILL.md +337 -0
- package/src/__augment-tests__/augment.ts +81 -0
- package/src/__augment-tests__/augmented.check.ts +116 -0
- package/src/__internal.ts +77 -44
- package/src/bin/rango.ts +312 -15
- package/src/browser/action-coordinator.ts +114 -0
- package/src/browser/action-fence.ts +47 -0
- package/src/browser/app-shell.ts +39 -0
- package/src/browser/app-version.ts +14 -0
- package/src/browser/connection-warmup.ts +134 -0
- package/src/browser/cookie-name.ts +140 -0
- package/src/browser/event-controller.ts +293 -202
- package/src/browser/history-state.ts +101 -0
- package/src/browser/index.ts +3 -3
- package/src/browser/intercept-utils.ts +52 -0
- package/src/browser/invalidate-client-cache.ts +52 -0
- package/src/browser/link-interceptor.ts +24 -4
- package/src/browser/logging.ts +11 -0
- package/src/browser/merge-segment-loaders.ts +20 -12
- package/src/browser/navigation-bridge.ts +385 -576
- package/src/browser/navigation-client.ts +245 -75
- package/src/browser/navigation-store-handle.ts +38 -0
- package/src/browser/navigation-store.ts +184 -118
- package/src/browser/navigation-transaction.ts +247 -0
- package/src/browser/network-error-handler.ts +88 -0
- package/src/browser/partial-update.ts +412 -364
- package/src/browser/prefetch/cache.ts +359 -0
- package/src/browser/prefetch/fetch.ts +452 -0
- package/src/browser/prefetch/observer.ts +65 -0
- package/src/browser/prefetch/policy.ts +48 -0
- package/src/browser/prefetch/queue.ts +209 -0
- package/src/browser/prefetch/resource-ready.ts +77 -0
- package/src/browser/rango-state.ts +194 -0
- package/src/browser/react/Link.tsx +275 -68
- package/src/browser/react/NavigationProvider.tsx +265 -109
- package/src/browser/react/ScrollRestoration.tsx +10 -6
- package/src/browser/react/context.ts +11 -0
- package/src/browser/react/filter-segment-order.ts +70 -0
- package/src/browser/react/index.ts +0 -48
- package/src/browser/react/location-state-shared.ts +272 -60
- package/src/browser/react/location-state.ts +90 -20
- package/src/browser/react/mount-context.ts +6 -1
- 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 +35 -66
- package/src/browser/react/use-handle.ts +39 -126
- package/src/browser/react/use-href.tsx +8 -1
- package/src/browser/react/use-link-status.ts +39 -13
- package/src/browser/react/use-navigation.ts +53 -69
- package/src/browser/react/use-params.ts +75 -0
- package/src/browser/react/use-pathname.ts +47 -0
- package/src/browser/react/use-reverse.ts +106 -0
- package/src/browser/react/use-router.ts +98 -0
- package/src/browser/react/use-search-params.ts +51 -0
- package/src/browser/react/use-segments.ts +72 -99
- package/src/browser/response-adapter.ts +164 -0
- package/src/browser/rsc-router.tsx +300 -72
- package/src/browser/scroll-restoration.ts +138 -50
- package/src/browser/segment-reconciler.ts +243 -0
- package/src/browser/segment-structure-assert.ts +17 -1
- package/src/browser/server-action-bridge.ts +668 -613
- package/src/browser/types.ts +223 -51
- package/src/browser/validate-redirect-origin.ts +56 -0
- package/src/build/collect-fallback-refs.ts +107 -0
- package/src/build/generate-manifest.ts +252 -161
- package/src/build/generate-route-types.ts +41 -1038
- package/src/build/index.ts +12 -7
- package/src/build/prefix-tree-utils.ts +123 -0
- package/src/build/route-trie.ts +225 -42
- package/src/build/route-types/ast-helpers.ts +25 -0
- package/src/build/route-types/ast-route-extraction.ts +105 -0
- package/src/build/route-types/codegen.ts +113 -0
- package/src/build/route-types/include-resolution.ts +812 -0
- package/src/build/route-types/param-extraction.ts +51 -0
- package/src/build/route-types/per-module-writer.ts +144 -0
- package/src/build/route-types/router-processing.ts +695 -0
- package/src/build/route-types/scan-filter.ts +85 -0
- package/src/build/route-types/source-scan.ts +216 -0
- package/src/build/runtime-discovery.ts +223 -0
- package/src/cache/background-task.ts +34 -0
- package/src/cache/cache-error.ts +104 -0
- package/src/cache/cache-key-utils.ts +60 -0
- package/src/cache/cache-policy.ts +199 -0
- package/src/cache/cache-runtime.ts +525 -0
- package/src/cache/cache-scope.ts +298 -332
- package/src/cache/cache-tag.ts +103 -0
- package/src/cache/cf/cf-base64.ts +33 -0
- package/src/cache/cf/cf-cache-constants.ts +127 -0
- package/src/cache/cf/cf-cache-store.ts +2500 -158
- package/src/cache/cf/cf-cache-types.ts +349 -0
- package/src/cache/cf/cf-kv-utils.ts +46 -0
- package/src/cache/cf/cf-tag-marker-memo.ts +105 -0
- package/src/cache/cf/index.ts +17 -17
- package/src/cache/document-cache.ts +199 -92
- package/src/cache/handle-capture.ts +81 -0
- package/src/cache/handle-snapshot.ts +111 -0
- package/src/cache/index.ts +29 -35
- package/src/cache/memory-segment-store.ts +363 -30
- package/src/cache/profile-registry.ts +88 -0
- package/src/cache/read-through-swr.ts +178 -0
- package/src/cache/segment-codec.ts +248 -0
- package/src/cache/shell-cache.ts +386 -0
- package/src/cache/tag-invalidation.ts +230 -0
- package/src/cache/taint.ts +153 -0
- package/src/cache/types.ts +156 -211
- package/src/cache/vercel/index.ts +11 -0
- package/src/cache/vercel/vercel-cache-store.ts +1102 -0
- package/src/client.rsc.tsx +43 -21
- package/src/client.tsx +131 -347
- package/src/cloudflare/index.ts +11 -0
- package/src/cloudflare/tracing.ts +109 -0
- package/src/component-utils.ts +23 -4
- package/src/components/DefaultDocument.tsx +13 -3
- package/src/context-var.ts +168 -0
- package/src/debug.ts +19 -9
- package/src/decode-loader-results.ts +52 -0
- package/src/defer.ts +185 -0
- package/src/deps/ssr.ts +0 -1
- package/src/encode-kv.ts +49 -0
- package/src/errors.ts +106 -10
- package/src/escape-script.ts +52 -0
- package/src/handle.ts +110 -35
- package/src/handles/MetaTags.tsx +83 -59
- package/src/handles/Scripts.tsx +183 -0
- package/src/handles/breadcrumbs.ts +93 -0
- package/src/handles/deferred-resolution.ts +127 -0
- package/src/handles/is-thenable.ts +18 -0
- package/src/handles/meta.ts +44 -53
- package/src/handles/script.ts +244 -0
- package/src/host/cookie-handler.ts +20 -65
- package/src/host/errors.ts +21 -30
- package/src/host/index.ts +13 -9
- package/src/host/pattern-matcher.ts +50 -79
- package/src/host/router.ts +151 -121
- package/src/host/testing.ts +45 -32
- package/src/host/types.ts +52 -11
- package/src/host/utils.ts +2 -2
- package/src/href-client.ts +192 -57
- package/src/index.rsc.ts +177 -35
- package/src/index.ts +255 -71
- package/src/internal-debug.ts +9 -2
- package/src/loader-store.ts +500 -0
- package/src/loader.rsc.ts +31 -99
- package/src/loader.ts +30 -12
- package/src/missing-id-error.ts +68 -0
- package/src/outlet-context.ts +1 -1
- package/src/outlet-provider.tsx +41 -0
- package/src/prerender/param-hash.ts +16 -14
- package/src/prerender/store.ts +121 -21
- package/src/prerender.ts +460 -26
- package/src/redirect-origin.ts +100 -0
- package/src/regex-escape.ts +8 -0
- package/src/render-error-thrower.tsx +20 -0
- package/src/response-utils.ts +62 -0
- package/src/reverse.ts +198 -128
- package/src/root-error-boundary.tsx +42 -48
- package/src/route-content-wrapper.tsx +22 -77
- package/src/route-definition/dsl-helpers.ts +1116 -0
- package/src/route-definition/helper-factories.ts +88 -0
- package/src/route-definition/helpers-types.ts +505 -0
- package/src/route-definition/index.ts +54 -0
- package/src/route-definition/redirect.ts +134 -0
- package/src/route-definition/resolve-handler-use.ts +160 -0
- package/src/route-definition/use-item-types.ts +29 -0
- package/src/route-definition.ts +1 -1481
- package/src/route-map-builder.ts +82 -144
- package/src/route-name.ts +53 -0
- package/src/route-types.ts +71 -45
- package/src/router/basename.ts +14 -0
- package/src/router/content-negotiation.ts +263 -0
- package/src/router/debug-manifest.ts +72 -0
- package/src/router/error-handling.ts +54 -27
- package/src/router/find-match.ts +245 -0
- package/src/router/handler-context.ts +377 -125
- package/src/router/instrument.ts +350 -0
- package/src/router/intercept-resolution.ts +59 -28
- package/src/router/lazy-includes.ts +254 -0
- package/src/router/loader-resolution.ts +421 -157
- package/src/router/logging.ts +106 -6
- package/src/router/manifest.ts +131 -57
- package/src/router/match-api.ts +167 -246
- package/src/router/match-context.ts +4 -24
- package/src/router/match-handlers.ts +440 -0
- package/src/router/match-middleware/background-revalidation.ts +117 -93
- package/src/router/match-middleware/cache-lookup.ts +297 -150
- package/src/router/match-middleware/cache-store.ts +123 -51
- package/src/router/match-middleware/intercept-resolution.ts +44 -43
- package/src/router/match-middleware/segment-resolution.ts +64 -22
- package/src/router/match-pipelines.ts +11 -87
- package/src/router/match-result.ts +121 -50
- package/src/router/metrics.ts +219 -28
- package/src/router/middleware-types.ts +93 -0
- package/src/router/middleware.ts +505 -441
- package/src/router/navigation-snapshot.ts +133 -0
- package/src/router/params-util.ts +23 -0
- package/src/router/parse-pattern.ts +115 -0
- package/src/router/pattern-matching.ts +311 -142
- package/src/router/prefetch-cache-ttl.ts +51 -0
- package/src/router/prefetch-limits.ts +37 -0
- package/src/router/prerender-match.ts +547 -0
- package/src/router/preview-match.ts +102 -0
- package/src/router/request-classification.ts +278 -0
- package/src/router/revalidation.ts +203 -62
- package/src/router/route-snapshot.ts +246 -0
- package/src/router/router-context.ts +45 -48
- package/src/router/router-interfaces.ts +554 -0
- package/src/router/router-options.ts +779 -0
- package/src/router/router-registry.ts +21 -0
- package/src/router/segment-resolution/fresh.ts +772 -0
- package/src/router/segment-resolution/helpers.ts +348 -0
- package/src/router/segment-resolution/loader-cache.ts +250 -0
- package/src/router/segment-resolution/loader-mask.ts +44 -0
- package/src/router/segment-resolution/revalidation.ts +1331 -0
- package/src/router/segment-resolution/static-store.ts +81 -0
- package/src/router/segment-resolution/streamed-handler-telemetry.ts +52 -0
- package/src/router/segment-resolution/view-transition-default.ts +56 -0
- package/src/router/segment-resolution.ts +25 -1354
- package/src/router/segment-wrappers.ts +292 -0
- package/src/router/state-cookie-name.ts +33 -0
- package/src/router/substitute-pattern-params.ts +75 -0
- package/src/router/telemetry-otel.ts +261 -0
- package/src/router/telemetry.ts +377 -0
- package/src/router/timeout.ts +128 -0
- package/src/router/tracing.ts +206 -0
- package/src/router/trie-matching.ts +240 -61
- package/src/router/types.ts +23 -70
- package/src/router/url-params.ts +57 -0
- package/src/router.ts +781 -2378
- package/src/rsc/full-payload.ts +70 -0
- package/src/rsc/handler-context.ts +46 -0
- package/src/rsc/handler.ts +905 -1142
- package/src/rsc/helpers.ts +275 -19
- package/src/rsc/index.ts +2 -25
- package/src/rsc/json-route-result.ts +38 -0
- package/src/rsc/loader-fetch.ts +305 -0
- package/src/rsc/manifest-init.ts +77 -0
- package/src/rsc/nonce.ts +14 -0
- package/src/rsc/origin-guard.ts +155 -0
- package/src/rsc/progressive-enhancement.ts +502 -0
- package/src/rsc/redirect-guard.ts +99 -0
- package/src/rsc/response-cache-serve.ts +238 -0
- package/src/rsc/response-error.ts +104 -0
- package/src/rsc/response-route-handler.ts +257 -0
- package/src/rsc/rsc-rendering.ts +337 -0
- package/src/rsc/runtime-warnings.ts +55 -0
- package/src/rsc/server-action.ts +522 -0
- package/src/rsc/shell-capture.ts +439 -0
- package/src/rsc/ssr-setup.ts +144 -0
- package/src/rsc/transition-gate.ts +89 -0
- package/src/rsc/types.ts +95 -12
- package/src/runtime-env.ts +18 -0
- package/src/search-params.ts +99 -82
- package/src/segment-content-promise.ts +67 -0
- package/src/segment-loader-promise.ts +149 -0
- package/src/segment-system.tsx +349 -134
- package/src/serialize.ts +243 -0
- package/src/server/context.ts +452 -85
- package/src/server/cookie-parse.ts +32 -0
- package/src/server/cookie-store.ts +310 -0
- package/src/server/fetchable-loader-store.ts +11 -6
- package/src/server/handle-store.ts +123 -42
- package/src/server/live.ts +130 -0
- package/src/server/loader-registry.ts +51 -100
- package/src/server/request-context.ts +842 -157
- package/src/server.ts +15 -8
- package/src/ssr/index.tsx +412 -136
- package/src/ssr/ssr-root.tsx +228 -0
- package/src/static-handler.ts +45 -18
- package/src/testing/cache-status.ts +162 -0
- package/src/testing/collect-handle.ts +46 -0
- package/src/testing/dispatch.ts +701 -0
- package/src/testing/dom.entry.ts +22 -0
- package/src/testing/e2e/fixture.ts +188 -0
- package/src/testing/e2e/index.ts +128 -0
- package/src/testing/e2e/matchers.ts +35 -0
- package/src/testing/e2e/page-helpers.ts +272 -0
- package/src/testing/e2e/parity.ts +387 -0
- package/src/testing/e2e/server.ts +195 -0
- package/src/testing/flight-matchers.ts +97 -0
- package/src/testing/flight-normalize.ts +11 -0
- package/src/testing/flight-runtime.d.ts +57 -0
- package/src/testing/flight-tree.ts +682 -0
- package/src/testing/flight.entry.ts +52 -0
- package/src/testing/flight.ts +257 -0
- package/src/testing/generated-routes.ts +199 -0
- package/src/testing/index.ts +105 -0
- package/src/testing/internal/context.ts +371 -0
- package/src/testing/internal/flight-client-globals.ts +30 -0
- package/src/testing/internal/seed-vars.ts +54 -0
- package/src/testing/render-handler.ts +357 -0
- package/src/testing/render-route.tsx +584 -0
- package/src/testing/run-loader.ts +385 -0
- package/src/testing/run-middleware.ts +205 -0
- package/src/testing/run-transition-when.ts +164 -0
- package/src/testing/vitest-stubs/cloudflare-email.ts +9 -0
- package/src/testing/vitest-stubs/cloudflare-workers.ts +21 -0
- package/src/testing/vitest-stubs/plugin-rsc.ts +16 -0
- package/src/testing/vitest-stubs/version.ts +5 -0
- package/src/testing/vitest.ts +305 -0
- package/src/theme/ThemeProvider.tsx +40 -72
- package/src/theme/ThemeScript.tsx +12 -14
- package/src/theme/constants.ts +57 -15
- package/src/theme/index.ts +3 -20
- package/src/theme/theme-context.ts +5 -35
- package/src/theme/theme-script.ts +43 -39
- package/src/theme/use-theme.ts +0 -3
- package/src/types/boundaries.ts +123 -0
- package/src/types/cache-types.ts +207 -0
- package/src/types/error-types.ts +132 -0
- package/src/types/global-namespace.ts +113 -0
- package/src/types/handler-context.ts +839 -0
- package/src/types/index.ts +81 -0
- package/src/types/loader-types.ts +212 -0
- package/src/types/request-scope.ts +112 -0
- package/src/types/route-config.ts +138 -0
- package/src/types/route-entry.ts +114 -0
- package/src/types/segments.ts +271 -0
- package/src/types.ts +1 -1795
- package/src/urls/include-helper.ts +162 -0
- package/src/urls/include-provider.ts +71 -0
- package/src/urls/index.ts +43 -0
- package/src/urls/path-helper-types.ts +413 -0
- package/src/urls/path-helper.ts +275 -0
- package/src/urls/pattern-types.ts +124 -0
- package/src/urls/response-types.ts +109 -0
- package/src/urls/type-extraction.ts +316 -0
- package/src/urls/urls-function.ts +80 -0
- package/src/urls.ts +1 -1341
- package/src/use-loader.tsx +406 -141
- package/src/vercel/index.ts +11 -0
- package/src/vercel/tracing.ts +88 -0
- package/src/vite/debug.ts +185 -0
- package/src/vite/discovery/bundle-postprocess.ts +182 -0
- package/src/vite/discovery/discover-routers.ts +389 -0
- package/src/vite/discovery/discovery-errors.ts +255 -0
- package/src/vite/discovery/gate-state.ts +171 -0
- package/src/vite/discovery/prerender-collection.ts +467 -0
- package/src/vite/discovery/route-types-writer.ts +214 -0
- package/src/vite/discovery/self-gen-tracking.ts +73 -0
- package/src/vite/discovery/state.ts +161 -0
- package/src/vite/discovery/virtual-module-codegen.ts +183 -0
- package/src/vite/index.ts +23 -2255
- package/src/vite/inject-client-debug.ts +36 -0
- package/src/vite/plugin-types.ts +303 -0
- package/src/vite/plugins/cjs-to-esm.ts +90 -0
- package/src/vite/plugins/client-ref-dedup.ts +120 -0
- package/src/vite/plugins/client-ref-hashing.ts +118 -0
- package/src/vite/plugins/cloudflare-protocol-loader-hook.d.mts +23 -0
- package/src/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/src/vite/plugins/cloudflare-protocol-stub.ts +194 -0
- package/src/vite/{expose-action-id.ts → plugins/expose-action-id.ts} +88 -110
- package/src/vite/{expose-id-utils.ts → plugins/expose-id-utils.ts} +89 -79
- package/src/vite/plugins/expose-ids/export-analysis.ts +363 -0
- package/src/vite/plugins/expose-ids/handler-transform.ts +130 -0
- package/src/vite/plugins/expose-ids/loader-transform.ts +64 -0
- package/src/vite/plugins/expose-ids/router-transform.ts +199 -0
- package/src/vite/plugins/expose-ids/types.ts +45 -0
- package/src/vite/plugins/expose-internal-ids.ts +805 -0
- package/src/vite/plugins/performance-tracks.ts +89 -0
- package/src/vite/plugins/refresh-cmd.ts +127 -0
- package/src/vite/plugins/use-cache-transform.ts +313 -0
- package/src/vite/plugins/vercel-output.ts +384 -0
- package/src/vite/plugins/version-injector.ts +94 -0
- package/src/vite/plugins/version-plugin.ts +263 -0
- package/src/vite/plugins/virtual-entries.ts +234 -0
- package/src/vite/plugins/virtual-stub-plugin.ts +29 -0
- package/src/vite/rango.ts +560 -0
- package/src/vite/router-discovery.ts +1638 -0
- package/src/vite/{ast-handler-extract.ts → utils/ast-handler-extract.ts} +200 -37
- package/src/vite/utils/banner.ts +36 -0
- package/src/vite/utils/bundle-analysis.ts +132 -0
- package/src/vite/utils/client-chunks.ts +184 -0
- package/src/vite/utils/directive-prologue.ts +40 -0
- package/src/vite/utils/forward-user-plugins.ts +171 -0
- package/src/vite/utils/manifest-utils.ts +15 -0
- package/src/vite/utils/package-resolution.ts +89 -0
- package/src/vite/utils/prerender-utils.ts +249 -0
- package/src/vite/utils/shared-utils.ts +269 -0
- package/CLAUDE.md +0 -43
- package/dist/vite/index.named-routes.gen.ts +0 -103
- package/src/browser/lru-cache.ts +0 -69
- package/src/browser/react/use-client-cache.ts +0 -56
- package/src/browser/request-controller.ts +0 -164
- package/src/browser/shallow.ts +0 -35
- package/src/cache/memory-store.ts +0 -253
- package/src/handles/index.ts +0 -6
- package/src/href-context.ts +0 -33
- package/src/network-error-thrower.tsx +0 -21
- package/src/router.gen.ts +0 -6
- package/src/static-handler.gen.ts +0 -5
- package/src/urls.gen.ts +0 -8
- package/src/vite/expose-internal-ids.ts +0 -1167
- package/src/vite/package-resolution.ts +0 -125
- package/src/vite/virtual-entries.ts +0 -114
- /package/src/vite/{version.d.ts → plugins/version.d.ts} +0 -0
package/src/host/testing.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Helper functions for testing host routing.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import { matchPattern } from
|
|
7
|
+
import { matchPattern, parseRequest } from "./pattern-matcher.js";
|
|
8
8
|
|
|
9
9
|
export interface CreateTestRequestOptions {
|
|
10
10
|
host: string;
|
|
@@ -14,23 +14,11 @@ export interface CreateTestRequestOptions {
|
|
|
14
14
|
headers?: Record<string, string>;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
|
-
/**
|
|
18
|
-
* Create a test request with specific host and cookies
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* ```ts
|
|
22
|
-
* const request = createTestRequest({
|
|
23
|
-
* host: 'admin.example.com',
|
|
24
|
-
* path: '/dashboard',
|
|
25
|
-
* cookies: { 'x-requested-host': 'api.example.com' }
|
|
26
|
-
* });
|
|
27
|
-
* ```
|
|
28
|
-
*/
|
|
29
17
|
export function createTestRequest(options: CreateTestRequestOptions): Request {
|
|
30
18
|
const {
|
|
31
19
|
host,
|
|
32
|
-
path =
|
|
33
|
-
method =
|
|
20
|
+
path = "/",
|
|
21
|
+
method = "GET",
|
|
34
22
|
cookies = {},
|
|
35
23
|
headers = {},
|
|
36
24
|
} = options;
|
|
@@ -38,12 +26,11 @@ export function createTestRequest(options: CreateTestRequestOptions): Request {
|
|
|
38
26
|
const url = `http://${host}${path}`;
|
|
39
27
|
const requestHeaders = new Headers(headers);
|
|
40
28
|
|
|
41
|
-
// Add cookies if provided
|
|
42
29
|
if (Object.keys(cookies).length > 0) {
|
|
43
30
|
const cookieString = Object.entries(cookies)
|
|
44
31
|
.map(([key, value]) => `${key}=${encodeURIComponent(value)}`)
|
|
45
|
-
.join(
|
|
46
|
-
requestHeaders.set(
|
|
32
|
+
.join("; ");
|
|
33
|
+
requestHeaders.set("cookie", cookieString);
|
|
47
34
|
}
|
|
48
35
|
|
|
49
36
|
return new Request(url, {
|
|
@@ -52,28 +39,54 @@ export function createTestRequest(options: CreateTestRequestOptions): Request {
|
|
|
52
39
|
});
|
|
53
40
|
}
|
|
54
41
|
|
|
42
|
+
function matchPatterns(
|
|
43
|
+
pattern: string | string[],
|
|
44
|
+
hostname: string,
|
|
45
|
+
pathname: string,
|
|
46
|
+
parts: string[],
|
|
47
|
+
): boolean {
|
|
48
|
+
const patterns = Array.isArray(pattern) ? pattern : [pattern];
|
|
49
|
+
return patterns.some((p) => matchPattern(p, hostname, pathname, parts));
|
|
50
|
+
}
|
|
51
|
+
|
|
55
52
|
/**
|
|
56
|
-
* Test if a pattern matches a hostname
|
|
53
|
+
* Test if a pattern matches a hostname (and, for path-based patterns, a pathname).
|
|
54
|
+
*
|
|
55
|
+
* `pathname` defaults to `"/"`, so a host-only pattern works with two args. Pass
|
|
56
|
+
* the third arg to test a path-based pattern (`**.workers.dev/admin`,
|
|
57
|
+
* `localhost/shop`) — without it those patterns can never match.
|
|
57
58
|
*
|
|
58
59
|
* @example
|
|
59
60
|
* ```ts
|
|
60
|
-
* expect(testPattern(
|
|
61
|
-
* expect(testPattern([
|
|
61
|
+
* expect(testPattern("admin.*", "admin.example.com")).toBe(true);
|
|
62
|
+
* expect(testPattern(["*", "www.*"], "example.com")).toBe(true);
|
|
63
|
+
* expect(testPattern("**.workers.dev/admin", "foo.workers.dev", "/admin")).toBe(true);
|
|
62
64
|
* ```
|
|
63
65
|
*/
|
|
64
66
|
export function testPattern(
|
|
65
67
|
pattern: string | string[],
|
|
66
|
-
hostname: string
|
|
68
|
+
hostname: string,
|
|
69
|
+
pathname: string = "/",
|
|
67
70
|
): boolean {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
const pathname = '/';
|
|
71
|
-
|
|
72
|
-
for (const p of patterns) {
|
|
73
|
-
if (matchPattern(p, hostname, pathname, parts)) {
|
|
74
|
-
return true;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
71
|
+
return matchPatterns(pattern, hostname, pathname, hostname.split("."));
|
|
72
|
+
}
|
|
77
73
|
|
|
78
|
-
|
|
74
|
+
/**
|
|
75
|
+
* Test if a pattern matches a `Request` — the hostname AND pathname are taken
|
|
76
|
+
* from the request URL (via the same `parseRequest` the host router uses), so a
|
|
77
|
+
* path-based pattern is tested against a real request without splitting the URL
|
|
78
|
+
* by hand.
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* const req = new Request("https://foo.workers.dev/admin");
|
|
83
|
+
* expect(matchesHost("**.workers.dev/admin", req)).toBe(true);
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export function matchesHost(
|
|
87
|
+
pattern: string | string[],
|
|
88
|
+
request: Request,
|
|
89
|
+
): boolean {
|
|
90
|
+
const { hostname, pathname, parts } = parseRequest(request);
|
|
91
|
+
return matchPatterns(pattern, hostname, pathname, parts);
|
|
79
92
|
}
|
package/src/host/types.ts
CHANGED
|
@@ -4,12 +4,15 @@
|
|
|
4
4
|
* Type definitions for the host-based routing system.
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
+
import type { RouterRequestInput } from "../router/router-interfaces.js";
|
|
8
|
+
|
|
7
9
|
/**
|
|
8
|
-
* Handler function that processes a request and returns a response
|
|
10
|
+
* Handler function that processes a request and returns a response.
|
|
11
|
+
* The input parameter receives the same RouterRequestInput passed to match().
|
|
9
12
|
*/
|
|
10
13
|
export type Handler = (
|
|
11
14
|
request: Request,
|
|
12
|
-
|
|
15
|
+
input: RouterRequestInput<any>,
|
|
13
16
|
) => Response | Promise<Response>;
|
|
14
17
|
|
|
15
18
|
/**
|
|
@@ -18,12 +21,13 @@ export type Handler = (
|
|
|
18
21
|
export type LazyHandler = () => Promise<{ default: Handler | HostRouter }>;
|
|
19
22
|
|
|
20
23
|
/**
|
|
21
|
-
* Middleware function that can intercept and modify requests/responses
|
|
24
|
+
* Middleware function that can intercept and modify requests/responses.
|
|
25
|
+
* The input parameter receives the same RouterRequestInput passed to match().
|
|
22
26
|
*/
|
|
23
27
|
export type Middleware = (
|
|
24
28
|
request: Request,
|
|
25
|
-
|
|
26
|
-
next: () => Promise<Response
|
|
29
|
+
input: RouterRequestInput<any>,
|
|
30
|
+
next: () => Promise<Response>,
|
|
27
31
|
) => Promise<Response>;
|
|
28
32
|
|
|
29
33
|
/**
|
|
@@ -31,12 +35,24 @@ export type Middleware = (
|
|
|
31
35
|
*/
|
|
32
36
|
export type HostPattern = string | string[];
|
|
33
37
|
|
|
38
|
+
/**
|
|
39
|
+
* Whether a route entry is an inline request handler or a lazy module mount.
|
|
40
|
+
*
|
|
41
|
+
* Stored on the entry so discovery and runtime act on the consumer's declared
|
|
42
|
+
* intent instead of inferring it from the function's shape (arity/return value),
|
|
43
|
+
* which is ambiguous: a lazy loader may declare an ignored param, and an inline
|
|
44
|
+
* handler may be async. `.map()` registers `"handler"`, `.lazy()` registers
|
|
45
|
+
* `"lazy"`.
|
|
46
|
+
*/
|
|
47
|
+
export type RouteEntryKind = "handler" | "lazy";
|
|
48
|
+
|
|
34
49
|
/**
|
|
35
50
|
* Result from testing a hostname against patterns
|
|
36
51
|
*/
|
|
37
52
|
export interface HostMatchResult {
|
|
38
53
|
pattern: string;
|
|
39
54
|
handler: Handler | LazyHandler;
|
|
55
|
+
kind: RouteEntryKind;
|
|
40
56
|
}
|
|
41
57
|
|
|
42
58
|
/**
|
|
@@ -49,9 +65,24 @@ export interface HostRouteBuilder {
|
|
|
49
65
|
use(...middleware: Middleware[]): HostRouteBuilder;
|
|
50
66
|
|
|
51
67
|
/**
|
|
52
|
-
* Map to
|
|
68
|
+
* Map to an inline request handler `(request, input) => Response`.
|
|
69
|
+
*
|
|
70
|
+
* For a lazily-imported sub-app or handler module, use {@link lazy} instead -
|
|
71
|
+
* `.map(() => import(...))` is rejected (the return type is not a `Response`)
|
|
72
|
+
* and would not be discovered at build time.
|
|
73
|
+
*/
|
|
74
|
+
map(handler: Handler): HostRouter;
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Mount a lazily-imported handler or host router:
|
|
78
|
+
* `.lazy(() => import("./sub-app"))`.
|
|
79
|
+
*
|
|
80
|
+
* The loader takes no arguments and resolves to a module whose `default`
|
|
81
|
+
* export is a request `Handler` or a nested `HostRouter`. Only `.lazy()`
|
|
82
|
+
* entries are invoked during build-time discovery to trigger the sub-app's
|
|
83
|
+
* `createRouter()` registration.
|
|
53
84
|
*/
|
|
54
|
-
|
|
85
|
+
lazy(handler: LazyHandler): HostRouter;
|
|
55
86
|
}
|
|
56
87
|
|
|
57
88
|
/**
|
|
@@ -71,7 +102,7 @@ export interface HostRouter {
|
|
|
71
102
|
/**
|
|
72
103
|
* Match an incoming request
|
|
73
104
|
*/
|
|
74
|
-
match(request: Request,
|
|
105
|
+
match(request: Request, input?: RouterRequestInput<any>): Promise<Response>;
|
|
75
106
|
|
|
76
107
|
/**
|
|
77
108
|
* Register fallback handler for allowed hosts without valid cookie
|
|
@@ -79,9 +110,13 @@ export interface HostRouter {
|
|
|
79
110
|
fallback(): HostRouteBuilder;
|
|
80
111
|
|
|
81
112
|
/**
|
|
82
|
-
* Test which handler would match a hostname
|
|
113
|
+
* Test which handler would match a hostname (and optional pathname).
|
|
114
|
+
*
|
|
115
|
+
* `pathname` defaults to `"/"`. Pass it to probe path-prefixed patterns
|
|
116
|
+
* such as `host(["example.com/admin"])`, which only match when the request
|
|
117
|
+
* path is under the prefix.
|
|
83
118
|
*/
|
|
84
|
-
test(hostname: string): HostMatchResult | null;
|
|
119
|
+
test(hostname: string, pathname?: string): HostMatchResult | null;
|
|
85
120
|
}
|
|
86
121
|
|
|
87
122
|
/**
|
|
@@ -101,7 +136,11 @@ export interface HostOverrideConfig {
|
|
|
101
136
|
/**
|
|
102
137
|
* Optional validation function
|
|
103
138
|
*/
|
|
104
|
-
validate?: (
|
|
139
|
+
validate?: (
|
|
140
|
+
request: Request,
|
|
141
|
+
cookieValue: string,
|
|
142
|
+
input: RouterRequestInput<any>,
|
|
143
|
+
) => string;
|
|
105
144
|
}
|
|
106
145
|
|
|
107
146
|
/**
|
|
@@ -126,6 +165,8 @@ export interface RouteEntry {
|
|
|
126
165
|
patterns: string[];
|
|
127
166
|
middleware: Middleware[];
|
|
128
167
|
handler: Handler | LazyHandler;
|
|
168
|
+
/** Whether `handler` is an inline request handler or a lazy module mount. */
|
|
169
|
+
kind: RouteEntryKind;
|
|
129
170
|
isFallback?: boolean;
|
|
130
171
|
}
|
|
131
172
|
|
package/src/host/utils.ts
CHANGED
|
@@ -15,11 +15,11 @@
|
|
|
15
15
|
* app: ['*', 'www.*']
|
|
16
16
|
* });
|
|
17
17
|
*
|
|
18
|
-
* router.host(hosts.admin).
|
|
18
|
+
* router.host(hosts.admin).lazy(() => import("./apps/admin")); // Type-safe!
|
|
19
19
|
* ```
|
|
20
20
|
*/
|
|
21
21
|
export function defineHosts<T extends Record<string, string | string[]>>(
|
|
22
|
-
hosts: T
|
|
22
|
+
hosts: T,
|
|
23
23
|
): Readonly<T> {
|
|
24
24
|
return Object.freeze(hosts);
|
|
25
25
|
}
|
package/src/href-client.ts
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
import type { GetRegisteredRoutes } from "./types.js";
|
|
18
|
-
import type {
|
|
18
|
+
import type { JsonSerialize } from "./serialize.js";
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
21
|
* Parse constraint values into a union type for paths
|
|
@@ -45,30 +45,34 @@ type ParseConstraintPath<T extends string> =
|
|
|
45
45
|
export type PatternToPath<T extends string> =
|
|
46
46
|
// Optional + constrained param in middle: /:param(a|b)?/rest
|
|
47
47
|
T extends `${infer Before}:${infer _Name}(${infer Constraint})?/${infer After}`
|
|
48
|
-
?
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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;
|
|
72
76
|
|
|
73
77
|
/**
|
|
74
78
|
* Allow optional query string (?...) and/or hash fragment (#...) suffix
|
|
@@ -85,59 +89,186 @@ type WithSuffix<T extends string> =
|
|
|
85
89
|
/**
|
|
86
90
|
* Helper type to get pattern from routes, handling string values and { path, response } objects
|
|
87
91
|
*/
|
|
88
|
-
type RoutePattern<TRoutes, K extends keyof TRoutes> =
|
|
89
|
-
|
|
90
|
-
: TRoutes[K] extends { readonly path: infer P extends string }
|
|
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;
|
|
92
97
|
|
|
93
98
|
/**
|
|
94
99
|
* Reverse lookup: find route name where the pattern matches TPattern
|
|
95
100
|
*/
|
|
96
|
-
type NameForPattern<
|
|
97
|
-
|
|
98
|
-
TRoutes = GetRegisteredRoutes
|
|
99
|
-
> = {
|
|
100
|
-
[K in keyof TRoutes]: RoutePattern<TRoutes, K> extends TPattern ? K : never
|
|
101
|
+
type NameForPattern<TPattern extends string, TRoutes = GetRegisteredRoutes> = {
|
|
102
|
+
[K in keyof TRoutes]: RoutePattern<TRoutes, K> extends TPattern ? K : never;
|
|
101
103
|
}[keyof TRoutes];
|
|
102
104
|
|
|
103
105
|
/**
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
106
|
+
* Strip a query (`?…`) and/or hash (`#…`) suffix before matching, so a concrete
|
|
107
|
+
* URL like `/api/health?ts=1` still resolves to its route's response. Removes
|
|
108
|
+
* from the earliest of `?`/`#`: a `#` before the first `?` (the query is part of
|
|
109
|
+
* a fragment, e.g. `/health#top?x=1`) is handled, as is a `/:` that only appears
|
|
110
|
+
* inside the query (e.g. `/health?next=/:id`).
|
|
111
|
+
*/
|
|
112
|
+
type StripPathSuffix<T extends string> = T extends `${infer Base}?${string}`
|
|
113
|
+
? Base extends `${infer Frag}#${string}`
|
|
114
|
+
? Frag
|
|
115
|
+
: Base
|
|
116
|
+
: T extends `${infer Base}#${string}`
|
|
117
|
+
? Base
|
|
118
|
+
: T;
|
|
119
|
+
|
|
120
|
+
/** Extract a route entry's response payload (or `never` for RSC routes). */
|
|
121
|
+
type ResponsePayloadOf<TRoutes, K extends keyof TRoutes> = TRoutes[K] extends {
|
|
122
|
+
readonly response: infer R;
|
|
123
|
+
}
|
|
124
|
+
? Exclude<R, Response>
|
|
125
|
+
: never;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Look up the response payload for a route, keyed by either a route pattern
|
|
129
|
+
* (`/api/products/:id`) or a concrete path (`/api/products/123`). The same type
|
|
130
|
+
* serves a pattern lookup and a typed `fetch` wrapper that forwards a concrete
|
|
131
|
+
* `Rango.Path`:
|
|
108
132
|
*
|
|
109
|
-
*
|
|
110
|
-
* PathResponse<"/api/
|
|
133
|
+
* PathResponse<"/api/products/:id"> → Product // by pattern
|
|
134
|
+
* PathResponse<"/api/products/123"> → Product // by concrete path
|
|
111
135
|
*
|
|
112
|
-
*
|
|
113
|
-
*
|
|
136
|
+
* The query/hash suffix is stripped first; the stripped key is then treated as a
|
|
137
|
+
* pattern when it contains a `/:param` segment and matched exactly (precise even
|
|
138
|
+
* for nested dynamic routes), otherwise as a concrete path matched against each
|
|
139
|
+
* route's `PatternToPath` template. Because those holes are `${string}`
|
|
140
|
+
* (slash-greedy), a concrete path under a *nested* dynamic route can match several
|
|
141
|
+
* patterns and union their responses — pattern lookups do not have this
|
|
142
|
+
* looseness. RSC routes (no response) and unmatched keys resolve to `never`.
|
|
114
143
|
*/
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
144
|
+
type ResponsePayloadFor<
|
|
145
|
+
TPath extends string,
|
|
146
|
+
TRoutes = GetRegisteredRoutes,
|
|
147
|
+
> = ResponsePayloadForKey<StripPathSuffix<TPath>, TRoutes>;
|
|
148
|
+
|
|
149
|
+
type ResponsePayloadForKey<
|
|
150
|
+
TKey extends string,
|
|
151
|
+
TRoutes,
|
|
152
|
+
> = TKey extends `${string}/:${string}`
|
|
153
|
+
? {
|
|
154
|
+
[K in keyof TRoutes]: RoutePattern<TRoutes, K> extends TKey
|
|
155
|
+
? ResponsePayloadOf<TRoutes, K>
|
|
156
|
+
: never;
|
|
157
|
+
}[keyof TRoutes]
|
|
158
|
+
: {
|
|
159
|
+
[K in keyof TRoutes]: TKey extends PatternToPath<RoutePattern<TRoutes, K>>
|
|
160
|
+
? ResponsePayloadOf<TRoutes, K>
|
|
161
|
+
: never;
|
|
162
|
+
}[keyof TRoutes];
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Public response type for a route, keyed by pattern or concrete path. JSON
|
|
166
|
+
* response routes send the handler's return value verbatim (bare), so the
|
|
167
|
+
* payload is wrapped only in `JsonSerialize` to describe the JSON **wire** value
|
|
168
|
+
* a consumer receives from `fetch().then(r => r.json())` — e.g. a handler
|
|
169
|
+
* returning `{ createdAt: Date }` resolves here to `{ createdAt: string }`.
|
|
170
|
+
*/
|
|
171
|
+
export type PathResponse<
|
|
172
|
+
TPath extends string,
|
|
173
|
+
TRoutes = GetRegisteredRoutes,
|
|
174
|
+
> = JsonSerialize<ResponsePayloadFor<TPath, TRoutes>>;
|
|
121
175
|
|
|
122
176
|
/**
|
|
123
177
|
* Strip trailing slash from a path (e.g., "/blog/" -> "/blog" | "/blog/")
|
|
124
178
|
* Allows navigation to include() prefixes without requiring trailing slash
|
|
125
179
|
*/
|
|
126
|
-
type OptionalTrailingSlash<T extends string> =
|
|
127
|
-
|
|
180
|
+
type OptionalTrailingSlash<T extends string> = T extends `${infer Base}/`
|
|
181
|
+
? Base extends ""
|
|
182
|
+
? T
|
|
183
|
+
: Base | T
|
|
184
|
+
: T;
|
|
128
185
|
|
|
129
186
|
/**
|
|
130
187
|
* Union of all valid paths from registered routes
|
|
131
188
|
*
|
|
132
|
-
* Generated from
|
|
189
|
+
* Generated from Rango.RegisteredRoutes via module augmentation.
|
|
133
190
|
* Allows optional query strings and hash fragments.
|
|
134
191
|
*/
|
|
135
192
|
export type ValidPaths<TRoutes = GetRegisteredRoutes> =
|
|
136
193
|
keyof TRoutes extends never
|
|
137
194
|
? `/${string}` // Fallback when no routes are registered
|
|
138
|
-
: WithSuffix<
|
|
139
|
-
|
|
140
|
-
|
|
195
|
+
: WithSuffix<
|
|
196
|
+
{
|
|
197
|
+
[K in keyof TRoutes]: OptionalTrailingSlash<
|
|
198
|
+
PatternToPath<RoutePattern<TRoutes, K>>
|
|
199
|
+
>;
|
|
200
|
+
}[keyof TRoutes]
|
|
201
|
+
>;
|
|
202
|
+
|
|
203
|
+
// Module-scoped alias so the ambient `Rango.PathResponse` below can reference
|
|
204
|
+
// the module-level `PathResponse` without the global namespace shadowing the
|
|
205
|
+
// name when both are called `PathResponse`.
|
|
206
|
+
type GlobalPathResponse<
|
|
207
|
+
TPattern extends string,
|
|
208
|
+
TRoutes = GetRegisteredRoutes,
|
|
209
|
+
> = PathResponse<TPattern, TRoutes>;
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Ambient path types on the `Rango` namespace.
|
|
213
|
+
*
|
|
214
|
+
* These live on the same global namespace consumers already augment for
|
|
215
|
+
* `Rango.Env` / `Rango.Vars`, so they are reachable with no import wherever the
|
|
216
|
+
* router's types are in scope. They are the public, recommended surface for
|
|
217
|
+
* typing anything that wraps `href()`. `ValidPaths` / `PathResponse` stay as the
|
|
218
|
+
* internal building blocks behind them.
|
|
219
|
+
*/
|
|
220
|
+
declare global {
|
|
221
|
+
namespace Rango {
|
|
222
|
+
/**
|
|
223
|
+
* Union of every valid route path accepted by `href()`.
|
|
224
|
+
*
|
|
225
|
+
* Type a wrapper's path parameter as `Rango.Path` so it shares `href()`'s
|
|
226
|
+
* compile-time validation against the registered routes:
|
|
227
|
+
*
|
|
228
|
+
* ```ts
|
|
229
|
+
* import { href } from "@rangojs/router/client";
|
|
230
|
+
*
|
|
231
|
+
* export const appHref = (path: Rango.Path) => href(path);
|
|
232
|
+
* ```
|
|
233
|
+
*
|
|
234
|
+
* Resolves from `Rango.RegisteredRoutes` when augmented, otherwise the
|
|
235
|
+
* auto-generated `Rango.GeneratedRouteMap`, otherwise a permissive
|
|
236
|
+
* `/${string}` fallback.
|
|
237
|
+
*/
|
|
238
|
+
type Path<TRoutes = GetRegisteredRoutes> = ValidPaths<TRoutes>;
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Response payload for a route, looked up from the global route map by
|
|
242
|
+
* either a route pattern (`/api/products/:id`) or a concrete path
|
|
243
|
+
* (`/api/products/123`). Because it accepts a concrete `Rango.Path`, it
|
|
244
|
+
* doubles as the return type of a typed `fetch` wrapper:
|
|
245
|
+
*
|
|
246
|
+
* ```ts
|
|
247
|
+
* type Product = Rango.PathResponse<"/api/products/:id">; // by pattern
|
|
248
|
+
* type Same = Rango.PathResponse<"/api/products/42">; // by concrete path
|
|
249
|
+
*
|
|
250
|
+
* const get = async <T extends Rango.Path>(
|
|
251
|
+
* path: T,
|
|
252
|
+
* ): Promise<Rango.PathResponse<T>> =>
|
|
253
|
+
* fetch(href(path)).then((r) => r.json());
|
|
254
|
+
* ```
|
|
255
|
+
*
|
|
256
|
+
* The payload is the JSON **wire** shape (via `Rango.JsonSerialize`), not the
|
|
257
|
+
* handler's raw return — a handler returning `{ createdAt: Date }` resolves
|
|
258
|
+
* here to `{ createdAt: string }` (bare, no envelope), matching what
|
|
259
|
+
* `fetch().then(r => r.json())` actually yields.
|
|
260
|
+
*
|
|
261
|
+
* Only resolves once `Rango.RegisteredRoutes` carries response metadata (the
|
|
262
|
+
* generated map has paths and search but no payloads). Pass an explicit route
|
|
263
|
+
* map as the second argument to look up against a non-global map (rarely
|
|
264
|
+
* needed in app code).
|
|
265
|
+
*/
|
|
266
|
+
type PathResponse<
|
|
267
|
+
TPath extends string,
|
|
268
|
+
TRoutes = GetRegisteredRoutes,
|
|
269
|
+
> = GlobalPathResponse<TPath, TRoutes>;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
141
272
|
|
|
142
273
|
/**
|
|
143
274
|
* Type-safe href function for client-side use
|
|
@@ -167,9 +298,10 @@ export type ValidPaths<TRoutes = GetRegisteredRoutes> =
|
|
|
167
298
|
*/
|
|
168
299
|
export function href<T extends ValidPaths>(path: T, mount?: string): string {
|
|
169
300
|
if (mount && mount !== "/") {
|
|
170
|
-
|
|
301
|
+
const normalizedMount = mount.endsWith("/") ? mount.slice(0, -1) : mount;
|
|
302
|
+
return normalizedMount + path;
|
|
171
303
|
}
|
|
172
|
-
return path;
|
|
304
|
+
return path as string;
|
|
173
305
|
}
|
|
174
306
|
|
|
175
307
|
/**
|
|
@@ -181,7 +313,10 @@ export interface ResponseHrefProps {
|
|
|
181
313
|
"data-external": "";
|
|
182
314
|
}
|
|
183
315
|
|
|
184
|
-
type ResponseHrefFn = <T extends ValidPaths>(
|
|
316
|
+
type ResponseHrefFn = <T extends ValidPaths>(
|
|
317
|
+
path: T,
|
|
318
|
+
mount?: string,
|
|
319
|
+
) => ResponseHrefProps;
|
|
185
320
|
|
|
186
321
|
function createResponseHrefTag(): ResponseHrefFn {
|
|
187
322
|
return (path, mount) => ({
|