@useavalon/avalon 0.1.11 → 0.1.13

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.
Files changed (141) hide show
  1. package/README.md +54 -54
  2. package/mod.ts +302 -302
  3. package/package.json +49 -26
  4. package/src/build/integration-bundler-plugin.ts +116 -116
  5. package/src/build/integration-config.ts +168 -168
  6. package/src/build/integration-detection-plugin.ts +117 -117
  7. package/src/build/integration-resolver-plugin.ts +90 -90
  8. package/src/build/island-manifest.ts +269 -269
  9. package/src/build/island-types-generator.ts +476 -476
  10. package/src/build/mdx-island-transform.ts +464 -464
  11. package/src/build/mdx-plugin.ts +98 -98
  12. package/src/build/page-island-transform.ts +598 -598
  13. package/src/build/prop-extractors/index.ts +21 -21
  14. package/src/build/prop-extractors/lit.ts +140 -140
  15. package/src/build/prop-extractors/qwik.ts +16 -16
  16. package/src/build/prop-extractors/solid.ts +125 -125
  17. package/src/build/prop-extractors/svelte.ts +194 -194
  18. package/src/build/prop-extractors/vue.ts +111 -111
  19. package/src/build/sidecar-file-manager.ts +104 -104
  20. package/src/build/sidecar-renderer.ts +30 -30
  21. package/src/client/adapters/index.ts +21 -13
  22. package/src/client/components.ts +35 -35
  23. package/src/client/css-hmr-handler.ts +344 -344
  24. package/src/client/framework-adapter.ts +462 -462
  25. package/src/client/hmr-coordinator.ts +396 -396
  26. package/src/client/hmr-error-overlay.js +533 -533
  27. package/src/client/main.js +824 -816
  28. package/src/client/types/framework-runtime.d.ts +68 -68
  29. package/src/client/types/vite-hmr.d.ts +46 -46
  30. package/src/client/types/vite-virtual-modules.d.ts +70 -60
  31. package/src/components/Image.tsx +123 -123
  32. package/src/components/IslandErrorBoundary.tsx +145 -145
  33. package/src/components/LayoutDataErrorBoundary.tsx +141 -141
  34. package/src/components/LayoutErrorBoundary.tsx +127 -127
  35. package/src/components/PersistentIsland.tsx +52 -52
  36. package/src/components/StreamingErrorBoundary.tsx +233 -233
  37. package/src/components/StreamingLayout.tsx +538 -538
  38. package/src/core/components/component-analyzer.ts +192 -192
  39. package/src/core/components/component-detection.ts +508 -508
  40. package/src/core/components/enhanced-framework-detector.ts +500 -500
  41. package/src/core/components/framework-registry.ts +563 -563
  42. package/src/core/content/mdx-processor.ts +46 -46
  43. package/src/core/integrations/index.ts +19 -19
  44. package/src/core/integrations/loader.ts +125 -125
  45. package/src/core/integrations/registry.ts +175 -175
  46. package/src/core/islands/island-persistence.ts +325 -325
  47. package/src/core/islands/island-state-serializer.ts +258 -258
  48. package/src/core/islands/persistent-island-context.tsx +80 -80
  49. package/src/core/islands/use-persistent-state.ts +68 -68
  50. package/src/core/layout/enhanced-layout-resolver.ts +322 -322
  51. package/src/core/layout/layout-cache-manager.ts +485 -485
  52. package/src/core/layout/layout-composer.ts +357 -357
  53. package/src/core/layout/layout-data-loader.ts +516 -516
  54. package/src/core/layout/layout-discovery.ts +243 -243
  55. package/src/core/layout/layout-matcher.ts +299 -299
  56. package/src/core/layout/layout-types.ts +110 -110
  57. package/src/core/modules/framework-module-resolver.ts +273 -273
  58. package/src/islands/component-analysis.ts +213 -213
  59. package/src/islands/css-utils.ts +565 -565
  60. package/src/islands/discovery/index.ts +80 -80
  61. package/src/islands/discovery/registry.ts +340 -340
  62. package/src/islands/discovery/resolver.ts +477 -477
  63. package/src/islands/discovery/scanner.ts +386 -386
  64. package/src/islands/discovery/types.ts +117 -117
  65. package/src/islands/discovery/validator.ts +544 -544
  66. package/src/islands/discovery/watcher.ts +368 -368
  67. package/src/islands/framework-detection.ts +428 -428
  68. package/src/islands/integration-loader.ts +490 -490
  69. package/src/islands/island.tsx +565 -565
  70. package/src/islands/render-cache.ts +550 -550
  71. package/src/islands/types.ts +80 -80
  72. package/src/islands/universal-css-collector.ts +157 -157
  73. package/src/islands/universal-head-collector.ts +137 -137
  74. package/src/layout-system.d.ts +592 -592
  75. package/src/layout-system.ts +218 -218
  76. package/src/middleware/discovery.ts +268 -268
  77. package/src/middleware/executor.ts +315 -315
  78. package/src/middleware/index.ts +76 -76
  79. package/src/middleware/types.ts +99 -99
  80. package/src/nitro/build-config.ts +575 -575
  81. package/src/nitro/config.ts +483 -483
  82. package/src/nitro/error-handler.ts +636 -636
  83. package/src/nitro/index.ts +173 -173
  84. package/src/nitro/island-manifest.ts +584 -584
  85. package/src/nitro/middleware-adapter.ts +260 -260
  86. package/src/nitro/renderer.ts +1471 -1471
  87. package/src/nitro/route-discovery.ts +439 -439
  88. package/src/nitro/types.ts +321 -321
  89. package/src/render/collect-css.ts +198 -198
  90. package/src/render/error-pages.ts +79 -79
  91. package/src/render/isolated-ssr-renderer.ts +654 -654
  92. package/src/render/ssr.ts +1030 -1030
  93. package/src/schemas/api.ts +30 -30
  94. package/src/schemas/core.ts +64 -64
  95. package/src/schemas/index.ts +212 -212
  96. package/src/schemas/layout.ts +279 -279
  97. package/src/schemas/routing/index.ts +38 -38
  98. package/src/schemas/routing.ts +376 -376
  99. package/src/types/as-island.ts +20 -20
  100. package/src/types/image.d.ts +106 -106
  101. package/src/types/index.d.ts +22 -22
  102. package/src/types/island-jsx.d.ts +33 -33
  103. package/src/types/island-prop.d.ts +20 -20
  104. package/src/types/layout.ts +285 -285
  105. package/src/types/mdx.d.ts +6 -6
  106. package/src/types/routing.ts +555 -555
  107. package/src/types/types.ts +5 -5
  108. package/src/types/urlpattern.d.ts +49 -49
  109. package/src/types/vite-env.d.ts +11 -11
  110. package/src/utils/dev-logger.ts +299 -299
  111. package/src/utils/fs.ts +151 -151
  112. package/src/vite-plugin/auto-discover.ts +551 -551
  113. package/src/vite-plugin/config.ts +266 -266
  114. package/src/vite-plugin/errors.ts +127 -127
  115. package/src/vite-plugin/image-optimization.ts +156 -156
  116. package/src/vite-plugin/integration-activator.ts +126 -126
  117. package/src/vite-plugin/island-sidecar-plugin.ts +176 -176
  118. package/src/vite-plugin/module-discovery.ts +189 -189
  119. package/src/vite-plugin/nitro-integration.ts +1354 -1354
  120. package/src/vite-plugin/plugin.ts +403 -409
  121. package/src/vite-plugin/types.ts +327 -327
  122. package/src/vite-plugin/validation.ts +228 -228
  123. package/src/client/adapters/index.js +0 -12
  124. package/src/client/adapters/lit-adapter.js +0 -467
  125. package/src/client/adapters/lit-adapter.ts +0 -654
  126. package/src/client/adapters/preact-adapter.js +0 -223
  127. package/src/client/adapters/preact-adapter.ts +0 -331
  128. package/src/client/adapters/qwik-adapter.js +0 -259
  129. package/src/client/adapters/qwik-adapter.ts +0 -345
  130. package/src/client/adapters/react-adapter.js +0 -220
  131. package/src/client/adapters/react-adapter.ts +0 -353
  132. package/src/client/adapters/solid-adapter.js +0 -295
  133. package/src/client/adapters/solid-adapter.ts +0 -451
  134. package/src/client/adapters/svelte-adapter.js +0 -368
  135. package/src/client/adapters/svelte-adapter.ts +0 -524
  136. package/src/client/adapters/vue-adapter.js +0 -278
  137. package/src/client/adapters/vue-adapter.ts +0 -467
  138. package/src/client/components.js +0 -23
  139. package/src/client/css-hmr-handler.js +0 -263
  140. package/src/client/framework-adapter.js +0 -283
  141. package/src/client/hmr-coordinator.js +0 -274
@@ -1,555 +1,555 @@
1
- /**
2
- * Enhanced TypeScript support for file-system routing
3
- *
4
- * This module provides strongly typed interfaces, utility types, and type guards
5
- * for the file-system routing system, ensuring type safety throughout the routing pipeline.
6
- */
7
-
8
- import type { ComponentType } from 'preact';
9
- import { h } from 'preact';
10
- import type {
11
- LoaderContext,
12
- Metadata
13
- } from '../schemas/routing.ts';
14
- import type { LayoutConfig } from '../schemas/layout.ts';
15
- import process from "node:process";
16
-
17
- // === Route Parameter Type Extraction ===
18
-
19
- /**
20
- * Extract route parameters from a route pattern string
21
- *
22
- * @example
23
- * ```typescript
24
- * type BlogParams = ExtractRouteParams<'/blog/[slug]'>; // { slug: string }
25
- * type UserParams = ExtractRouteParams<'/users/[id]/posts/[postId]'>; // { id: string; postId: string }
26
- * type CatchAllParams = ExtractRouteParams<'/docs/[...path]'>; // { path: string[] }
27
- * ```
28
- */
29
- export type ExtractRouteParams<T extends string> = T extends `${string}[${infer Param}]${infer Rest}`
30
- ? Param extends `...${infer RestParam}`
31
- ? { [K in RestParam]: string[] } & ExtractRouteParams<Rest>
32
- : Param extends `${infer OptionalParam}?`
33
- ? { [K in OptionalParam]?: string } & ExtractRouteParams<Rest>
34
- : { [K in Param]: string } & ExtractRouteParams<Rest>
35
- : Record<PropertyKey, never>;
36
-
37
- /**
38
- * Extract optional route parameters from a route pattern string
39
- *
40
- * @example
41
- * ```typescript
42
- * type OptionalParams = ExtractOptionalParams<'/blog/[[...slug]]'>; // { slug?: string[] }
43
- * ```
44
- */
45
- export type ExtractOptionalParams<T extends string> = T extends `${string}[[${infer Param}]]${infer Rest}`
46
- ? Param extends `...${infer RestParam}`
47
- ? { [K in RestParam]?: string[] } & ExtractOptionalParams<Rest>
48
- : { [K in Param]?: string } & ExtractOptionalParams<Rest>
49
- : Record<PropertyKey, never>;
50
-
51
- /**
52
- * Combine required and optional route parameters
53
- */
54
- export type RouteParameters<T extends string> = ExtractRouteParams<T> & ExtractOptionalParams<T>;
55
-
56
- // === Strongly Typed Page Components ===
57
-
58
- /**
59
- * Strongly typed page component with route parameters
60
- *
61
- * @example
62
- * ```typescript
63
- * const BlogPost: TypedPageComponent<'/blog/[slug]'> = ({ params, query, data }) => {
64
- * // params.slug is typed as string
65
- * return <div>Blog post: {params.slug}</div>;
66
- * };
67
- * ```
68
- */
69
- export type TypedPageComponent<TRoute extends string = string> = ComponentType<{
70
- params: RouteParameters<TRoute>;
71
- query: URLSearchParams;
72
- data?: unknown;
73
- }>;
74
-
75
- /**
76
- * Page component with custom data type
77
- *
78
- * @example
79
- * ```typescript
80
- * interface BlogPostData {
81
- * title: string;
82
- * content: string;
83
- * }
84
- *
85
- * const BlogPost: TypedPageComponentWithData<'/blog/[slug]', BlogPostData> = ({ params, data }) => {
86
- * // data is typed as BlogPostData | undefined
87
- * return <div>{data?.title}</div>;
88
- * };
89
- * ```
90
- */
91
- export type TypedPageComponentWithData<TRoute extends string, TData> = ComponentType<{
92
- params: RouteParameters<TRoute>;
93
- query: URLSearchParams;
94
- data?: TData;
95
- }>;
96
-
97
- // === Strongly Typed Metadata Generators ===
98
-
99
- /**
100
- * Strongly typed metadata generator function
101
- *
102
- * @example
103
- * ```typescript
104
- * const generateMetadata: TypedMetadataGenerator<'/blog/[slug]'> = async ({ slug }) => {
105
- * // slug is typed as string
106
- * return {
107
- * title: `Blog Post: ${slug}`,
108
- * description: `Read about ${slug}`,
109
- * };
110
- * };
111
- * ```
112
- */
113
- export type TypedMetadataGenerator<TRoute extends string> = (params: RouteParameters<TRoute>) => Promise<Metadata>;
114
-
115
- /**
116
- * Metadata generator with custom context
117
- */
118
- export type TypedMetadataGeneratorWithContext<TRoute extends string, TContext = unknown> = (
119
- params: RouteParameters<TRoute>,
120
- context: TContext
121
- ) => Promise<Metadata>;
122
-
123
- // === Strongly Typed Page Loaders ===
124
-
125
- /**
126
- * Strongly typed page loader function
127
- *
128
- * @example
129
- * ```typescript
130
- * const loader: TypedPageLoader<'/blog/[slug]', BlogPostData> = async ({ params, request }) => {
131
- * // params.slug is typed as string
132
- * const post = await fetchBlogPost(params.slug);
133
- * return post;
134
- * };
135
- * ```
136
- */
137
- export type TypedPageLoader<TRoute extends string, TData = unknown> = (
138
- context: LoaderContext & { params: RouteParameters<TRoute> }
139
- ) => Promise<TData>;
140
-
141
- /**
142
- * Page loader with custom context
143
- */
144
- export type TypedPageLoaderWithContext<TRoute extends string, TData = unknown, TContext = unknown> = (
145
- context: LoaderContext & { params: RouteParameters<TRoute> },
146
- customContext: TContext
147
- ) => Promise<TData>;
148
-
149
- // === Strongly Typed Route Modules ===
150
-
151
- /**
152
- * Strongly typed route page module
153
- *
154
- * @example
155
- * ```typescript
156
- * const pageModule: TypedRoutePageModule<'/blog/[slug]', BlogPostData> = {
157
- * default: BlogPostComponent,
158
- * generateMetadata: async ({ slug }) => ({ title: slug }),
159
- * loader: async ({ params }) => fetchBlogPost(params.slug),
160
- * };
161
- * ```
162
- */
163
- export type TypedRoutePageModule<TRoute extends string, TData = unknown> = {
164
- default: TypedPageComponent<TRoute>;
165
- layoutConfig?: LayoutConfig;
166
- generateMetadata?: TypedMetadataGenerator<TRoute>;
167
- loader?: TypedPageLoader<TRoute, TData>;
168
- };
169
-
170
- // === API Route Types ===
171
-
172
- /**
173
- * Strongly typed API handler function
174
- *
175
- * @example
176
- * ```typescript
177
- * export const GET: TypedApiHandler<'/api/users/[id]'> = async (request, { params }) => {
178
- * // params.id is typed as string
179
- * const user = await getUser(params.id);
180
- * return Response.json(user);
181
- * };
182
- * ```
183
- */
184
- export type TypedApiHandler<TRoute extends string> = (
185
- request: Request,
186
- context: LoaderContext & { params: RouteParameters<TRoute> }
187
- ) => Promise<Response>;
188
-
189
- /**
190
- * Strongly typed API module with all HTTP methods
191
- */
192
- export type TypedApiModule<TRoute extends string> = {
193
- GET?: TypedApiHandler<TRoute>;
194
- POST?: TypedApiHandler<TRoute>;
195
- PUT?: TypedApiHandler<TRoute>;
196
- DELETE?: TypedApiHandler<TRoute>;
197
- PATCH?: TypedApiHandler<TRoute>;
198
- HEAD?: TypedApiHandler<TRoute>;
199
- OPTIONS?: TypedApiHandler<TRoute>;
200
- };
201
-
202
- // === Route Validation Types ===
203
-
204
- /**
205
- * Route pattern validation
206
- */
207
- export type ValidRoutePattern<T extends string> = T extends `${string}[${string}]${string}`
208
- ? T
209
- : T extends `${string}(${string})${string}`
210
- ? T
211
- : T extends `${string}_${string}`
212
- ? never // Private files/folders
213
- : T;
214
-
215
- /**
216
- * Valid file extensions for routes
217
- */
218
- export type ValidRouteExtension = '.tsx' | '.ts' | '.jsx' | '.js';
219
-
220
- /**
221
- * Route file path validation
222
- */
223
- export type ValidRouteFile<T extends string> = T extends `${string}${ValidRouteExtension}`
224
- ? T extends `${string}_${string}`
225
- ? never // Private files
226
- : T
227
- : never;
228
-
229
- // === Component Prop Validation ===
230
-
231
- /**
232
- * Validate that a component accepts the correct props for a route
233
- */
234
- export type ValidatePageComponent<TRoute extends string, TComponent> = TComponent extends ComponentType<infer TProps>
235
- ? TProps extends { params: RouteParameters<TRoute> }
236
- ? TComponent
237
- : never
238
- : never;
239
-
240
- /**
241
- * Props validation for page components
242
- */
243
- export interface PageComponentProps<TRoute extends string = string, TData = unknown> {
244
- params: RouteParameters<TRoute>;
245
- query: URLSearchParams;
246
- data?: TData;
247
- }
248
-
249
- /**
250
- * Validate page component props
251
- */
252
- export type ValidatePageProps<TRoute extends string, TProps> = TProps extends PageComponentProps<TRoute>
253
- ? TProps
254
- : never;
255
-
256
- // === Utility Types for Route Analysis ===
257
-
258
- /**
259
- * Check if a route has dynamic segments
260
- */
261
- export type HasDynamicSegments<T extends string> = T extends `${string}[${string}]${string}` ? true : false;
262
-
263
- /**
264
- * Check if a route has catch-all segments
265
- */
266
- export type HasCatchAllSegments<T extends string> = T extends `${string}[...${string}]${string}` ? true : false;
267
-
268
- /**
269
- * Check if a route has optional segments
270
- */
271
- export type HasOptionalSegments<T extends string> = T extends `${string}[[${string}]]${string}` ? true : false;
272
-
273
- /**
274
- * Get the static parts of a route
275
- */
276
- export type GetStaticParts<T extends string> = T extends `${infer Static}[${string}]${infer Rest}`
277
- ? Static | GetStaticParts<Rest>
278
- : T;
279
-
280
- /**
281
- * Count dynamic segments in a route
282
- */
283
- export type CountDynamicSegments<
284
- T extends string,
285
- Count extends readonly unknown[] = []
286
- > = T extends `${string}[${string}]${infer Rest}` ? CountDynamicSegments<Rest, [...Count, unknown]> : Count['length'];
287
-
288
- // === Error Handling Types ===
289
-
290
- /**
291
- * Route error types
292
- */
293
- export type RouteErrorType =
294
- | 'ROUTE_NOT_FOUND'
295
- | 'INVALID_PARAMS'
296
- | 'LOADER_ERROR'
297
- | 'METADATA_ERROR'
298
- | 'COMPONENT_ERROR'
299
- | 'VALIDATION_ERROR';
300
-
301
- /**
302
- * Route error with context
303
- */
304
- export interface RouteError<TRoute extends string = string> {
305
- type: RouteErrorType;
306
- route: TRoute;
307
- params?: Partial<RouteParameters<TRoute>>;
308
- message: string;
309
- originalError?: Error;
310
- suggestions?: string[];
311
- }
312
-
313
- /**
314
- * Error boundary props for routes
315
- */
316
- export interface RouteErrorBoundaryProps<TRoute extends string = string> {
317
- error: RouteError<TRoute>;
318
- retry: () => void;
319
- fallback?: ComponentType<{ error: RouteError<TRoute> }>;
320
- }
321
-
322
- // === Development and Debugging Types ===
323
-
324
- /**
325
- * Route debugging information
326
- */
327
- export interface RouteDebugInfo<TRoute extends string = string> {
328
- route: TRoute;
329
- params: RouteParameters<TRoute>;
330
- staticParts: string[];
331
- dynamicSegments: string[];
332
- hasOptionalSegments: HasOptionalSegments<TRoute>;
333
- hasCatchAllSegments: HasCatchAllSegments<TRoute>;
334
- priority: number;
335
- filePath: string;
336
- }
337
-
338
- /**
339
- * Route performance metrics
340
- */
341
- export interface RoutePerformanceMetrics {
342
- discoveryTime: number;
343
- loadTime: number;
344
- renderTime: number;
345
- metadataResolutionTime: number;
346
- cacheHits: number;
347
- cacheMisses: number;
348
- }
349
-
350
- // === Type Guards and Validation Functions ===
351
-
352
- /**
353
- * Type guard for route parameters
354
- */
355
- export function isValidRouteParams<TRoute extends string>(
356
- params: unknown,
357
- expectedParams: (keyof RouteParameters<TRoute>)[]
358
- ): params is RouteParameters<TRoute> {
359
- if (!params || typeof params !== 'object') {
360
- return false;
361
- }
362
-
363
- const paramObj = params as Record<string, unknown>;
364
-
365
- // Check that all expected parameters are present and are strings or string arrays
366
- for (const param of expectedParams) {
367
- const value = paramObj[param as string];
368
- if (value === undefined) {
369
- return false;
370
- }
371
- if (typeof value !== 'string' && !Array.isArray(value)) {
372
- return false;
373
- }
374
- if (Array.isArray(value) && !value.every(item => typeof item === 'string')) {
375
- return false;
376
- }
377
- }
378
-
379
- return true;
380
- }
381
-
382
- /**
383
- * Type guard for page component props
384
- */
385
- export function isValidPageProps<TRoute extends string>(
386
- props: unknown,
387
- expectedParams: (keyof RouteParameters<TRoute>)[]
388
- ): props is PageComponentProps<TRoute> {
389
- if (!props || typeof props !== 'object') {
390
- return false;
391
- }
392
-
393
- const propsObj = props as Record<string, unknown>;
394
-
395
- // Check required properties
396
- if (!propsObj.params || !propsObj.query) {
397
- return false;
398
- }
399
-
400
- // Validate params
401
- if (!isValidRouteParams<TRoute>(propsObj.params, expectedParams)) {
402
- return false;
403
- }
404
-
405
- // Validate query
406
- if (!(propsObj.query instanceof URLSearchParams)) {
407
- return false;
408
- }
409
-
410
- return true;
411
- }
412
-
413
- /**
414
- * Validate route pattern syntax
415
- */
416
- export function isValidRoutePattern(pattern: string): boolean {
417
- // Check for valid bracket syntax
418
- const bracketRegex = /\[([^\]]+)\]/g;
419
- const matches = pattern.match(bracketRegex);
420
-
421
- if (matches) {
422
- for (const match of matches) {
423
- const param = match.slice(1, -1); // Remove brackets
424
-
425
- // Check for valid parameter names
426
- if (!/^[a-zA-Z_]\w*(\?)?$/.test(param) && !/^\.\.\.[a-zA-Z_]\w*$/.test(param)) {
427
- return false;
428
- }
429
- }
430
- }
431
-
432
- // Check for valid route group syntax
433
- const groupRegex = /\(([^)]+)\)/g;
434
- const groupMatches = pattern.match(groupRegex);
435
-
436
- if (groupMatches) {
437
- for (const match of groupMatches) {
438
- const group = match.slice(1, -1); // Remove parentheses
439
-
440
- // Check for valid group names
441
- if (!/^[a-zA-Z_]\w*-*\w*$/.test(group)) {
442
- return false;
443
- }
444
- }
445
- }
446
-
447
- return true;
448
- }
449
-
450
- // === Helper Functions for Type Safety ===
451
-
452
- /**
453
- * Create a typed page component with parameter validation
454
- */
455
- export function createTypedPageComponent<TRoute extends string>(
456
- component: TypedPageComponent<TRoute>,
457
- expectedParams: (keyof RouteParameters<TRoute>)[]
458
- ): TypedPageComponent<TRoute> {
459
- // Return a wrapper component that validates props
460
- const WrappedComponent: TypedPageComponent<TRoute> = (props) => {
461
- if (process.env?.NODE_ENV === 'development') {
462
- if (!isValidPageProps<TRoute>(props, expectedParams)) {
463
- console.warn('Invalid props passed to typed page component:', props);
464
- }
465
- }
466
- return h(component, props);
467
- };
468
-
469
- return WrappedComponent;
470
- }
471
-
472
- /**
473
- * Create a typed metadata generator with parameter validation
474
- */
475
- export function createTypedMetadataGenerator<TRoute extends string>(
476
- generator: TypedMetadataGenerator<TRoute>,
477
- expectedParams: (keyof RouteParameters<TRoute>)[]
478
- ): TypedMetadataGenerator<TRoute> {
479
- return async params => {
480
- // Validate params in development
481
- if (process.env?.NODE_ENV === 'development') {
482
- if (!isValidRouteParams<TRoute>(params, expectedParams)) {
483
- console.warn('Invalid params passed to typed metadata generator:', params);
484
- }
485
- }
486
-
487
- return await generator(params);
488
- };
489
- }
490
-
491
- /**
492
- * Create a typed page loader with parameter validation
493
- */
494
- export function createTypedPageLoader<TRoute extends string, TData = unknown>(
495
- loader: TypedPageLoader<TRoute, TData>,
496
- expectedParams: (keyof RouteParameters<TRoute>)[]
497
- ): TypedPageLoader<TRoute, TData> {
498
- return async context => {
499
- // Validate params in development
500
- if (process.env?.NODE_ENV === 'development') {
501
- if (!isValidRouteParams<TRoute>(context.params, expectedParams)) {
502
- console.warn('Invalid params passed to typed page loader:', context.params);
503
- }
504
- }
505
-
506
- return await loader(context);
507
- };
508
- }
509
-
510
- /**
511
- * Validate component accepts correct props for a route
512
- */
513
- export function validatePageComponent<TRoute extends string>(
514
- component: unknown,
515
- _expectedParams: (keyof RouteParameters<TRoute>)[]
516
- ): component is TypedPageComponent<TRoute> {
517
- // In TypeScript, we can't really validate function signatures at runtime
518
- // This is more of a development-time helper
519
- if (typeof component !== 'function') {
520
- return false;
521
- }
522
-
523
- // Could add more sophisticated validation in development mode
524
- return true;
525
- }
526
-
527
- /**
528
- * Create a typed API handler with parameter validation
529
- */
530
- export function createTypedApiHandler<TRoute extends string>(
531
- handler: TypedApiHandler<TRoute>,
532
- expectedParams: (keyof RouteParameters<TRoute>)[]
533
- ): TypedApiHandler<TRoute> {
534
- return async (request, context) => {
535
- // Validate params in development
536
- if (process.env?.NODE_ENV === 'development') {
537
- if (!isValidRouteParams<TRoute>(context.params, expectedParams)) {
538
- console.warn('Invalid params passed to typed API handler:', context.params);
539
- }
540
- }
541
-
542
- return await handler(request, context);
543
- };
544
- }
545
-
546
- // === Re-export commonly used types ===
547
- export type {
548
- RouteParams,
549
- LoaderContext,
550
- Metadata,
551
- PageProps,
552
- RoutePageModule,
553
- FileSystemRoute,
554
- ResolvedMetadata,
555
- } from '../schemas/routing.ts';
1
+ /**
2
+ * Enhanced TypeScript support for file-system routing
3
+ *
4
+ * This module provides strongly typed interfaces, utility types, and type guards
5
+ * for the file-system routing system, ensuring type safety throughout the routing pipeline.
6
+ */
7
+
8
+ import type { ComponentType } from 'preact';
9
+ import { h } from 'preact';
10
+ import type {
11
+ LoaderContext,
12
+ Metadata
13
+ } from '../schemas/routing.ts';
14
+ import type { LayoutConfig } from '../schemas/layout.ts';
15
+ import process from "node:process";
16
+
17
+ // === Route Parameter Type Extraction ===
18
+
19
+ /**
20
+ * Extract route parameters from a route pattern string
21
+ *
22
+ * @example
23
+ * ```typescript
24
+ * type BlogParams = ExtractRouteParams<'/blog/[slug]'>; // { slug: string }
25
+ * type UserParams = ExtractRouteParams<'/users/[id]/posts/[postId]'>; // { id: string; postId: string }
26
+ * type CatchAllParams = ExtractRouteParams<'/docs/[...path]'>; // { path: string[] }
27
+ * ```
28
+ */
29
+ export type ExtractRouteParams<T extends string> = T extends `${string}[${infer Param}]${infer Rest}`
30
+ ? Param extends `...${infer RestParam}`
31
+ ? { [K in RestParam]: string[] } & ExtractRouteParams<Rest>
32
+ : Param extends `${infer OptionalParam}?`
33
+ ? { [K in OptionalParam]?: string } & ExtractRouteParams<Rest>
34
+ : { [K in Param]: string } & ExtractRouteParams<Rest>
35
+ : Record<PropertyKey, never>;
36
+
37
+ /**
38
+ * Extract optional route parameters from a route pattern string
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * type OptionalParams = ExtractOptionalParams<'/blog/[[...slug]]'>; // { slug?: string[] }
43
+ * ```
44
+ */
45
+ export type ExtractOptionalParams<T extends string> = T extends `${string}[[${infer Param}]]${infer Rest}`
46
+ ? Param extends `...${infer RestParam}`
47
+ ? { [K in RestParam]?: string[] } & ExtractOptionalParams<Rest>
48
+ : { [K in Param]?: string } & ExtractOptionalParams<Rest>
49
+ : Record<PropertyKey, never>;
50
+
51
+ /**
52
+ * Combine required and optional route parameters
53
+ */
54
+ export type RouteParameters<T extends string> = ExtractRouteParams<T> & ExtractOptionalParams<T>;
55
+
56
+ // === Strongly Typed Page Components ===
57
+
58
+ /**
59
+ * Strongly typed page component with route parameters
60
+ *
61
+ * @example
62
+ * ```typescript
63
+ * const BlogPost: TypedPageComponent<'/blog/[slug]'> = ({ params, query, data }) => {
64
+ * // params.slug is typed as string
65
+ * return <div>Blog post: {params.slug}</div>;
66
+ * };
67
+ * ```
68
+ */
69
+ export type TypedPageComponent<TRoute extends string = string> = ComponentType<{
70
+ params: RouteParameters<TRoute>;
71
+ query: URLSearchParams;
72
+ data?: unknown;
73
+ }>;
74
+
75
+ /**
76
+ * Page component with custom data type
77
+ *
78
+ * @example
79
+ * ```typescript
80
+ * interface BlogPostData {
81
+ * title: string;
82
+ * content: string;
83
+ * }
84
+ *
85
+ * const BlogPost: TypedPageComponentWithData<'/blog/[slug]', BlogPostData> = ({ params, data }) => {
86
+ * // data is typed as BlogPostData | undefined
87
+ * return <div>{data?.title}</div>;
88
+ * };
89
+ * ```
90
+ */
91
+ export type TypedPageComponentWithData<TRoute extends string, TData> = ComponentType<{
92
+ params: RouteParameters<TRoute>;
93
+ query: URLSearchParams;
94
+ data?: TData;
95
+ }>;
96
+
97
+ // === Strongly Typed Metadata Generators ===
98
+
99
+ /**
100
+ * Strongly typed metadata generator function
101
+ *
102
+ * @example
103
+ * ```typescript
104
+ * const generateMetadata: TypedMetadataGenerator<'/blog/[slug]'> = async ({ slug }) => {
105
+ * // slug is typed as string
106
+ * return {
107
+ * title: `Blog Post: ${slug}`,
108
+ * description: `Read about ${slug}`,
109
+ * };
110
+ * };
111
+ * ```
112
+ */
113
+ export type TypedMetadataGenerator<TRoute extends string> = (params: RouteParameters<TRoute>) => Promise<Metadata>;
114
+
115
+ /**
116
+ * Metadata generator with custom context
117
+ */
118
+ export type TypedMetadataGeneratorWithContext<TRoute extends string, TContext = unknown> = (
119
+ params: RouteParameters<TRoute>,
120
+ context: TContext
121
+ ) => Promise<Metadata>;
122
+
123
+ // === Strongly Typed Page Loaders ===
124
+
125
+ /**
126
+ * Strongly typed page loader function
127
+ *
128
+ * @example
129
+ * ```typescript
130
+ * const loader: TypedPageLoader<'/blog/[slug]', BlogPostData> = async ({ params, request }) => {
131
+ * // params.slug is typed as string
132
+ * const post = await fetchBlogPost(params.slug);
133
+ * return post;
134
+ * };
135
+ * ```
136
+ */
137
+ export type TypedPageLoader<TRoute extends string, TData = unknown> = (
138
+ context: LoaderContext & { params: RouteParameters<TRoute> }
139
+ ) => Promise<TData>;
140
+
141
+ /**
142
+ * Page loader with custom context
143
+ */
144
+ export type TypedPageLoaderWithContext<TRoute extends string, TData = unknown, TContext = unknown> = (
145
+ context: LoaderContext & { params: RouteParameters<TRoute> },
146
+ customContext: TContext
147
+ ) => Promise<TData>;
148
+
149
+ // === Strongly Typed Route Modules ===
150
+
151
+ /**
152
+ * Strongly typed route page module
153
+ *
154
+ * @example
155
+ * ```typescript
156
+ * const pageModule: TypedRoutePageModule<'/blog/[slug]', BlogPostData> = {
157
+ * default: BlogPostComponent,
158
+ * generateMetadata: async ({ slug }) => ({ title: slug }),
159
+ * loader: async ({ params }) => fetchBlogPost(params.slug),
160
+ * };
161
+ * ```
162
+ */
163
+ export type TypedRoutePageModule<TRoute extends string, TData = unknown> = {
164
+ default: TypedPageComponent<TRoute>;
165
+ layoutConfig?: LayoutConfig;
166
+ generateMetadata?: TypedMetadataGenerator<TRoute>;
167
+ loader?: TypedPageLoader<TRoute, TData>;
168
+ };
169
+
170
+ // === API Route Types ===
171
+
172
+ /**
173
+ * Strongly typed API handler function
174
+ *
175
+ * @example
176
+ * ```typescript
177
+ * export const GET: TypedApiHandler<'/api/users/[id]'> = async (request, { params }) => {
178
+ * // params.id is typed as string
179
+ * const user = await getUser(params.id);
180
+ * return Response.json(user);
181
+ * };
182
+ * ```
183
+ */
184
+ export type TypedApiHandler<TRoute extends string> = (
185
+ request: Request,
186
+ context: LoaderContext & { params: RouteParameters<TRoute> }
187
+ ) => Promise<Response>;
188
+
189
+ /**
190
+ * Strongly typed API module with all HTTP methods
191
+ */
192
+ export type TypedApiModule<TRoute extends string> = {
193
+ GET?: TypedApiHandler<TRoute>;
194
+ POST?: TypedApiHandler<TRoute>;
195
+ PUT?: TypedApiHandler<TRoute>;
196
+ DELETE?: TypedApiHandler<TRoute>;
197
+ PATCH?: TypedApiHandler<TRoute>;
198
+ HEAD?: TypedApiHandler<TRoute>;
199
+ OPTIONS?: TypedApiHandler<TRoute>;
200
+ };
201
+
202
+ // === Route Validation Types ===
203
+
204
+ /**
205
+ * Route pattern validation
206
+ */
207
+ export type ValidRoutePattern<T extends string> = T extends `${string}[${string}]${string}`
208
+ ? T
209
+ : T extends `${string}(${string})${string}`
210
+ ? T
211
+ : T extends `${string}_${string}`
212
+ ? never // Private files/folders
213
+ : T;
214
+
215
+ /**
216
+ * Valid file extensions for routes
217
+ */
218
+ export type ValidRouteExtension = '.tsx' | '.ts' | '.jsx' | '.js';
219
+
220
+ /**
221
+ * Route file path validation
222
+ */
223
+ export type ValidRouteFile<T extends string> = T extends `${string}${ValidRouteExtension}`
224
+ ? T extends `${string}_${string}`
225
+ ? never // Private files
226
+ : T
227
+ : never;
228
+
229
+ // === Component Prop Validation ===
230
+
231
+ /**
232
+ * Validate that a component accepts the correct props for a route
233
+ */
234
+ export type ValidatePageComponent<TRoute extends string, TComponent> = TComponent extends ComponentType<infer TProps>
235
+ ? TProps extends { params: RouteParameters<TRoute> }
236
+ ? TComponent
237
+ : never
238
+ : never;
239
+
240
+ /**
241
+ * Props validation for page components
242
+ */
243
+ export interface PageComponentProps<TRoute extends string = string, TData = unknown> {
244
+ params: RouteParameters<TRoute>;
245
+ query: URLSearchParams;
246
+ data?: TData;
247
+ }
248
+
249
+ /**
250
+ * Validate page component props
251
+ */
252
+ export type ValidatePageProps<TRoute extends string, TProps> = TProps extends PageComponentProps<TRoute>
253
+ ? TProps
254
+ : never;
255
+
256
+ // === Utility Types for Route Analysis ===
257
+
258
+ /**
259
+ * Check if a route has dynamic segments
260
+ */
261
+ export type HasDynamicSegments<T extends string> = T extends `${string}[${string}]${string}` ? true : false;
262
+
263
+ /**
264
+ * Check if a route has catch-all segments
265
+ */
266
+ export type HasCatchAllSegments<T extends string> = T extends `${string}[...${string}]${string}` ? true : false;
267
+
268
+ /**
269
+ * Check if a route has optional segments
270
+ */
271
+ export type HasOptionalSegments<T extends string> = T extends `${string}[[${string}]]${string}` ? true : false;
272
+
273
+ /**
274
+ * Get the static parts of a route
275
+ */
276
+ export type GetStaticParts<T extends string> = T extends `${infer Static}[${string}]${infer Rest}`
277
+ ? Static | GetStaticParts<Rest>
278
+ : T;
279
+
280
+ /**
281
+ * Count dynamic segments in a route
282
+ */
283
+ export type CountDynamicSegments<
284
+ T extends string,
285
+ Count extends readonly unknown[] = []
286
+ > = T extends `${string}[${string}]${infer Rest}` ? CountDynamicSegments<Rest, [...Count, unknown]> : Count['length'];
287
+
288
+ // === Error Handling Types ===
289
+
290
+ /**
291
+ * Route error types
292
+ */
293
+ export type RouteErrorType =
294
+ | 'ROUTE_NOT_FOUND'
295
+ | 'INVALID_PARAMS'
296
+ | 'LOADER_ERROR'
297
+ | 'METADATA_ERROR'
298
+ | 'COMPONENT_ERROR'
299
+ | 'VALIDATION_ERROR';
300
+
301
+ /**
302
+ * Route error with context
303
+ */
304
+ export interface RouteError<TRoute extends string = string> {
305
+ type: RouteErrorType;
306
+ route: TRoute;
307
+ params?: Partial<RouteParameters<TRoute>>;
308
+ message: string;
309
+ originalError?: Error;
310
+ suggestions?: string[];
311
+ }
312
+
313
+ /**
314
+ * Error boundary props for routes
315
+ */
316
+ export interface RouteErrorBoundaryProps<TRoute extends string = string> {
317
+ error: RouteError<TRoute>;
318
+ retry: () => void;
319
+ fallback?: ComponentType<{ error: RouteError<TRoute> }>;
320
+ }
321
+
322
+ // === Development and Debugging Types ===
323
+
324
+ /**
325
+ * Route debugging information
326
+ */
327
+ export interface RouteDebugInfo<TRoute extends string = string> {
328
+ route: TRoute;
329
+ params: RouteParameters<TRoute>;
330
+ staticParts: string[];
331
+ dynamicSegments: string[];
332
+ hasOptionalSegments: HasOptionalSegments<TRoute>;
333
+ hasCatchAllSegments: HasCatchAllSegments<TRoute>;
334
+ priority: number;
335
+ filePath: string;
336
+ }
337
+
338
+ /**
339
+ * Route performance metrics
340
+ */
341
+ export interface RoutePerformanceMetrics {
342
+ discoveryTime: number;
343
+ loadTime: number;
344
+ renderTime: number;
345
+ metadataResolutionTime: number;
346
+ cacheHits: number;
347
+ cacheMisses: number;
348
+ }
349
+
350
+ // === Type Guards and Validation Functions ===
351
+
352
+ /**
353
+ * Type guard for route parameters
354
+ */
355
+ export function isValidRouteParams<TRoute extends string>(
356
+ params: unknown,
357
+ expectedParams: (keyof RouteParameters<TRoute>)[]
358
+ ): params is RouteParameters<TRoute> {
359
+ if (!params || typeof params !== 'object') {
360
+ return false;
361
+ }
362
+
363
+ const paramObj = params as Record<string, unknown>;
364
+
365
+ // Check that all expected parameters are present and are strings or string arrays
366
+ for (const param of expectedParams) {
367
+ const value = paramObj[param as string];
368
+ if (value === undefined) {
369
+ return false;
370
+ }
371
+ if (typeof value !== 'string' && !Array.isArray(value)) {
372
+ return false;
373
+ }
374
+ if (Array.isArray(value) && !value.every(item => typeof item === 'string')) {
375
+ return false;
376
+ }
377
+ }
378
+
379
+ return true;
380
+ }
381
+
382
+ /**
383
+ * Type guard for page component props
384
+ */
385
+ export function isValidPageProps<TRoute extends string>(
386
+ props: unknown,
387
+ expectedParams: (keyof RouteParameters<TRoute>)[]
388
+ ): props is PageComponentProps<TRoute> {
389
+ if (!props || typeof props !== 'object') {
390
+ return false;
391
+ }
392
+
393
+ const propsObj = props as Record<string, unknown>;
394
+
395
+ // Check required properties
396
+ if (!propsObj.params || !propsObj.query) {
397
+ return false;
398
+ }
399
+
400
+ // Validate params
401
+ if (!isValidRouteParams<TRoute>(propsObj.params, expectedParams)) {
402
+ return false;
403
+ }
404
+
405
+ // Validate query
406
+ if (!(propsObj.query instanceof URLSearchParams)) {
407
+ return false;
408
+ }
409
+
410
+ return true;
411
+ }
412
+
413
+ /**
414
+ * Validate route pattern syntax
415
+ */
416
+ export function isValidRoutePattern(pattern: string): boolean {
417
+ // Check for valid bracket syntax
418
+ const bracketRegex = /\[([^\]]+)\]/g;
419
+ const matches = pattern.match(bracketRegex);
420
+
421
+ if (matches) {
422
+ for (const match of matches) {
423
+ const param = match.slice(1, -1); // Remove brackets
424
+
425
+ // Check for valid parameter names
426
+ if (!/^[a-zA-Z_]\w*(\?)?$/.test(param) && !/^\.\.\.[a-zA-Z_]\w*$/.test(param)) {
427
+ return false;
428
+ }
429
+ }
430
+ }
431
+
432
+ // Check for valid route group syntax
433
+ const groupRegex = /\(([^)]+)\)/g;
434
+ const groupMatches = pattern.match(groupRegex);
435
+
436
+ if (groupMatches) {
437
+ for (const match of groupMatches) {
438
+ const group = match.slice(1, -1); // Remove parentheses
439
+
440
+ // Check for valid group names
441
+ if (!/^[a-zA-Z_]\w*-*\w*$/.test(group)) {
442
+ return false;
443
+ }
444
+ }
445
+ }
446
+
447
+ return true;
448
+ }
449
+
450
+ // === Helper Functions for Type Safety ===
451
+
452
+ /**
453
+ * Create a typed page component with parameter validation
454
+ */
455
+ export function createTypedPageComponent<TRoute extends string>(
456
+ component: TypedPageComponent<TRoute>,
457
+ expectedParams: (keyof RouteParameters<TRoute>)[]
458
+ ): TypedPageComponent<TRoute> {
459
+ // Return a wrapper component that validates props
460
+ const WrappedComponent: TypedPageComponent<TRoute> = (props) => {
461
+ if (process.env?.NODE_ENV === 'development') {
462
+ if (!isValidPageProps<TRoute>(props, expectedParams)) {
463
+ console.warn('Invalid props passed to typed page component:', props);
464
+ }
465
+ }
466
+ return h(component, props);
467
+ };
468
+
469
+ return WrappedComponent;
470
+ }
471
+
472
+ /**
473
+ * Create a typed metadata generator with parameter validation
474
+ */
475
+ export function createTypedMetadataGenerator<TRoute extends string>(
476
+ generator: TypedMetadataGenerator<TRoute>,
477
+ expectedParams: (keyof RouteParameters<TRoute>)[]
478
+ ): TypedMetadataGenerator<TRoute> {
479
+ return async params => {
480
+ // Validate params in development
481
+ if (process.env?.NODE_ENV === 'development') {
482
+ if (!isValidRouteParams<TRoute>(params, expectedParams)) {
483
+ console.warn('Invalid params passed to typed metadata generator:', params);
484
+ }
485
+ }
486
+
487
+ return await generator(params);
488
+ };
489
+ }
490
+
491
+ /**
492
+ * Create a typed page loader with parameter validation
493
+ */
494
+ export function createTypedPageLoader<TRoute extends string, TData = unknown>(
495
+ loader: TypedPageLoader<TRoute, TData>,
496
+ expectedParams: (keyof RouteParameters<TRoute>)[]
497
+ ): TypedPageLoader<TRoute, TData> {
498
+ return async context => {
499
+ // Validate params in development
500
+ if (process.env?.NODE_ENV === 'development') {
501
+ if (!isValidRouteParams<TRoute>(context.params, expectedParams)) {
502
+ console.warn('Invalid params passed to typed page loader:', context.params);
503
+ }
504
+ }
505
+
506
+ return await loader(context);
507
+ };
508
+ }
509
+
510
+ /**
511
+ * Validate component accepts correct props for a route
512
+ */
513
+ export function validatePageComponent<TRoute extends string>(
514
+ component: unknown,
515
+ _expectedParams: (keyof RouteParameters<TRoute>)[]
516
+ ): component is TypedPageComponent<TRoute> {
517
+ // In TypeScript, we can't really validate function signatures at runtime
518
+ // This is more of a development-time helper
519
+ if (typeof component !== 'function') {
520
+ return false;
521
+ }
522
+
523
+ // Could add more sophisticated validation in development mode
524
+ return true;
525
+ }
526
+
527
+ /**
528
+ * Create a typed API handler with parameter validation
529
+ */
530
+ export function createTypedApiHandler<TRoute extends string>(
531
+ handler: TypedApiHandler<TRoute>,
532
+ expectedParams: (keyof RouteParameters<TRoute>)[]
533
+ ): TypedApiHandler<TRoute> {
534
+ return async (request, context) => {
535
+ // Validate params in development
536
+ if (process.env?.NODE_ENV === 'development') {
537
+ if (!isValidRouteParams<TRoute>(context.params, expectedParams)) {
538
+ console.warn('Invalid params passed to typed API handler:', context.params);
539
+ }
540
+ }
541
+
542
+ return await handler(request, context);
543
+ };
544
+ }
545
+
546
+ // === Re-export commonly used types ===
547
+ export type {
548
+ RouteParams,
549
+ LoaderContext,
550
+ Metadata,
551
+ PageProps,
552
+ RoutePageModule,
553
+ FileSystemRoute,
554
+ ResolvedMetadata,
555
+ } from '../schemas/routing.ts';