ts-procedures 10.0.0 → 10.1.1

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 (42) hide show
  1. package/CHANGELOG.md +55 -0
  2. package/build/adapters/hono/envelope-parity.test.js +17 -6
  3. package/build/adapters/hono/envelope-parity.test.js.map +1 -1
  4. package/build/codegen/constants.js +2 -2
  5. package/build/codegen/constants.js.map +1 -1
  6. package/build/exports.d.ts +1 -0
  7. package/build/exports.js +4 -0
  8. package/build/exports.js.map +1 -1
  9. package/build/schema/typebox.d.ts +1 -1
  10. package/build/schema/typebox.js +20 -2
  11. package/build/schema/typebox.js.map +1 -1
  12. package/build/schema/typebox.test.js +10 -0
  13. package/build/schema/typebox.test.js.map +1 -1
  14. package/build/server/doc-registry.js +3 -0
  15. package/build/server/doc-registry.js.map +1 -1
  16. package/build/server/doc-registry.test.js +31 -0
  17. package/build/server/doc-registry.test.js.map +1 -1
  18. package/build/server/index.d.ts +1 -0
  19. package/build/server/index.js +1 -0
  20. package/build/server/index.js.map +1 -1
  21. package/build/server/spec-version.d.ts +24 -0
  22. package/build/server/spec-version.js +26 -0
  23. package/build/server/spec-version.js.map +1 -0
  24. package/build/server/types.d.ts +15 -0
  25. package/build/version.d.ts +9 -0
  26. package/build/version.js +11 -0
  27. package/build/version.js.map +1 -0
  28. package/docs/doc-envelope-spec-v1.md +296 -0
  29. package/docs/doc-envelope-v1.schema.json +253 -0
  30. package/docs/http-integrations.md +27 -1
  31. package/package.json +1 -1
  32. package/src/adapters/hono/envelope-parity.test.ts +18 -6
  33. package/src/codegen/constants.ts +2 -2
  34. package/src/exports.ts +4 -0
  35. package/src/schema/typebox.test.ts +12 -0
  36. package/src/schema/typebox.ts +21 -2
  37. package/src/server/doc-registry.test.ts +34 -0
  38. package/src/server/doc-registry.ts +3 -0
  39. package/src/server/index.ts +1 -0
  40. package/src/server/spec-version.ts +27 -0
  41. package/src/server/types.ts +15 -0
  42. package/src/version.ts +11 -0
@@ -0,0 +1,296 @@
1
+ # ts-procedures DocEnvelope — Consumer Spec (`specVersion: 1`)
2
+
3
+ > **This document describes DocEnvelope `specVersion: 1`.** The filename is
4
+ > pinned to the spec version (`-v1`) so future format changes land as a sibling
5
+ > (`doc-envelope-spec-v2.md`) and the differences stay diffable. Check the
6
+ > envelope's `specVersion` field (absent ⇒ `1`) to know which spec applies.
7
+
8
+ A spec for a GUI that fetches a `DocEnvelope` JSON document from a ts-procedures
9
+ server URL and renders it. This is the **stable wire contract** between a
10
+ ts-procedures server and any documentation/codegen consumer. It is a *frozen*
11
+ contract in the framework (changing field names/nesting breaks codegen goldens),
12
+ so you can build against it confidently.
13
+
14
+ Companion file: `doc-envelope-v1.schema.json` (JSON Schema Draft-07) — validate
15
+ fetched documents against it.
16
+
17
+ ---
18
+
19
+ ## 1. How you get the document
20
+
21
+ There is **no built-in endpoint**. A server author calls
22
+ `builder.toDocEnvelope()` (or `DocRegistry.toJSON()`) and exposes the resulting
23
+ JSON at a route of their choosing (e.g. `GET /api/docs`, `/openapi`, a static
24
+ `.json` file, etc.).
25
+
26
+ So your website's "paste a URL" flow should:
27
+
28
+ 1. `fetch(url)` the user-supplied URL.
29
+ 2. Expect `Content-Type` ~ JSON; `JSON.parse` the body.
30
+ 3. Validate that it is an object with a `routes` **array**. That single check is
31
+ what the official codegen uses as its "is this an envelope?" gate. If
32
+ `routes` is missing/not-an-array, show a clear "not a ts-procedures docs
33
+ document" error.
34
+ 4. Optionally validate fully against `doc-envelope-v1.schema.json`.
35
+
36
+ CORS: the server must allow your origin, or you'll need a proxy. Worth surfacing
37
+ as a distinct error from "bad JSON".
38
+
39
+ ---
40
+
41
+ ## 2. Top-level shape
42
+
43
+ ```ts
44
+ interface DocEnvelope {
45
+ specVersion?: number // envelope wire-format version; absent ⇒ treat as 1
46
+ generatorVersion?: string // producing ts-procedures version, e.g. "10.0.0" (informational)
47
+ basePath: string // e.g. "/api" (may be "")
48
+ headers: HeaderDoc[] // global headers (often [])
49
+ errors: ErrorDoc[] // the error catalog
50
+ routes: AnyRouteDoc[] // every procedure/route
51
+ }
52
+ ```
53
+
54
+ The authoritative producer (`DocRegistry.toJSON`) emits these six keys.
55
+ Resilience notes:
56
+
57
+ - **`specVersion`** is your compatibility signal — a small monotonic integer
58
+ that increments **only** when the envelope format changes (never on routine
59
+ package releases). **Branch your parser on this.** Treat a **missing**
60
+ `specVersion` as `1` (documents predating the field). Refuse / warn on a
61
+ `specVersion` higher than your GUI knows about. Current value: `1`.
62
+ - **`generatorVersion`** is the producing ts-procedures package version (e.g.
63
+ `"10.0.0"`). **Informational only** — show it in a footer/tooltip, but do
64
+ **not** use it to decide how to parse; it changes every release regardless of
65
+ format. (Note: one internal codegen *input* fixture carries an unrelated
66
+ top-level `"version"` string and uppercase methods — that's a loose
67
+ hand-authored fixture, not server output; ignore `version` if you ever see it.)
68
+ - Be tolerant of **extra top-level keys** — treat unknown keys as opaque.
69
+ - `headers` and `errors` are commonly empty arrays. Render gracefully.
70
+
71
+ ---
72
+
73
+ ## 3. The error catalog (`errors`)
74
+
75
+ ```ts
76
+ interface ErrorDoc {
77
+ name: string // unique; routes reference it by this name
78
+ statusCode: number // HTTP status, e.g. 400, 404, 418, 500
79
+ description: string
80
+ schema?: Record<string, unknown> // JSON Schema of the error body (optional)
81
+ }
82
+ ```
83
+
84
+ Framework defaults are auto-merged and always present unless opted out:
85
+ `ProcedureValidationError` (400), `ProcedureYieldValidationError` (500),
86
+ `ProcedureError` (500), `ProcedureRegistrationError` (500). User taxonomy errors
87
+ (e.g. `NotFound` 404, `Teapot` 418) appear alongside them.
88
+
89
+ **GUI tip:** build a `Map<name, ErrorDoc>`. Each route's `errors: string[]` is a
90
+ list of these names — render them as links/badges that resolve to the catalog
91
+ entry (status code + description + body schema).
92
+
93
+ ---
94
+
95
+ ## 4. Global headers (`headers`)
96
+
97
+ ```ts
98
+ interface HeaderDoc {
99
+ name: string
100
+ description?: string
101
+ required?: boolean
102
+ example?: string
103
+ }
104
+ ```
105
+
106
+ Simple list; render as a small table. Often empty.
107
+
108
+ ---
109
+
110
+ ## 5. Routes — discriminated union on `kind`
111
+
112
+ `routes[]` mixes four kinds. **Always switch on `route.kind`.** Every route has a
113
+ `name` and a `jsonSchema` object; the rest varies by kind.
114
+
115
+ | `kind` | Transport | Path field(s) | Method field | Payload schema keys |
116
+ |-----------------|----------------------|----------------------|---------------------|----------------------------------------------|
117
+ | `rpc` | JSON-RPC (POST) | `path` | `method: "post"` | `body`, `response` |
118
+ | `stream` | RPC stream (SSE/text)| `path` | `methods: string[]` | `params`, `yieldType`, `returnType` |
119
+ | `api` | REST | `path?`, `fullPath` | `method` | `req.{pathParams,query,body,headers}`, `res.{body,headers}` |
120
+ | `http-stream` | REST stream (SSE) | `path?`, `fullPath` | `method` | `req.{...}`, `res.{headers}`, `yield`, `returnType` |
121
+
122
+ Common fields: `name` (string), `errors?` (string[] → catalog names),
123
+ `scope` (grouping; `string | string[]` for rpc/stream, `string` for api/http-stream).
124
+
125
+ > **No per-route `description`.** A `description` passed at procedure-definition
126
+ > time is **not** carried into the route doc. Don't build UI that depends on it.
127
+
128
+ ### 5.1 `rpc`
129
+
130
+ ```jsonc
131
+ {
132
+ "kind": "rpc",
133
+ "name": "GetUser",
134
+ "version": 1,
135
+ "scope": "users",
136
+ "path": "/api/users/get-user/1", // full path; embeds scope + version
137
+ "method": "post",
138
+ "jsonSchema": {
139
+ "body": { "type": "object", "required": ["id"], "properties": { "id": { "type": "string" } } },
140
+ "response": { "type": "object", "required": ["id","name"], "properties": { "id":{"type":"string"}, "name":{"type":"string"} } }
141
+ },
142
+ "errors": ["Teapot"]
143
+ }
144
+ ```
145
+
146
+ - `jsonSchema.body` = request input (runtime-validated).
147
+ - `jsonSchema.response` = response shape (documentation only).
148
+ - Both are optional (a no-arg/no-return procedure yields `jsonSchema: {}`).
149
+
150
+ ### 5.2 `stream` (RPC stream)
151
+
152
+ ```jsonc
153
+ {
154
+ "kind": "stream",
155
+ "name": "WatchUser",
156
+ "version": 2,
157
+ "scope": ["users", "live"],
158
+ "path": "/api/users/live/watch-user/2",
159
+ "methods": ["post", "get"],
160
+ "streamMode": "sse", // "sse" | "text"
161
+ "jsonSchema": {
162
+ "params": { "type": "object", "required": ["id"], "properties": { "id": { "type": "string" } } },
163
+ "yieldType": { /* see note */ },
164
+ "returnType": { "type": "object", "required": ["total"], "properties": { "total": { "type": "number" } } }
165
+ }
166
+ }
167
+ ```
168
+
169
+ - `params` = input (query params for GET, body for POST).
170
+ - **`yieldType` in `sse` mode is the SSE envelope**, not the bare value:
171
+ ```jsonc
172
+ {
173
+ "type": "object",
174
+ "description": "SSE message envelope. The data field contains the procedure yield value.",
175
+ "required": ["data", "event", "id"],
176
+ "properties": {
177
+ "data": { /* the actual per-yield value schema */ },
178
+ "event": { "type": "string" },
179
+ "id": { "type": "string" },
180
+ "retry": { "type": "number" }
181
+ }
182
+ }
183
+ ```
184
+ In `text` mode `yieldType` is the bare value schema. **GUI tip:** to show "what
185
+ each event carries", detect the SSE envelope (object with `data`/`event`/`id`)
186
+ and surface `properties.data`; otherwise show `yieldType` directly.
187
+ - `returnType` = value delivered on stream completion (carried by the `return`
188
+ SSE event). Optional.
189
+ - `errors` here are pre-stream errors only.
190
+
191
+ ### 5.3 `api` (REST)
192
+
193
+ ```jsonc
194
+ {
195
+ "kind": "api",
196
+ "name": "UpdateUser",
197
+ "scope": "users",
198
+ "path": "/users/:id", // as registered (may have :params)
199
+ "method": "put",
200
+ "fullPath": "/api/users/:id", // resolved, incl. basePath — prefer this for display
201
+ "successStatus": 200, // optional; defaults POST→201, DELETE→204, else 200
202
+ "jsonSchema": {
203
+ "req": {
204
+ "pathParams": { "type":"object", "required":["id"], "properties":{"id":{"type":"string"}} },
205
+ "query": { "type":"object", "properties":{"dryRun":{"type":"boolean"}} },
206
+ "body": { "type":"object", "required":["name"], "properties":{"name":{"type":"string"}} }
207
+ // "headers" also possible
208
+ },
209
+ "res": {
210
+ "body": { "type":"object", "required":["id","name"], "properties":{"id":{"type":"string"},"name":{"type":"string"}} },
211
+ "headers": { "type":"object", "required":["x-rev"], "properties":{"x-rev":{"type":"string"}} }
212
+ }
213
+ },
214
+ "errors": ["Teapot"]
215
+ }
216
+ ```
217
+
218
+ - `req` channels: any of `pathParams`, `query`, `body`, `headers` (all optional).
219
+ - `res` channels: `body`, `headers` (both optional).
220
+ - `jsonSchema.req`/`res` may be `{}` or absent for trivial endpoints.
221
+ - Prefer `fullPath` for the displayed URL; `path` may be omitted in some
222
+ envelopes.
223
+
224
+ ### 5.4 `http-stream` (REST stream)
225
+
226
+ ```jsonc
227
+ {
228
+ "kind": "http-stream",
229
+ "name": "TailUser",
230
+ "scope": "users",
231
+ "path": "/users/:id/tail",
232
+ "method": "get",
233
+ "fullPath": "/api/users/:id/tail",
234
+ "streamMode": "sse",
235
+ "jsonSchema": {
236
+ "req": { "pathParams": { "type":"object", "required":["id"], "properties":{"id":{"type":"string"}} } },
237
+ "res": { "headers": { /* optional response headers schema */ } },
238
+ "yield": { "type":"object", "required":["line"], "properties":{"line":{"type":"string"}} },
239
+ "returnType": { "type":"object", "required":["lines"], "properties":{"lines":{"type":"number"}} }
240
+ }
241
+ }
242
+ ```
243
+
244
+ - Same `req` channels as `api`.
245
+ - `res` has **`headers` only** (no `body` — streaming responses have no single body).
246
+ - `yield` = per-event value schema, **raw** (NOT wrapped in an SSE envelope,
247
+ unlike `kind: "stream"`).
248
+ - `returnType` = completion value. Optional.
249
+
250
+ ---
251
+
252
+ ## 6. Embedded payload schemas
253
+
254
+ Every `jsonSchema.*` leaf (`body`, `response`, `params`, `yield`, `req.query`,
255
+ error `schema`, …) is a plain **JSON Schema (Draft-07)** object as produced by
256
+ the server's schema layer (TypeBox → JSON Schema). Expect standard keywords:
257
+ `type`, `properties`, `required`, `items`, `enum`, `const`, `format`, `oneOf`,
258
+ nested objects/arrays, etc.
259
+
260
+ **Render these with a generic JSON-Schema viewer.** Don't hard-code field lists.
261
+ Useful libraries: `@apidevtools/json-schema-ref-parser` (deref `$ref` if any),
262
+ or any JSON-Schema-to-table/form renderer. They are validation-grade schemas, so
263
+ you can also drive a "try it" form or example generator from them.
264
+
265
+ ---
266
+
267
+ ## 7. Suggested rendering model
268
+
269
+ 1. **Header:** `basePath`, route count, kind breakdown.
270
+ 2. **Sidebar:** group routes by `scope` (join array scopes with `/`), then by
271
+ `kind`. Label each with `name` + method/path.
272
+ 3. **Route detail:**
273
+ - Title: `name`; badge: `kind`; method(s); display path (`fullPath` ?? `path`).
274
+ - For `rpc`/`stream`: also show `version`.
275
+ - **Request** section from `body`/`params`/`req.*`.
276
+ - **Response** section from `response`/`res.*`; for streams, a **Stream**
277
+ section from `yield`/`yieldType` (+ `returnType`).
278
+ - **Errors** section: resolve each `errors[]` name against the catalog and
279
+ show status + description + body schema.
280
+ 4. **Errors page:** full `errors[]` catalog.
281
+ 5. **Headers page:** `headers[]` table.
282
+
283
+ ---
284
+
285
+ ## 8. Defensive checklist for the GUI
286
+
287
+ - Validate `routes` is an array before anything else; otherwise it's not an envelope.
288
+ - Switch on `kind`; render unknown `kind` values generically (future-proofing).
289
+ - Treat HTTP methods case-insensitively (canonical output is lowercase, but
290
+ normalize anyway).
291
+ - Tolerate empty/absent `headers`, `errors`, `jsonSchema`, `req`, `res`.
292
+ - Tolerate extra/unknown properties at every level.
293
+ - Read `specVersion` first (default 1 if absent) and branch parsing on it; warn on unknown-higher.
294
+ - Don't expect a per-route `description`. `generatorVersion` is display-only, never a parse signal.
295
+ - Prefer `fullPath` over `path` for `api`/`http-stream` display.
296
+ - Unwrap the SSE envelope for `stream` `yieldType.data` when showing "event payload".
@@ -0,0 +1,253 @@
1
+ {
2
+ "$schema": "http://json-schema.org/draft-07/schema#",
3
+ "$id": "https://ts-procedures.dev/schemas/doc-envelope-v1.schema.json",
4
+ "title": "ts-procedures DocEnvelope (specVersion 1)",
5
+ "description": "The JSON document a ts-procedures server exposes (via builder.toDocEnvelope() / DocRegistry.toJSON()). Describes every procedure/route the server serves, the global error catalog, and global headers. Payload schemas embedded under each route's `jsonSchema` are arbitrary JSON Schema (Draft-07) objects and are intentionally left unconstrained here.",
6
+ "type": "object",
7
+ "required": ["basePath", "headers", "errors", "routes"],
8
+ "properties": {
9
+ "specVersion": {
10
+ "type": "integer",
11
+ "minimum": 1,
12
+ "description": "Wire-format version of the envelope. A small monotonic integer, bumped ONLY when the envelope shape changes — not on every package release. Branch on this to pick a parser. ABSENT means treat as 1 (envelopes predating this field). Emitted by ts-procedures servers; optional here so older/hand-built documents still validate."
13
+ },
14
+ "generatorVersion": {
15
+ "type": "string",
16
+ "description": "ts-procedures package version that produced the envelope, e.g. \"10.0.0\". Informational only (telemetry/debugging) — do NOT use it for parse-compatibility decisions; use specVersion."
17
+ },
18
+ "basePath": {
19
+ "type": "string",
20
+ "description": "Path prefix shared by all routes, e.g. \"/api\". May be the empty string."
21
+ },
22
+ "headers": {
23
+ "type": "array",
24
+ "description": "Global HTTP headers documented for the whole API.",
25
+ "items": { "$ref": "#/definitions/HeaderDoc" }
26
+ },
27
+ "errors": {
28
+ "type": "array",
29
+ "description": "Catalog of every error the API may return (framework defaults + user-defined taxonomy). Routes reference these by name via their `errors` array.",
30
+ "items": { "$ref": "#/definitions/ErrorDoc" }
31
+ },
32
+ "routes": {
33
+ "type": "array",
34
+ "description": "All procedures/routes. Discriminated union on `kind`.",
35
+ "items": { "$ref": "#/definitions/AnyRouteDoc" }
36
+ }
37
+ },
38
+ "additionalProperties": true,
39
+ "definitions": {
40
+ "JsonSchema": {
41
+ "description": "An embedded JSON Schema (Draft-07) object describing a payload. Left unconstrained on purpose — render it generically.",
42
+ "type": "object",
43
+ "additionalProperties": true
44
+ },
45
+ "HeaderDoc": {
46
+ "type": "object",
47
+ "required": ["name"],
48
+ "properties": {
49
+ "name": { "type": "string" },
50
+ "description": { "type": "string" },
51
+ "required": { "type": "boolean" },
52
+ "example": { "type": "string" }
53
+ },
54
+ "additionalProperties": true
55
+ },
56
+ "ErrorDoc": {
57
+ "type": "object",
58
+ "required": ["name", "statusCode", "description"],
59
+ "properties": {
60
+ "name": {
61
+ "type": "string",
62
+ "description": "Unique error name. Routes link to it via their `errors` array."
63
+ },
64
+ "statusCode": {
65
+ "type": "integer",
66
+ "description": "HTTP status code returned for this error.",
67
+ "minimum": 100,
68
+ "maximum": 599
69
+ },
70
+ "description": { "type": "string" },
71
+ "schema": {
72
+ "$ref": "#/definitions/JsonSchema",
73
+ "description": "JSON Schema of the error response body. Optional."
74
+ }
75
+ },
76
+ "additionalProperties": true
77
+ },
78
+
79
+ "AnyRouteDoc": {
80
+ "oneOf": [
81
+ { "$ref": "#/definitions/RPCRouteDoc" },
82
+ { "$ref": "#/definitions/StreamRouteDoc" },
83
+ { "$ref": "#/definitions/APIRouteDoc" },
84
+ { "$ref": "#/definitions/HttpStreamRouteDoc" }
85
+ ]
86
+ },
87
+
88
+ "HttpMethod": {
89
+ "description": "HTTP method. Canonical wire form is lowercase; treat case-insensitively when rendering.",
90
+ "type": "string",
91
+ "enum": ["get", "post", "put", "delete", "patch", "head", "GET", "POST", "PUT", "DELETE", "PATCH", "HEAD"]
92
+ },
93
+
94
+ "StreamMode": {
95
+ "type": "string",
96
+ "enum": ["sse", "text"],
97
+ "description": "Stream transport. `sse` = Server-Sent Events; `text` = raw text chunks."
98
+ },
99
+
100
+ "Scope": {
101
+ "description": "Logical grouping. A single segment or an array of segments.",
102
+ "oneOf": [
103
+ { "type": "string" },
104
+ { "type": "array", "items": { "type": "string" } }
105
+ ]
106
+ },
107
+
108
+ "RPCRouteDoc": {
109
+ "title": "RPC route (kind = rpc)",
110
+ "description": "JSON-RPC-style procedure. Always POST. Path embeds scope + version.",
111
+ "type": "object",
112
+ "required": ["kind", "name", "path", "method", "scope", "version", "jsonSchema"],
113
+ "properties": {
114
+ "kind": { "const": "rpc" },
115
+ "name": { "type": "string" },
116
+ "path": { "type": "string", "description": "Full path incl. basePath + version, e.g. /api/users/get-user/1" },
117
+ "method": { "const": "post" },
118
+ "scope": { "$ref": "#/definitions/Scope" },
119
+ "version": { "type": "number" },
120
+ "errors": { "type": "array", "items": { "type": "string" } },
121
+ "jsonSchema": {
122
+ "type": "object",
123
+ "properties": {
124
+ "body": { "$ref": "#/definitions/JsonSchema", "description": "Request body schema (validated)." },
125
+ "response": { "$ref": "#/definitions/JsonSchema", "description": "Response body schema (doc only)." }
126
+ },
127
+ "additionalProperties": true
128
+ }
129
+ },
130
+ "additionalProperties": true
131
+ },
132
+
133
+ "StreamRouteDoc": {
134
+ "title": "RPC stream route (kind = stream)",
135
+ "description": "Streaming RPC procedure. Served over both POST and GET. Path embeds scope + version.",
136
+ "type": "object",
137
+ "required": ["kind", "name", "path", "methods", "streamMode", "scope", "version", "jsonSchema"],
138
+ "properties": {
139
+ "kind": { "const": "stream" },
140
+ "name": { "type": "string" },
141
+ "path": { "type": "string" },
142
+ "methods": {
143
+ "type": "array",
144
+ "description": "Allowed methods, conventionally [\"post\", \"get\"].",
145
+ "items": { "type": "string", "enum": ["get", "post"] }
146
+ },
147
+ "streamMode": { "$ref": "#/definitions/StreamMode" },
148
+ "scope": { "$ref": "#/definitions/Scope" },
149
+ "version": { "type": "number" },
150
+ "errors": { "type": "array", "items": { "type": "string" }, "description": "Pre-stream errors only." },
151
+ "jsonSchema": {
152
+ "type": "object",
153
+ "properties": {
154
+ "params": { "$ref": "#/definitions/JsonSchema", "description": "Query params (GET) or body (POST)." },
155
+ "yieldType": {
156
+ "$ref": "#/definitions/JsonSchema",
157
+ "description": "Schema for each streamed value. In `sse` mode this is the SSE envelope { data, event, id, retry } whose `data` holds the user's yield schema; in `text` mode it is the raw yield schema."
158
+ },
159
+ "returnType": { "$ref": "#/definitions/JsonSchema", "description": "Final return value schema." }
160
+ },
161
+ "additionalProperties": true
162
+ }
163
+ },
164
+ "additionalProperties": true
165
+ },
166
+
167
+ "APIRouteDoc": {
168
+ "title": "REST route (kind = api)",
169
+ "description": "REST-style HTTP endpoint with explicit method + path.",
170
+ "type": "object",
171
+ "required": ["kind", "name", "method", "fullPath", "jsonSchema"],
172
+ "properties": {
173
+ "kind": { "const": "api" },
174
+ "name": { "type": "string" },
175
+ "scope": { "type": "string" },
176
+ "path": { "type": "string", "description": "Path as registered (may include :params), e.g. /users/:id. May be omitted in some envelopes; prefer fullPath." },
177
+ "method": { "$ref": "#/definitions/HttpMethod" },
178
+ "fullPath": { "type": "string", "description": "Resolved path incl. basePath prefix, e.g. /api/users/:id." },
179
+ "successStatus": { "type": "integer", "description": "Status on success. Defaults: POST→201, DELETE→204, else 200." },
180
+ "errors": { "type": "array", "items": { "type": "string" } },
181
+ "jsonSchema": {
182
+ "type": "object",
183
+ "properties": {
184
+ "req": {
185
+ "type": "object",
186
+ "properties": {
187
+ "pathParams": { "$ref": "#/definitions/JsonSchema" },
188
+ "query": { "$ref": "#/definitions/JsonSchema" },
189
+ "body": { "$ref": "#/definitions/JsonSchema" },
190
+ "headers": { "$ref": "#/definitions/JsonSchema" }
191
+ },
192
+ "additionalProperties": true
193
+ },
194
+ "res": {
195
+ "type": "object",
196
+ "properties": {
197
+ "body": { "$ref": "#/definitions/JsonSchema" },
198
+ "headers": { "$ref": "#/definitions/JsonSchema" }
199
+ },
200
+ "additionalProperties": true
201
+ }
202
+ },
203
+ "additionalProperties": true
204
+ }
205
+ },
206
+ "additionalProperties": true
207
+ },
208
+
209
+ "HttpStreamRouteDoc": {
210
+ "title": "REST stream route (kind = http-stream)",
211
+ "description": "REST-style streaming endpoint. Response has no single body; values arrive via `yield`. Currently always SSE.",
212
+ "type": "object",
213
+ "required": ["kind", "name", "method", "fullPath", "streamMode", "jsonSchema"],
214
+ "properties": {
215
+ "kind": { "const": "http-stream" },
216
+ "name": { "type": "string" },
217
+ "scope": { "type": "string" },
218
+ "path": { "type": "string" },
219
+ "method": { "$ref": "#/definitions/HttpMethod" },
220
+ "fullPath": { "type": "string" },
221
+ "streamMode": { "$ref": "#/definitions/StreamMode" },
222
+ "errors": { "type": "array", "items": { "type": "string" } },
223
+ "jsonSchema": {
224
+ "type": "object",
225
+ "properties": {
226
+ "req": {
227
+ "type": "object",
228
+ "properties": {
229
+ "pathParams": { "$ref": "#/definitions/JsonSchema" },
230
+ "query": { "$ref": "#/definitions/JsonSchema" },
231
+ "body": { "$ref": "#/definitions/JsonSchema" },
232
+ "headers": { "$ref": "#/definitions/JsonSchema" }
233
+ },
234
+ "additionalProperties": true
235
+ },
236
+ "res": {
237
+ "type": "object",
238
+ "description": "Response headers only — streaming responses have no single body.",
239
+ "properties": {
240
+ "headers": { "$ref": "#/definitions/JsonSchema" }
241
+ },
242
+ "additionalProperties": true
243
+ },
244
+ "yield": { "$ref": "#/definitions/JsonSchema", "description": "Schema for each streamed value. NOT wrapped in an SSE envelope (unlike kind=stream)." },
245
+ "returnType": { "$ref": "#/definitions/JsonSchema", "description": "Final return value schema." }
246
+ },
247
+ "additionalProperties": true
248
+ }
249
+ },
250
+ "additionalProperties": true
251
+ }
252
+ }
253
+ }
@@ -311,7 +311,33 @@ const docs = new DocRegistry({ errors: appErrors, basePath: '/api' })
311
311
 
312
312
  `http-stream` route docs include the route's `yield` and `res.headers` schemas. (Through v8, live envelopes silently dropped these for `http-stream` routes, so generated clients lost those types unless the envelope was hand-authored; v9 fixed this.)
313
313
 
314
- The `DocRegistry` output is the input for [Client Code Generation](./client-and-codegen.md).
314
+ ### Envelope versioning
315
+
316
+ Every envelope carries two top-level metadata fields so downstream consumers (docs viewers, codegen, dashboards) can reason about format compatibility:
317
+
318
+ ```jsonc
319
+ {
320
+ "specVersion": 1, // wire-format version of the envelope itself
321
+ "generatorVersion": "10.0.0", // ts-procedures package version that produced it
322
+ "basePath": "/api",
323
+ "headers": [],
324
+ "errors": [ /* ... */ ],
325
+ "routes": [ /* ... */ ]
326
+ }
327
+ ```
328
+
329
+ - **`specVersion`** is a small monotonic integer, bumped **only** when the envelope shape changes in a way parsers must react to — never on ordinary package releases. Branch on it. An envelope with no `specVersion` predates the field and must be treated as `1`. The current value is exported as `DOC_ENVELOPE_SPEC_VERSION` from `ts-procedures` and `ts-procedures/server`.
330
+ - **`generatorVersion`** is the producing package version (`DOC_ENVELOPE_GENERATOR_VERSION`, exported from `ts-procedures/server`). It is informational only — for telemetry and "which release served this?" — and must **not** drive parse-compatibility decisions, since it churns on every release regardless of the envelope format.
331
+
332
+ If you'd rather not advertise the exact package version on a public docs endpoint, strip it with the `transform` hook:
333
+
334
+ ```typescript
335
+ app.get('/docs', (c) => c.json(
336
+ builder.toDocEnvelope({ transform: ({ generatorVersion, ...env }) => env }),
337
+ ))
338
+ ```
339
+
340
+ The `DocRegistry` output is the input for [Client Code Generation](./client-and-codegen.md). Building a third-party docs viewer or other tool that consumes the served envelope? The full wire contract — every field, per-kind shapes, and a validatable JSON Schema — is in [DocEnvelope Spec](./doc-envelope-spec-v1.md) (`doc-envelope-v1.schema.json`).
315
341
 
316
342
  ## Type Exports
317
343
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-procedures",
3
- "version": "10.0.0",
3
+ "version": "10.1.1",
4
4
  "description": "A TypeScript RPC framework that creates type-safe, schema-validated procedure calls with a single function definition. Define your procedures once and get full type inference, runtime validation, and framework integration hooks.",
5
5
  "main": "build/exports.js",
6
6
  "types": "build/exports.d.ts",
@@ -3,8 +3,10 @@ import { dirname, resolve } from 'node:path'
3
3
  import { fileURLToPath } from 'node:url'
4
4
  import { describe, expect, it } from 'vitest'
5
5
  import { Type } from 'typebox'
6
+ import pkg from '../../../package.json' with { type: 'json' }
6
7
  import { Procedures } from '../../core/procedures.js'
7
8
  import { HonoAppBuilder, defineErrorTaxonomy } from './index.js'
9
+ import { DOC_ENVELOPE_SPEC_VERSION } from '../../server/spec-version.js'
8
10
  import type { RPCConfig } from '../../server/types.js'
9
11
 
10
12
  /**
@@ -16,12 +18,15 @@ import type { RPCConfig } from '../../server/types.js'
16
18
  * The envelope is codegen's input; if this test fails, generated client output
17
19
  * WILL drift.
18
20
  *
19
- * ONE documented deviation from the raw v8 capture: v8's CreateHttpStream
20
- * dropped the `yield` schema (and `res.headers`) from http-stream route docs —
21
- * the registration stored `yieldType` while the doc builder read `yield`. v9
22
- * fixes that, so the fixture's TailUser route was patched to include the
23
- * `yield` schema the builder now (correctly) emits. Everything else is the
24
- * untouched v8 capture.
21
+ * TWO documented deviations from the raw v8 capture:
22
+ * 1. v8's CreateHttpStream dropped the `yield` schema (and `res.headers`) from
23
+ * http-stream route docs — the registration stored `yieldType` while the
24
+ * doc builder read `yield`. v9 fixes that, so the fixture's TailUser route
25
+ * was patched to include the `yield` schema the builder now emits.
26
+ * 2. v9 adds envelope-level `specVersion`/`generatorVersion` metadata that v8
27
+ * never emitted. These are asserted explicitly below, then stripped before
28
+ * the deep-equal so the rest stays byte-for-byte with the v8 capture.
29
+ * Everything else is the untouched v8 capture.
25
30
  */
26
31
  class TeapotError extends Error {}
27
32
 
@@ -121,6 +126,13 @@ describe('DocEnvelope parity with v8.6.0', () => {
121
126
  const expected = JSON.parse(await readFile(fixturePath, 'utf-8'))
122
127
  const actual = JSON.parse(JSON.stringify(builder.toDocEnvelope()))
123
128
 
129
+ // Deviation #2: assert the new v9 envelope metadata, then strip it so the
130
+ // remaining shape matches the untouched v8 capture.
131
+ expect(actual.specVersion).toBe(DOC_ENVELOPE_SPEC_VERSION)
132
+ expect(actual.generatorVersion).toBe(pkg.version)
133
+ delete actual.specVersion
134
+ delete actual.generatorVersion
135
+
124
136
  expect(actual).toEqual(expected)
125
137
  })
126
138
  })
@@ -1,4 +1,4 @@
1
- import pkg from '../../package.json' with { type: 'json' }
1
+ import { PACKAGE_VERSION } from '../version.js'
2
2
 
3
3
  /**
4
4
  * Stable, version-agnostic token embedded in every generated file (across all
@@ -7,4 +7,4 @@ import pkg from '../../package.json' with { type: 'json' }
7
7
  * the package version (an orphan left by an older release must still match).
8
8
  */
9
9
  export const CODEGEN_SIGNATURE = 'ts-procedures-codegen'
10
- export const CODEGEN_HEADER = `// Auto-generated by ${CODEGEN_SIGNATURE} (v${pkg.version}) — do not edit`
10
+ export const CODEGEN_HEADER = `// Auto-generated by ${CODEGEN_SIGNATURE} (v${PACKAGE_VERSION}) — do not edit`
package/src/exports.ts CHANGED
@@ -56,5 +56,9 @@ export type { Infer, TSchemaLib, TJSONSchema, Prettify } from './schema/json-sch
56
56
 
57
57
  // Doc envelope — offline codegen input
58
58
  export { writeDocEnvelope } from './server/doc-envelope.js'
59
+ // `generatorVersion` metadata lives on `ts-procedures/server`
60
+ // (DOC_ENVELOPE_GENERATOR_VERSION) — it's niche telemetry. The spec version is
61
+ // the one consumers branch on, so it gets the prominent top-level export.
62
+ export { DOC_ENVELOPE_SPEC_VERSION } from './server/spec-version.js'
59
63
  export type { DocEnvelopeSource } from './server/doc-envelope.js'
60
64
  export type { DocEnvelope } from './server/types.js'