move trpc to api
This commit is contained in:
66
packages/trpc/src/trpc.ts
Normal file
66
packages/trpc/src/trpc.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import { getAuth } from '@clerk/fastify';
|
||||
import { initTRPC, TRPCError } from '@trpc/server';
|
||||
import type { CreateFastifyContextOptions } from '@trpc/server/adapters/fastify';
|
||||
import superjson from 'superjson';
|
||||
import { ZodError } from 'zod';
|
||||
|
||||
export function createContext({ req, res }: CreateFastifyContextOptions) {
|
||||
return {
|
||||
req,
|
||||
res,
|
||||
session: getAuth(req),
|
||||
// we do not get types for `setCookie` from fastify
|
||||
// so define it here and be safe in routers
|
||||
setCookie: (
|
||||
key: string,
|
||||
value: string,
|
||||
options: {
|
||||
maxAge: number;
|
||||
path: string;
|
||||
}
|
||||
) => {
|
||||
// @ts-ignore
|
||||
// eslint-disable-next-line
|
||||
res.setCookie(key, value, options);
|
||||
},
|
||||
};
|
||||
}
|
||||
export type Context = Awaited<ReturnType<typeof createContext>>;
|
||||
|
||||
const t = initTRPC.context<Context>().create({
|
||||
transformer: superjson,
|
||||
errorFormatter({ shape, error }) {
|
||||
return {
|
||||
...shape,
|
||||
data: {
|
||||
...shape.data,
|
||||
zodError:
|
||||
error.cause instanceof ZodError ? error.cause.flatten() : null,
|
||||
},
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
const enforceUserIsAuthed = t.middleware(async ({ ctx, next }) => {
|
||||
if (!ctx.session?.userId) {
|
||||
throw new TRPCError({ code: 'UNAUTHORIZED', message: 'Not authenticated' });
|
||||
}
|
||||
try {
|
||||
return next({
|
||||
ctx: {
|
||||
session: { ...ctx.session },
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Failes to get user', error);
|
||||
throw new TRPCError({
|
||||
code: 'UNAUTHORIZED',
|
||||
message: 'Failed to get user',
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export const createTRPCRouter = t.router;
|
||||
|
||||
export const publicProcedure = t.procedure;
|
||||
export const protectedProcedure = t.procedure.use(enforceUserIsAuthed);
|
||||
Reference in New Issue
Block a user