@rangojs/router 0.0.0-experimental.d7eeaa75 → 0.0.0-experimental.d98a8e9d
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/README.md +120 -25
- package/dist/bin/rango.js +147 -57
- package/dist/testing/vitest.js +82 -0
- package/dist/vite/index.js +2154 -861
- package/dist/vite/plugins/cloudflare-protocol-loader-hook.mjs +76 -0
- package/package.json +57 -11
- package/skills/api-client/SKILL.md +211 -0
- package/skills/breadcrumbs/SKILL.md +3 -1
- package/skills/bundle-analysis/SKILL.md +159 -0
- package/skills/cache-guide/SKILL.md +220 -30
- package/skills/caching/SKILL.md +116 -8
- package/skills/composability/SKILL.md +27 -2
- package/skills/document-cache/SKILL.md +78 -55
- package/skills/handler-use/SKILL.md +364 -0
- package/skills/hooks/SKILL.md +229 -20
- package/skills/host-router/SKILL.md +45 -20
- package/skills/i18n/SKILL.md +276 -0
- package/skills/intercept/SKILL.md +46 -4
- package/skills/layout/SKILL.md +28 -7
- package/skills/links/SKILL.md +247 -17
- package/skills/loader/SKILL.md +219 -9
- package/skills/middleware/SKILL.md +47 -12
- package/skills/migrate-nextjs/SKILL.md +562 -0
- package/skills/migrate-react-router/SKILL.md +769 -0
- package/skills/mime-routes/SKILL.md +27 -0
- package/skills/observability/SKILL.md +137 -0
- package/skills/parallel/SKILL.md +71 -6
- package/skills/prerender/SKILL.md +14 -33
- package/skills/rango/SKILL.md +243 -22
- package/skills/react-compiler/SKILL.md +168 -0
- package/skills/response-routes/SKILL.md +122 -47
- package/skills/route/SKILL.md +57 -4
- package/skills/router-setup/SKILL.md +3 -3
- package/skills/server-actions/SKILL.md +751 -0
- package/skills/streams-and-websockets/SKILL.md +283 -0
- package/skills/testing/SKILL.md +128 -0
- package/skills/testing/bindings.md +89 -0
- package/skills/testing/cache-prerender.md +98 -0
- package/skills/testing/client-components.md +121 -0
- package/skills/testing/e2e-parity.md +124 -0
- package/skills/testing/flight.md +89 -0
- package/skills/testing/handles.md +127 -0
- package/skills/testing/loader.md +108 -0
- package/skills/testing/middleware.md +97 -0
- package/skills/testing/render-handler.md +102 -0
- package/skills/testing/response-routes.md +94 -0
- package/skills/testing/reverse-and-types.md +83 -0
- package/skills/testing/server-actions.md +89 -0
- package/skills/testing/server-tree.md +128 -0
- package/skills/testing/setup.md +120 -0
- package/skills/typesafety/SKILL.md +319 -27
- package/skills/use-cache/SKILL.md +34 -5
- package/skills/view-transitions/SKILL.md +294 -0
- package/src/__augment-tests__/augment.ts +81 -0
- package/src/__augment-tests__/augmented.check.ts +116 -0
- package/src/browser/action-coordinator.ts +53 -36
- package/src/browser/app-shell.ts +52 -0
- package/src/browser/event-controller.ts +86 -70
- package/src/browser/history-state.ts +21 -0
- package/src/browser/index.ts +3 -3
- package/src/browser/navigation-bridge.ts +84 -11
- package/src/browser/navigation-client.ts +104 -68
- package/src/browser/navigation-store.ts +32 -9
- package/src/browser/navigation-transaction.ts +10 -28
- package/src/browser/partial-update.ts +64 -26
- package/src/browser/prefetch/cache.ts +183 -44
- package/src/browser/prefetch/fetch.ts +228 -37
- package/src/browser/prefetch/queue.ts +36 -5
- package/src/browser/rango-state.ts +53 -13
- package/src/browser/react/Link.tsx +30 -2
- package/src/browser/react/NavigationProvider.tsx +72 -31
- package/src/browser/react/filter-segment-order.ts +51 -7
- package/src/browser/react/index.ts +3 -0
- package/src/browser/react/location-state-shared.ts +175 -4
- package/src/browser/react/location-state.ts +39 -13
- package/src/browser/react/use-handle.ts +17 -9
- package/src/browser/react/use-navigation.ts +22 -2
- package/src/browser/react/use-params.ts +20 -8
- package/src/browser/react/use-reverse.ts +106 -0
- package/src/browser/react/use-router.ts +22 -2
- package/src/browser/react/use-segments.ts +11 -8
- package/src/browser/response-adapter.ts +32 -1
- package/src/browser/rsc-router.tsx +69 -22
- package/src/browser/scroll-restoration.ts +22 -14
- package/src/browser/segment-reconciler.ts +36 -14
- package/src/browser/segment-structure-assert.ts +2 -2
- package/src/browser/server-action-bridge.ts +23 -30
- package/src/browser/types.ts +21 -0
- package/src/build/collect-fallback-refs.ts +107 -0
- package/src/build/generate-manifest.ts +60 -35
- package/src/build/generate-route-types.ts +2 -0
- package/src/build/index.ts +8 -1
- package/src/build/prefix-tree-utils.ts +123 -0
- package/src/build/route-trie.ts +95 -25
- package/src/build/route-types/codegen.ts +4 -4
- package/src/build/route-types/include-resolution.ts +1 -1
- package/src/build/route-types/per-module-writer.ts +7 -4
- package/src/build/route-types/router-processing.ts +55 -14
- package/src/build/route-types/scan-filter.ts +1 -1
- package/src/build/route-types/source-scan.ts +118 -0
- package/src/build/runtime-discovery.ts +9 -20
- package/src/cache/cache-scope.ts +28 -42
- package/src/cache/cf/cf-cache-store.ts +54 -13
- package/src/client.rsc.tsx +3 -0
- package/src/client.tsx +96 -205
- package/src/context-var.ts +5 -5
- package/src/decode-loader-results.ts +36 -0
- package/src/errors.ts +30 -4
- package/src/handle.ts +32 -14
- package/src/host/index.ts +2 -2
- package/src/host/router.ts +129 -57
- package/src/host/types.ts +31 -2
- package/src/host/utils.ts +1 -1
- package/src/href-client.ts +140 -21
- package/src/index.rsc.ts +10 -6
- package/src/index.ts +54 -17
- package/src/loader-store.ts +500 -0
- package/src/loader.rsc.ts +25 -7
- package/src/loader.ts +16 -9
- package/src/missing-id-error.ts +68 -0
- package/src/outlet-context.ts +1 -1
- package/src/prerender.ts +27 -6
- package/src/response-utils.ts +37 -0
- package/src/reverse.ts +65 -36
- package/src/route-content-wrapper.tsx +6 -28
- package/src/route-definition/dsl-helpers.ts +384 -257
- package/src/route-definition/helper-factories.ts +29 -139
- package/src/route-definition/helpers-types.ts +100 -28
- package/src/route-definition/resolve-handler-use.ts +6 -0
- package/src/route-definition/use-item-types.ts +32 -0
- package/src/route-types.ts +26 -41
- package/src/router/basename.ts +14 -0
- package/src/router/content-negotiation.ts +15 -2
- package/src/router/error-handling.ts +1 -1
- package/src/router/find-match.ts +54 -6
- package/src/router/handler-context.ts +21 -38
- package/src/router/intercept-resolution.ts +4 -18
- package/src/router/lazy-includes.ts +41 -22
- package/src/router/loader-resolution.ts +82 -36
- package/src/router/manifest.ts +41 -19
- package/src/router/match-api.ts +4 -3
- package/src/router/match-handlers.ts +63 -20
- package/src/router/match-middleware/cache-lookup.ts +44 -91
- package/src/router/match-middleware/cache-store.ts +3 -2
- package/src/router/match-result.ts +53 -32
- package/src/router/metrics.ts +1 -1
- package/src/router/middleware-types.ts +15 -26
- package/src/router/middleware.ts +99 -84
- package/src/router/pattern-matching.ts +116 -19
- package/src/router/prerender-match.ts +1 -1
- package/src/router/preview-match.ts +3 -1
- package/src/router/request-classification.ts +4 -28
- package/src/router/revalidation.ts +58 -2
- package/src/router/router-interfaces.ts +45 -28
- package/src/router/router-options.ts +40 -1
- package/src/router/router-registry.ts +2 -5
- package/src/router/segment-resolution/fresh.ts +27 -6
- package/src/router/segment-resolution/revalidation.ts +147 -106
- package/src/router/segment-resolution/view-transition-default.ts +36 -0
- package/src/router/substitute-pattern-params.ts +56 -0
- package/src/router/telemetry.ts +99 -0
- package/src/router/trie-matching.ts +40 -16
- package/src/router/types.ts +8 -0
- package/src/router/url-params.ts +49 -0
- package/src/router.ts +52 -30
- package/src/rsc/handler-context.ts +2 -2
- package/src/rsc/handler.ts +28 -69
- package/src/rsc/helpers.ts +91 -43
- package/src/rsc/index.ts +1 -1
- package/src/rsc/manifest-init.ts +28 -41
- package/src/rsc/origin-guard.ts +28 -10
- package/src/rsc/progressive-enhancement.ts +4 -0
- package/src/rsc/response-error.ts +79 -12
- package/src/rsc/response-route-handler.ts +57 -61
- package/src/rsc/rsc-rendering.ts +35 -51
- package/src/rsc/runtime-warnings.ts +9 -10
- package/src/rsc/server-action.ts +17 -37
- package/src/rsc/ssr-setup.ts +16 -0
- package/src/rsc/types.ts +8 -2
- package/src/runtime-env.ts +18 -0
- package/src/search-params.ts +4 -4
- package/src/segment-content-promise.ts +67 -0
- package/src/segment-loader-promise.ts +122 -0
- package/src/segment-system.tsx +132 -116
- package/src/serialize.ts +243 -0
- package/src/server/context.ts +175 -53
- package/src/server/cookie-store.ts +28 -4
- package/src/server/request-context.ts +67 -51
- package/src/ssr/index.tsx +5 -1
- package/src/static-handler.ts +25 -3
- package/src/testing/cache-status.ts +166 -0
- package/src/testing/collect-handle.ts +63 -0
- package/src/testing/dispatch.ts +581 -0
- package/src/testing/dom.entry.ts +22 -0
- package/src/testing/e2e/fixture.ts +188 -0
- package/src/testing/e2e/index.ts +149 -0
- package/src/testing/e2e/matchers.ts +51 -0
- package/src/testing/e2e/page-helpers.ts +272 -0
- package/src/testing/e2e/parity.ts +326 -0
- package/src/testing/e2e/server.ts +195 -0
- package/src/testing/flight-matchers.ts +110 -0
- package/src/testing/flight-normalize.ts +38 -0
- package/src/testing/flight-runtime.d.ts +57 -0
- package/src/testing/flight-tree.ts +682 -0
- package/src/testing/flight.entry.ts +51 -0
- package/src/testing/flight.ts +234 -0
- package/src/testing/generated-routes.ts +223 -0
- package/src/testing/index.ts +106 -0
- package/src/testing/internal/context.ts +304 -0
- package/src/testing/internal/flight-client-globals.ts +30 -0
- package/src/testing/internal/seed-vars.ts +42 -0
- package/src/testing/render-handler.ts +323 -0
- package/src/testing/render-route.tsx +590 -0
- package/src/testing/run-loader.ts +363 -0
- package/src/testing/run-middleware.ts +205 -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 +285 -0
- package/src/types/global-namespace.ts +39 -26
- package/src/types/handler-context.ts +68 -50
- package/src/types/index.ts +1 -0
- package/src/types/loader-types.ts +11 -9
- package/src/types/request-scope.ts +126 -0
- package/src/types/route-entry.ts +11 -0
- package/src/types/segments.ts +35 -2
- package/src/urls/include-helper.ts +34 -67
- package/src/urls/index.ts +1 -5
- package/src/urls/path-helper-types.ts +41 -7
- package/src/urls/path-helper.ts +17 -52
- package/src/urls/pattern-types.ts +36 -19
- package/src/urls/response-types.ts +22 -29
- package/src/urls/type-extraction.ts +58 -139
- package/src/urls/urls-function.ts +1 -5
- package/src/use-loader.tsx +413 -42
- package/src/vite/debug.ts +185 -0
- package/src/vite/discovery/bundle-postprocess.ts +6 -6
- package/src/vite/discovery/discover-routers.ts +106 -75
- package/src/vite/discovery/discovery-errors.ts +194 -0
- package/src/vite/discovery/gate-state.ts +171 -0
- package/src/vite/discovery/prerender-collection.ts +67 -26
- package/src/vite/discovery/route-types-writer.ts +40 -84
- package/src/vite/discovery/self-gen-tracking.ts +27 -1
- package/src/vite/discovery/state.ts +33 -0
- package/src/vite/discovery/virtual-module-codegen.ts +13 -23
- package/src/vite/index.ts +2 -0
- package/src/vite/plugin-types.ts +67 -0
- package/src/vite/plugins/cjs-to-esm.ts +8 -7
- package/src/vite/plugins/client-ref-dedup.ts +16 -0
- package/src/vite/plugins/client-ref-hashing.ts +28 -5
- 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 +214 -0
- package/src/vite/plugins/expose-action-id.ts +54 -30
- package/src/vite/plugins/expose-id-utils.ts +12 -8
- package/src/vite/plugins/expose-ids/export-analysis.ts +100 -20
- package/src/vite/plugins/expose-ids/handler-transform.ts +8 -61
- package/src/vite/plugins/expose-ids/loader-transform.ts +3 -5
- package/src/vite/plugins/expose-ids/router-transform.ts +20 -3
- package/src/vite/plugins/expose-internal-ids.ts +496 -486
- package/src/vite/plugins/performance-tracks.ts +29 -25
- package/src/vite/plugins/use-cache-transform.ts +65 -50
- package/src/vite/plugins/version-injector.ts +39 -23
- package/src/vite/plugins/version-plugin.ts +59 -2
- package/src/vite/plugins/virtual-entries.ts +2 -2
- package/src/vite/rango.ts +116 -29
- package/src/vite/router-discovery.ts +750 -100
- package/src/vite/utils/ast-handler-extract.ts +15 -15
- package/src/vite/utils/banner.ts +1 -1
- package/src/vite/utils/bundle-analysis.ts +4 -2
- package/src/vite/utils/client-chunks.ts +190 -0
- package/src/vite/utils/forward-user-plugins.ts +193 -0
- package/src/vite/utils/manifest-utils.ts +8 -59
- package/src/vite/utils/package-resolution.ts +41 -1
- package/src/vite/utils/prerender-utils.ts +21 -6
- package/src/vite/utils/shared-utils.ts +107 -26
- package/src/browser/action-response-classifier.ts +0 -99
|
@@ -1,10 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
4
|
-
AllUseItems,
|
|
5
|
-
RouteUseItem,
|
|
6
|
-
UrlPatternsBrand,
|
|
7
|
-
} from "../route-types.js";
|
|
1
|
+
import type { TrailingSlashMode } from "../types.js";
|
|
2
|
+
import type { AllUseItems, UrlPatternsBrand } from "../route-types.js";
|
|
8
3
|
import type { SearchSchema } from "../search-params.js";
|
|
9
4
|
import { RESPONSE_TYPE } from "./response-types.js";
|
|
10
5
|
import type { DefaultEnv } from "../types.js";
|
|
@@ -54,16 +49,6 @@ export interface PathOptions<
|
|
|
54
49
|
[RESPONSE_TYPE]?: string;
|
|
55
50
|
}
|
|
56
51
|
|
|
57
|
-
/**
|
|
58
|
-
* Internal representation of a URL pattern definition
|
|
59
|
-
*/
|
|
60
|
-
export interface PathDefinition {
|
|
61
|
-
pattern: string;
|
|
62
|
-
name?: string;
|
|
63
|
-
handler: ReactNode | Handler<any, any, any>;
|
|
64
|
-
use?: RouteUseItem[];
|
|
65
|
-
}
|
|
66
|
-
|
|
67
52
|
/**
|
|
68
53
|
* Result of urls() - contains the route definitions
|
|
69
54
|
*/
|
|
@@ -72,8 +57,6 @@ export interface UrlPatterns<
|
|
|
72
57
|
TRoutes extends Record<string, any> = Record<string, string>,
|
|
73
58
|
TResponses extends Record<string, unknown> = Record<string, unknown>,
|
|
74
59
|
> {
|
|
75
|
-
/** Internal: route definitions */
|
|
76
|
-
readonly definitions: PathDefinition[];
|
|
77
60
|
/** Internal: compiled handler function */
|
|
78
61
|
readonly handler: () => AllUseItems[];
|
|
79
62
|
/** Internal: trailing slash config per route name */
|
|
@@ -88,6 +71,40 @@ export interface UrlPatterns<
|
|
|
88
71
|
readonly _responses?: TResponses;
|
|
89
72
|
}
|
|
90
73
|
|
|
74
|
+
/**
|
|
75
|
+
* Extract the phantom env type carried by a UrlPatterns value.
|
|
76
|
+
*/
|
|
77
|
+
export type UrlPatternsEnv<T> =
|
|
78
|
+
T extends UrlPatterns<infer TEnv, any, any> ? TEnv : never;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Guards `routes()` env compatibility without over-constraining.
|
|
82
|
+
*
|
|
83
|
+
* - An env-agnostic block (its env is `unknown` — e.g. a shared urls() module,
|
|
84
|
+
* or an app that does not augment `Rango.Env`) attaches to any router.
|
|
85
|
+
* - A block carrying a concrete env is accepted only when the router env
|
|
86
|
+
* (`TRouterEnv`) satisfies it; resolves to `never` otherwise, so a
|
|
87
|
+
* `urls<{ DB: D1Database }>()` cannot be mounted on a `createRouter<{}>()`.
|
|
88
|
+
*
|
|
89
|
+
* Use as `patterns: T & EnvCompatible<T, TEnv>` so `T` still infers from the
|
|
90
|
+
* argument — a bare `EnvCompatible<T, TEnv>` parameter sits in a non-inferrable
|
|
91
|
+
* conditional position and would collapse `T` to its constraint.
|
|
92
|
+
*
|
|
93
|
+
* Known limitation: `TRouterEnv extends ...` distributes over a union router env,
|
|
94
|
+
* so a `urls<A>()` block is accepted on `createRouter<A | B>()` even though the
|
|
95
|
+
* `B` arm cannot supply `A`'s env. Suppressing distribution with
|
|
96
|
+
* `[TRouterEnv] extends [...]` would close that edge but breaks the common
|
|
97
|
+
* generic-`TEnv` call sites (a deferred type parameter can't resolve the tuple
|
|
98
|
+
* conditional, so the intersection stops reducing to `T`). A router has one env,
|
|
99
|
+
* so a union env is not a supported pattern; the distributive form is kept.
|
|
100
|
+
*/
|
|
101
|
+
export type EnvCompatible<TPatterns, TRouterEnv> =
|
|
102
|
+
unknown extends UrlPatternsEnv<TPatterns>
|
|
103
|
+
? TPatterns
|
|
104
|
+
: TRouterEnv extends UrlPatternsEnv<TPatterns>
|
|
105
|
+
? TPatterns
|
|
106
|
+
: never;
|
|
107
|
+
|
|
91
108
|
/**
|
|
92
109
|
* Options for include()
|
|
93
110
|
*/
|
|
@@ -5,6 +5,7 @@ import type {
|
|
|
5
5
|
DefaultVars,
|
|
6
6
|
} from "../types/global-namespace.js";
|
|
7
7
|
import type { UseItems, ResponseRouteUseItem } from "../route-types.js";
|
|
8
|
+
import type { RequestScope } from "../types/request-scope.js";
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* Reverse function for response handler contexts.
|
|
@@ -31,21 +32,32 @@ type ResponseReverseFunction = [DefaultReverseRouteMap] extends [
|
|
|
31
32
|
* Symbol marking a route as a response route (non-RSC).
|
|
32
33
|
* Stored on PathOptions and UrlPatterns to signal the trie to short-circuit.
|
|
33
34
|
*/
|
|
34
|
-
export const RESPONSE_TYPE: unique symbol = Symbol.for(
|
|
35
|
-
"rangojs.responseType",
|
|
36
|
-
) as any;
|
|
35
|
+
export const RESPONSE_TYPE: unique symbol = Symbol.for("rangojs.responseType");
|
|
37
36
|
|
|
38
37
|
/**
|
|
39
|
-
*
|
|
40
|
-
*
|
|
38
|
+
* Shared shape of a response-route handler: a function returning TReturn (or a
|
|
39
|
+
* promise of it), plus an optional composable `use` thunk merged at mount time.
|
|
41
40
|
*/
|
|
42
|
-
|
|
41
|
+
type ResponseHandlerOf<
|
|
42
|
+
TReturn,
|
|
43
|
+
TParams = Record<string, string>,
|
|
44
|
+
TEnv = any,
|
|
45
|
+
> = ((
|
|
43
46
|
ctx: ResponseHandlerContext<TParams, TEnv>,
|
|
44
|
-
) =>
|
|
47
|
+
) => TReturn | Promise<TReturn>) & {
|
|
45
48
|
/** Composable default DSL items merged when the handler is mounted. */
|
|
46
49
|
use?: () => UseItems<ResponseRouteUseItem>;
|
|
47
50
|
};
|
|
48
51
|
|
|
52
|
+
/**
|
|
53
|
+
* Handler that must return Response (not ReactNode).
|
|
54
|
+
* Used by path.image(), path.stream(), path.any() (binary/streaming data).
|
|
55
|
+
*/
|
|
56
|
+
export type ResponseHandler<
|
|
57
|
+
TParams = Record<string, string>,
|
|
58
|
+
TEnv = any,
|
|
59
|
+
> = ResponseHandlerOf<Response, TParams, TEnv>;
|
|
60
|
+
|
|
49
61
|
/**
|
|
50
62
|
* JSON-serializable value type for auto-wrap support.
|
|
51
63
|
*/
|
|
@@ -64,12 +76,7 @@ export type JsonValue =
|
|
|
64
76
|
export type JsonResponseHandler<
|
|
65
77
|
TParams = Record<string, string>,
|
|
66
78
|
TEnv = any,
|
|
67
|
-
> =
|
|
68
|
-
ctx: ResponseHandlerContext<TParams, TEnv>,
|
|
69
|
-
) => JsonValue | Response | Promise<JsonValue | Response>) & {
|
|
70
|
-
/** Composable default DSL items merged when the handler is mounted. */
|
|
71
|
-
use?: () => UseItems<ResponseRouteUseItem>;
|
|
72
|
-
};
|
|
79
|
+
> = ResponseHandlerOf<JsonValue | Response, TParams, TEnv>;
|
|
73
80
|
|
|
74
81
|
/**
|
|
75
82
|
* Handler for text-based response routes (text, html, xml).
|
|
@@ -78,12 +85,7 @@ export type JsonResponseHandler<
|
|
|
78
85
|
export type TextResponseHandler<
|
|
79
86
|
TParams = Record<string, string>,
|
|
80
87
|
TEnv = any,
|
|
81
|
-
> =
|
|
82
|
-
ctx: ResponseHandlerContext<TParams, TEnv>,
|
|
83
|
-
) => string | Response | Promise<string | Response>) & {
|
|
84
|
-
/** Composable default DSL items merged when the handler is mounted. */
|
|
85
|
-
use?: () => UseItems<ResponseRouteUseItem>;
|
|
86
|
-
};
|
|
88
|
+
> = ResponseHandlerOf<string | Response, TParams, TEnv>;
|
|
87
89
|
|
|
88
90
|
/**
|
|
89
91
|
* Lighter handler context for response routes.
|
|
@@ -93,19 +95,10 @@ export type TextResponseHandler<
|
|
|
93
95
|
export interface ResponseHandlerContext<
|
|
94
96
|
TParams = Record<string, string>,
|
|
95
97
|
TEnv = any,
|
|
96
|
-
> {
|
|
97
|
-
request: Request;
|
|
98
|
+
> extends RequestScope<TEnv> {
|
|
98
99
|
params: TParams;
|
|
99
100
|
/** @internal Phantom property for params type invariance. Prevents mounting handlers on wrong routes. */
|
|
100
101
|
readonly _paramCheck?: (params: TParams) => TParams;
|
|
101
|
-
/** Platform bindings (DB, KV, secrets, etc.). */
|
|
102
|
-
env: TEnv;
|
|
103
|
-
/** Query parameters from the URL (system params like `_rsc*` are filtered). */
|
|
104
|
-
searchParams: URLSearchParams;
|
|
105
|
-
/** The full URL object (with system params filtered). */
|
|
106
|
-
url: URL;
|
|
107
|
-
/** The pathname portion of the request URL. */
|
|
108
|
-
pathname: string;
|
|
109
102
|
reverse: ResponseReverseFunction;
|
|
110
103
|
/** Read a variable set by middleware via ctx.set(key, value) or ctx.set(ContextVar, value). */
|
|
111
104
|
get: {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { JsonSerialize } from "../serialize.js";
|
|
2
2
|
import type {
|
|
3
3
|
TypedRouteItem,
|
|
4
4
|
TypedIncludeItem,
|
|
@@ -6,11 +6,7 @@ import type {
|
|
|
6
6
|
TypedCacheItem,
|
|
7
7
|
TypedTransitionItem,
|
|
8
8
|
} from "../route-types.js";
|
|
9
|
-
import type {
|
|
10
|
-
LocalOnlyInclude,
|
|
11
|
-
UnnamedRoute,
|
|
12
|
-
UrlPatterns,
|
|
13
|
-
} from "./pattern-types.js";
|
|
9
|
+
import type { LocalOnlyInclude, UnnamedRoute } from "./pattern-types.js";
|
|
14
10
|
|
|
15
11
|
// ============================================================================
|
|
16
12
|
// Route Type Extraction Utilities
|
|
@@ -67,62 +63,6 @@ type PrefixPatterns<
|
|
|
67
63
|
: TRoutes[K];
|
|
68
64
|
};
|
|
69
65
|
|
|
70
|
-
/**
|
|
71
|
-
* Depth counter for limiting recursion (max 40 levels)
|
|
72
|
-
* Supports up to 40 sibling items at any level of a urls() call
|
|
73
|
-
* Note: Higher values hit TypeScript's internal recursion limits
|
|
74
|
-
*/
|
|
75
|
-
type Depth = [
|
|
76
|
-
never,
|
|
77
|
-
0,
|
|
78
|
-
1,
|
|
79
|
-
2,
|
|
80
|
-
3,
|
|
81
|
-
4,
|
|
82
|
-
5,
|
|
83
|
-
6,
|
|
84
|
-
7,
|
|
85
|
-
8,
|
|
86
|
-
9,
|
|
87
|
-
10,
|
|
88
|
-
11,
|
|
89
|
-
12,
|
|
90
|
-
13,
|
|
91
|
-
14,
|
|
92
|
-
15,
|
|
93
|
-
16,
|
|
94
|
-
17,
|
|
95
|
-
18,
|
|
96
|
-
19,
|
|
97
|
-
20,
|
|
98
|
-
21,
|
|
99
|
-
22,
|
|
100
|
-
23,
|
|
101
|
-
24,
|
|
102
|
-
25,
|
|
103
|
-
26,
|
|
104
|
-
27,
|
|
105
|
-
28,
|
|
106
|
-
29,
|
|
107
|
-
30,
|
|
108
|
-
31,
|
|
109
|
-
32,
|
|
110
|
-
33,
|
|
111
|
-
34,
|
|
112
|
-
35,
|
|
113
|
-
36,
|
|
114
|
-
37,
|
|
115
|
-
38,
|
|
116
|
-
39,
|
|
117
|
-
];
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Force TypeScript to eagerly evaluate a type.
|
|
121
|
-
* This helps with interface extension by creating a "concrete" object type.
|
|
122
|
-
*/
|
|
123
|
-
type Simplify<T> =
|
|
124
|
-
T extends Record<string, string> ? { [K in keyof T]: T[K] } : T;
|
|
125
|
-
|
|
126
66
|
/**
|
|
127
67
|
* Convert a union type to an intersection type.
|
|
128
68
|
* Used to combine route maps from multiple siblings without recursive tuple processing.
|
|
@@ -135,13 +75,11 @@ type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
|
|
|
135
75
|
|
|
136
76
|
/**
|
|
137
77
|
* Extract routes from a single item (path, include, layout, cache with children)
|
|
138
|
-
* D is the current depth level for nested layouts/caches
|
|
139
78
|
*/
|
|
140
|
-
type ExtractRoutesFromItem<T
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
T extends TypedRouteItem<infer TName, infer TPattern, any, infer TSearch>
|
|
79
|
+
type ExtractRoutesFromItem<T> =
|
|
80
|
+
// TypedRouteItem: extract name -> pattern (exclude unnamed routes)
|
|
81
|
+
// When search schema is non-empty, value becomes { path, search } object
|
|
82
|
+
T extends TypedRouteItem<infer TName, infer TPattern, any, infer TSearch>
|
|
145
83
|
? TName extends string
|
|
146
84
|
? TName extends UnnamedRoute
|
|
147
85
|
? {} // Exclude unnamed routes from type map
|
|
@@ -185,14 +123,10 @@ type ExtractRoutesFromItem<T, D extends number = 40> = [D] extends [never]
|
|
|
185
123
|
* Extract routes from an array of items using mapped types.
|
|
186
124
|
* Uses UnionToIntersection to combine routes without recursive tuple processing,
|
|
187
125
|
* removing the sibling limit that was caused by TypeScript recursion limits.
|
|
188
|
-
* D is passed to ExtractRoutesFromItem for nested depth tracking.
|
|
189
126
|
*/
|
|
190
|
-
type ExtractRoutesFromItems<
|
|
191
|
-
T extends readonly any[],
|
|
192
|
-
D extends number = 40,
|
|
193
|
-
> = T extends readonly any[]
|
|
127
|
+
type ExtractRoutesFromItems<T extends readonly any[]> = T extends readonly any[]
|
|
194
128
|
? UnionToIntersection<
|
|
195
|
-
{ [K in keyof T]: ExtractRoutesFromItem<T[K]
|
|
129
|
+
{ [K in keyof T]: ExtractRoutesFromItem<T[K]> }[number]
|
|
196
130
|
> extends infer R
|
|
197
131
|
? R extends Record<string, any>
|
|
198
132
|
? R
|
|
@@ -203,12 +137,8 @@ type ExtractRoutesFromItems<
|
|
|
203
137
|
/**
|
|
204
138
|
* Main utility: extract route map from urls() callback return type
|
|
205
139
|
* Uses mapped types for sibling processing (no sibling limit).
|
|
206
|
-
* Uses Simplify to force eager evaluation for interface extension compatibility.
|
|
207
140
|
*/
|
|
208
|
-
export type ExtractRoutes<T extends readonly any[]> = ExtractRoutesFromItems<
|
|
209
|
-
T,
|
|
210
|
-
40
|
|
211
|
-
>;
|
|
141
|
+
export type ExtractRoutes<T extends readonly any[]> = ExtractRoutesFromItems<T>;
|
|
212
142
|
|
|
213
143
|
// ============================================================================
|
|
214
144
|
// Response Type Extraction Utilities
|
|
@@ -236,9 +166,8 @@ type PrefixKeys<
|
|
|
236
166
|
* Extract response data types from a single item.
|
|
237
167
|
* Parallel to ExtractRoutesFromItem but extracts name -> TData mapping.
|
|
238
168
|
*/
|
|
239
|
-
type ExtractResponsesFromItem<T
|
|
240
|
-
|
|
241
|
-
: T extends TypedRouteItem<infer TName, any, infer TData>
|
|
169
|
+
type ExtractResponsesFromItem<T> =
|
|
170
|
+
T extends TypedRouteItem<infer TName, any, infer TData>
|
|
242
171
|
? TName extends string
|
|
243
172
|
? TName extends UnnamedRoute
|
|
244
173
|
? {}
|
|
@@ -272,86 +201,72 @@ type ExtractResponsesFromItem<T, D extends number = 40> = [D] extends [never]
|
|
|
272
201
|
* Extract responses from an array of items using mapped types.
|
|
273
202
|
* Parallel to ExtractRoutesFromItems.
|
|
274
203
|
*/
|
|
275
|
-
type ExtractResponsesFromItems<
|
|
276
|
-
T extends readonly any[]
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
? R
|
|
204
|
+
type ExtractResponsesFromItems<T extends readonly any[]> =
|
|
205
|
+
T extends readonly any[]
|
|
206
|
+
? UnionToIntersection<
|
|
207
|
+
{ [K in keyof T]: ExtractResponsesFromItem<T[K]> }[number]
|
|
208
|
+
> extends infer R
|
|
209
|
+
? R extends Record<string, unknown>
|
|
210
|
+
? R
|
|
211
|
+
: {}
|
|
284
212
|
: {}
|
|
285
|
-
: {}
|
|
286
|
-
: {};
|
|
213
|
+
: {};
|
|
287
214
|
|
|
288
215
|
/**
|
|
289
216
|
* Main utility: extract response data type map from urls() callback return type.
|
|
290
217
|
* Parallel to ExtractRoutes.
|
|
291
218
|
*/
|
|
292
219
|
export type ExtractResponses<T extends readonly any[]> =
|
|
293
|
-
ExtractResponsesFromItems<T
|
|
220
|
+
ExtractResponsesFromItems<T>;
|
|
294
221
|
|
|
295
222
|
// ============================================================================
|
|
296
|
-
//
|
|
223
|
+
// Response Error (RFC 9457 problem+json) Type
|
|
297
224
|
// ============================================================================
|
|
298
225
|
|
|
299
226
|
/**
|
|
300
|
-
*
|
|
301
|
-
*
|
|
302
|
-
|
|
303
|
-
export type ExtractRouteNames<T extends UrlPatterns<any>> =
|
|
304
|
-
T extends UrlPatterns<infer _TEnv>
|
|
305
|
-
? string // For now, will be refined with full implementation
|
|
306
|
-
: never;
|
|
307
|
-
|
|
308
|
-
/**
|
|
309
|
-
* Extract params for a specific route name
|
|
310
|
-
*/
|
|
311
|
-
export type ExtractPathParams<
|
|
312
|
-
T extends UrlPatterns<any>,
|
|
313
|
-
K extends string,
|
|
314
|
-
> = ExtractParams<string>; // Will be refined with pattern tracking
|
|
315
|
-
|
|
316
|
-
// ============================================================================
|
|
317
|
-
// Response Envelope Types
|
|
318
|
-
// ============================================================================
|
|
319
|
-
|
|
320
|
-
/**
|
|
321
|
-
* Error shape returned in the `{ error }` side of a JSON response envelope.
|
|
322
|
-
*/
|
|
323
|
-
export interface ResponseError {
|
|
324
|
-
message: string;
|
|
325
|
-
code?: string;
|
|
326
|
-
type?: string;
|
|
327
|
-
stack?: string;
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
/**
|
|
331
|
-
* Discriminated union envelope for JSON response routes.
|
|
332
|
-
* Consumers check `result.error` to discriminate between success and failure.
|
|
227
|
+
* RFC 9457 (problem+json) error body returned by JSON response routes on a
|
|
228
|
+
* non-2xx status. Sent verbatim as the response body (not wrapped) with
|
|
229
|
+
* content-type `application/problem+json`.
|
|
333
230
|
*
|
|
334
231
|
* @example
|
|
335
232
|
* ```typescript
|
|
336
|
-
* const
|
|
337
|
-
* if (
|
|
338
|
-
*
|
|
233
|
+
* const res = await fetch(url);
|
|
234
|
+
* if (!res.ok) {
|
|
235
|
+
* const problem: ProblemDetails = await res.json();
|
|
236
|
+
* console.log(problem.code, problem.detail); // "NOT_FOUND", "Product not found"
|
|
339
237
|
* return;
|
|
340
238
|
* }
|
|
341
|
-
*
|
|
239
|
+
* const product = await res.json(); // bare value, no envelope
|
|
342
240
|
* ```
|
|
343
241
|
*/
|
|
344
|
-
export
|
|
345
|
-
|
|
346
|
-
|
|
242
|
+
export interface ProblemDetails {
|
|
243
|
+
/**
|
|
244
|
+
* URI reference identifying the problem type. Omitted in this phase (per RFC
|
|
245
|
+
* 9457 an absent `type` is treated as `"about:blank"` — no semantics beyond
|
|
246
|
+
* the HTTP status); per-route problem-type URIs arrive with the
|
|
247
|
+
* declared-errors map later.
|
|
248
|
+
*/
|
|
249
|
+
type?: string;
|
|
250
|
+
/** Short, human-readable summary (the HTTP status reason phrase). */
|
|
251
|
+
title: string;
|
|
252
|
+
/** The HTTP status code. */
|
|
253
|
+
status: number;
|
|
254
|
+
/** Human-readable explanation specific to this occurrence (the error message). */
|
|
255
|
+
detail: string;
|
|
256
|
+
/** Stable machine-readable error code (`RouterError.code`, else `"INTERNAL"`). */
|
|
257
|
+
code: string;
|
|
258
|
+
/** Stack trace, included in development only. */
|
|
259
|
+
stack?: string;
|
|
260
|
+
}
|
|
347
261
|
|
|
348
262
|
// ============================================================================
|
|
349
263
|
// Response Type Consumer Utilities
|
|
350
264
|
// ============================================================================
|
|
351
265
|
|
|
352
266
|
/**
|
|
353
|
-
* Extract the response
|
|
354
|
-
*
|
|
267
|
+
* Extract the JSON response payload type for a named route from a UrlPatterns
|
|
268
|
+
* instance. JSON response routes send the handler's return value verbatim
|
|
269
|
+
* (bare), so this resolves to the wire value a consumer receives — no envelope.
|
|
355
270
|
*
|
|
356
271
|
* @example
|
|
357
272
|
* ```typescript
|
|
@@ -360,13 +275,17 @@ export type ResponseEnvelope<T> =
|
|
|
360
275
|
* ]);
|
|
361
276
|
*
|
|
362
277
|
* type HealthData = RouteResponse<typeof apiPatterns, "health">;
|
|
363
|
-
* //
|
|
278
|
+
* // { status: string; timestamp: number }
|
|
364
279
|
* ```
|
|
280
|
+
*
|
|
281
|
+
* The payload is the JSON wire shape (via `Rango.JsonSerialize`), matching
|
|
282
|
+
* `Rango.PathResponse` and what `fetch().then(r => r.json())` actually yields —
|
|
283
|
+
* e.g. a `Date` field resolves as `string`.
|
|
365
284
|
*/
|
|
366
285
|
export type RouteResponse<TPatterns, TName extends string> = TPatterns extends {
|
|
367
286
|
readonly _responses?: infer R;
|
|
368
287
|
}
|
|
369
288
|
? TName extends keyof R
|
|
370
|
-
?
|
|
289
|
+
? JsonSerialize<Exclude<R[TName], Response>>
|
|
371
290
|
: never
|
|
372
291
|
: never;
|
|
@@ -3,7 +3,7 @@ import type { AllUseItems } from "../route-types.js";
|
|
|
3
3
|
import { getContext } from "../server/context";
|
|
4
4
|
import { invariant } from "../errors";
|
|
5
5
|
import { createRouteHelpers } from "../route-definition.js";
|
|
6
|
-
import type {
|
|
6
|
+
import type { UrlPatterns } from "./pattern-types.js";
|
|
7
7
|
import type { PathHelpers } from "./path-helper-types.js";
|
|
8
8
|
import type { ExtractRoutes, ExtractResponses } from "./type-extraction.js";
|
|
9
9
|
import { createPathHelper, attachPathResponseTags } from "./path-helper.js";
|
|
@@ -34,9 +34,6 @@ export function urls<
|
|
|
34
34
|
>(
|
|
35
35
|
builder: (helpers: PathHelpers<TEnv>) => TItems,
|
|
36
36
|
): UrlPatterns<TEnv, ExtractRoutes<TItems>, ExtractResponses<TItems>> {
|
|
37
|
-
// Collect path definitions during build
|
|
38
|
-
const definitions: PathDefinition[] = [];
|
|
39
|
-
|
|
40
37
|
// Create the handler function that will be called by the router
|
|
41
38
|
const handler = () => {
|
|
42
39
|
invariant(
|
|
@@ -82,7 +79,6 @@ export function urls<
|
|
|
82
79
|
// trailingSlash config is populated when handler() runs
|
|
83
80
|
// We expose it via a getter that reads from the context after handler execution
|
|
84
81
|
return {
|
|
85
|
-
definitions,
|
|
86
82
|
handler,
|
|
87
83
|
get trailingSlash() {
|
|
88
84
|
// Get the trailingSlash map from the current context
|