@rangojs/router 0.0.0-experimental.110 → 0.0.0-experimental.111

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.
@@ -11,122 +11,40 @@ import {
11
11
  when,
12
12
  errorBoundary,
13
13
  notFoundBoundary,
14
- loaderFn,
15
- loadingFn,
16
- transitionFn,
17
- routeFn,
14
+ route,
15
+ loader,
16
+ loading,
17
+ transition,
18
18
  } from "./dsl-helpers.js";
19
19
  import RootLayout from "../server/root-layout";
20
20
  import { invariant } from "../errors";
21
21
 
22
- /*
23
- * Create revalidate helper
24
- */
25
- const createRevalidateHelper = <TEnv>(): RouteHelpers<
26
- any,
27
- TEnv
28
- >["revalidate"] => {
29
- return revalidate as RouteHelpers<any, TEnv>["revalidate"];
30
- };
31
-
32
- /**
33
- * Create errorBoundary helper
34
- */
35
- const createErrorBoundaryHelper = <TEnv>(): RouteHelpers<
36
- any,
37
- TEnv
38
- >["errorBoundary"] => {
39
- return errorBoundary as RouteHelpers<any, TEnv>["errorBoundary"];
40
- };
41
-
42
- /**
43
- * Create notFoundBoundary helper
44
- */
45
- const createNotFoundBoundaryHelper = <TEnv>(): RouteHelpers<
46
- any,
47
- TEnv
48
- >["notFoundBoundary"] => {
49
- return notFoundBoundary as RouteHelpers<any, TEnv>["notFoundBoundary"];
50
- };
51
-
52
22
  /**
53
- * Create middleware helper
23
+ * Assemble the RouteHelpers object. The helpers are the DSL functions
24
+ * themselves; the single cast erases the phantom generics (and the extra
25
+ * `route` key) that the per-router RouteHelpers<T, TEnv> type carries but the
26
+ * runtime functions do not.
54
27
  */
55
- const createMiddlewareHelper = <TEnv>(): RouteHelpers<
56
- any,
28
+ function buildRouteHelpers<T extends RouteDefinition, TEnv>(): RouteHelpers<
29
+ T,
57
30
  TEnv
58
- >["middleware"] => {
59
- return middleware as RouteHelpers<any, TEnv>["middleware"];
60
- };
61
-
62
- /**
63
- * Create parallel helper
64
- */
65
- const createParallelHelper = <TEnv>(): RouteHelpers<any, TEnv>["parallel"] => {
66
- return parallel as RouteHelpers<any, TEnv>["parallel"];
67
- };
68
-
69
- /**
70
- * Create intercept helper
71
- */
72
- const createInterceptHelper = <
73
- const T extends RouteDefinition,
74
- TEnv,
75
- >(): RouteHelpers<T, TEnv>["intercept"] => {
76
- return intercept as RouteHelpers<T, TEnv>["intercept"];
77
- };
78
-
79
- /**
80
- * Create loader helper
81
- */
82
- const createLoaderHelper = <TEnv>(): RouteHelpers<any, TEnv>["loader"] => {
83
- return loaderFn as RouteHelpers<any, TEnv>["loader"];
84
- };
85
-
86
- /**
87
- * Create loading helper
88
- */
89
- const createLoadingHelper = (): RouteHelpers<any, any>["loading"] => {
90
- return loadingFn;
91
- };
92
-
93
- /**
94
- * Create route helper
95
- */
96
- const createRouteHelper = <
97
- const T extends RouteDefinition,
98
- TEnv,
99
- >(): RouteHelpers<T, TEnv>["route"] => {
100
- return routeFn as unknown as RouteHelpers<T, TEnv>["route"];
101
- };
102
-
103
- /**
104
- * Create layout helper
105
- */
106
- const createLayoutHelper = <TEnv>(): RouteHelpers<any, TEnv>["layout"] => {
107
- return layout as RouteHelpers<any, TEnv>["layout"];
108
- };
109
-
110
- /**
111
- * Create when helper for intercept conditions
112
- */
113
- const createWhenHelper = (): RouteHelpers<any, any>["when"] => {
114
- return when;
115
- };
116
-
117
- /**
118
- * Create cache helper for cache configuration
119
- */
120
- const createCacheHelper = (): RouteHelpers<any, any>["cache"] => {
121
- return cache;
122
- };
123
-
124
- /**
125
- * Create transition helper
126
- */
127
- const createTransitionHelper = (): RouteHelpers<any, any>["transition"] => {
128
- return transitionFn as RouteHelpers<any, any>["transition"];
129
- };
31
+ > {
32
+ return {
33
+ route,
34
+ layout,
35
+ parallel,
36
+ intercept,
37
+ middleware,
38
+ revalidate,
39
+ loader,
40
+ loading,
41
+ errorBoundary,
42
+ notFoundBoundary,
43
+ when,
44
+ cache,
45
+ transition,
46
+ } as unknown as RouteHelpers<T, TEnv>;
47
+ }
130
48
 
131
49
  /**
132
50
  * Branded type for route handlers that carries the route type info.
@@ -152,21 +70,7 @@ export function map<const T extends RouteDefinition, TEnv = DefaultEnv>(
152
70
  "map() expects a builder function as its argument",
153
71
  );
154
72
  // Create helpers
155
- const helpers: RouteHelpers<T, TEnv> = {
156
- route: createRouteHelper<T, TEnv>(),
157
- layout: createLayoutHelper<TEnv>(),
158
- parallel: createParallelHelper<TEnv>(),
159
- intercept: createInterceptHelper<T, TEnv>(),
160
- middleware: createMiddlewareHelper<TEnv>(),
161
- revalidate: createRevalidateHelper<TEnv>(),
162
- loader: createLoaderHelper<TEnv>(),
163
- loading: createLoadingHelper(),
164
- errorBoundary: createErrorBoundaryHelper<TEnv>(),
165
- notFoundBoundary: createNotFoundBoundaryHelper<TEnv>(),
166
- when: createWhenHelper(),
167
- cache: createCacheHelper(),
168
- transition: createTransitionHelper(),
169
- };
73
+ const helpers = buildRouteHelpers<T, TEnv>();
170
74
 
171
75
  return [layout(RootLayout, () => builder(helpers))].flat(3);
172
76
  };
@@ -182,19 +86,5 @@ export function createRouteHelpers<
182
86
  T extends RouteDefinition,
183
87
  TEnv,
184
88
  >(): RouteHelpers<T, TEnv> {
185
- return {
186
- route: createRouteHelper<T, TEnv>(),
187
- layout: createLayoutHelper<TEnv>(),
188
- parallel: createParallelHelper<TEnv>(),
189
- intercept: createInterceptHelper<T, TEnv>(),
190
- middleware: createMiddlewareHelper<TEnv>(),
191
- revalidate: createRevalidateHelper<TEnv>(),
192
- loader: createLoaderHelper<TEnv>(),
193
- loading: createLoadingHelper(),
194
- errorBoundary: createErrorBoundaryHelper<TEnv>(),
195
- notFoundBoundary: createNotFoundBoundaryHelper<TEnv>(),
196
- when: createWhenHelper(),
197
- cache: createCacheHelper(),
198
- transition: createTransitionHelper(),
199
- };
89
+ return buildRouteHelpers<T, TEnv>();
200
90
  }
@@ -0,0 +1,32 @@
1
+ import type { AllUseItems, WhenItem } from "../route-types.js";
2
+
3
+ /**
4
+ * The set of valid use-item `type` discriminants — the single runtime source of
5
+ * truth for "is this a well-formed use item?" shape validation.
6
+ *
7
+ * Declared via a `Record<...>` so that adding a member to the union without
8
+ * updating this map is a compile error. `when` is included because when() items
9
+ * are valid inside intercept() even though WhenItem is not part of AllUseItems
10
+ * (it lives only in InterceptUseItem). This is shape validation only; per-mount-
11
+ * site rules remain the narrower hand-written tables in resolve-handler-use.ts.
12
+ */
13
+ const USE_ITEM_TYPES: Record<AllUseItems["type"] | WhenItem["type"], true> = {
14
+ layout: true,
15
+ route: true,
16
+ middleware: true,
17
+ revalidate: true,
18
+ parallel: true,
19
+ intercept: true,
20
+ loader: true,
21
+ loading: true,
22
+ errorBoundary: true,
23
+ notFoundBoundary: true,
24
+ when: true,
25
+ cache: true,
26
+ transition: true,
27
+ include: true,
28
+ };
29
+
30
+ export const ALL_USE_ITEM_TYPES: ReadonlySet<string> = new Set(
31
+ Object.keys(USE_ITEM_TYPES),
32
+ );
@@ -5,47 +5,43 @@
5
5
  */
6
6
 
7
7
  /**
8
- * Branded return types for route helpers
8
+ * Brand for UrlPatterns nominal typing (see pattern-types.ts). The route-item
9
+ * types below are discriminated by their `type` literal, so they carry no brand.
9
10
  */
10
- export declare const LayoutBrand: unique symbol;
11
- export declare const RouteBrand: unique symbol;
12
- export declare const ParallelBrand: unique symbol;
13
- export declare const InterceptBrand: unique symbol;
14
- export declare const MiddlewareBrand: unique symbol;
15
- export declare const RevalidateBrand: unique symbol;
16
- export declare const LoaderBrand: unique symbol;
17
- export declare const LoadingBrand: unique symbol;
18
- export declare const ErrorBoundaryBrand: unique symbol;
19
- export declare const NotFoundBoundaryBrand: unique symbol;
20
- export declare const WhenBrand: unique symbol;
21
- export declare const CacheBrand: unique symbol;
22
- export declare const TransitionBrand: unique symbol;
23
- export declare const IncludeBrand: unique symbol;
24
11
  export declare const UrlPatternsBrand: unique symbol;
25
12
 
26
13
  export type LayoutItem = {
27
14
  name: string;
28
15
  type: "layout";
29
16
  uses?: AllUseItems[];
30
- [LayoutBrand]: void;
31
17
  };
32
18
 
33
19
  /**
34
- * Typed layout item that carries child routes as phantom type
35
- * Used for type inference in urls() API
20
+ * Phantom inference fields attached to wrapper items (layout/cache/transition)
21
+ * so the urls() type extractor can read their child routes/responses. The fields
22
+ * never exist at runtime.
36
23
  */
37
- export type TypedLayoutItem<
24
+ type WithChildren<
25
+ TBase,
38
26
  TChildRoutes extends Record<string, any> = Record<string, string>,
39
27
  TChildResponses extends Record<string, unknown> = Record<string, unknown>,
40
- > = LayoutItem & {
28
+ > = TBase & {
41
29
  readonly __childRoutes?: TChildRoutes;
42
30
  readonly __childResponses?: TChildResponses;
43
31
  };
32
+
33
+ /**
34
+ * Typed layout item that carries child routes as phantom type
35
+ * Used for type inference in urls() API
36
+ */
37
+ export type TypedLayoutItem<
38
+ TChildRoutes extends Record<string, any> = Record<string, string>,
39
+ TChildResponses extends Record<string, unknown> = Record<string, unknown>,
40
+ > = WithChildren<LayoutItem, TChildRoutes, TChildResponses>;
44
41
  export type RouteItem = {
45
42
  name: string;
46
43
  type: "route";
47
44
  uses?: AllUseItems[];
48
- [RouteBrand]: void;
49
45
  };
50
46
 
51
47
  /**
@@ -67,64 +63,53 @@ export type ParallelItem = {
67
63
  name: string;
68
64
  type: "parallel";
69
65
  uses?: ParallelUseItem[];
70
- [ParallelBrand]: void;
71
66
  };
72
67
  export type InterceptItem = {
73
68
  name: string;
74
69
  type: "intercept";
75
70
  uses?: InterceptUseItem[];
76
- [InterceptBrand]: void;
77
71
  };
78
72
  export type LoaderItem = {
79
73
  name: string;
80
74
  type: "loader";
81
75
  uses?: LoaderUseItem[];
82
- [LoaderBrand]: void;
83
76
  };
84
77
  export type MiddlewareItem = {
85
78
  name: string;
86
79
  type: "middleware";
87
80
  uses?: AllUseItems[];
88
- [MiddlewareBrand]: void;
89
81
  };
90
82
  export type RevalidateItem = {
91
83
  name: string;
92
84
  type: "revalidate";
93
85
  uses?: AllUseItems[];
94
- [RevalidateBrand]: void;
95
86
  };
96
87
  export type LoadingItem = {
97
88
  name: string;
98
89
  type: "loading";
99
- [LoadingBrand]: void;
100
90
  };
101
91
  export type ErrorBoundaryItem = {
102
92
  name: string;
103
93
  type: "errorBoundary";
104
94
  uses?: AllUseItems[];
105
- [ErrorBoundaryBrand]: void;
106
95
  };
107
96
  export type NotFoundBoundaryItem = {
108
97
  name: string;
109
98
  type: "notFoundBoundary";
110
99
  uses?: AllUseItems[];
111
- [NotFoundBoundaryBrand]: void;
112
100
  };
113
101
  export type WhenItem = {
114
102
  name: string;
115
103
  type: "when";
116
- [WhenBrand]: void;
117
104
  };
118
105
  export type CacheItem = {
119
106
  name: string;
120
107
  type: "cache";
121
108
  uses?: AllUseItems[];
122
- [CacheBrand]: void;
123
109
  };
124
110
  export type TransitionItem = {
125
111
  name: string;
126
112
  type: "transition";
127
- [TransitionBrand]: void;
128
113
  };
129
114
 
130
115
  /**
@@ -134,10 +119,7 @@ export type TransitionItem = {
134
119
  export type TypedTransitionItem<
135
120
  TChildRoutes extends Record<string, any> = Record<string, string>,
136
121
  TChildResponses extends Record<string, unknown> = Record<string, unknown>,
137
- > = TransitionItem & {
138
- readonly __childRoutes?: TChildRoutes;
139
- readonly __childResponses?: TChildResponses;
140
- };
122
+ > = WithChildren<TransitionItem, TChildRoutes, TChildResponses>;
141
123
 
142
124
  /**
143
125
  * Typed cache item that carries child routes as phantom type
@@ -146,10 +128,7 @@ export type TypedTransitionItem<
146
128
  export type TypedCacheItem<
147
129
  TChildRoutes extends Record<string, any> = Record<string, string>,
148
130
  TChildResponses extends Record<string, unknown> = Record<string, unknown>,
149
- > = CacheItem & {
150
- readonly __childRoutes?: TChildRoutes;
151
- readonly __childResponses?: TChildResponses;
152
- };
131
+ > = WithChildren<CacheItem, TChildRoutes, TChildResponses>;
153
132
 
154
133
  /**
155
134
  * Include item for URL pattern composition (used by urls() API)
@@ -184,7 +163,6 @@ export type IncludeItem = {
184
163
  */
185
164
  includeScope?: string;
186
165
  };
187
- [IncludeBrand]: void;
188
166
  };
189
167
 
190
168
  /**
@@ -10,7 +10,7 @@ import type {
10
10
  ShouldRevalidateFn,
11
11
  TransitionConfig,
12
12
  } from "../types";
13
- import { invariant } from "../errors";
13
+ import { invariant, DslContextError } from "../errors";
14
14
  import type { DefaultRouteName } from "../types/global-namespace.js";
15
15
 
16
16
  // ============================================================================
@@ -71,6 +71,10 @@ export type EntryPropCommon = {
71
71
  };
72
72
 
73
73
  /**
74
+ * Attachments resolved by walking the parent chain, not owned by the entry:
75
+ * middleware composes downward; revalidate and the error/notFound boundaries are
76
+ * resolved by nearest-ancestor lookup. Inherited, not a single execution chain.
77
+ *
74
78
  * @internal This type is an implementation detail and may change without notice.
75
79
  */
76
80
  export type EntryPropDatas = {
@@ -80,6 +84,16 @@ export type EntryPropDatas = {
80
84
  notFoundBoundary: (ReactNode | NotFoundBoundaryHandler)[];
81
85
  };
82
86
 
87
+ /**
88
+ * Render-time presentation fields shared by every entry variant.
89
+ *
90
+ * @internal This type is an implementation detail and may change without notice.
91
+ */
92
+ export type EntryPropRender = {
93
+ loading?: ReactNode | false;
94
+ transition?: TransitionConfig;
95
+ };
96
+
83
97
  /**
84
98
  * Loader entry stored in EntryData
85
99
  * Contains the loader definition and its revalidation rules
@@ -158,11 +172,9 @@ export type InterceptEntry = {
158
172
  };
159
173
 
160
174
  export interface ParallelEntryData
161
- extends EntryPropCommon, EntryPropDatas, EntryPropSegments {
175
+ extends EntryPropCommon, EntryPropDatas, EntryPropSegments, EntryPropRender {
162
176
  type: "parallel";
163
177
  handler: Record<`@${string}`, Handler<any, any, any> | ReactNode>;
164
- loading?: ReactNode | false;
165
- transition?: TransitionConfig;
166
178
  /** Set when any parallel slot is a Static definition */
167
179
  isStaticPrerender?: true;
168
180
  /** Per-slot static handler $$ids for build-time store lookup */
@@ -171,6 +183,13 @@ export interface ParallelEntryData
171
183
 
172
184
  export type ParallelEntries = Partial<Record<`@${string}`, ParallelEntryData>>;
173
185
 
186
+ /**
187
+ * This entry's own structural children plus its owned loaders. `loader` lives
188
+ * here (not in EntryPropDatas) because loaders are owned by the entry, not
189
+ * inherited from ancestors.
190
+ *
191
+ * @internal This type is an implementation detail and may change without notice.
192
+ */
174
193
  export type EntryPropSegments = {
175
194
  loader: LoaderEntry[];
176
195
  layout: EntryData[];
@@ -182,8 +201,6 @@ export type EntryData =
182
201
  | ({
183
202
  type: "route";
184
203
  handler: Handler<any, any, any>;
185
- loading?: ReactNode | false;
186
- transition?: TransitionConfig;
187
204
  /** URL pattern for this route (used by path() in urls()) */
188
205
  pattern?: string;
189
206
  /** Set when handler is a Prerender definition */
@@ -205,29 +222,28 @@ export type EntryData =
205
222
  responseType?: string;
206
223
  } & EntryPropCommon &
207
224
  EntryPropDatas &
208
- EntryPropSegments)
225
+ EntryPropSegments &
226
+ EntryPropRender)
209
227
  | ({
210
228
  type: "layout";
211
229
  handler: ReactNode | Handler<any, any, any>;
212
- loading?: ReactNode | false;
213
- transition?: TransitionConfig;
214
230
  /** Set when handler is a Static definition (build-time only) */
215
231
  isStaticPrerender?: true;
216
232
  /** Static handler $$id for build-time store lookup */
217
233
  staticHandlerId?: string;
218
234
  } & EntryPropCommon &
219
235
  EntryPropDatas &
220
- EntryPropSegments)
236
+ EntryPropSegments &
237
+ EntryPropRender)
221
238
  | ParallelEntryData
222
239
  | ({
223
240
  type: "cache";
224
241
  /** Cache entries create cache boundaries and render like layouts (with Outlet) */
225
242
  handler: ReactNode | Handler<any, any, any>;
226
- loading?: ReactNode | false;
227
- transition?: TransitionConfig;
228
243
  } & EntryPropCommon &
229
244
  EntryPropDatas &
230
- EntryPropSegments);
245
+ EntryPropSegments &
246
+ EntryPropRender);
231
247
 
232
248
  /**
233
249
  * Tracked include info for build-time manifest generation
@@ -307,6 +323,24 @@ export const RangoContext: AsyncLocalStorage<HelperContext> = ((
307
323
  globalThis as any
308
324
  )[RSC_CONTEXT_KEY] ??= new AsyncLocalStorage<HelperContext>());
309
325
 
326
+ /** shortCode prefix letter per entry type (e.g. "L0", "R2", "M1C0"). */
327
+ const SHORT_CODE_PREFIX: Record<
328
+ "layout" | "parallel" | "route" | "loader" | "cache",
329
+ string
330
+ > = {
331
+ layout: "L",
332
+ parallel: "P",
333
+ route: "R",
334
+ loader: "D",
335
+ cache: "C",
336
+ };
337
+
338
+ /** Post-increment a named per-store counter, returning the prior value. */
339
+ function bumpCounter(store: HelperContext, key: string): number {
340
+ store.counters[key] ??= 0;
341
+ return store.counters[key]++;
342
+ }
343
+
310
344
  export const getContext = (): {
311
345
  context: AsyncLocalStorage<HelperContext>;
312
346
  getStore: () => HelperContext;
@@ -373,10 +407,7 @@ export const getContext = (): {
373
407
  ) => {
374
408
  const store = context.getStore();
375
409
  invariant(store, "No context RangoContext available");
376
- store.counters[type] ??= 0;
377
- const index = store.counters[type];
378
- store.counters[type] = index + 1;
379
- return `$${type}.${index}`;
410
+ return `$${type}.${bumpCounter(store, type)}`;
380
411
  },
381
412
  getShortCode: (
382
413
  type: "layout" | "parallel" | "route" | "loader" | "cache",
@@ -385,16 +416,7 @@ export const getContext = (): {
385
416
  invariant(store, "No context RangoContext available");
386
417
 
387
418
  const parent = store.parent;
388
- const prefix =
389
- type === "layout"
390
- ? "L"
391
- : type === "parallel"
392
- ? "P"
393
- : type === "loader"
394
- ? "D"
395
- : type === "cache"
396
- ? "C"
397
- : "R";
419
+ const prefix = SHORT_CODE_PREFIX[type];
398
420
  const mountPrefix =
399
421
  store.mountIndex !== undefined ? `M${store.mountIndex}` : "";
400
422
 
@@ -405,10 +427,7 @@ export const getContext = (): {
405
427
  const counterKey = mountPrefix
406
428
  ? `${mountPrefix}_root_${type}`
407
429
  : `root_${type}`;
408
- store.counters[counterKey] ??= 0;
409
- const index = store.counters[counterKey];
410
- store.counters[counterKey] = index + 1;
411
- return `${mountPrefix}${prefix}${index}`;
430
+ return `${mountPrefix}${prefix}${bumpCounter(store, counterKey)}`;
412
431
  } else {
413
432
  // Child entry: use parent-scoped counter with includeScope appended.
414
433
  // When we're evaluating a lazy include's direct children, includeScope
@@ -416,10 +435,7 @@ export const getContext = (): {
416
435
  // parent's counter namespace so routes inside one include cannot
417
436
  // collide with siblings declared outside it.
418
437
  const counterKey = `${parent.shortCode}${includeScope}_${type}`;
419
- store.counters[counterKey] ??= 0;
420
- const index = store.counters[counterKey];
421
- store.counters[counterKey] = index + 1;
422
- return `${parent.shortCode}${includeScope}${prefix}${index}`;
438
+ return `${parent.shortCode}${includeScope}${prefix}${bumpCounter(store, counterKey)}`;
423
439
  }
424
440
  },
425
441
  runWithStore: <T>(
@@ -493,6 +509,31 @@ export const getContext = (): {
493
509
  };
494
510
  };
495
511
 
512
+ /**
513
+ * Acquire the active DSL build context, throwing `message` if a helper was
514
+ * called outside a urls()/map() builder. Returns the store API and the live
515
+ * HelperContext so callers avoid a second getContext() lookup.
516
+ */
517
+ export function requireDslContext(message: string): {
518
+ store: ReturnType<typeof getContext>;
519
+ ctx: HelperContext;
520
+ } {
521
+ const store = getContext();
522
+ const ctx = store.context.getStore();
523
+ if (!ctx) {
524
+ // The only reason the store is absent here is that a route-definition helper
525
+ // ran with no active RangoContext — i.e. outside a urls()/map() builder.
526
+ // Record that as the cause so the throw is self-explanatory, not a bare
527
+ // "must be called inside urls()" with no indication of the mechanism.
528
+ throw new DslContextError(message, {
529
+ cause:
530
+ "RangoContext store is undefined: a route-definition helper was called " +
531
+ "outside an active urls()/map() builder.",
532
+ });
533
+ }
534
+ return { store, ctx };
535
+ }
536
+
496
537
  /**
497
538
  * Run a callback with specific URL and name prefixes
498
539
  * Used by include() to apply prefixes to nested patterns