SuperAPI AI 工具集

next-action-handler

数据与分析

查看项目

更新于 10-16 01:17

技能介绍

用途:next-action-handler 为 Next.js 项目安装并配置一个标准化的服务端操作(server action)层,基于 next-safe-action、better-auth、pino 与 zod,统一错误、日志和鉴权上下文。

主要能力:提供 actionClient 与 authedActionClient 两个操作客户端;支持 action 元数据 actionName(必填,供日志使用);支持 inputSchema 输入校验和 outputSchema 输出校验;结果返回 SafeActionResult 联合类型,data、serverError、validationErrors 三者仅出现一个;内置错误类(如 BadRequestError、UnauthorizedError、ForbiddenError、NotFoundError、RateLimitError、DatabaseError、InternalServerError 等)及错误转换器(fromBetterAuthError、toDatabaseError);自动日志记录,无 serverError 时执行 logActionExecution,所有错误执行 logActionError。

使用方式:本地开发可执行 npm install -D next-action-handler,然后在项目根目录运行 npx next-action-handler@latest add 应用完整配置。安装路径按 lib/、app/lib/ 顺序检测,否则新建 lib/next-action-handler/。安装依赖包括 better-auth、next-safe-action、pino、pino-pretty、server-only、zod。使用 authedActionClient 前必须创建 auth-helpers.ts,提供 requireUser()。每个 action 必须调用 .metadata({ actionName })。

适用场景:适用于 Next.js 项目中需要统一服务端操作错误处理、输入输出校验、鉴权与日志的场景。

限制:需自行创建 auth-helpers.ts;输入校验失败进入 validationErrors,输出校验失败进入 serverError;自定义 handleServerError 时需保留 ActionOutputDataValidationError 分支;其余版本、收费等信息暂未提供。