Skip to content

Commit 80b90ea

Browse files
committed
Add README.md
1 parent 3a86e33 commit 80b90ea

File tree

6 files changed

+97
-15
lines changed

6 files changed

+97
-15
lines changed

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: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
<br />
6+
<hr />
7+
</div>
8+
9+
---
10+
11+
## Generate an OpenAPI spec from your tRPC server, in one line of code
12+
13+
### Step 1: Install `trpc-openapi-2`
14+
15+
```
16+
# npm
17+
npm install trpc-openapi-2
18+
19+
# yarn
20+
yarn install trpc-openapi-2
21+
22+
# pnpm
23+
pnpm install trpc-openapi-2
24+
```
25+
26+
### Step 2: Generate OpenAPI spec
27+
28+
```typescript
29+
import { trpcToOpenApi } from "trpc-openapi-2";
30+
31+
// generate OpenAPI spec
32+
const openApiSpec = trpcToOpenApi({
33+
apiTitle: "Your API",
34+
apiVersion: "1.0.0",
35+
basePath: "/trpc",
36+
router: trpcRouter,
37+
});
38+
39+
// express example: serve openapi spec at /openapi.json
40+
app.get("/openapi.json", (_, res) => res.json(openApiSpec));
41+
```
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 } }) // add this
59+
.input(...
60+
});
61+
```

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: 3 additions & 1 deletion
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

@@ -83,12 +84,13 @@ describe("trpcToOpenApi", () => {
8384
.input(z.object({ name: z.string() }))
8485
.query(() => undefined),
8586
}),
86-
}),
87+
})
8788
);
8889

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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@ 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 {
2626
openapi: "3.1.0",
2727
info: { title: apiTitle, version: apiVersion },
2828
paths: getPathsForRouterRecord(
2929
basePath,
30-
router._def.procedures as RouterRecord,
30+
router._def.procedures as RouterRecord
3131
),
3232
};
3333
}
@@ -43,12 +43,12 @@ const PROCEDURE_TYPE_HTTP_METHOD_MAP: Record<
4343

4444
function getPathsForRouterRecord(
4545
basePath: string,
46-
routerRecord: RouterRecord,
46+
routerRecord: RouterRecord
4747
): OpenAPIV3_1.PathsObject {
4848
const paths: OpenAPIV3_1.PathsObject = {};
4949

5050
for (const [procedureName, procedureOrRouterRecord] of entries(
51-
routerRecord,
51+
routerRecord
5252
)) {
5353
Object.assign(
5454
paths,
@@ -58,7 +58,7 @@ function getPathsForRouterRecord(
5858
procedureName: String(procedureName),
5959
procedure: procedureOrRouterRecord,
6060
})
61-
: getPathsForRouterRecord(basePath, procedureOrRouterRecord),
61+
: getPathsForRouterRecord(basePath, procedureOrRouterRecord)
6262
);
6363
}
6464

@@ -95,7 +95,7 @@ function getPathsForProcedure({
9595
const content = {
9696
"application/json": {
9797
schema: zodToJsonSchema(
98-
def.inputs[0] as ZodSchema,
98+
def.inputs[0] as ZodSchema
9999
) as OpenAPIV3.SchemaObject,
100100
},
101101
};
@@ -118,7 +118,7 @@ function getPathsForProcedure({
118118
}
119119

120120
function isProcedure(
121-
maybeProcedure: AnyProcedure | RouterRecord,
121+
maybeProcedure: AnyProcedure | RouterRecord
122122
): maybeProcedure is AnyProcedure {
123123
return (maybeProcedure as AnyProcedure)._def.procedure === true;
124124
}

0 commit comments

Comments
 (0)