@bractjs/bractjs 0.1.28 → 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 (46) hide show
  1. package/package.json +3 -2
  2. package/src/__tests__/fixtures/app/routes/features-demo.tsx +28 -0
  3. package/src/__tests__/headers.test.ts +111 -0
  4. package/src/__tests__/integration.test.ts +34 -0
  5. package/src/__tests__/layout-registry.test.ts +7 -3
  6. package/src/__tests__/matcher.test.ts +29 -0
  7. package/src/__tests__/module-registry.test.ts +2 -3
  8. package/src/__tests__/route-lint.test.ts +5 -0
  9. package/src/__tests__/route-middleware.test.ts +84 -0
  10. package/src/__tests__/scanner.test.ts +46 -1
  11. package/src/__tests__/security-fixes.test.ts +201 -0
  12. package/src/__tests__/use-matches.test.ts +54 -0
  13. package/src/build/route-lint.ts +3 -3
  14. package/src/client/ClientRouter.tsx +118 -18
  15. package/src/client/hooks/useMatches.ts +32 -0
  16. package/src/client/router.tsx +7 -1
  17. package/src/client/rpc.ts +11 -1
  18. package/src/codegen/module-registry.ts +13 -21
  19. package/src/codegen/route-codegen.ts +8 -3
  20. package/src/config/load.ts +1 -0
  21. package/src/index.ts +11 -3
  22. package/src/server/action-handler.ts +1 -20
  23. package/src/server/adapter.ts +16 -0
  24. package/src/server/api-route.ts +47 -0
  25. package/src/server/csp.ts +9 -3
  26. package/src/server/csrf.ts +10 -3
  27. package/src/server/headers.ts +49 -0
  28. package/src/server/layout.ts +12 -19
  29. package/src/server/matcher.ts +29 -2
  30. package/src/server/matches.ts +50 -0
  31. package/src/server/middleware.ts +66 -0
  32. package/src/server/proto-guard.ts +56 -0
  33. package/src/server/render.ts +34 -16
  34. package/src/server/request-handler.ts +67 -27
  35. package/src/server/scanner.ts +45 -3
  36. package/src/server/search.ts +5 -1
  37. package/src/server/serve.ts +28 -3
  38. package/src/server/session.ts +12 -1
  39. package/src/server/validate.ts +4 -1
  40. package/src/shared/context.ts +3 -1
  41. package/src/shared/route-types.ts +108 -0
  42. package/types/config.d.ts +3 -0
  43. package/types/index.d.ts +17 -0
  44. package/types/route.d.ts +76 -1
  45. package/LICENSE +0 -21
  46. package/README.md +0 -1331
package/types/route.d.ts CHANGED
@@ -63,6 +63,32 @@ export type ActionFunction<T = unknown> = (
63
63
 
64
64
  export type MetaFunction<T = unknown> = (args: MetaArgs<T>) => MetaDescriptor[];
65
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
+
66
92
  export interface BeforeLoadArgs {
67
93
  params: Record<string, string>;
68
94
  context: Record<string, unknown>;
@@ -90,10 +116,38 @@ export interface ShouldRevalidateArgs {
90
116
 
91
117
  export type ShouldRevalidateFunction = (args: ShouldRevalidateArgs) => boolean;
92
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
+
93
139
  export interface RouteModule<TLoader = unknown, TAction = unknown> {
94
140
  loader?: LoaderFunction<TLoader>;
95
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>;
96
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[];
97
151
  beforeLoad?: BeforeLoadFunction;
98
152
  shouldRevalidate?: ShouldRevalidateFunction;
99
153
  /** Zod/Valibot-compatible schema validating search params before loaders run (400 on failure). */
@@ -111,7 +165,28 @@ export interface RouteModule<TLoader = unknown, TAction = unknown> {
111
165
  default?: ComponentType;
112
166
  }
113
167
 
114
- 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 };
115
190
 
116
191
  export interface RouteFile {
117
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.