-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate tRPC from Next.js app (#930)
- Loading branch information
Showing
32 changed files
with
1,131 additions
and
272 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,10 @@ | ||
import { env } from "@dotkomonline/env" | ||
import { createProxyRoute } from "@dotkomonline/proxy-nextjs" | ||
|
||
const handler = createProxyRoute({ | ||
mountPath: "/api/trpc", | ||
apiEndpoint: env.RPC_HOST, | ||
}) | ||
|
||
export const GET = handler | ||
export const POST = handler |
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,4 @@ | ||
{ | ||
"$schema": "./node_modules/@biomejs/biome/configuration_schema.json", | ||
"extends": ["../../biome.json"] | ||
} |
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,30 @@ | ||
{ | ||
"name": "@dotkomonline/rpc", | ||
"version": "0.1.0", | ||
"private": true, | ||
"type": "module", | ||
"scripts": { | ||
"dev": "tsx src/index.ts", | ||
"build": "tsc", | ||
"lint": "biome check . --apply", | ||
"lint-check": "biome check .", | ||
"type-check": "tsc --noEmit" | ||
}, | ||
"dependencies": { | ||
"@dotkomonline/env": "workspace:*", | ||
"@dotkomonline/gateway-trpc": "workspace:*", | ||
"@fastify/cors": "^9.0.1", | ||
"@trpc/server": "^10.45.0", | ||
"fastify": "^4.28.0", | ||
"zod": "^3.22.4" | ||
}, | ||
"devDependencies": { | ||
"@biomejs/biome": "1.6.4", | ||
"@dotkomonline/config": "workspace:*", | ||
"@dotkomonline/tsconfig": "workspace:*", | ||
"@types/node": "^20.12.7", | ||
"tslib": "^2.6.2", | ||
"typescript": "^5.4.5", | ||
"tsx": "^4.15.6" | ||
} | ||
} |
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,51 @@ | ||
import { env } from "@dotkomonline/env" | ||
import { type AppRouter, JwtService, appRouter, createContext } from "@dotkomonline/gateway-trpc" | ||
import fastifyCors from "@fastify/cors" | ||
import { type FastifyTRPCPluginOptions, fastifyTRPCPlugin } from "@trpc/server/adapters/fastify" | ||
import type { CreateFastifyContextOptions } from "@trpc/server/dist/adapters/fastify" | ||
import fastify from "fastify" | ||
|
||
const jwtService = new JwtService(env.WEB_AUTH0_ISSUER, [ | ||
env.WEB_AUTH0_CLIENT_ID, | ||
env.DASHBOARD_AUTH0_CLIENT_ID, | ||
env.GTX_AUTH0_CLIENT_ID, | ||
]) | ||
|
||
const allowedOrigins = env.RPC_ALLOWED_ORIGINS.split(",") | ||
|
||
export async function createFastifyContext({ req }: CreateFastifyContextOptions) { | ||
const bearer = req.headers.authorization | ||
if (bearer !== undefined) { | ||
const token = bearer.substring("Bearer ".length) | ||
const principal = await jwtService.verify(token) | ||
return createContext({ principal: principal.payload.sub ?? null }) | ||
} | ||
|
||
return createContext({ | ||
principal: null, | ||
}) | ||
} | ||
|
||
const server = fastify({ | ||
maxParamLength: 5000, | ||
}) | ||
server.register(fastifyCors, { | ||
origin: allowedOrigins, | ||
methods: ["GET", "POST", "PUT", "DELETE"], | ||
allowedHeaders: ["Content-Type", "Authorization"], | ||
credentials: true, | ||
}) | ||
|
||
server.register(fastifyTRPCPlugin, { | ||
prefix: "/api/trpc", | ||
trpcOptions: { | ||
router: appRouter, | ||
createContext: createFastifyContext, | ||
onError: ({ path, error }) => { | ||
// report to error monitoring | ||
console.error(`Error in tRPC handler on path '${path}':`, error) | ||
}, | ||
} satisfies FastifyTRPCPluginOptions<AppRouter>["trpcOptions"], | ||
}) | ||
|
||
await server.listen({ port: 4444 }) |
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 @@ | ||
{ | ||
"extends": "../../packages/tsconfig/tsconfig.json", | ||
"include": ["./**/*.ts", "./**/*.tsx"], | ||
"exclude": [], | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"jsx": "preserve", | ||
"incremental": true, | ||
"strictNullChecks": true | ||
} | ||
} |
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,10 @@ | ||
import { env } from "@dotkomonline/env" | ||
import { createProxyRoute } from "@dotkomonline/proxy-nextjs" | ||
|
||
const handler = createProxyRoute({ | ||
mountPath: "/api/trpc", | ||
apiEndpoint: env.RPC_HOST, | ||
}) | ||
|
||
export const GET = handler | ||
export const POST = handler |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
"use server" | ||
|
||
import { appRouter, createCallerFactory, createContextInner } from "@dotkomonline/gateway-trpc" | ||
import { appRouter, createCallerFactory, createContext } from "@dotkomonline/gateway-trpc" | ||
|
||
const createCaller = createCallerFactory(appRouter) | ||
// TODO: Add a way to get the userId from the request | ||
export const getServerClient = async () => createCaller(await createContextInner({ auth: null })) | ||
export const getUnauthorizedServerClient = async () => createCaller(await createContextInner({ auth: null })) | ||
export const getServerClient = async () => createCaller(await createContext({ principal: null })) | ||
export const getUnauthorizedServerClient = async () => createCaller(await createContext({ principal: null })) |
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
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
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 |
---|---|---|
@@ -1,34 +1,17 @@ | ||
import { authOptions } from "@dotkomonline/auth/src/web.app" | ||
import { createServiceLayer } from "@dotkomonline/core" | ||
import { kysely } from "@dotkomonline/db" | ||
import type { inferAsyncReturnType } from "@trpc/server" | ||
import type { CreateNextContextOptions } from "@trpc/server/adapters/next" | ||
import { getServerSession } from "next-auth" | ||
|
||
interface AuthContextProps { | ||
auth: { | ||
userId: string | ||
} | null | ||
principal: string | null | ||
} | ||
|
||
export const createContextInner = async (opts: AuthContextProps) => { | ||
export const createContext = async (opts: AuthContextProps) => { | ||
const services = await createServiceLayer({ db: kysely }) | ||
return { | ||
...services, | ||
auth: opts.auth, | ||
principal: opts.principal, | ||
} | ||
} | ||
|
||
export const createContext = async (opts: CreateNextContextOptions) => { | ||
const session = await getServerSession(opts.req, opts.res, authOptions) | ||
if (session !== null) { | ||
return createContextInner({ | ||
auth: { | ||
userId: session.user.id, | ||
}, | ||
}) | ||
} | ||
return createContextInner({ auth: null }) | ||
} | ||
|
||
export type Context = inferAsyncReturnType<typeof createContext> |
Oops, something went wrong.