@ts-pf/server-http 0.3.3 → 0.3.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ts-pf/server-http",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "description": "Fetch Request/Response handler and HTTP HandlerPlugins",
5
5
  "keywords": [
6
6
  "ts-pf",
@@ -50,12 +50,12 @@
50
50
  "lint": "biome check src tests"
51
51
  },
52
52
  "dependencies": {
53
- "@ts-pf/http": "0.2.1",
54
- "@ts-pf/protocol": "0.2.1",
55
- "@ts-pf/server": "0.4.2"
53
+ "@ts-pf/http": "0.2.2",
54
+ "@ts-pf/protocol": "0.2.2",
55
+ "@ts-pf/server": "0.4.3"
56
56
  },
57
57
  "devDependencies": {
58
- "@ts-pf/contract": "0.2.2",
58
+ "@ts-pf/contract": "0.2.3",
59
59
  "zod": "^4.0.0"
60
60
  }
61
61
  }
@@ -66,6 +66,52 @@ Errors thrown after a streamed response has started (`StreamCodec` / `SseCodec`
66
66
 
67
67
  A JSON envelope object with keys but no `input` key (`{"id":1}` instead of `{"input":{"id":1}}`) is `400 BAD_REQUEST` `Request body must be {"input": ...}`, whichever codec carries it (plain JSON, the multipart `rpc` part, or a JSONL input line). `{}` and `{"input":null}` still mean no input.
68
68
 
69
+ ## Cacheable reads and public REST (userland)
70
+
71
+ The wire is POST only, so a CDN or browser cannot cache a call by URL. When a read needs HTTP caching, bridge it in userland: `HandlerPlugin.onRequest` runs before the method check, so it can turn `GET {prefix}/{path}?input=<JSON>` into the POST `FetchHandler` serves. Opt in per procedure in the contract, and only for procedures with no side effects:
72
+
73
+ ```ts no-check
74
+ find: procedure
75
+ .meta({ http: { method: 'GET', cacheControl: 'public, max-age=60' } })
76
+ .input(z.object({ id: z.number() }))
77
+
78
+ new FetchHandler(app, {
79
+ plugins: [
80
+ new CORSPlugin({ origin }),
81
+ new GetBridgePlugin(app, '/rpc'), // last, so no later onRequest re-wraps its request
82
+ ],
83
+ })
84
+ ```
85
+
86
+ `GetBridgePlugin` (`examples/plugins/src/get-bridge.ts`, smoke-tested): `lookupProcedure(app, path)` → contract meta `http.method === 'GET'` → `new Request(url, { method: 'POST', headers, body: JSON.stringify({ input: JSON.parse(q) }), signal })`; `onResponse` sets the meta `Cache-Control` on success unless the call already set one, and `no-store` on failures and on any response with `Set-Cookie`. A GET to a procedure that did not opt in falls through to `405 METHOD_NOT_ALLOWED`. The client half is a Fetch `Interceptor` on `FetchLink` that resends the JSON POST for listed paths as a GET with sorted keys and keeps POST above ~8 KB of URL (`examples/plugins/src/get-link.ts`). The GET drops `x-ts-pf-protocol` (absent is accepted) so a cross-origin read stays a simple request.
87
+
88
+ - `public` only for data that is the same for every caller. A shared-cache hit never reaches the server, so auth middleware does not run for later callers. Per-user reads use `private` (browser cache only). A per-call `Cache-Control` / `Vary` from `ResponseHeadersPlugin` (`context.resHeaders`) wins over the meta default.
89
+ - A GET runs from a plain link, cookies included: never opt in a procedure that writes.
90
+ - GET is CORS-safelisted, so `CORSPlugin` needs no `allowMethods` change. Your own headers (`Authorization`, custom) still make it preflight, and browsers cache a preflight per URL, so each distinct `?input=` preflights once.
91
+ - The wire and the `http` meta key are what a future first-party binding would use, so the bridge can be dropped without touching contracts or URLs.
92
+
93
+ A public REST API for third parties is a userland router next to `FetchHandler`, calling the same `app` through `createLocalClient` so middleware, validation, and typed errors run:
94
+
95
+ ```ts
96
+ import { httpStatus } from '@ts-pf/http'
97
+ import { isPFError } from '@ts-pf/protocol'
98
+ import { createLocalClient } from '@ts-pf/server'
99
+
100
+ // GET /v1/planets/:id, routed by your server before handler.handle()
101
+ export async function getPlanet(id: string): Promise<Response> {
102
+ const api = createLocalClient(app, { context })
103
+ try {
104
+ return Response.json(await api.planet.find({ id: Number(id) }))
105
+ } catch (error) {
106
+ if (!isPFError(error)) throw error
107
+ return Response.json(
108
+ { code: error.code, message: error.message },
109
+ { status: httpStatus(error) },
110
+ )
111
+ }
112
+ }
113
+ ```
114
+
69
115
  ## API
70
116
 
71
117
  - `FetchHandler`, `FetchHandlerOnError`, `HandleResult`, `HandlerPlugin`
@@ -79,6 +125,7 @@ A JSON envelope object with keys but no `input` key (`{"id":1}` instead of `{"in
79
125
 
80
126
  ## Don't
81
127
 
82
- - Serve REST, OpenAPI, or `catalog.json` from `FetchHandler`.
128
+ - Serve REST, OpenAPI, or `catalog.json` from `FetchHandler`. REST routes are userland, beside it.
129
+ - Bridge GET for a procedure with side effects, or mark per-user data `public`.
83
130
  - Port `HandlerPlugin` onto WS / stdio / MessagePort.
84
131
  - Put file size caps here — those are `MultipartCodec` options.