Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Fastify adapter #177

Merged
merged 15 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,29 @@ import { appRouter } from './appRouter';
export const openApi = createOpenApiAwsLambdaHandler({ router: appRouter });
```

#### With Fastify

Please see [full example here](examples/with-fastify).

```typescript
import { fastifyTRPCPlugin } from '@trpc/server/adapters/fastify';
import Fastify from 'fastify';
import { fastifyTRPCOpenApiPlugin } from 'trpc-openapi';

import { appRouter } from './router';

const fastify = Fastify();

async function main() {
await fastify.register(fastifyTRPCPlugin, { router: appRouter });
await fastify.register(fastifyTRPCOpenApiPlugin, { router: appRouter }); /* 👈 */

await fastify.listen({ port: 3000 });
}

main();
```

## Types

#### GenerateOpenApiDocumentOptions
Expand Down
16 changes: 8 additions & 8 deletions examples/with-express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
"dev": "ts-node-dev --respawn --transpile-only --exit-child ./src/index.ts"
},
"dependencies": {
"@trpc/server": "^10.9.0",
"@trpc/server": "^10.27.1",
"cors": "^2.8.5",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.0",
"swagger-ui-express": "^4.6.0",
"swagger-ui-express": "^4.6.3",
"uuid": "^9.0.0",
"zod": "^3.20.2"
"zod": "^3.21.4"
},
"devDependencies": {
"@types/cors": "^2.8.13",
"@types/express": "^4.17.16",
"@types/jsonwebtoken": "^9.0.1",
"@types/node": "^18.11.18",
"@types/express": "^4.17.17",
"@types/jsonwebtoken": "^9.0.2",
"@types/node": "^20.2.3",
"@types/swagger-ui-express": "^4.1.3",
"@types/uuid": "^9.0.0",
"@types/uuid": "^9.0.1",
"ts-node": "^10.9.1",
"ts-node-dev": "^2.0.0",
"typescript": "^4.9.4"
"typescript": "^5.0.4"
}
}
11 changes: 11 additions & 0 deletions examples/with-fastify/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# [**`trpc-openapi`**](../../README.md) (with-fastify)

### Getting started

Make sure your current working directory is at `/trpc-openapi` root.

```bash
npm install
npm run build
npm run dev -w with-fastify
```
27 changes: 27 additions & 0 deletions examples/with-fastify/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "with-fastify",
"version": "1.0.0",
"private": true,
"scripts": {
"dev": "ts-node-dev --respawn --transpile-only --exit-child ./src/index.ts"
},
"dependencies": {
"@fastify/cors": "^8.2.1",
"@fastify/swagger": "^8.5.1",
"@trpc/server": "^10.27.1",
"fastify": "^4.17.0",
"jsonwebtoken": "^9.0.0",
"uuid": "^9.0.0",
"zod": "^3.21.4"
},
"devDependencies": {
"@types/cors": "^2.8.13",
"@types/express": "^4.17.17",
"@types/jsonwebtoken": "^9.0.2",
"@types/node": "^20.2.3",
"@types/uuid": "^9.0.1",
"ts-node": "^10.9.1",
"ts-node-dev": "^2.0.0",
"typescript": "^5.0.4"
}
}
67 changes: 67 additions & 0 deletions examples/with-fastify/src/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
export type User = {
id: string;
email: string;
passcode: string;
name: string;
};

export type Post = {
id: string;
content: string;
userId: string;
};

export const database: { users: User[]; posts: Post[] } = {
users: [
{
id: '3dcb4a1f-0c91-42c5-834f-26d227c532e2',
email: '[email protected]',
passcode: '1234',
name: 'James',
},
{
id: 'ea120573-2eb4-495e-be48-1b2debac2640',
email: '[email protected]',
passcode: '9876',
name: 'Alex',
},
{
id: '2ee1c07c-7537-48f5-b5d8-8740e165cd62',
email: '[email protected]',
passcode: '1234',
name: 'Sachin',
},
],
posts: [
{
id: 'fc206d47-6d50-4b6a-9779-e9eeaee59aa4',
content: 'Hello world',
userId: '3dcb4a1f-0c91-42c5-834f-26d227c532e2',
},
{
id: 'a10479a2-a397-441e-b451-0b649d15cfd6',
content: 'tRPC is so awesome',
userId: 'ea120573-2eb4-495e-be48-1b2debac2640',
},
{
id: 'de6867c7-13f1-4932-a69b-e96fd245ee72',
content: 'Know the ropes',
userId: '3dcb4a1f-0c91-42c5-834f-26d227c532e2',
},
{
id: '15a742b3-82f6-4fba-9fed-2d1328a4500a',
content: 'Fight fire with fire',
userId: 'ea120573-2eb4-495e-be48-1b2debac2640',
},
{
id: '31afa9ad-bc37-4e74-8d8b-1c1656184a33',
content: 'I ate breakfast today',
userId: '3dcb4a1f-0c91-42c5-834f-26d227c532e2',
},
{
id: '557cb26a-b26e-4329-a5b4-137327616ead',
content: 'Par for the course',
userId: '2ee1c07c-7537-48f5-b5d8-8740e165cd62',
},
],
};
63 changes: 63 additions & 0 deletions examples/with-fastify/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
eslint-disable
@typescript-eslint/no-misused-promises,
@typescript-eslint/no-unsafe-argument,
@typescript-eslint/no-explicit-any,
promise/always-return
*/
import cors from '@fastify/cors';
import fastifySwagger from '@fastify/swagger';
import { fastifyTRPCPlugin } from '@trpc/server/adapters/fastify';
import Fastify from 'fastify';
import { fastifyTRPCOpenApiPlugin } from 'trpc-openapi';

import { openApiDocument } from './openapi';
import { appRouter, createContext } from './router';

const app = Fastify();

async function main() {
// Setup CORS
await app.register(cors);

// Handle incoming tRPC requests
await app.register(fastifyTRPCPlugin, {
prefix: '/trpc',
useWss: false,
trpcOptions: { router: appRouter, createContext },
} as any);

// Handle incoming OpenAPI requests
await app.register(fastifyTRPCOpenApiPlugin, {
basePath: '/api',
router: appRouter,
createContext,
});

// Serve the OpenAPI document
app.get('/openapi.json', () => openApiDocument);

// Server Swagger UI
await app.register(fastifySwagger, {
routePrefix: '/docs',
mode: 'static',
specification: { document: openApiDocument },
uiConfig: { displayOperationId: true },
exposeRoute: true,
});

await app
.listen({ port: 3000 })
.then((address) => {
app.swagger();
console.log(`Server started on ${address}\nSwagger UI: http://localhost:3000/docs`);
})
.catch((e) => {
throw e;
});
}

main().catch((err) => {
console.error(err);
process.exit(1);
});
13 changes: 13 additions & 0 deletions examples/with-fastify/src/openapi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { generateOpenApiDocument } from 'trpc-openapi';

import { appRouter } from './router';

// Generate OpenAPI schema document
export const openApiDocument = generateOpenApiDocument(appRouter, {
title: 'Example CRUD API',
description: 'OpenAPI compliant REST API built using tRPC with Fastify',
version: '1.0.0',
baseUrl: 'http://localhost:3000/api',
docsUrl: 'https://github.com/jlalmes/trpc-openapi',
tags: ['auth', 'users', 'posts'],
});
Loading