|
| 1 | +<div align="center"> |
| 2 | + <h1>trpc-openapi-2</h1> |
| 3 | + <a href="https://www.npmjs.com/package/trpc-openapi-2"><img src="https://img.shields.io/npm/v/trpc-openapi-2.svg?style=flat&color=brightgreen" target="_blank" /></a> |
| 4 | + <a href="./LICENSE"><img src="https://img.shields.io/badge/license-MIT-black" /></a> |
| 5 | +</div> |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## Generate an OpenAPI spec from your tRPC server, in one line of code |
| 10 | + |
| 11 | +### Step 1: Install `trpc-openapi-2` |
| 12 | + |
| 13 | +```bash |
| 14 | +# npm |
| 15 | +npm install trpc-openapi-2 |
| 16 | + |
| 17 | +# yarn |
| 18 | +yarn install trpc-openapi-2 |
| 19 | + |
| 20 | +# pnpm |
| 21 | +pnpm install trpc-openapi-2 |
| 22 | +``` |
| 23 | + |
| 24 | +### Step 2: Generate OpenAPI spec |
| 25 | + |
| 26 | +```typescript |
| 27 | +import { trpcToOpenApi } from "trpc-openapi-2"; |
| 28 | + |
| 29 | +// generate OpenAPI spec |
| 30 | +const openApiSpec = trpcToOpenApi({ |
| 31 | + apiTitle: "Your API", |
| 32 | + apiVersion: "1.0.0", |
| 33 | + basePath: "/trpc", |
| 34 | + router: trpcRouter, |
| 35 | +}); |
| 36 | + |
| 37 | +// express example: serve openapi spec at /openapi.json |
| 38 | +app.get("/openapi.json", (_, res) => res.json(openApiSpec)); |
| 39 | +``` |
| 40 | + |
| 41 | +## Configuring the OpenAPI spec |
| 42 | + |
| 43 | +### Excluding certain procedures |
| 44 | + |
| 45 | +#### Step 1: Add `OpenApiMeta` to your `initTRPC` call: |
| 46 | + |
| 47 | +```typescript |
| 48 | +import { OpenApiMeta } from "trpc-openapi-2"; |
| 49 | + |
| 50 | +const t = initTRPC.meta<OpenApiMeta>().create(); |
| 51 | +``` |
| 52 | + |
| 53 | +#### Step 2: Use .meta() in your procedure |
| 54 | + |
| 55 | +```typescript |
| 56 | +const router = t.router({ |
| 57 | + myProcedure: t.procedure |
| 58 | + .meta({ openapi: { ignore: true } }) /* 👈 */ |
| 59 | + .input(... |
| 60 | +}); |
| 61 | +``` |
| 62 | +
|
| 63 | +## Comparison |
| 64 | +
|
| 65 | +[`trpc-openapi`](https://github.com/trpc/trpc-openapi) |
| 66 | +and its new fork [`trpc-to-openapi`](https://github.com/mcampa/trpc-to-openapi) |
| 67 | +are the two relevant libraries. |
| 68 | +
|
| 69 | +### They modify your API by adding new endpoints |
| 70 | +
|
| 71 | +**These other libraries do not simply generate an OpenAPI spec for your existing tRPC server.** |
| 72 | +They add _new endpoints_ to your server and then generate an OpenAPI spec for those new endpoints. |
| 73 | +
|
| 74 | +For example: |
| 75 | +
|
| 76 | +```typescript |
| 77 | +// trpc-openapi example: |
| 78 | + |
| 79 | +export const appRouter = t.router({ |
| 80 | + sayHello: t.procedure |
| 81 | + |
| 82 | + // trpc-openapi adds a new endpoint to your server (`/say-hello`) |
| 83 | + // and the generated OpenAPI spec only includes this new `/say-hello` |
| 84 | + // endpoint, not the original `/trpc/sayHello` procedure |
| 85 | + .meta({ /* 👉 */ openapi: { method: "GET", path: "/say-hello" } }), |
| 86 | +}); |
| 87 | +``` |
| 88 | +
|
| 89 | +In comparison, `trpc-openapi-2` simply generates an OpenAPI spec for your existing tRPC API, |
| 90 | +without modifying your API functionality at all. |
| 91 | +
|
| 92 | +### They require you to use `.meta()` on every procedure |
| 93 | +
|
| 94 | +These libraries require that you add `.meta()` to every procedure that you |
| 95 | +want included in your OpenAPI spec. In comparison, with `trpc-openapi-2` you can generate |
| 96 | +the full OpenAPI spec by calling `trpcToOpenApi()` without modifying your procedures at all. |
0 commit comments