|
| 1 | +/** |
| 2 | + * YOU PROBABLY DON'T NEED TO EDIT THIS FILE, UNLESS: |
| 3 | + * 1. You want to modify request context (see Part 1) |
| 4 | + * 2. You want to create a new middleware or type of procedure (see Part 3) |
| 5 | + * |
| 6 | + * tl;dr - this is where all the tRPC server stuff is created and plugged in. |
| 7 | + * The pieces you will need to use are documented accordingly near the end |
| 8 | + */ |
| 9 | +import { initTRPC, TRPCError } from "@trpc/server"; |
| 10 | +import superjson from "superjson"; |
| 11 | +import { ZodError } from "zod"; |
| 12 | + |
| 13 | +import { getAuth } from "@clerk/nextjs/server"; |
| 14 | +import {worxpace} from "@acme/prisma" |
| 15 | +// import { NextApiRequest } from "@trpc/server/adapters/next"; |
| 16 | +import * as trpcNext from '@trpc/server/adapters/next' |
| 17 | + |
| 18 | +/** |
| 19 | + * 1. CONTEXT |
| 20 | + * |
| 21 | + * This section defines the "contexts" that are available in the backend API. |
| 22 | + * |
| 23 | + * These allow you to access things when processing a request, like the database, the session, etc. |
| 24 | + * |
| 25 | + * This helper generates the "internals" for a tRPC context. The API handler and RSC clients each |
| 26 | + * wrap this and provides the required context. |
| 27 | + * |
| 28 | + * @see https://trpc.io/docs/server/context |
| 29 | + */ |
| 30 | +export const createTRPCContext = async (opts: trpcNext.CreateNextContextOptions) => { |
| 31 | + const auth = getAuth(opts.req) |
| 32 | + // const source = opts.headers.get("x-trpc-source") ?? "unknown"; |
| 33 | + // console.log(">>> tRPC Request from", source, "by", auth?.userId); |
| 34 | + |
| 35 | + return { |
| 36 | + auth, |
| 37 | + db: worxpace, |
| 38 | + }; |
| 39 | +}; |
| 40 | + |
| 41 | +/** |
| 42 | + * 2. INITIALIZATION |
| 43 | + * |
| 44 | + * This is where the trpc api is initialized, connecting the context and |
| 45 | + * transformer |
| 46 | + */ |
| 47 | +const t = initTRPC.context<typeof createTRPCContext>().create({ |
| 48 | + transformer: superjson, |
| 49 | + errorFormatter: ({ shape, error }) => ({ |
| 50 | + ...shape, |
| 51 | + data: { |
| 52 | + ...shape.data, |
| 53 | + zodError: error.cause instanceof ZodError ? error.cause.flatten() : null, |
| 54 | + }, |
| 55 | + }), |
| 56 | +}); |
| 57 | + |
| 58 | +/** |
| 59 | + * Create a server-side caller |
| 60 | + * @see https://trpc.io/docs/server/server-side-calls |
| 61 | + */ |
| 62 | +export const createCallerFactory = t.createCallerFactory; |
| 63 | + |
| 64 | +/** |
| 65 | + * 3. ROUTER & PROCEDURE (THE IMPORTANT BIT) |
| 66 | + * |
| 67 | + * These are the pieces you use to build your tRPC API. You should import these |
| 68 | + * a lot in the /src/server/api/routers folder |
| 69 | + */ |
| 70 | + |
| 71 | +/** |
| 72 | + * This is how you create new routers and subrouters in your tRPC API |
| 73 | + * @see https://trpc.io/docs/router |
| 74 | + */ |
| 75 | +export const createTRPCRouter = t.router; |
| 76 | + |
| 77 | +/** |
| 78 | + * Public (unauthed) procedure |
| 79 | + * |
| 80 | + * This is the base piece you use to build new queries and mutations on your |
| 81 | + * tRPC API. It does not guarantee that a user querying is authorized, but you |
| 82 | + * can still access user session data if they are logged in |
| 83 | + */ |
| 84 | +export const publicProcedure = t.procedure; |
| 85 | + |
| 86 | +/** |
| 87 | + * Protected (authenticated) procedure |
| 88 | + * |
| 89 | + * If you want a query or mutation to ONLY be accessible to logged in users, use this. It verifies |
| 90 | + * the `AuthObject` is valid and guarantees `ctx.auth.userId` is not null. |
| 91 | + * |
| 92 | + * @see https://trpc.io/docs/procedures |
| 93 | + */ |
| 94 | +export const protectedProcedure = t.procedure.use(({ ctx, next }) => { |
| 95 | + if (!ctx.auth.userId) { |
| 96 | + throw new TRPCError({ code: "UNAUTHORIZED" }); |
| 97 | + } |
| 98 | + return next({ |
| 99 | + ctx: { |
| 100 | + // infers the `auth` as non-nullable |
| 101 | + auth: ctx.auth, |
| 102 | + }, |
| 103 | + }); |
| 104 | +}); |
0 commit comments