SuperAPI AI 工具集

next

测试与质量

查看项目

更新于 10-15 23:24

技能介绍

用途:@emulators/adapter-next 把模拟器直接嵌入 Next.js App Router 应用,与主应用同源运行,便于在 Vercel 预览部署等 OAuth 回调 URL 每次变化的环境中使用。

主要能力:通过 createEmulateHandler 创建 catch-all 路由,按服务挂载模拟器(如 /emulate/github/、/emulate/google/);支持 Auth.js/NextAuth 将 provider 指向同源模拟器路径;提供 withEmulate 包装 Next.js 配置,把模拟器字体纳入 serverless 追踪;支持持久化适配器,默认内存状态在冷启动时重置。

使用方式:安装 npm install @emulators/adapter-next @emulators/github @emulators/google,按需只装所需模拟器。在 app/emulate/[...path]/route.ts 中导出 createEmulateHandler 返回的 GET、POST、PUT、PATCH、DELETE、OPTIONS,并配置 services 与 seed。Auth.js 中把 authorization、token、userinfo 指向模拟器路径;未配置 oauth_apps 时跳过 client_id、client_secret、redirect_uri 校验。持久化可传入自定义适配器(如 Vercel KV、Redis)或使用 @emulators/core 的 filePersistence('.emulate/state.json')。

限制:需要 Node.js 运行时而非 Edge,因为模拟器使用 crypto.randomBytes;并发写入为最后写入胜出,生成身份需用 initialize 原子选择初始快照;持久化快照应保持私有。自定义模拟器需用 emulate 的 defineEmulator 定义,部署时把 emulate 作为运行时依赖,并在测试清理时 await handler.close()。