pollo/src/server/api/trpc.ts

110 lines
3 KiB
TypeScript
Raw Normal View History

2023-04-20 04:20:00 -06:00
/**
* YOU PROBABLY DON'T NEED TO EDIT THIS FILE, UNLESS:
2023-08-12 17:12:42 -06:00
* 1. You want to modify request context (see Part 1)
* 2. You want to create a new middleware or type of procedure (see Part 3)
2023-04-20 04:20:00 -06:00
*
2023-08-12 17:12:42 -06:00
* tl;dr - this is where all the tRPC server stuff is created and plugged in.
* The pieces you will need to use are documented accordingly near the end
2023-04-20 04:20:00 -06:00
*/
/**
* 1. CONTEXT
*
2023-08-12 17:12:42 -06:00
* This section defines the "contexts" that are available in the backend API
*
* These allow you to access things like the database, the session, etc, when
* processing a request
2023-04-20 04:20:00 -06:00
*
*/
import { type CreateNextContextOptions } from "@trpc/server/adapters/next";
2023-08-12 17:12:42 -06:00
import { getAuth } from "@clerk/nextjs/server";
import type {
SignedInAuthObject,
SignedOutAuthObject,
} from "@clerk/nextjs/api";
2023-04-20 04:20:00 -06:00
2023-08-12 17:12:42 -06:00
import { prisma } from "../db";
2023-04-20 04:20:00 -06:00
2023-08-12 17:12:42 -06:00
interface AuthContext {
auth: SignedInAuthObject | SignedOutAuthObject;
}
2023-04-20 04:20:00 -06:00
/**
2023-08-12 17:12:42 -06:00
* This helper generates the "internals" for a tRPC context. If you need to use
* it, you can export it from here
2023-04-20 04:20:00 -06:00
*
* Examples of things you may need it for:
2023-08-12 17:12:42 -06:00
* - testing, so we dont have to mock Next.js' req/res
* - trpc's `createSSGHelpers` where we don't have req/res
2023-04-20 04:20:00 -06:00
* @see https://create.t3.gg/en/usage/trpc#-servertrpccontextts
*/
2023-08-12 17:12:42 -06:00
const createInnerTRPCContext = ({ auth }: AuthContext) => {
2023-04-20 04:20:00 -06:00
return {
2023-08-12 17:12:42 -06:00
auth,
2023-04-20 04:20:00 -06:00
prisma,
};
};
/**
2023-08-12 17:12:42 -06:00
* This is the actual context you'll use in your router. It will be used to
* process every request that goes through your tRPC endpoint
* @link https://trpc.io/docs/context
2023-04-20 04:20:00 -06:00
*/
2023-08-12 17:12:42 -06:00
export const createTRPCContext = (opts: CreateNextContextOptions) => {
return createInnerTRPCContext({ auth: getAuth(opts.req) });
2023-04-20 04:20:00 -06:00
};
/**
* 2. INITIALIZATION
*
2023-08-12 17:12:42 -06:00
* This is where the trpc api is initialized, connecting the context and
* transformer
2023-04-20 04:20:00 -06:00
*/
import { initTRPC, TRPCError } from "@trpc/server";
2023-07-11 17:22:54 -06:00
import superjson from "superjson";
2023-08-12 17:12:42 -06:00
import { OpenApiMeta } from "trpc-openapi";
2023-04-20 04:20:00 -06:00
2023-08-01 00:31:48 -06:00
const t = initTRPC
.context<typeof createTRPCContext>()
2023-08-12 17:12:42 -06:00
.meta<OpenApiMeta>()
2023-08-01 00:31:48 -06:00
.create({
transformer: superjson,
errorFormatter({ shape }) {
return shape;
},
});
2023-04-20 04:20:00 -06:00
2023-08-12 17:12:42 -06:00
// check if the user is signed in, otherwise through a UNAUTHORIZED CODE
const isAuthed = t.middleware(({ next, ctx }) => {
if (!ctx.auth.userId) {
throw new TRPCError({ code: "UNAUTHORIZED" });
}
return next({
ctx: {
auth: ctx.auth,
},
});
});
2023-04-20 04:20:00 -06:00
/**
* 3. ROUTER & PROCEDURE (THE IMPORTANT BIT)
*
2023-08-12 17:12:42 -06:00
* These are the pieces you use to build your tRPC API. You should import these
* a lot in the /src/server/api/routers folder
2023-04-20 04:20:00 -06:00
*/
/**
2023-08-12 17:12:42 -06:00
* This is how you create new routers and subrouters in your tRPC API
2023-04-20 04:20:00 -06:00
* @see https://trpc.io/docs/router
*/
export const createTRPCRouter = t.router;
/**
2023-08-12 17:12:42 -06:00
* Public (unauthed) procedure
2023-04-20 04:20:00 -06:00
*
2023-08-12 17:12:42 -06:00
* This is the base piece you use to build new queries and mutations on your
* tRPC API. It does not guarantee that a user querying is authorized, but you
* can still access user session data if they are logged in
2023-04-20 04:20:00 -06:00
*/
export const publicProcedure = t.procedure;
2023-08-12 17:12:42 -06:00
export const protectedProcedure = t.procedure.use(isAuthed);