Files
ai-novel-studio/server/utils/http.ts
T
jocay 3c9b65d982 AI 小说工坊:主编 + 作家多模型协作写作应用
- Nuxt 4 + Nuxt UI 4 + AI SDK 7 + Drizzle/SQLite
- 主编规划大纲与章节简报、选派作家;作家流式写入章节文件并实时推送前端
- 逐章确认模式(默认)与自动模式;摘要与故事记忆
- 酒馆角色卡(PNG/JSON,V1-V3)与世界书导入
- 自定义最高优先级系统提示词、审稿/修改/多版本、任务日志与提示词检视
- 模拟 LLM 服务与端到端测试脚本

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-18 02:04:40 +08:00

43 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { H3Event } from 'h3'
import type { output, ZodObject, ZodType } from 'zod'
async function readRawJson(event: H3Event): Promise<Record<string, unknown>> {
const raw = await readRawBody(event, 'utf8')
if (!raw) return {}
try { return JSON.parse(raw) }
catch { throw createError({ statusCode: 400, message: '请求体不是合法的 JSON' }) }
}
function validationError(issues: { path: PropertyKey[], message: string }[]) {
const first = issues[0]
return createError({ statusCode: 400, message: `参数错误:${first?.path.map(String).join('.') || ''} ${first?.message || ''}`.trim() })
}
export async function readJson<T>(event: H3Event, schema: ZodType<T>): Promise<T> {
const parsed = schema.safeParse(await readRawJson(event))
if (!parsed.success) throw validationError(parsed.error.issues)
return parsed.data
}
/**
* 读取部分更新的请求体:只保留请求中实际出现的字段
* (zod 4 中 optional 包裹的 default 仍会生效,直接 partial() 会把缺省字段覆盖成默认值)
*/
export async function readPatch<S extends ZodObject>(event: H3Event, schema: S): Promise<Partial<output<S>>> {
const data = await readRawJson(event)
const parsed = schema.partial().safeParse(data)
if (!parsed.success) throw validationError(parsed.error.issues)
const result = parsed.data as Record<string, unknown>
const out: Record<string, unknown> = {}
for (const k of Object.keys(data)) {
if (k in result) out[k] = result[k]
}
return out as Partial<output<S>>
}
export function param(event: H3Event, name = 'id') {
const v = getRouterParam(event, name)
if (!v) throw createError({ statusCode: 400, message: `缺少参数 ${name}` })
return v
}