@teyik0/furin 0.2.0-alpha.1 → 0.2.0-alpha.3

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 (84) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +33 -33
  3. package/dist/adapter/static.d.ts +1 -1
  4. package/dist/build/compile-entry.ts +109 -109
  5. package/dist/build/entry-template.ts +156 -156
  6. package/dist/build/index.js +1724 -687
  7. package/dist/build/route-types.d.ts +3 -1
  8. package/dist/build/server-routes-entry.ts +36 -36
  9. package/dist/build/shared.d.ts +1 -0
  10. package/dist/build/ssg-cache.d.ts +1 -1
  11. package/dist/cli/index.js +1673 -658
  12. package/dist/client/link.js +883 -89
  13. package/dist/client/router/index.d.ts +4 -0
  14. package/dist/client/router/link-utils.d.ts +3 -1
  15. package/dist/client/router/navigation.d.ts +10 -0
  16. package/dist/client/router/provider.d.ts +1 -1
  17. package/dist/client/router/search/index.d.ts +18 -0
  18. package/dist/client/router/search-store.d.ts +21 -0
  19. package/dist/client/router/types.d.ts +14 -1
  20. package/dist/client/sync.d.ts +25 -0
  21. package/dist/client.d.ts +1 -0
  22. package/dist/client.js +44 -0
  23. package/dist/furin.d.ts +9 -1
  24. package/dist/furin.js +1578 -258
  25. package/dist/server/auto-invalidate/runtime.d.ts +1 -1
  26. package/dist/server/dev-page-plugin.d.ts +3 -0
  27. package/dist/server/render/assemble.d.ts +2 -0
  28. package/dist/server/render/isr.d.ts +2 -1
  29. package/dist/server/render/ssg.d.ts +3 -2
  30. package/dist/server/render/ssr.d.ts +5 -4
  31. package/dist/server/router/hmr.d.ts +4 -3
  32. package/dist/server/router/index.d.ts +1 -1
  33. package/dist/server/router/index.js +1 -2
  34. package/dist/server/router/index.ts +1 -2
  35. package/dist/server/router/plugin.d.ts +2 -1
  36. package/dist/server/router/schemas.d.ts +25 -17
  37. package/dist/server/sync/config.d.ts +6 -0
  38. package/dist/server/sync/index.d.ts +4 -0
  39. package/dist/server/sync/plugin.d.ts +369 -0
  40. package/dist/server/sync/stream.d.ts +47 -0
  41. package/dist/shared/search-params.d.ts +17 -0
  42. package/package.json +10 -5
  43. package/src/adapter/README.md +13 -13
  44. package/src/adapter/static.ts +23 -6
  45. package/src/build/client.ts +152 -149
  46. package/src/build/compile-entry.ts +109 -109
  47. package/src/build/entry-template.ts +156 -156
  48. package/src/build/hydrate.ts +340 -331
  49. package/src/build/index.ts +152 -152
  50. package/src/build/route-types.ts +145 -115
  51. package/src/build/scan-server.ts +95 -95
  52. package/src/build/server-routes-entry.ts +36 -36
  53. package/src/build/shared.ts +80 -78
  54. package/src/build/ssg-cache.ts +32 -31
  55. package/src/build/types.ts +89 -89
  56. package/src/client/link.tsx +13 -4
  57. package/src/client/router/context.ts +2 -0
  58. package/src/client/router/index.ts +9 -0
  59. package/src/client/router/link-utils.ts +184 -180
  60. package/src/client/router/navigation.ts +61 -0
  61. package/src/client/router/provider.tsx +804 -723
  62. package/src/client/router/search/index.ts +99 -0
  63. package/src/client/router/search-store.ts +86 -0
  64. package/src/client/router/types.ts +276 -263
  65. package/src/client/sync.ts +98 -0
  66. package/src/client.ts +369 -355
  67. package/src/furin.ts +465 -437
  68. package/src/server/auto-invalidate/runtime.ts +134 -133
  69. package/src/server/dev-page-plugin.ts +454 -449
  70. package/src/server/render/assemble.ts +160 -133
  71. package/src/server/render/isr.ts +323 -295
  72. package/src/server/render/not-found.ts +2 -0
  73. package/src/server/render/ssg.ts +15 -4
  74. package/src/server/render/ssr.ts +585 -545
  75. package/src/server/router/hmr.ts +14 -10
  76. package/src/server/router/index.ts +1 -2
  77. package/src/server/router/patterns.ts +5 -0
  78. package/src/server/router/plugin.ts +384 -401
  79. package/src/server/router/schemas.ts +172 -57
  80. package/src/server/sync/config.ts +29 -0
  81. package/src/server/sync/index.ts +14 -0
  82. package/src/server/sync/plugin.ts +108 -0
  83. package/src/server/sync/stream.ts +144 -0
  84. package/src/shared/search-params.ts +182 -0
package/src/client.ts CHANGED
@@ -1,355 +1,369 @@
1
- /*
2
- biome-ignore-all lint/complexity/noBannedTypes: The fundamental problem is that
3
- `Record<string, unknown>` requires an index signature, and any type without one
4
- (like `{}`, `object`, or a named interface) won't satisfy it. But `{}` is the only type that:
5
- 1. Satisfies `Record<string, unknown>` as a generic default (TS special-cases `{}`)
6
- 2. Doesn't have an index signature (so unknown prop access errors)
7
- 3. Is transparent in intersections(`{} & T = T`)
8
- */
9
-
10
- import type { Cookie, StatusMap } from "elysia";
11
- import type { AnySchema, HTTPHeaders, UnwrapSchema } from "elysia/types";
12
- import type { RequestLogger } from "evlog";
13
-
14
- // biome-ignore lint/performance/noBarrelFile: client.ts is the canonical DX entry for furin/client consumers — not a barrel
15
- export { Await, useAsyncError, useAsyncValue } from "./shared/await.tsx";
16
-
17
- declare const UNSET: unique symbol;
18
- type Unset = typeof UNSET;
19
-
20
- // Expands interfaces into plain mapped types so they satisfy Record<string, unknown>.
21
- // Interfaces lack an implicit index signature; mapped types have one.
22
- type ToRecord<T> = { [K in keyof T]: T[K] };
23
-
24
- type ResolvedSchema<T> = [T] extends [Unset]
25
- ? Unset
26
- : T extends AnySchema
27
- ? UnwrapSchema<T>
28
- : Unset;
29
-
30
- type MergeSchema<TParent, TOwn> = [TParent] extends [Unset]
31
- ? TOwn
32
- : [TOwn] extends [Unset]
33
- ? TParent
34
- : TParent & TOwn;
35
-
36
- type NormalizeUnset<T> = [T] extends [Unset] ? {} : T;
37
-
38
- /**
39
- * Transforms each key of a parent-data record into an individual `Promise<T>`.
40
- * This is what child loaders see for inherited fields — direct values from
41
- * `RouteContext` (request, params, set, …) remain unchanged.
42
- *
43
- * `Awaited<T[K]>` mirrors the JS Promise-chaining auto-flatten used by
44
- * `createLoaderCtx`: when an ancestor wrapped a field with `defer()` (so the
45
- * raw value is itself a `Promise<U>`), the type collapses to `Promise<U>`
46
- * instead of `Promise<Promise<U>>`. Otherwise TypeScript would force a
47
- * redundant `await await ctx.field` dance.
48
- */
49
- type PromisifyData<T extends Record<string, unknown>> = {
50
- [K in keyof T]: Promise<Awaited<T[K]>>;
51
- };
52
-
53
- export interface RouteContext<TParams = {}, TQuery = {}> {
54
- cookie: Record<string, Cookie<unknown>>;
55
- headers: Record<string, string | undefined>;
56
- /** Request-scoped logger. Call `log.set({})` to attach fields to the wide event for this render. */
57
- log: RequestLogger;
58
- params: NormalizeUnset<TParams>;
59
- path: string;
60
- query: NormalizeUnset<TQuery>;
61
- redirect: (url: string, status?: 301 | 302 | 303 | 307 | 308) => Response;
62
- request: Request;
63
- set: {
64
- headers: HTTPHeaders;
65
- status?: number | keyof StatusMap;
66
- };
67
- }
68
-
69
- export interface ComponentProps<TParams = {}, TQuery = {}> {
70
- params: NormalizeUnset<TParams>;
71
- path: string;
72
- query: NormalizeUnset<TQuery>;
73
- }
74
-
75
- type ResolveParent<T> =
76
- T extends RouteRef<infer D, infer P, infer Q>
77
- ? { data: D; params: P; query: Q }
78
- : { data: {}; params: Unset; query: Unset };
79
-
80
- interface Resolved<TParentRef, TLoaderData, TParamsSchema = Unset, TQuerySchema = Unset> {
81
- // `Omit<TLoaderData, "__isDeferred">` strips the runtime brand that `defer()`
82
- // attaches to its return value. A layout/route loader wrapped with `defer()`
83
- // would otherwise leak `__isDeferred: true` into descendant loader contexts
84
- // and component props.
85
- data: ToRecord<ResolveParent<TParentRef>["data"] & Omit<TLoaderData, "__isDeferred">>;
86
- params: MergeSchema<ResolveParent<TParentRef>["params"], ResolvedSchema<TParamsSchema>>;
87
- query: MergeSchema<ResolveParent<TParentRef>["query"], ResolvedSchema<TQuerySchema>>;
88
- }
89
-
90
- export type MetaDescriptor =
91
- | { charSet: "utf-8" }
92
- | { title: string }
93
- | { name: string; content: string }
94
- | { property: string; content: string }
95
- | { httpEquiv: string; content: string }
96
- | { "script:ld+json": object }
97
- | { tagName: "meta" | "link"; [name: string]: string | undefined };
98
-
99
- export interface HeadOptions {
100
- links?: Array<{ rel: string; href: string; [key: string]: string }>;
101
- meta?: MetaDescriptor[];
102
- /**
103
- * Inline scripts injected into `<head>`.
104
- *
105
- * **Security warning:** `children` is injected as raw HTML — never pass
106
- * user-controlled or loader-derived data here without sanitisation.
107
- */
108
- scripts?: Array<{
109
- src?: string;
110
- type?: string;
111
- children?: string;
112
- [key: string]: string | undefined;
113
- }>;
114
- /**
115
- * Inline styles injected into `<head>`.
116
- *
117
- * **Security warning:** `children` is injected as raw HTML — never pass
118
- * user-controlled or loader-derived data here without sanitisation.
119
- */
120
- styles?: Array<{ type?: string; children: string }>;
121
- }
122
-
123
- // Extracts the resolved return type from a loader function type.
124
- // Using ReturnType + Awaited here means TLoader is inferred as the whole function
125
- // first, and then we extract the data — so inference is order-independent.
126
- // The `__isDeferred` key (DEFERRED_BRAND, see below) is stripped: a `defer()`
127
- // return carries that runtime marker, but it must never surface as a
128
- // component / head() prop — it is an internal implementation detail.
129
- type ExtractLoaderReturn<TLoader> = TLoader extends (...args: never[]) => unknown
130
- ? Awaited<ReturnType<TLoader>> extends object
131
- ? ToRecord<Omit<Awaited<ReturnType<TLoader>>, "__isDeferred">>
132
- : {}
133
- : {};
134
-
135
- export interface PageConfig<
136
- TParentData extends Record<string, unknown>,
137
- TParams,
138
- TQuery,
139
- TPageLoaderData extends object = {},
140
- > {
141
- component: React.FC<TParentData & TPageLoaderData & ComponentProps<TParams, TQuery>>;
142
- head?: (ctx: ComponentProps<TParams, TQuery> & TParentData & TPageLoaderData) => HeadOptions;
143
- loader?: (
144
- ctx: RouteContext<TParams, TQuery> & PromisifyData<TParentData>
145
- ) => Promise<TPageLoaderData> | TPageLoaderData;
146
- staticParams?: () => Promise<NormalizeUnset<TParams>[]> | NormalizeUnset<TParams>[];
147
- tags?: string[];
148
- }
149
-
150
- export interface RuntimeRoute {
151
- __type: "FURIN_ROUTE";
152
- layout?: React.FC<Record<string, unknown> & { children: React.ReactNode }>;
153
- loader?(ctx: Record<string, unknown>): Promise<Record<string, unknown>> | Record<string, unknown>;
154
- mode?: "ssr" | "ssg" | "isr";
155
- params?: unknown;
156
- parent?: RuntimeRoute;
157
- query?: unknown;
158
- revalidate?: number;
159
- tags?: string[];
160
- }
161
-
162
- export interface RuntimePage {
163
- __type: "FURIN_PAGE";
164
- _route: RuntimeRoute;
165
- component: React.FC<Record<string, unknown>>;
166
- head?(ctx: Record<string, unknown>): HeadOptions;
167
- loader?(ctx: Record<string, unknown>): Promise<Record<string, unknown>> | Record<string, unknown>;
168
- staticParams?(): Promise<Record<string, string>[]> | Record<string, string>[];
169
- tags?: string[];
170
- }
171
-
172
- export interface RouteRef<
173
- TData extends Record<string, unknown> = Record<string, unknown>,
174
- TParams = unknown,
175
- TQuery = unknown,
176
- > {
177
- readonly __brand: "FURIN_ROUTE_REF";
178
- readonly __phantom: { data: TData; params: TParams; query: TQuery };
179
- }
180
-
181
- interface PageResult<
182
- TData extends Record<string, unknown>,
183
- TParams,
184
- TQuery,
185
- TPageLoaderData extends object,
186
- > {
187
- __type: "FURIN_PAGE";
188
- _route: Route<TData, TParams, TQuery>;
189
- component: React.FC<TData & TPageLoaderData & ComponentProps<TParams, TQuery>>;
190
- head?: (ctx: ComponentProps<TParams, TQuery> & TData & TPageLoaderData) => HeadOptions;
191
- loader?: (
192
- ctx: RouteContext<TParams, TQuery> & PromisifyData<TData>
193
- ) => Promise<TPageLoaderData> | TPageLoaderData;
194
- tags?: string[];
195
- }
196
-
197
- export interface Route<TParentData extends Record<string, unknown>, TParams, TQuery> {
198
- __type: "FURIN_ROUTE";
199
- layout?: React.FC<TParentData & { children: React.ReactNode } & ComponentProps<TParams, TQuery>>;
200
- loader?(
201
- ctx: RouteContext<TParams, TQuery> & PromisifyData<TParentData>
202
- ): Promise<TParentData> | TParentData;
203
- mode?: "ssr" | "ssg" | "isr";
204
-
205
- // Overload 1 — loader present (required).
206
- // Two type params: TLoader is inferred solely from the `loader` position; TPageLoaderData
207
- // has no inference sites (all NoInfer) so TypeScript applies its default AFTER TLoader is
208
- // resolved — making declaration order of head/component irrelevant.
209
- page<
210
- TLoader extends (ctx: RouteContext<TParams, TQuery> & PromisifyData<TParentData>) => unknown,
211
- TPageLoaderData extends object = ExtractLoaderReturn<TLoader>,
212
- >(config: {
213
- loader: TLoader;
214
- mode?: "ssr" | "ssg" | "isr";
215
- revalidate?: number;
216
- staticParams?: () => Promise<NormalizeUnset<TParams>[]> | NormalizeUnset<TParams>[];
217
- tags?: string[];
218
- component: React.FC<NoInfer<TParentData & TPageLoaderData & ComponentProps<TParams, TQuery>>>;
219
- head?: (
220
- ctx: NoInfer<ComponentProps<TParams, TQuery> & TParentData & TPageLoaderData>
221
- ) => HeadOptions;
222
- }): PageResult<TParentData, TParams, TQuery, TPageLoaderData>;
223
-
224
- // Overload 2 — no loader.
225
- page(config: {
226
- mode?: "ssr" | "ssg" | "isr";
227
- revalidate?: number;
228
- staticParams?: () => Promise<NormalizeUnset<TParams>[]> | NormalizeUnset<TParams>[];
229
- tags?: string[];
230
- component: React.FC<TParentData & ComponentProps<TParams, TQuery>>;
231
- head?: (ctx: ComponentProps<TParams, TQuery> & TParentData) => HeadOptions;
232
- }): PageResult<TParentData, TParams, TQuery, {}>;
233
-
234
- params?: unknown;
235
- parent?: RuntimeRoute;
236
- query?: unknown;
237
-
238
- /** Branded ref for type inference when used as a parent. */
239
- ref: RouteRef<TParentData, TParams, TQuery>;
240
- revalidate?: number;
241
- tags?: string[];
242
- }
243
-
244
- export function createRoute<
245
- TParentRef extends RouteRef | undefined = undefined,
246
- TParamsSchema extends AnySchema | Unset = Unset,
247
- TQuerySchema extends AnySchema | Unset = Unset,
248
- TLoaderData extends object = {},
249
- >(config?: {
250
- parent?: { ref: TParentRef } & { __type: "FURIN_ROUTE" };
251
- mode?: "ssr" | "ssg" | "isr";
252
- revalidate?: number;
253
- params?: TParamsSchema;
254
- query?: TQuerySchema;
255
- tags?: string[];
256
- loader?: (
257
- ctx: RouteContext<
258
- Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["params"],
259
- Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["query"]
260
- > &
261
- PromisifyData<ResolveParent<TParentRef>["data"]>
262
- ) => Promise<TLoaderData> | TLoaderData;
263
- layout?: React.FC<
264
- Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["data"] & {
265
- children: React.ReactNode;
266
- } & ComponentProps<
267
- Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["params"],
268
- Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["query"]
269
- >
270
- >;
271
- }): Route<
272
- Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["data"],
273
- Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["params"],
274
- Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["query"]
275
- > {
276
- type R = Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>;
277
-
278
- const route = {
279
- ...config,
280
- __type: "FURIN_ROUTE" as const,
281
- ref: {} as RouteRef<R["data"], R["params"], R["query"]>,
282
-
283
- // biome-ignore lint/suspicious/noExplicitAny: implementation signature for both overloads
284
- page(pageConfig: any) {
285
- return {
286
- ...pageConfig,
287
- __type: "FURIN_PAGE" as const,
288
- _route: route,
289
- };
290
- },
291
- };
292
- return route as Route<R["data"], R["params"], R["query"]>;
293
- }
294
-
295
- export type InferProps<T> = T extends {
296
- __type: "FURIN_PAGE";
297
- component: React.FC<infer P>;
298
- }
299
- ? P
300
- : T extends Route<infer D, infer P, infer Q>
301
- ? D & { children: React.ReactNode } & ComponentProps<P, Q>
302
- : never;
303
-
304
- // ── Deferred data ──────────────────────────────────────────────────────────────
305
-
306
- /**
307
- * Brand key that marks an object as deferred. Using a `const` string avoids
308
- * a `unique symbol` that would complicate cross-module inference.
309
- */
310
- const DEFERRED_BRAND = "__isDeferred" as const;
311
-
312
- /**
313
- * A loader return value that contains a mix of synchronous scalar fields and
314
- * lazy `Promise<T>` fields. Scalar fields are serialised into the initial HTML
315
- * shell; Promise fields are streamed as late `<script>` resolution chunks.
316
- *
317
- * @example
318
- * loader: () => defer({
319
- * title: "My Board", // synchronous — available immediately
320
- * stats: fetchStats(), // Promise — streamed when it resolves
321
- * })
322
- */
323
- export type DeferredData<T extends Record<string, unknown>> = T & {
324
- readonly [DEFERRED_BRAND]: true;
325
- };
326
-
327
- /**
328
- * Wraps loader data so that Promise-valued fields are streamed lazily while
329
- * scalar fields are embedded in the initial HTML shell immediately.
330
- *
331
- * Use in any loader — page (`route.page({ loader })`) or route/layout
332
- * (`createRoute({ loader })`). Promise-valued fields are streamed lazily;
333
- * scalar fields are embedded in the initial HTML shell.
334
- */
335
- export function defer<T extends Record<string, unknown>>(data: T): DeferredData<T> {
336
- if (Object.hasOwn(data, DEFERRED_BRAND)) {
337
- throw new Error(
338
- `[furin] defer() received an object with a reserved key "${DEFERRED_BRAND}". Rename this field to avoid conflicts with the deferred-data runtime.`
339
- );
340
- }
341
- return { ...data, [DEFERRED_BRAND]: true } as DeferredData<T>;
342
- }
343
-
344
- /**
345
- * Type guard for DeferredData. Used by the render pipeline to distinguish a
346
- * plain loader return from a deferred one.
347
- */
348
- export function isDeferred(v: unknown): v is DeferredData<Record<string, unknown>> {
349
- return (
350
- typeof v === "object" &&
351
- v !== null &&
352
- Object.hasOwn(v, DEFERRED_BRAND) &&
353
- (v as Record<typeof DEFERRED_BRAND, unknown>)[DEFERRED_BRAND] === true
354
- );
355
- }
1
+ /*
2
+ biome-ignore-all lint/complexity/noBannedTypes: The fundamental problem is that
3
+ `Record<string, unknown>` requires an index signature, and any type without one
4
+ (like `{}`, `object`, or a named interface) won't satisfy it. But `{}` is the only type that:
5
+ 1. Satisfies `Record<string, unknown>` as a generic default (TS special-cases `{}`)
6
+ 2. Doesn't have an index signature (so unknown prop access errors)
7
+ 3. Is transparent in intersections(`{} & T = T`)
8
+ */
9
+ /*
10
+ biome-ignore-all lint/performance/noBarrelFile: client.ts is the canonical DX
11
+ entry for furin/client consumers, not a generic internal barrel.
12
+ */
13
+
14
+ import type { Cookie, StatusMap } from "elysia";
15
+ import type { AnySchema, HTTPHeaders, UnwrapSchema } from "elysia/types";
16
+ import type { RequestLogger } from "evlog";
17
+
18
+ export {
19
+ type SyncMutation,
20
+ type SyncMutationContext,
21
+ type SyncMutationErrorContext,
22
+ type SyncMutationHeaders,
23
+ type SyncMutationOptions,
24
+ type SyncMutationRunner,
25
+ type SyncMutationSuccessContext,
26
+ type UseSyncOptions,
27
+ useSync,
28
+ } from "./client/sync.ts";
29
+ export { Await, useAsyncError, useAsyncValue } from "./shared/await.tsx";
30
+
31
+ declare const UNSET: unique symbol;
32
+ type Unset = typeof UNSET;
33
+
34
+ // Expands interfaces into plain mapped types so they satisfy Record<string, unknown>.
35
+ // Interfaces lack an implicit index signature; mapped types have one.
36
+ type ToRecord<T> = { [K in keyof T]: T[K] };
37
+
38
+ type ResolvedSchema<T> = [T] extends [Unset]
39
+ ? Unset
40
+ : T extends AnySchema
41
+ ? UnwrapSchema<T>
42
+ : Unset;
43
+
44
+ type MergeSchema<TParent, TOwn> = [TParent] extends [Unset]
45
+ ? TOwn
46
+ : [TOwn] extends [Unset]
47
+ ? TParent
48
+ : TParent & TOwn;
49
+
50
+ type NormalizeUnset<T> = [T] extends [Unset] ? {} : T;
51
+
52
+ /**
53
+ * Transforms each key of a parent-data record into an individual `Promise<T>`.
54
+ * This is what child loaders see for inherited fields — direct values from
55
+ * `RouteContext` (request, params, set, …) remain unchanged.
56
+ *
57
+ * `Awaited<T[K]>` mirrors the JS Promise-chaining auto-flatten used by
58
+ * `createLoaderCtx`: when an ancestor wrapped a field with `defer()` (so the
59
+ * raw value is itself a `Promise<U>`), the type collapses to `Promise<U>`
60
+ * instead of `Promise<Promise<U>>`. Otherwise TypeScript would force a
61
+ * redundant `await await ctx.field` dance.
62
+ */
63
+ type PromisifyData<T extends Record<string, unknown>> = {
64
+ [K in keyof T]: Promise<Awaited<T[K]>>;
65
+ };
66
+
67
+ export interface RouteContext<TParams = {}, TQuery = {}> {
68
+ cookie: Record<string, Cookie<unknown>>;
69
+ headers: Record<string, string | undefined>;
70
+ /** Request-scoped logger. Call `log.set({})` to attach fields to the wide event for this render. */
71
+ log: RequestLogger;
72
+ params: NormalizeUnset<TParams>;
73
+ path: string;
74
+ query: NormalizeUnset<TQuery>;
75
+ redirect: (url: string, status?: 301 | 302 | 303 | 307 | 308) => Response;
76
+ request: Request;
77
+ set: {
78
+ headers: HTTPHeaders;
79
+ status?: number | keyof StatusMap;
80
+ };
81
+ }
82
+
83
+ export interface ComponentProps<TParams = {}, TQuery = {}> {
84
+ params: NormalizeUnset<TParams>;
85
+ path: string;
86
+ query: NormalizeUnset<TQuery>;
87
+ }
88
+
89
+ type ResolveParent<T> =
90
+ T extends RouteRef<infer D, infer P, infer Q>
91
+ ? { data: D; params: P; query: Q }
92
+ : { data: {}; params: Unset; query: Unset };
93
+
94
+ interface Resolved<TParentRef, TLoaderData, TParamsSchema = Unset, TQuerySchema = Unset> {
95
+ // `Omit<TLoaderData, "__isDeferred">` strips the runtime brand that `defer()`
96
+ // attaches to its return value. A layout/route loader wrapped with `defer()`
97
+ // would otherwise leak `__isDeferred: true` into descendant loader contexts
98
+ // and component props.
99
+ data: ToRecord<ResolveParent<TParentRef>["data"] & Omit<TLoaderData, "__isDeferred">>;
100
+ params: MergeSchema<ResolveParent<TParentRef>["params"], ResolvedSchema<TParamsSchema>>;
101
+ query: MergeSchema<ResolveParent<TParentRef>["query"], ResolvedSchema<TQuerySchema>>;
102
+ }
103
+
104
+ export type MetaDescriptor =
105
+ | { charSet: "utf-8" }
106
+ | { title: string }
107
+ | { name: string; content: string }
108
+ | { property: string; content: string }
109
+ | { httpEquiv: string; content: string }
110
+ | { "script:ld+json": object }
111
+ | { tagName: "meta" | "link"; [name: string]: string | undefined };
112
+
113
+ export interface HeadOptions {
114
+ links?: Array<{ rel: string; href: string; [key: string]: string }>;
115
+ meta?: MetaDescriptor[];
116
+ /**
117
+ * Inline scripts injected into `<head>`.
118
+ *
119
+ * **Security warning:** `children` is injected as raw HTML — never pass
120
+ * user-controlled or loader-derived data here without sanitisation.
121
+ */
122
+ scripts?: Array<{
123
+ src?: string;
124
+ type?: string;
125
+ children?: string;
126
+ [key: string]: string | undefined;
127
+ }>;
128
+ /**
129
+ * Inline styles injected into `<head>`.
130
+ *
131
+ * **Security warning:** `children` is injected as raw HTML — never pass
132
+ * user-controlled or loader-derived data here without sanitisation.
133
+ */
134
+ styles?: Array<{ type?: string; children: string }>;
135
+ }
136
+
137
+ // Extracts the resolved return type from a loader function type.
138
+ // Using ReturnType + Awaited here means TLoader is inferred as the whole function
139
+ // first, and then we extract the data — so inference is order-independent.
140
+ // The `__isDeferred` key (DEFERRED_BRAND, see below) is stripped: a `defer()`
141
+ // return carries that runtime marker, but it must never surface as a
142
+ // component / head() prop — it is an internal implementation detail.
143
+ type ExtractLoaderReturn<TLoader> = TLoader extends (...args: never[]) => unknown
144
+ ? Awaited<ReturnType<TLoader>> extends object
145
+ ? ToRecord<Omit<Awaited<ReturnType<TLoader>>, "__isDeferred">>
146
+ : {}
147
+ : {};
148
+
149
+ export interface PageConfig<
150
+ TParentData extends Record<string, unknown>,
151
+ TParams,
152
+ TQuery,
153
+ TPageLoaderData extends object = {},
154
+ > {
155
+ component: React.FC<TParentData & TPageLoaderData & ComponentProps<TParams, TQuery>>;
156
+ head?: (ctx: ComponentProps<TParams, TQuery> & TParentData & TPageLoaderData) => HeadOptions;
157
+ loader?: (
158
+ ctx: RouteContext<TParams, TQuery> & PromisifyData<TParentData>
159
+ ) => Promise<TPageLoaderData> | TPageLoaderData;
160
+ staticParams?: () => Promise<NormalizeUnset<TParams>[]> | NormalizeUnset<TParams>[];
161
+ tags?: string[];
162
+ }
163
+
164
+ export interface RuntimeRoute {
165
+ __type: "FURIN_ROUTE";
166
+ layout?: React.FC<Record<string, unknown> & { children: React.ReactNode }>;
167
+ loader?(ctx: Record<string, unknown>): Promise<Record<string, unknown>> | Record<string, unknown>;
168
+ mode?: "ssr" | "ssg" | "isr";
169
+ params?: unknown;
170
+ parent?: RuntimeRoute;
171
+ query?: unknown;
172
+ revalidate?: number;
173
+ tags?: string[];
174
+ }
175
+
176
+ export interface RuntimePage {
177
+ __type: "FURIN_PAGE";
178
+ _route: RuntimeRoute;
179
+ component: React.FC<Record<string, unknown>>;
180
+ head?(ctx: Record<string, unknown>): HeadOptions;
181
+ loader?(ctx: Record<string, unknown>): Promise<Record<string, unknown>> | Record<string, unknown>;
182
+ staticParams?(): Promise<Record<string, string>[]> | Record<string, string>[];
183
+ tags?: string[];
184
+ }
185
+
186
+ export interface RouteRef<
187
+ TData extends Record<string, unknown> = Record<string, unknown>,
188
+ TParams = unknown,
189
+ TQuery = unknown,
190
+ > {
191
+ readonly __brand: "FURIN_ROUTE_REF";
192
+ readonly __phantom: { data: TData; params: TParams; query: TQuery };
193
+ }
194
+
195
+ interface PageResult<
196
+ TData extends Record<string, unknown>,
197
+ TParams,
198
+ TQuery,
199
+ TPageLoaderData extends object,
200
+ > {
201
+ __type: "FURIN_PAGE";
202
+ _route: Route<TData, TParams, TQuery>;
203
+ component: React.FC<TData & TPageLoaderData & ComponentProps<TParams, TQuery>>;
204
+ head?: (ctx: ComponentProps<TParams, TQuery> & TData & TPageLoaderData) => HeadOptions;
205
+ loader?: (
206
+ ctx: RouteContext<TParams, TQuery> & PromisifyData<TData>
207
+ ) => Promise<TPageLoaderData> | TPageLoaderData;
208
+ tags?: string[];
209
+ }
210
+
211
+ export interface Route<TParentData extends Record<string, unknown>, TParams, TQuery> {
212
+ __type: "FURIN_ROUTE";
213
+ layout?: React.FC<TParentData & { children: React.ReactNode } & ComponentProps<TParams, TQuery>>;
214
+ loader?(
215
+ ctx: RouteContext<TParams, TQuery> & PromisifyData<TParentData>
216
+ ): Promise<TParentData> | TParentData;
217
+ mode?: "ssr" | "ssg" | "isr";
218
+
219
+ // Overload 1 — loader present (required).
220
+ // Two type params: TLoader is inferred solely from the `loader` position; TPageLoaderData
221
+ // has no inference sites (all NoInfer) so TypeScript applies its default AFTER TLoader is
222
+ // resolved — making declaration order of head/component irrelevant.
223
+ page<
224
+ TLoader extends (ctx: RouteContext<TParams, TQuery> & PromisifyData<TParentData>) => unknown,
225
+ TPageLoaderData extends object = ExtractLoaderReturn<TLoader>,
226
+ >(config: {
227
+ loader: TLoader;
228
+ mode?: "ssr" | "ssg" | "isr";
229
+ revalidate?: number;
230
+ staticParams?: () => Promise<NormalizeUnset<TParams>[]> | NormalizeUnset<TParams>[];
231
+ tags?: string[];
232
+ component: React.FC<NoInfer<TParentData & TPageLoaderData & ComponentProps<TParams, TQuery>>>;
233
+ head?: (
234
+ ctx: NoInfer<ComponentProps<TParams, TQuery> & TParentData & TPageLoaderData>
235
+ ) => HeadOptions;
236
+ }): PageResult<TParentData, TParams, TQuery, TPageLoaderData>;
237
+
238
+ // Overload 2 — no loader.
239
+ page(config: {
240
+ mode?: "ssr" | "ssg" | "isr";
241
+ revalidate?: number;
242
+ staticParams?: () => Promise<NormalizeUnset<TParams>[]> | NormalizeUnset<TParams>[];
243
+ tags?: string[];
244
+ component: React.FC<TParentData & ComponentProps<TParams, TQuery>>;
245
+ head?: (ctx: ComponentProps<TParams, TQuery> & TParentData) => HeadOptions;
246
+ }): PageResult<TParentData, TParams, TQuery, {}>;
247
+
248
+ params?: unknown;
249
+ parent?: RuntimeRoute;
250
+ query?: unknown;
251
+
252
+ /** Branded ref for type inference when used as a parent. */
253
+ ref: RouteRef<TParentData, TParams, TQuery>;
254
+ revalidate?: number;
255
+ tags?: string[];
256
+ }
257
+
258
+ export function createRoute<
259
+ TParentRef extends RouteRef | undefined = undefined,
260
+ TParamsSchema extends AnySchema | Unset = Unset,
261
+ TQuerySchema extends AnySchema | Unset = Unset,
262
+ TLoaderData extends object = {},
263
+ >(config?: {
264
+ parent?: { ref: TParentRef } & { __type: "FURIN_ROUTE" };
265
+ mode?: "ssr" | "ssg" | "isr";
266
+ revalidate?: number;
267
+ params?: TParamsSchema;
268
+ query?: TQuerySchema;
269
+ tags?: string[];
270
+ loader?: (
271
+ ctx: RouteContext<
272
+ Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["params"],
273
+ Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["query"]
274
+ > &
275
+ PromisifyData<ResolveParent<TParentRef>["data"]>
276
+ ) => Promise<TLoaderData> | TLoaderData;
277
+ layout?: React.FC<
278
+ Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["data"] & {
279
+ children: React.ReactNode;
280
+ } & ComponentProps<
281
+ Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["params"],
282
+ Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["query"]
283
+ >
284
+ >;
285
+ }): Route<
286
+ Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["data"],
287
+ Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["params"],
288
+ Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>["query"]
289
+ > {
290
+ type R = Resolved<TParentRef, TLoaderData, TParamsSchema, TQuerySchema>;
291
+
292
+ const route = {
293
+ ...config,
294
+ __type: "FURIN_ROUTE" as const,
295
+ ref: {} as RouteRef<R["data"], R["params"], R["query"]>,
296
+
297
+ // biome-ignore lint/suspicious/noExplicitAny: implementation signature for both overloads
298
+ page(pageConfig: any) {
299
+ return {
300
+ ...pageConfig,
301
+ __type: "FURIN_PAGE" as const,
302
+ _route: route,
303
+ };
304
+ },
305
+ };
306
+ return route as Route<R["data"], R["params"], R["query"]>;
307
+ }
308
+
309
+ export type InferProps<T> = T extends {
310
+ __type: "FURIN_PAGE";
311
+ component: React.FC<infer P>;
312
+ }
313
+ ? P
314
+ : T extends Route<infer D, infer P, infer Q>
315
+ ? D & { children: React.ReactNode } & ComponentProps<P, Q>
316
+ : never;
317
+
318
+ // ── Deferred data ──────────────────────────────────────────────────────────────
319
+
320
+ /**
321
+ * Brand key that marks an object as deferred. Using a `const` string avoids
322
+ * a `unique symbol` that would complicate cross-module inference.
323
+ */
324
+ const DEFERRED_BRAND = "__isDeferred" as const;
325
+
326
+ /**
327
+ * A loader return value that contains a mix of synchronous scalar fields and
328
+ * lazy `Promise<T>` fields. Scalar fields are serialised into the initial HTML
329
+ * shell; Promise fields are streamed as late `<script>` resolution chunks.
330
+ *
331
+ * @example
332
+ * loader: () => defer({
333
+ * title: "My Board", // synchronous — available immediately
334
+ * stats: fetchStats(), // Promise — streamed when it resolves
335
+ * })
336
+ */
337
+ export type DeferredData<T extends Record<string, unknown>> = T & {
338
+ readonly [DEFERRED_BRAND]: true;
339
+ };
340
+
341
+ /**
342
+ * Wraps loader data so that Promise-valued fields are streamed lazily while
343
+ * scalar fields are embedded in the initial HTML shell immediately.
344
+ *
345
+ * Use in any loader — page (`route.page({ loader })`) or route/layout
346
+ * (`createRoute({ loader })`). Promise-valued fields are streamed lazily;
347
+ * scalar fields are embedded in the initial HTML shell.
348
+ */
349
+ export function defer<T extends Record<string, unknown>>(data: T): DeferredData<T> {
350
+ if (Object.hasOwn(data, DEFERRED_BRAND)) {
351
+ throw new Error(
352
+ `[furin] defer() received an object with a reserved key "${DEFERRED_BRAND}". Rename this field to avoid conflicts with the deferred-data runtime.`
353
+ );
354
+ }
355
+ return { ...data, [DEFERRED_BRAND]: true } as DeferredData<T>;
356
+ }
357
+
358
+ /**
359
+ * Type guard for DeferredData. Used by the render pipeline to distinguish a
360
+ * plain loader return from a deferred one.
361
+ */
362
+ export function isDeferred(v: unknown): v is DeferredData<Record<string, unknown>> {
363
+ return (
364
+ typeof v === "object" &&
365
+ v !== null &&
366
+ Object.hasOwn(v, DEFERRED_BRAND) &&
367
+ (v as Record<typeof DEFERRED_BRAND, unknown>)[DEFERRED_BRAND] === true
368
+ );
369
+ }