TTeamDesk 学习手册

src/server/api/trpc.ts

此代码来自构建时的仓库快照。修改源码后重新构建即可更新。

ts
import { initTRPC, TRPCError } from '@trpc/server';
import { ZodError } from 'zod';
import type { PrismaClient } from '@/generated/prisma/client';

// userId 只能来自 Better Auth 校验后的 Cookie 会话,不接收客户端自报身份。
export type ApiContext = { db: PrismaClient; userId: string | null };
export const t = initTRPC.context<ApiContext>().create({
  errorFormatter({ shape, error }) {
    return {
      ...shape,
      message:
        error.code === 'INTERNAL_SERVER_ERROR'
          ? '服务暂不可用,请稍后重试'
          : shape.message,
      data: {
        ...shape.data,
        stack: undefined,
        zodError:
          error.cause instanceof ZodError ? error.cause.flatten() : null,
      },
    };
  },
});
export const protectedProcedure = t.procedure.use(({ ctx, next }) => {
  if (!ctx.userId)
    throw new TRPCError({ code: 'UNAUTHORIZED', message: '请先登录' });
  return next({ ctx: { ...ctx, userId: ctx.userId } });
});

搜索教程

输入关键词,搜索全部章节。

阅读设置

外观
正文字号

从一张工单出发,读懂数据如何流转。

设置与进度仅保存在当前浏览器。