Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit b592734

Browse files
jlalmesSeanCassiereNeilTheFisher
authored
Feat: Fastify adapter (#177)
Co-authored-by: SeanCassiere <[email protected]> Co-authored-by: Neil Fisher <[email protected]>
1 parent a51ce1a commit b592734

File tree

21 files changed

+3761
-27118
lines changed

21 files changed

+3761
-27118
lines changed

README.md

+23
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,29 @@ import { appRouter } from './appRouter';
283283
export const openApi = createOpenApiAwsLambdaHandler({ router: appRouter });
284284
```
285285

286+
#### With Fastify
287+
288+
Please see [full example here](examples/with-fastify).
289+
290+
```typescript
291+
import { fastifyTRPCPlugin } from '@trpc/server/adapters/fastify';
292+
import Fastify from 'fastify';
293+
import { fastifyTRPCOpenApiPlugin } from 'trpc-openapi';
294+
295+
import { appRouter } from './router';
296+
297+
const fastify = Fastify();
298+
299+
async function main() {
300+
await fastify.register(fastifyTRPCPlugin, { router: appRouter });
301+
await fastify.register(fastifyTRPCOpenApiPlugin, { router: appRouter }); /* 👈 */
302+
303+
await fastify.listen({ port: 3000 });
304+
}
305+
306+
main();
307+
```
308+
286309
## Types
287310

288311
#### GenerateOpenApiDocumentOptions

examples/with-express/package.json

+8-8
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@
66
"dev": "ts-node-dev --respawn --transpile-only --exit-child ./src/index.ts"
77
},
88
"dependencies": {
9-
"@trpc/server": "^10.9.0",
9+
"@trpc/server": "^10.27.1",
1010
"cors": "^2.8.5",
1111
"express": "^4.18.2",
1212
"jsonwebtoken": "^9.0.0",
13-
"swagger-ui-express": "^4.6.0",
13+
"swagger-ui-express": "^4.6.3",
1414
"uuid": "^9.0.0",
15-
"zod": "^3.20.2"
15+
"zod": "^3.21.4"
1616
},
1717
"devDependencies": {
1818
"@types/cors": "^2.8.13",
19-
"@types/express": "^4.17.16",
20-
"@types/jsonwebtoken": "^9.0.1",
21-
"@types/node": "^18.11.18",
19+
"@types/express": "^4.17.17",
20+
"@types/jsonwebtoken": "^9.0.2",
21+
"@types/node": "^20.2.3",
2222
"@types/swagger-ui-express": "^4.1.3",
23-
"@types/uuid": "^9.0.0",
23+
"@types/uuid": "^9.0.1",
2424
"ts-node": "^10.9.1",
2525
"ts-node-dev": "^2.0.0",
26-
"typescript": "^4.9.4"
26+
"typescript": "^5.0.4"
2727
}
2828
}

examples/with-fastify/README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# [**`trpc-openapi`**](../../README.md) (with-fastify)
2+
3+
### Getting started
4+
5+
Make sure your current working directory is at `/trpc-openapi` root.
6+
7+
```bash
8+
npm install
9+
npm run build
10+
npm run dev -w with-fastify
11+
```

examples/with-fastify/package.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "with-fastify",
3+
"version": "1.0.0",
4+
"private": true,
5+
"scripts": {
6+
"dev": "ts-node-dev --respawn --transpile-only --exit-child ./src/index.ts"
7+
},
8+
"dependencies": {
9+
"@fastify/cors": "^8.2.1",
10+
"@fastify/swagger": "^8.5.1",
11+
"@trpc/server": "^10.27.1",
12+
"fastify": "^4.17.0",
13+
"jsonwebtoken": "^9.0.0",
14+
"uuid": "^9.0.0",
15+
"zod": "^3.21.4"
16+
},
17+
"devDependencies": {
18+
"@types/cors": "^2.8.13",
19+
"@types/express": "^4.17.17",
20+
"@types/jsonwebtoken": "^9.0.2",
21+
"@types/node": "^20.2.3",
22+
"@types/uuid": "^9.0.1",
23+
"ts-node": "^10.9.1",
24+
"ts-node-dev": "^2.0.0",
25+
"typescript": "^5.0.4"
26+
}
27+
}

examples/with-fastify/src/database.ts

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
export type User = {
2+
id: string;
3+
email: string;
4+
passcode: string;
5+
name: string;
6+
};
7+
8+
export type Post = {
9+
id: string;
10+
content: string;
11+
userId: string;
12+
};
13+
14+
export const database: { users: User[]; posts: Post[] } = {
15+
users: [
16+
{
17+
id: '3dcb4a1f-0c91-42c5-834f-26d227c532e2',
18+
19+
passcode: '1234',
20+
name: 'James',
21+
},
22+
{
23+
id: 'ea120573-2eb4-495e-be48-1b2debac2640',
24+
25+
passcode: '9876',
26+
name: 'Alex',
27+
},
28+
{
29+
id: '2ee1c07c-7537-48f5-b5d8-8740e165cd62',
30+
31+
passcode: '1234',
32+
name: 'Sachin',
33+
},
34+
],
35+
posts: [
36+
{
37+
id: 'fc206d47-6d50-4b6a-9779-e9eeaee59aa4',
38+
content: 'Hello world',
39+
userId: '3dcb4a1f-0c91-42c5-834f-26d227c532e2',
40+
},
41+
{
42+
id: 'a10479a2-a397-441e-b451-0b649d15cfd6',
43+
content: 'tRPC is so awesome',
44+
userId: 'ea120573-2eb4-495e-be48-1b2debac2640',
45+
},
46+
{
47+
id: 'de6867c7-13f1-4932-a69b-e96fd245ee72',
48+
content: 'Know the ropes',
49+
userId: '3dcb4a1f-0c91-42c5-834f-26d227c532e2',
50+
},
51+
{
52+
id: '15a742b3-82f6-4fba-9fed-2d1328a4500a',
53+
content: 'Fight fire with fire',
54+
userId: 'ea120573-2eb4-495e-be48-1b2debac2640',
55+
},
56+
{
57+
id: '31afa9ad-bc37-4e74-8d8b-1c1656184a33',
58+
content: 'I ate breakfast today',
59+
userId: '3dcb4a1f-0c91-42c5-834f-26d227c532e2',
60+
},
61+
{
62+
id: '557cb26a-b26e-4329-a5b4-137327616ead',
63+
content: 'Par for the course',
64+
userId: '2ee1c07c-7537-48f5-b5d8-8740e165cd62',
65+
},
66+
],
67+
};

examples/with-fastify/src/index.ts

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
eslint-disable
3+
@typescript-eslint/no-misused-promises,
4+
@typescript-eslint/no-unsafe-argument,
5+
@typescript-eslint/no-explicit-any,
6+
promise/always-return
7+
*/
8+
import cors from '@fastify/cors';
9+
import fastifySwagger from '@fastify/swagger';
10+
import { fastifyTRPCPlugin } from '@trpc/server/adapters/fastify';
11+
import Fastify from 'fastify';
12+
import { fastifyTRPCOpenApiPlugin } from 'trpc-openapi';
13+
14+
import { openApiDocument } from './openapi';
15+
import { appRouter, createContext } from './router';
16+
17+
const app = Fastify();
18+
19+
async function main() {
20+
// Setup CORS
21+
await app.register(cors);
22+
23+
// Handle incoming tRPC requests
24+
await app.register(fastifyTRPCPlugin, {
25+
prefix: '/trpc',
26+
useWss: false,
27+
trpcOptions: { router: appRouter, createContext },
28+
} as any);
29+
30+
// Handle incoming OpenAPI requests
31+
await app.register(fastifyTRPCOpenApiPlugin, {
32+
basePath: '/api',
33+
router: appRouter,
34+
createContext,
35+
});
36+
37+
// Serve the OpenAPI document
38+
app.get('/openapi.json', () => openApiDocument);
39+
40+
// Server Swagger UI
41+
await app.register(fastifySwagger, {
42+
routePrefix: '/docs',
43+
mode: 'static',
44+
specification: { document: openApiDocument },
45+
uiConfig: { displayOperationId: true },
46+
exposeRoute: true,
47+
});
48+
49+
await app
50+
.listen({ port: 3000 })
51+
.then((address) => {
52+
app.swagger();
53+
console.log(`Server started on ${address}\nSwagger UI: http://localhost:3000/docs`);
54+
})
55+
.catch((e) => {
56+
throw e;
57+
});
58+
}
59+
60+
main().catch((err) => {
61+
console.error(err);
62+
process.exit(1);
63+
});

examples/with-fastify/src/openapi.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { generateOpenApiDocument } from 'trpc-openapi';
2+
3+
import { appRouter } from './router';
4+
5+
// Generate OpenAPI schema document
6+
export const openApiDocument = generateOpenApiDocument(appRouter, {
7+
title: 'Example CRUD API',
8+
description: 'OpenAPI compliant REST API built using tRPC with Fastify',
9+
version: '1.0.0',
10+
baseUrl: 'http://localhost:3000/api',
11+
docsUrl: 'https://github.com/jlalmes/trpc-openapi',
12+
tags: ['auth', 'users', 'posts'],
13+
});

0 commit comments

Comments
 (0)