-
-
Notifications
You must be signed in to change notification settings - Fork 773
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
200 additions
and
5 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
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,14 +1,18 @@ | ||
import { api } from '@/lib/trpc/server'; | ||
import { SignOutButton } from '@clerk/nextjs'; | ||
|
||
/** | ||
* This is the main page for the dashboard. | ||
* @returns A Next.js RSC page component. | ||
*/ | ||
export default function DashboardHome() { | ||
export default async function DashboardHome() { | ||
const { message } = await api.greeting.protected(); | ||
|
||
return ( | ||
<main> | ||
<h1>Hello World</h1> | ||
<SignOutButton /> | ||
<h1>{message}</h1> | ||
</main> | ||
); | ||
} |
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,29 @@ | ||
import type { NextRequest } from 'next/server'; | ||
|
||
import { fetchRequestHandler } from '@trpc/server/adapters/fetch'; | ||
|
||
import { env } from '@/env'; | ||
import { appRouter } from '@/server'; | ||
import { createTRPCContext } from '@/server/trpc'; | ||
|
||
const createContext = async (req: NextRequest) => { | ||
return createTRPCContext({ | ||
headers: req.headers, | ||
}); | ||
}; | ||
|
||
const handler = (req: NextRequest) => | ||
fetchRequestHandler({ | ||
endpoint: '/api/trpc', | ||
req, | ||
router: appRouter, | ||
createContext: () => createContext(req), | ||
onError: ({ path, error }) => { | ||
env.NODE_ENV === 'development' && | ||
console.error( | ||
`❌ tRPC failed on ${path ?? '<no-path>'}: ${error.message}`, | ||
); | ||
}, | ||
}); | ||
|
||
export { handler as GET, handler as POST }; |
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,68 @@ | ||
'use client'; | ||
|
||
import { useState } from 'react'; | ||
|
||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import { loggerLink, unstable_httpBatchStreamLink } from '@trpc/client'; | ||
import { createTRPCReact } from '@trpc/react-query'; | ||
import { type inferRouterInputs, type inferRouterOutputs } from '@trpc/server'; | ||
import superjson from 'superjson'; | ||
|
||
import { getBaseUrl } from '@/lib/utils'; | ||
import { type AppRouter } from '@/server'; | ||
|
||
const createQueryClient = () => new QueryClient(); | ||
|
||
let clientQueryClientSingleton: QueryClient | undefined = undefined; | ||
const getQueryClient = () => { | ||
if (typeof window === 'undefined') { | ||
return createQueryClient(); | ||
} | ||
return (clientQueryClientSingleton ??= createQueryClient()); | ||
}; | ||
|
||
export const api = createTRPCReact<AppRouter>(); | ||
|
||
export type RouterInputs = inferRouterInputs<AppRouter>; | ||
export type RouterOutputs = inferRouterOutputs<AppRouter>; | ||
|
||
/** | ||
* The TRPC React Provider is a component that initializes trpc and react query. | ||
* @param props Props for the TRPC React Provider. | ||
* @param props.children The children of the TRPC React Provider, which would be | ||
* the entire page/layout in most cases. | ||
* @returns React Provider component that initializes trpc and react query | ||
* integration for the client. | ||
*/ | ||
export function TRPCReactProvider(props: { children: React.ReactNode }) { | ||
const queryClient = getQueryClient(); | ||
|
||
const [trpcClient] = useState(() => | ||
api.createClient({ | ||
links: [ | ||
loggerLink({ | ||
enabled: (op) => | ||
process.env.NODE_ENV === 'development' || | ||
(op.direction === 'down' && op.result instanceof Error), | ||
}), | ||
unstable_httpBatchStreamLink({ | ||
transformer: superjson, | ||
url: getBaseUrl() + '/api/trpc', | ||
headers: () => { | ||
const headers = new Headers(); | ||
headers.set('x-trpc-source', 'nextjs-react'); | ||
return headers; | ||
}, | ||
}), | ||
], | ||
}), | ||
); | ||
|
||
return ( | ||
<QueryClientProvider client={queryClient}> | ||
<api.Provider client={trpcClient} queryClient={queryClient}> | ||
{props.children} | ||
</api.Provider> | ||
</QueryClientProvider> | ||
); | ||
} |
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,18 @@ | ||
import 'server-only'; | ||
|
||
import { cache } from 'react'; | ||
import { headers } from 'next/headers'; | ||
|
||
import { createCaller } from '@/server'; | ||
import { createTRPCContext } from '@/server/trpc'; | ||
|
||
const createContext = cache(async () => { | ||
const heads = new Headers(headers()); | ||
heads.set('x-trpc-source', 'rsc'); | ||
|
||
return createTRPCContext({ | ||
headers: heads, | ||
}); | ||
}); | ||
|
||
export const api = createCaller(createContext); |
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 { greetingRouter } from './routers/greeting'; | ||
import { createCallerFactory, createRouter } from './trpc'; | ||
|
||
export const appRouter = createRouter({ | ||
greeting: greetingRouter, | ||
}); | ||
|
||
export type AppRouter = typeof appRouter; | ||
|
||
export const createCaller = createCallerFactory(appRouter); |
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 { z } from 'zod'; | ||
|
||
import { createRouter, protectedProcedure } from '../trpc'; | ||
|
||
export const greetingRouter = createRouter({ | ||
protected: protectedProcedure | ||
.input(z.object({ name: z.string().optional() }).optional()) | ||
.query(({ input, ctx }) => { | ||
return { | ||
message: `Hello, ${input?.name ?? ctx.session.firstName ?? ''}, I'm from the tRPC procedure!`, | ||
}; | ||
}), | ||
}); |
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,46 @@ | ||
import { currentUser } from '@clerk/nextjs/server'; | ||
import { initTRPC, TRPCError } from '@trpc/server'; | ||
import superjson from 'superjson'; | ||
import { ZodError } from 'zod'; | ||
|
||
import { db } from '@/db'; | ||
|
||
export const createTRPCContext = async (opts: { headers: Headers }) => { | ||
const session = await currentUser(); | ||
|
||
return { | ||
db, | ||
session, | ||
...opts, | ||
}; | ||
}; | ||
|
||
const t = initTRPC.context<typeof createTRPCContext>().create({ | ||
transformer: superjson, | ||
errorFormatter({ shape, error }) { | ||
return { | ||
...shape, | ||
data: { | ||
...shape.data, | ||
zodError: | ||
error.cause instanceof ZodError ? error.cause.flatten() : null, | ||
}, | ||
}; | ||
}, | ||
}); | ||
|
||
export const createCallerFactory = t.createCallerFactory; | ||
export const createRouter = t.router; | ||
export const publicProcedure = t.procedure; | ||
|
||
export const protectedProcedure = t.procedure.use(({ ctx, next }) => { | ||
if (!ctx.session) { | ||
throw new TRPCError({ code: 'UNAUTHORIZED' }); | ||
} | ||
|
||
return next({ | ||
ctx: { | ||
session: { ...ctx.session }, | ||
}, | ||
}); | ||
}); |