@trpc/openapi 11.22.2-canary.16.ea96ec538 → 11.22.2-canary.37.6d6969c7c

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
@@ -2,7 +2,7 @@
2
2
  "name": "@trpc/openapi",
3
3
  "type": "module",
4
4
  "sideEffects": false,
5
- "version": "11.22.2-canary.16.ea96ec538",
5
+ "version": "11.22.2-canary.37.6d6969c7c",
6
6
  "description": "OpenAPI document generator for tRPC routers",
7
7
  "author": "KATT",
8
8
  "license": "MIT",
@@ -17,6 +17,7 @@
17
17
  },
18
18
  "scripts": {
19
19
  "build": "tsdown",
20
+ "prepack": "node ../../www/build/skills.ts --package .",
20
21
  "dev": "tsdown --watch",
21
22
  "lint": "oxlint src",
22
23
  "codegen": "tsx test/scripts/codegen.run.ts",
@@ -56,7 +57,8 @@
56
57
  "!**/*.test.*",
57
58
  "!**/__tests__",
58
59
  "!test",
59
- "heyapi"
60
+ "heyapi",
61
+ "skills"
60
62
  ],
61
63
  "publishConfig": {
62
64
  "access": "public"
@@ -67,7 +69,8 @@
67
69
  "devDependencies": {
68
70
  "@hey-api/openapi-ts": "^0.99.0",
69
71
  "@swagger-api/apidom-ls": "^1.6.0",
70
- "@trpc/server": "11.22.2-canary.16.ea96ec538",
72
+ "@tanstack/intent": "0.5.6",
73
+ "@trpc/server": "11.22.2-canary.37.6d6969c7c",
71
74
  "@types/node": "^24.13.4",
72
75
  "bson": "^7.2.0",
73
76
  "ion-js": "^5.2.1",
@@ -82,7 +85,7 @@
82
85
  },
83
86
  "peerDependencies": {
84
87
  "@hey-api/openapi-ts": ">=0.94.5",
85
- "@trpc/server": "11.22.2-canary.16.ea96ec538",
88
+ "@trpc/server": "11.22.2-canary.37.6d6969c7c",
86
89
  "typescript": ">=5.8.2 <7",
87
90
  "zod": ">=4.0.0"
88
91
  },
@@ -97,5 +100,8 @@
97
100
  "funding": [
98
101
  "https://trpc.io/sponsor"
99
102
  ],
100
- "gitHead": "ea96ec538fd7710ee79930bdd6fc16a2a702afaf"
103
+ "keywords": [
104
+ "tanstack-intent"
105
+ ],
106
+ "gitHead": "6d6969c7cd8eb140c8dcc42e33750b4e88eaaa0d"
101
107
  }
@@ -0,0 +1,365 @@
1
+ ---
2
+ name: openapi
3
+ description: Generate OpenAPI 3.1 spec from a tRPC router with @trpc/openapi CLI (trpc-openapi) or generateOpenAPIDocument(). Generate typed REST client with @hey-api/openapi-ts, createTRPCHeyApiTypeResolvers() and configureTRPCHeyApiClient(), and read results from result.data. Configure transformers (superjson, EJSON) for generated clients. Compare spec versions with oasdiff. Alpha status.
4
+ requires:
5
+ - server-setup
6
+ sources:
7
+ - www/v11/docs/openapi.md
8
+ metadata:
9
+ type: composition
10
+ library: trpc
11
+ library_version: 11.22.2-canary.37.6d6969c7c
12
+ ---
13
+
14
+ <!-- Generated from www/v11/docs/openapi.md by www/build/skills.ts. Edit the docs instead. -->
15
+
16
+ # OpenAPI (alpha)
17
+
18
+ From https://trpc.io/docs/openapi
19
+
20
+ > [!WARNING] Caution
21
+ > This package is in alpha. APIs may change without notice.
22
+
23
+
24
+ The `@trpc/openapi` package generates an OpenAPI 3.1 specification from your tRPC router. Use the spec to:
25
+
26
+ - Generate a typed API client in any language
27
+ - Call tRPC endpoints via HTTP tools like Postman or Insomnia
28
+ - Enable AI agent integrations such as MCP servers
29
+
30
+ ## Install
31
+
32
+ ```bash
33
+ pnpm add @trpc/openapi
34
+ ```
35
+
36
+ > [!NOTE]
37
+ > `@trpc/openapi` is currently versioned like 11.x.x-alpha, and should work with any recent tRPC v11 version, but as always we recommend aligning the version numbers
38
+
39
+
40
+ ## Adapting your tRPC setup
41
+
42
+ The generator works with your existing router — no annotations or decorators required. A few things to be aware of:
43
+
44
+ - **No output types needed** — unlike other OpenAPI tools, `.output()` schemas are optional. The generator infers return types from your implementation automatically.
45
+ - **Transformers** — if your server uses a [data transformer](https://trpc.io/docs/server/data-transformers), your OpenAPI clients must use the same one. See [Transformers](https://trpc.io/docs/openapi#transformers) for setup and cross-language options.
46
+ - **Subscriptions** — currently excluded from the generated spec. SSE support is planned.
47
+ - **Descriptions** — Zod `.describe()` calls and JSDoc comments on types, routers, and procedures, all become `description` fields in the spec.
48
+
49
+ ## Generate the spec
50
+
51
+ ### CLI
52
+
53
+ ```bash
54
+ pnpm exec trpc-openapi ./src/server/router.ts
55
+ ```
56
+
57
+ | Option | Default | Description |
58
+ | --------------------- | -------------- | -------------------------------------------------------------------- |
59
+ | `-e, --export <name>` | `AppRouter` | Name of the exported router type |
60
+ | `-o, --output <file>` | `openapi.json` | Output file path |
61
+ | `--title <text>` | `tRPC API` | OpenAPI `info.title` |
62
+ | `--version <ver>` | `0.0.0` | OpenAPI `info.version` |
63
+ | `--server-url <url>` | | Base URL (including any prefix), e.g. `https://api.example.com/trpc` |
64
+
65
+ ```bash
66
+ pnpm exec trpc-openapi ./src/server/router.ts -o api.json --title "My API" --version 1.0.0 --server-url https://api.example.com/trpc
67
+ ```
68
+
69
+ ### Programmatic
70
+
71
+ ```ts title="scripts/generate-openapi.ts"
72
+ import { generateOpenAPIDocument } from '@trpc/openapi';
73
+
74
+ const doc = await generateOpenAPIDocument('./src/server/router.ts', {
75
+ exportName: 'AppRouter',
76
+ title: 'My API',
77
+ version: '1.0.0',
78
+ servers: [{ url: 'https://api.example.com/trpc' }],
79
+ });
80
+ ```
81
+
82
+ The generator builds the spec by statically analysing your router's TypeScript types. It also tries to import the router file to pick up Zod `.describe()` text, and carries on from the types alone if that import fails. Because of that, point it at a module that only defines the router, not one that starts your server.
83
+
84
+ ## Generate a client from the spec
85
+
86
+ Any OpenAPI client generator should work, but the most tested integration is with [Hey API](https://heyapi.dev/openapi-ts/get-started).
87
+
88
+ A generated client will produce typed SDK functions matching your tRPC procedures:
89
+
90
+ - **Queries** → `GET /procedure.path`
91
+ - **Mutations** → `POST /procedure.path`
92
+ - **Subscriptions** are ignored (SSE coming soon)
93
+
94
+ ### Hey API (TypeScript)
95
+
96
+ [Hey API Documentation](https://heyapi.dev/)
97
+
98
+ ```bash
99
+ pnpm add @trpc/openapi @hey-api/openapi-ts
100
+ ```
101
+
102
+ Out of the box, an OpenAPI-generated client won't know about your transformer setup or how to encode query parameters. The `@trpc/openapi/heyapi` package provides a `configureTRPCHeyApiClient` helper that bridges this gap — it configures request serialisation, response parsing, and error deserialization so the generated SDK works correctly with tRPC endpoints.
103
+
104
+ #### Without a transformer
105
+
106
+ You can generate your client using Hey API's CLI or programmatic API in this case
107
+
108
+ ```bash
109
+ pnpm exec openapi-ts -i openapi.json -o ./generated
110
+ ```
111
+
112
+ Next a little configuration is required at runtime:
113
+
114
+ ```ts title="src/usage.ts"
115
+ import { configureTRPCHeyApiClient } from '@trpc/openapi/heyapi';
116
+ import { client } from './generated/client.gen';
117
+ import { Sdk } from './generated/sdk.gen';
118
+
119
+ configureTRPCHeyApiClient(client, {
120
+ baseUrl: 'http://localhost:3000',
121
+ });
122
+ const sdk = new Sdk({ client });
123
+
124
+ const result = await sdk.greeting({ query: { input: { name: 'World' } } });
125
+ const user = await sdk.user.create({ body: { name: 'Bob', age: 30 } });
126
+ ```
127
+
128
+ #### With a transformer (superjson, devalue, etc.)
129
+
130
+ > [!WARNING]
131
+ > If your backend uses a [data transformer](https://trpc.io/docs/server/data-transformers) like `superjson`, you **must** pass it to the client config. Without this, Dates, Maps, Sets, and other non-JSON types may be silently wrong.
132
+
133
+
134
+ First generate your client code using Hey API's programmatic API, this way you can use `createTRPCHeyApiTypeResolvers` to ensure your emitted types are correct:
135
+
136
+ ```ts title="src/client.ts"
137
+ import { createClient } from '@hey-api/openapi-ts';
138
+ import { createTRPCHeyApiTypeResolvers } from '@trpc/openapi/heyapi';
139
+
140
+ const openApiJson = './path/to/openapi.json'
141
+ const outputDir = './generated'
142
+
143
+ await createClient({
144
+ input: openApiJson,
145
+ output: outputDir,
146
+ plugins: [
147
+ {
148
+ name: '@hey-api/typescript',
149
+ // Important: this ensures that your emitted types like Dates are correct
150
+ '~resolvers': createTRPCHeyApiTypeResolvers(),
151
+ },
152
+ {
153
+ name: '@hey-api/sdk',
154
+ operations: { strategy: 'single' },
155
+ },
156
+ ],
157
+ });
158
+ ```
159
+
160
+ At runtime configure the generated client with your transformer, you can then pass native types directly and get them back deserialised:
161
+
162
+ ```ts title="src/usage.ts"
163
+ import { configureTRPCHeyApiClient } from '@trpc/openapi/heyapi';
164
+ import superjson from 'superjson';
165
+ import { client } from './generated/client.gen';
166
+ import { Sdk } from './generated/sdk.gen';
167
+
168
+ configureTRPCHeyApiClient(client, {
169
+ baseUrl: 'http://localhost:3000',
170
+ // Important, this transformer must match your tRPC API's transformer:
171
+ transformer: superjson,
172
+ });
173
+ const sdk = new Sdk({ client });
174
+
175
+ const event = await sdk.getEvent({
176
+ query: { input: { id: 'evt_1', at: new Date('2025-06-15T10:00:00Z') } },
177
+ });
178
+ // event.data.result.data.at is a Date object ✅
179
+
180
+ const created = await sdk.createEvent({
181
+ body: { name: 'Conference', at: new Date('2025-09-01T09:00:00Z') },
182
+ });
183
+ ```
184
+
185
+ #### Reading results and errors
186
+
187
+ The SDK returns tRPC's [response envelope](https://trpc.io/docs/rpc#http-response-specification), so a procedure's return value is under `result.data`. A failed call resolves with `error` set to the tRPC error envelope instead of throwing:
188
+
189
+ ```ts title="src/usage.ts"
190
+ import { configureTRPCHeyApiClient } from '@trpc/openapi/heyapi';
191
+ import { client } from './generated/client.gen';
192
+ import { Sdk } from './generated/sdk.gen';
193
+
194
+ configureTRPCHeyApiClient(client, { baseUrl: 'http://localhost:3000' });
195
+ const sdk = new Sdk({ client });
196
+
197
+ const { data, error } = await sdk.user.byId({ query: { input: '1' } });
198
+ if (error) {
199
+ console.log(error.error.data.code); // e.g. 'NOT_FOUND'
200
+ } else {
201
+ console.log(data?.result.data); // what the procedure returned
202
+ }
203
+ ```
204
+
205
+ ### Using a different generator or language
206
+
207
+ The generated OpenAPI spec works with any OpenAPI-compatible client generator which can:
208
+
209
+ - Emit accurate types for classes like Date
210
+ - Support customising Search Params and request/response body serialization
211
+
212
+ To integrate correctly with tRPC's protocol, you need to set up your generated client to do two things:
213
+
214
+ - **Transformers** — If your tRPC API uses a transformer, the client must serialise inputs and deserialise outputs using the same format
215
+ - **Query Inputs** — GET requests encode input as `?input=<JSON>`, not as individual query parameters
216
+
217
+ See the [Hey API config source](https://github.com/trpc/trpc/blob/f346e9bb97ff3c8a7e874f59110a47730293097a/packages/openapi/src/heyapi/index.ts) for a complete reference implementation.
218
+
219
+ ### Transformers
220
+
221
+ tRPC [data transformers](https://trpc.io/docs/server/data-transformers) let you send rich types like `Date`, `Map`, `Set`, and `BigInt` over the wire. When using the OpenAPI client, the same transformer must be configured on both the server and client so that inputs are serialised and outputs are deserialised correctly.
222
+
223
+ Any transformer that implements the tRPC `DataTransformer` interface (`serialize` / `deserialize`) works with `configureTRPCHeyApiClient`. Below are some tested options.
224
+
225
+ #### SuperJSON
226
+
227
+ The most popular transformer for TypeScript-to-TypeScript setups. Handles `Date`, `Map`, `Set`, `BigInt`, `RegExp`, and more.
228
+
229
+ ```bash
230
+ pnpm add superjson
231
+ ```
232
+
233
+ ```ts title="src/server.ts"
234
+ import { initTRPC } from '@trpc/server';
235
+ import superjson from 'superjson';
236
+
237
+ const t = initTRPC.create({ transformer: superjson });
238
+ ```
239
+
240
+ ```ts title="src/client.ts"
241
+ import { configureTRPCHeyApiClient } from '@trpc/openapi/heyapi';
242
+ import superjson from 'superjson';
243
+ import { client } from './generated/client.gen';
244
+
245
+ configureTRPCHeyApiClient(client, {
246
+ baseUrl: 'http://localhost:3000',
247
+ transformer: superjson,
248
+ });
249
+ ```
250
+
251
+ See the [superjson test](https://github.com/trpc/trpc/blob/main/packages/openapi/test/generate.test.ts) for a full end-to-end example.
252
+
253
+ #### MongoDB Extended JSON v2
254
+
255
+ [EJSON](https://www.mongodb.com/docs/manual/reference/mongodb-extended-json/#mongodb-extended-json-v2-usage) is a good choice when you need cross-language support. The `bson` npm package provides `EJSON.serialize` / `EJSON.deserialize` which map directly to a tRPC `DataTransformer`.
256
+
257
+ Available in: C, C#, C++, Go, Java, Node.js, Perl, PHP, Python, Ruby, Scala
258
+
259
+ ```bash
260
+ pnpm add bson
261
+ ```
262
+
263
+ ```ts title="src/transformer.ts"
264
+ import { EJSON } from 'bson';
265
+ import type { Document } from 'bson';
266
+ import type { TRPCDataTransformer } from '@trpc/server';
267
+
268
+ export const ejsonTransformer: TRPCDataTransformer = {
269
+ serialize: (value) => EJSON.serialize(value),
270
+ deserialize: (value) => EJSON.deserialize(value as Document),
271
+ };
272
+ ```
273
+
274
+ ```ts title="src/client.ts"
275
+ import { configureTRPCHeyApiClient } from '@trpc/openapi/heyapi';
276
+ import { client } from './generated/client.gen';
277
+ import { ejsonTransformer } from './transformer';
278
+
279
+ configureTRPCHeyApiClient(client, {
280
+ baseUrl: 'http://localhost:3000',
281
+ transformer: ejsonTransformer,
282
+ });
283
+ ```
284
+
285
+ See the [MongoDB EJSON test](https://github.com/trpc/trpc/blob/main/packages/openapi/test/mongoEjson.test.ts) for a full end-to-end example.
286
+
287
+ #### Amazon Ion
288
+
289
+ [Amazon Ion](https://amazon-ion.github.io/ion-docs/) is a richly-typed data format with broad language support. It doesn't directly support the `TRPCDataTransformer` interface and requires a bit of boilerplate to make work with tRPC in JS/TS, but may be a good choice for your own system.
290
+
291
+ Available in: C, C#, D, Go, Java, JavaScript, PHP, Python, Rust
292
+
293
+ ```bash
294
+ pnpm add ion-js
295
+ ```
296
+
297
+ See the [Amazon Ion test](https://github.com/trpc/trpc/blob/main/packages/openapi/test/amazonIon.test.ts) for the transformer implementation, boilerplate, and a full end-to-end example.
298
+
299
+ #### Writing a custom transformer
300
+
301
+ Any object with `serialize` and `deserialize` methods works:
302
+
303
+ ```ts
304
+ import type { TRPCDataTransformer } from '@trpc/server';
305
+
306
+ const myTransformer: TRPCDataTransformer = {
307
+ serialize: (value) => {
308
+ /* encode rich types */
309
+ },
310
+ deserialize: (value) => {
311
+ /* decode them back */
312
+ },
313
+ };
314
+ ```
315
+
316
+ Pass it to both `initTRPC.create({ transformer })` on the server and `configureTRPCHeyApiClient(client, { transformer })` on the client. See the [data transformers docs](https://trpc.io/docs/server/data-transformers) for more details.
317
+
318
+ ### Full example
319
+
320
+ For a complete, runnable project that ties all of these steps together, see the [openapi-codegen example](https://github.com/trpc/trpc/tree/main/examples/openapi-codegen). Its [codegen script](https://github.com/trpc/trpc/blob/main/examples/openapi-codegen/src/scripts/codegen.ts) generates the spec with `generateOpenAPIDocument` and then the SDK with Hey API in one step.
321
+
322
+ ## API changelog and breaking-change checks with `oasdiff`
323
+
324
+ After generating OpenAPI specs, you can compare two versions with [`oasdiff`](https://github.com/oasdiff/oasdiff).
325
+
326
+ For installation options (Homebrew, Docker, binaries, etc), see the official docs:
327
+
328
+ - [oasdiff install docs](https://github.com/oasdiff/oasdiff/blob/main/docs/README.md)
329
+ - [oasdiff changelog and breaking checks](https://github.com/oasdiff/oasdiff/blob/main/docs/BREAKING-CHANGES.md)
330
+
331
+ Using this you can quick generate changelogs or detect inadvertent breaking changes to APIs to help plan and coordinate releases
332
+
333
+ ```sh
334
+ # Get a complete changelog including minor and breaking changes
335
+ $ oasdiff changelog packages/openapi/test/routers/superjsonRouter.openapi.json /tmp/superjsonRouter.openapi.next.json
336
+
337
+ 3 changes: 2 error, 0 warning, 1 info
338
+ error [new-required-request-property] in API POST /createEvent
339
+ added the new required request property 'location'
340
+ error [api-path-removed-without-deprecation] in API GET /getBigInt
341
+ api path removed without deprecation
342
+ info [endpoint-added] in API GET /health
343
+ endpoint added
344
+
345
+ # Get a list of breaking changes if any
346
+ $ oasdiff breaking packages/openapi/test/routers/superjsonRouter.openapi.json /tmp/superjsonRouter.openapi.next.json
347
+
348
+ 2 changes: 2 error, 0 warning, 0 info
349
+ error [new-required-request-property] in API POST /createEvent
350
+ added the new required request property 'location'
351
+ error [api-path-removed-without-deprecation] in API GET /getBigInt
352
+ api path removed without deprecation
353
+ ```
354
+
355
+ ## Common mistakes
356
+
357
+ - **Using the wrong export name.** `--export` (`exportName`) defaults to `AppRouter`. If your file exports the router as `appRouter`, pass `-e appRouter`. When the name isn't found, the error lists the file's exports.
358
+ - **Leaving the tRPC prefix out of the base URL.** The spec's paths are relative to where tRPC is mounted (`/user.create`), so if your adapter serves tRPC under `/trpc`, both `--server-url` and the client's `baseUrl` need to end in `/trpc`.
359
+ - **Skipping `createTRPCHeyApiTypeResolvers()` when you use a transformer.** Without it, Hey API types `Date` fields as `string` and `bigint` fields as `number`.
360
+ - **Configuring a transformer on the server only.** `configureTRPCHeyApiClient` needs the same `transformer` as your `initTRPC`, or rich types arrive still encoded.
361
+ - **Expecting subscriptions in the spec.** The generator skips subscription procedures.
362
+
363
+ ## Related skills
364
+
365
+ - `server-setup` (https://trpc.io/docs/server/overview): `npx @tanstack/intent@latest load @trpc/server#server-setup`