This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: SeanCassiere <[email protected]> Co-authored-by: Neil Fisher <[email protected]>
- Loading branch information
1 parent
a51ce1a
commit b592734
Showing
21 changed files
with
3,761 additions
and
27,118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
}); |
Oops, something went wrong.