@bractjs/bractjs 0.1.27 → 0.1.29

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 (117) hide show
  1. package/bin/cli.ts +18 -1
  2. package/package.json +3 -2
  3. package/src/__tests__/codegen-write.test.ts +67 -0
  4. package/src/__tests__/codegen.test.ts +29 -2
  5. package/src/__tests__/compile-safety.test.ts +4 -0
  6. package/src/__tests__/csp.test.ts +10 -0
  7. package/src/__tests__/define-actions.test.ts +69 -0
  8. package/src/__tests__/env.test.ts +18 -0
  9. package/src/__tests__/fetcher-store.test.ts +67 -0
  10. package/src/__tests__/fixtures/app/root.tsx +7 -2
  11. package/src/__tests__/fixtures/app/routes/boom.tsx +9 -0
  12. package/src/__tests__/fixtures/app/routes/client-only.tsx +16 -0
  13. package/src/__tests__/fixtures/app/routes/counter.tsx +16 -0
  14. package/src/__tests__/fixtures/app/routes/data-only.tsx +16 -0
  15. package/src/__tests__/fixtures/app/routes/features-demo.tsx +28 -0
  16. package/src/__tests__/fixtures/app/routes/intent-demo.tsx +46 -0
  17. package/src/__tests__/fixtures/app/routes/protected-client-only.tsx +15 -0
  18. package/src/__tests__/fixtures/app/routes/search-demo.tsx +39 -0
  19. package/src/__tests__/form-data-helpers.test.ts +43 -0
  20. package/src/__tests__/headers.test.ts +111 -0
  21. package/src/__tests__/integration.test.ts +90 -0
  22. package/src/__tests__/layout-registry.test.ts +7 -3
  23. package/src/__tests__/loader.test.ts +32 -1
  24. package/src/__tests__/matcher.test.ts +29 -0
  25. package/src/__tests__/module-registry.test.ts +2 -3
  26. package/src/__tests__/nav-utils.test.ts +46 -0
  27. package/src/__tests__/prerender.test.ts +102 -0
  28. package/src/__tests__/programmatic-api.test.ts +20 -1
  29. package/src/__tests__/revalidation.test.ts +65 -0
  30. package/src/__tests__/route-lint.test.ts +79 -0
  31. package/src/__tests__/route-middleware.test.ts +84 -0
  32. package/src/__tests__/route-table.test.ts +33 -0
  33. package/src/__tests__/safe-validate.test.ts +96 -0
  34. package/src/__tests__/scanner.test.ts +46 -1
  35. package/src/__tests__/scroll-restoration.test.ts +66 -0
  36. package/src/__tests__/search-serializer.test.ts +42 -0
  37. package/src/__tests__/search-validation.test.ts +125 -0
  38. package/src/__tests__/security-fixes.test.ts +201 -0
  39. package/src/__tests__/security.test.ts +110 -1
  40. package/src/__tests__/selective-ssr.test.ts +85 -0
  41. package/src/__tests__/spa-mode.test.ts +77 -0
  42. package/src/__tests__/typed-routing.test.ts +51 -1
  43. package/src/__tests__/use-matches.test.ts +54 -0
  44. package/src/build/bundler.ts +33 -0
  45. package/src/build/prerender.ts +88 -0
  46. package/src/build/route-lint.ts +49 -0
  47. package/src/client/ClientRouter.tsx +339 -47
  48. package/src/client/cache.ts +8 -0
  49. package/src/client/components/Await.tsx +9 -2
  50. package/src/client/components/Form.tsx +23 -34
  51. package/src/client/components/Link.tsx +80 -9
  52. package/src/client/components/Outlet.tsx +8 -2
  53. package/src/client/components/ScrollRestoration.tsx +125 -0
  54. package/src/client/entry.tsx +39 -2
  55. package/src/client/fetcher-store.ts +61 -0
  56. package/src/client/form-utils.ts +3 -0
  57. package/src/client/hooks/useActionData.ts +7 -3
  58. package/src/client/hooks/useFetcher.ts +116 -33
  59. package/src/client/hooks/useFetchers.ts +23 -0
  60. package/src/client/hooks/useLoaderData.ts +8 -4
  61. package/src/client/hooks/useLocation.ts +27 -0
  62. package/src/client/hooks/useMatches.ts +32 -0
  63. package/src/client/hooks/useNavigate.ts +11 -6
  64. package/src/client/hooks/useRevalidator.ts +26 -0
  65. package/src/client/hooks/useSearch.ts +73 -0
  66. package/src/client/hooks/useSearchParams.ts +7 -2
  67. package/src/client/nav-utils.ts +26 -0
  68. package/src/client/prefetch.ts +110 -15
  69. package/src/client/registry.ts +24 -0
  70. package/src/client/revalidation.ts +25 -0
  71. package/src/client/router.tsx +34 -1
  72. package/src/client/rpc.ts +11 -1
  73. package/src/client/scroll-restoration.ts +48 -0
  74. package/src/client/search-serializer.ts +40 -0
  75. package/src/client/types.ts +6 -0
  76. package/src/codegen/module-registry.ts +13 -21
  77. package/src/codegen/route-codegen.ts +148 -10
  78. package/src/config/load.ts +22 -0
  79. package/src/dev/hmr-client.ts +3 -1
  80. package/src/dev/route-table.ts +27 -0
  81. package/src/dev/server.ts +106 -8
  82. package/src/dev/watcher.ts +25 -3
  83. package/src/index.ts +38 -6
  84. package/src/server/action-handler.ts +3 -13
  85. package/src/server/action-registry.ts +35 -0
  86. package/src/server/adapter.ts +16 -0
  87. package/src/server/api-route.ts +47 -0
  88. package/src/server/csp.ts +19 -4
  89. package/src/server/csrf.ts +36 -3
  90. package/src/server/env.ts +26 -5
  91. package/src/server/headers.ts +49 -0
  92. package/src/server/layout.ts +43 -20
  93. package/src/server/loader.ts +14 -8
  94. package/src/server/matcher.ts +29 -2
  95. package/src/server/matches.ts +50 -0
  96. package/src/server/middleware.ts +66 -0
  97. package/src/server/proto-guard.ts +56 -0
  98. package/src/server/render.ts +51 -18
  99. package/src/server/request-handler.ts +111 -29
  100. package/src/server/scanner.ts +45 -3
  101. package/src/server/search.ts +47 -0
  102. package/src/server/serve.ts +116 -4
  103. package/src/server/session.ts +12 -1
  104. package/src/server/spa.ts +62 -0
  105. package/src/server/stream-handler.ts +10 -1
  106. package/src/server/validate.ts +89 -14
  107. package/src/shared/context.ts +7 -0
  108. package/src/shared/define-actions.ts +39 -0
  109. package/src/shared/form-data.ts +34 -0
  110. package/src/shared/route-types.ts +191 -2
  111. package/templates/new-app/app/root.tsx +2 -1
  112. package/templates/new-app/bractjs.config.ts +7 -12
  113. package/types/config.d.ts +24 -0
  114. package/types/index.d.ts +182 -9
  115. package/types/route.d.ts +138 -3
  116. package/LICENSE +0 -21
  117. package/README.md +0 -1125
package/types/route.d.ts CHANGED
@@ -1,15 +1,47 @@
1
1
  import type { ComponentType } from "react";
2
2
 
3
- export interface LoaderArgs {
3
+ /** A parsed navigation location (see `useLocation`). `hash` is always "" during SSR. */
4
+ export interface RouterLocation {
5
+ pathname: string;
6
+ /** Raw query string including the leading `?`, or `""`. */
7
+ search: string;
8
+ /** Fragment including the leading `#`, or `""`. */
9
+ hash: string;
10
+ state: unknown;
11
+ key: string;
12
+ }
13
+
14
+ export interface LoaderArgs<TSearch extends Record<string, unknown> = Record<string, unknown>> {
4
15
  request: Request;
5
16
  params: Record<string, string>;
6
17
  context: Record<string, unknown>;
18
+ /**
19
+ * The request's search params, validated/coerced by the route's
20
+ * `searchSchema` export when present; otherwise the raw string record
21
+ * (repeated keys become arrays). Parameterize to skip the cast:
22
+ * `loader({ search }: LoaderArgs<BoardSearch>)`.
23
+ */
24
+ search: TSearch;
7
25
  }
8
26
 
9
- export interface ActionArgs extends LoaderArgs {
27
+ export interface ActionArgs<TSearch extends Record<string, unknown> = Record<string, unknown>>
28
+ extends LoaderArgs<TSearch> {
10
29
  formData: FormData;
11
30
  }
12
31
 
32
+ /**
33
+ * The data a route's loader resolves to, for typing `useLoaderData`. Pass the
34
+ * loader function type to infer it (`useLoaderData<typeof loader>()` →
35
+ * awaited return, `Response` excluded, `Deferred` fields preserved). A plain
36
+ * object type is returned as-is (back-compat).
37
+ */
38
+ export type LoaderData<T> = T extends (...args: never[]) => unknown
39
+ ? Exclude<Awaited<ReturnType<T>>, Response>
40
+ : T;
41
+
42
+ /** The data a route's action resolves to, for typing `useActionData`. See {@link LoaderData}. */
43
+ export type ActionData<T> = LoaderData<T>;
44
+
13
45
  export type MetaDescriptor =
14
46
  | { title: string }
15
47
  | { name: string; content: string }
@@ -31,27 +63,130 @@ export type ActionFunction<T = unknown> = (
31
63
 
32
64
  export type MetaFunction<T = unknown> = (args: MetaArgs<T>) => MetaDescriptor[];
33
65
 
66
+ export interface HeadersArgs<T = unknown> {
67
+ loaderData: T;
68
+ params: Record<string, string>;
69
+ request: Request;
70
+ /** Headers merged from ancestors in the chain (root → layout → this route). */
71
+ parentHeaders: Headers;
72
+ }
73
+
74
+ /**
75
+ * A module's optional `headers` export — set response headers (`Cache-Control`,
76
+ * `ETag`, `Vary`, …) on the document and `/_data` responses. Runs in chain
77
+ * order (root → layout → route); innermost wins per key.
78
+ */
79
+ export type HeadersFunction<T = unknown> = (args: HeadersArgs<T>) => HeadersInit;
80
+
81
+ /**
82
+ * A nested route-middleware function. Runs on the server in chain order
83
+ * (root → layout → route) before `beforeLoad`/action/loaders, with a shared
84
+ * mutable `context`. Return a `Response` to short-circuit; call `next()` to
85
+ * continue.
86
+ */
87
+ export type RouteMiddlewareFunction = (
88
+ ctx: { request: Request; params: Record<string, string>; context: Record<string, unknown> },
89
+ next: () => Promise<Response>,
90
+ ) => Promise<Response>;
91
+
34
92
  export interface BeforeLoadArgs {
35
93
  params: Record<string, string>;
36
94
  context: Record<string, unknown>;
37
95
  location: { pathname: string; search: string };
96
+ /** Validated search params (server-side only; absent in the client-side guard). */
97
+ search?: Record<string, unknown>;
38
98
  }
39
99
 
40
100
  export type BeforeLoadFunction = (
41
101
  args: BeforeLoadArgs,
42
102
  ) => void | Response | Promise<void | Response>;
43
103
 
104
+ /**
105
+ * Decide whether loader data should be refetched (SWR background refetch and
106
+ * post-mutation revalidation). Return `args.defaultShouldRevalidate` (true)
107
+ * for the default behavior.
108
+ */
109
+ export interface ShouldRevalidateArgs {
110
+ currentUrl: URL;
111
+ nextUrl: URL;
112
+ formMethod?: string;
113
+ actionStatus?: number;
114
+ defaultShouldRevalidate: boolean;
115
+ }
116
+
117
+ export type ShouldRevalidateFunction = (args: ShouldRevalidateArgs) => boolean;
118
+
119
+ /** A route's browser-side loader (RR7-style). See the package docs. */
120
+ export interface ClientLoaderFunction<T = unknown> {
121
+ (args: {
122
+ request: Request;
123
+ params: Record<string, string>;
124
+ search: Record<string, unknown>;
125
+ serverLoader: () => Promise<unknown>;
126
+ }): Promise<T> | T;
127
+ /** Run on initial hydration too (default: only on client navigation). */
128
+ hydrate?: boolean;
129
+ }
130
+
131
+ /** A route's browser-side action (RR7-style). See the package docs. */
132
+ export type ClientActionFunction<T = unknown> = (args: {
133
+ request: Request;
134
+ params: Record<string, string>;
135
+ formData: FormData;
136
+ serverAction: () => Promise<unknown>;
137
+ }) => Promise<T> | T;
138
+
44
139
  export interface RouteModule<TLoader = unknown, TAction = unknown> {
45
140
  loader?: LoaderFunction<TLoader>;
46
141
  action?: ActionFunction<TAction>;
142
+ /** Browser-side loader; see {@link ClientLoaderFunction}. */
143
+ clientLoader?: ClientLoaderFunction<TLoader>;
144
+ /** Browser-side action; see {@link ClientActionFunction}. */
145
+ clientAction?: ClientActionFunction<TAction>;
47
146
  meta?: MetaFunction<TLoader>;
147
+ /** Set response headers (Cache-Control/ETag/Vary/…) for this route's document and `/_data` responses. Chain order, innermost wins. */
148
+ headers?: HeadersFunction<TLoader>;
149
+ /** Nested middleware (root → layout → route), shared mutable `context`, runs before beforeLoad/action/loaders. A single fn or an array. */
150
+ middleware?: RouteMiddlewareFunction | RouteMiddlewareFunction[];
48
151
  beforeLoad?: BeforeLoadFunction;
152
+ shouldRevalidate?: ShouldRevalidateFunction;
153
+ /** Zod/Valibot-compatible schema validating search params before loaders run (400 on failure). */
154
+ searchSchema?: unknown;
155
+ /**
156
+ * Selective SSR: `true` (default) full SSR; `"data-only"` loaders run on the
157
+ * server but the component renders client-only; `false` neither the route
158
+ * loader nor the component runs during document SSR (beforeLoad still does).
159
+ */
160
+ ssr?: boolean | "data-only";
161
+ /** SSR'd in the component's place for `ssr: false` / `"data-only"` routes. */
162
+ Fallback?: ComponentType;
49
163
  handle?: Record<string, unknown>;
50
164
  ErrorBoundary?: ComponentType<{ error: unknown }>;
51
165
  default?: ComponentType;
52
166
  }
53
167
 
54
- export type Segment = string | { param: string } | { catchAll: string };
168
+ /**
169
+ * One entry in the matched route chain (see `useMatches`), outermost → innermost:
170
+ * root, layouts, then the leaf route.
171
+ */
172
+ export interface RouteMatch<TData = unknown, THandle = Record<string, unknown>> {
173
+ /** Stable id of the matched module — its appDir-relative file path. */
174
+ id: string;
175
+ /** The active URL pathname (shared across the chain). */
176
+ pathname: string;
177
+ /** The matched route params (shared across the chain). */
178
+ params: Record<string, string>;
179
+ /** This module's loader data slice. */
180
+ data: TData;
181
+ /** This module's static `handle` export, or `undefined`. */
182
+ handle: THandle | undefined;
183
+ }
184
+
185
+ export type Segment =
186
+ | string
187
+ | { param: string }
188
+ | { optional: string }
189
+ | { catchAll: string };
55
190
 
56
191
  export interface RouteFile {
57
192
  filePath: string;
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2026 YOUR_NAME
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.