Skip to content

Commit bffd67e

Browse files
authored
Add README.md (#5)
1 parent 3a86e33 commit bffd67e

File tree

8 files changed

+130
-9
lines changed

8 files changed

+130
-9
lines changed

.prettierrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"printWidth": 80,
3+
"trailingComma": "all"
4+
}

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Alex Johansson
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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.

eslint.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ import tseslint from "typescript-eslint";
55

66
export default tseslint.config(
77
eslint.configs.recommended,
8-
tseslint.configs.strict
8+
tseslint.configs.strict,
99
);

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
{
22
"name": "trpc-openapi-2",
33
"version": "1.0.0",
4-
"description": "Generate an OpenAPI spec from your trpc server, in one line of code",
4+
"description": "Generate an OpenAPI spec from your tRPC server, in one line of code",
55
"main": "lib/index.js",
66
"files": [
77
"lib",
8-
"!lib/**/__test__"
8+
"!lib/**/__test__",
9+
"README.md"
910
],
1011
"scripts": {
1112
"build": "tsc",

src/__test__/trpcToOpenApi.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ describe("trpcToOpenApi", () => {
1919
const openApiSpec = trpcToOpenApi({
2020
apiTitle: "My API",
2121
apiVersion: "1.0",
22+
basePath: "",
2223
router,
2324
});
2425

@@ -89,6 +90,7 @@ describe("trpcToOpenApi", () => {
8990
const openApiSpec = trpcToOpenApi({
9091
apiTitle: "My API",
9192
apiVersion: "1.0",
93+
basePath: "",
9294
router,
9395
});
9496

src/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import { trpcToOpenApi } from "./trpcToOpenApi";
2-
3-
export { trpcToOpenApi };
4-
export default trpcToOpenApi;
1+
export { trpcToOpenApi } from "./trpcToOpenApi";
52

63
export { type OpenApiMeta } from "./meta";

src/trpcToOpenApi.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import { entries } from "./utils";
1414
export function trpcToOpenApi({
1515
apiTitle,
1616
apiVersion,
17-
basePath = "",
17+
basePath,
1818
router,
1919
}: {
2020
apiTitle: string;
2121
apiVersion: string;
22-
basePath?: string;
22+
basePath: string;
2323
router: AnyTRPCRouter;
2424
}): OpenAPIV3_1.Document {
2525
return {

0 commit comments

Comments
 (0)