@dads-framework/http 0.1.0 → 0.2.0

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.
package/README.md CHANGED
@@ -1,8 +1,64 @@
1
1
  # @dads-framework/http — HTTP routes
2
2
 
3
- The one way routes exist. You should be able to ship an endpoint from this
4
- page alone — no Fastify knowledge required (Fastify is an implementation
5
- detail inside this lib; importing it anywhere else is banned).
3
+ Ship an endpoint with one declaration for the input schema, access policy,
4
+ handler, response, and generated OpenAPI. Define the route in its domain,
5
+ register it in the API, and test both allowed and denied requests. This
6
+ package owns the Fastify integration.
7
+
8
+ ## Try it
9
+
10
+ In a Node.js 22+ project, install the packages.
11
+
12
+ ```sh
13
+ npm install @dads-framework/http zod
14
+ ```
15
+
16
+ Save this as `example.mts` and run `node --experimental-strip-types example.mts`.
17
+
18
+ <!-- dads:check-example -->
19
+
20
+ ```ts
21
+ import {
22
+ createHttpApp,
23
+ getRoute,
24
+ publicAccess,
25
+ registerRoutes,
26
+ routeGroup,
27
+ } from "@dads-framework/http";
28
+ import { z } from "zod";
29
+
30
+ const greeting = getRoute({
31
+ path: "/greeting",
32
+ summary: "Read a public greeting",
33
+ access: publicAccess(),
34
+ response: z.object({ message: z.string() }),
35
+ handler: () => ({ message: "Hello" }),
36
+ });
37
+
38
+ const app = await createHttpApp({
39
+ nodeEnv: "test",
40
+ trustedOrigins: [],
41
+ logger: false,
42
+ });
43
+ try {
44
+ registerRoutes(app, {
45
+ routes: routeGroup("Greetings", [greeting]),
46
+ subjects: { resolve: () => Promise.resolve(null) },
47
+ authorization: { check: () => Promise.resolve(false) },
48
+ });
49
+ const response = await app.inject({ method: "GET", url: "/greeting" });
50
+ console.log(response.statusCode, response.body);
51
+ } finally {
52
+ await app.close();
53
+ }
54
+ ```
55
+
56
+ The injected request prints `200 {"data":{"message":"Hello"}}`. It runs
57
+ through the HTTP app without opening a port.
58
+
59
+ In the generated app, route declarations live with the owning domain.
60
+ `assembly/start-app.ts` registers them with identity and authorization.
61
+ [Notes routes](../../../apps/kit-workbench/domains/notes/server/routes.ts) · [Starter HTTP wiring](../../../apps/kit-workbench/assembly/start-app.ts)
6
62
 
7
63
  ## Define a route — the verb IS the function
8
64
 
@@ -140,22 +196,29 @@ observability.
140
196
  ## Testing
141
197
 
142
198
  ```ts
143
- import {
144
- buildTestApp,
145
- dataBodyOf,
146
- errorBodyOf,
147
- } from "@dads-framework/http/testing";
199
+ import { buildTestApp, dataBodyOf } from "@dads-framework/http/testing";
200
+ import { FakeAuthorization } from "@dads-framework/access/testing";
201
+ import { routeGroup } from "@dads-framework/http";
202
+
203
+ const id = "ca498064-95f7-4334-8d66-12703cd044a9";
204
+ const authorization = new FakeAuthorization();
205
+ authorization.seed({
206
+ resource: { type: "organizations/organization", id },
207
+ subject: { type: "user", id: "u1" },
208
+ relation: "view",
209
+ });
148
210
 
149
211
  const app = await buildTestApp({
150
212
  routes: routeGroup("Organizations", [getOrganization]),
151
- authorization: seededFake,
213
+ authorization,
152
214
  });
153
215
  const res = await app.inject({
154
216
  method: "GET",
155
- url: "/organizations/xyz",
217
+ url: `/organizations/${id}`,
156
218
  headers: { "x-test-subject": "u1" },
157
219
  });
158
- expect(dataBodyOf(res, organizationSchema).id).toBe("xyz");
220
+ console.log(dataBodyOf(res, organizationSchema).id); // ca498064-95f7-4334-8d66-12703cd044a9
221
+ await app.close();
159
222
  ```
160
223
 
161
224
  `buildTestApp` never opens a port (`listen` does not exist on it — pinned
@@ -178,9 +241,7 @@ an arbitrary schema) — `res.json()` is `any` and defeats the type walls.
178
241
 
179
242
  A surface that implements another party's protocol (Better Auth's
180
243
  `/api/auth/*`, the remote MCP endpoint) is not a route: it is a fetch-style
181
- handler mounted whole. That code is its own Nx project, `api-http-protocol-mount`
182
- in [`protocol-mount/`](./src/protocol-mounts/README.md), imported as
183
- `@dads-framework/http/protocol-mounts`.
244
+ handler mounted whole. Import the bridge from [`@dads-framework/http/protocol-mounts`](./src/protocol-mounts/README.md) in the API composition root.
184
245
  `mountFetchHandler` is the one bridge for that — every content type arrives as
185
246
  raw bytes, the request crosses to a WHATWG `Request` whose clones never share
186
247
  a stream (`to-web-request.ts`, the Node 22 undici clone trap), and the
@@ -204,6 +265,17 @@ model and a fake lookup port to `buildTestApp`.
204
265
 
205
266
  Set `currentTraceId` and `requestTrace` on `createHttpApp` to correlate request records and annotate the active request span. The observer receives the request ID and the audit actor kind.
206
267
 
268
+ ## Feature state previews
269
+
270
+ When a host configures `featureState.preview`, the resolver receives the
271
+ preview ID, authenticated subject, effective subject, and feature-state
272
+ context. By default the context comes from a request header. The host verifies the effective identity and context before
273
+ returning a preview. Set `featureState.contextFromRequest` to derive context
274
+ from matched route parameters instead of the caller’s header. Preview responses
275
+ are private and non-cacheable. A resolver may return a `scopeKey` for its feature
276
+ evaluator to match against
277
+ the context being evaluated.
278
+
207
279
  ## What it refuses to do
208
280
 
209
281
  Application code supplies identity, access policies, and business handlers. The HTTP kit enforces the declared route boundary and owns transport.
@@ -218,3 +290,7 @@ Its transport conformance runs over a real Fastify socket and against the
218
290
  contract package's independent in-memory Fetch host. The shared proof covers
219
291
  GET and POST beneath a prefix. Fastify-specific hooks and limits have their own
220
292
  tests.
293
+
294
+ A subject resolver can read the matched route's decoded `params` when its
295
+ session policy depends on a route parameter. Validate those values before
296
+ using them to select a policy.