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 9 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 @@ -284,6 +284,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
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.1.0",
"@fastify/swagger": "^7.4.1",
"@trpc/server": "^10.0.0-proxy-beta.18",
"fastify": "^4.8.1",
"jsonwebtoken": "^8.5.1",
"uuid": "^9.0.0",
"zod": "^3.19.1"
},
"devDependencies": {
"@types/cors": "^2.8.12",
"@types/express": "^4.17.14",
"@types/jsonwebtoken": "^8.5.9",
"@types/node": "^18.8.5",
"@types/uuid": "^8.3.4",
"ts-node": "^10.9.1",
"ts-node-dev": "^2.0.0",
"typescript": "^4.8.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