3c9b65d982
- 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>
29 lines
1.2 KiB
TypeScript
29 lines
1.2 KiB
TypeScript
type Method = 'GET' | 'POST' | 'PUT' | 'DELETE'
|
|
|
|
// 使用宽松签名,返回类型由调用方通过泛型指定
|
|
const rawFetch = $fetch as unknown as <T>(url: string, opts?: { method?: Method, body?: unknown }) => Promise<T>
|
|
|
|
/** 统一的接口调用:自动弹出错误提示 */
|
|
export function useApi() {
|
|
const toast = useToast()
|
|
|
|
async function call<T = unknown>(url: string, method: Method = 'GET', body?: unknown, opts: { silent?: boolean, title?: string } = {}): Promise<T> {
|
|
try {
|
|
return await rawFetch<T>(url, { method, body })
|
|
}
|
|
catch (err) {
|
|
if (!opts.silent) toast.add({ title: opts.title ?? '操作失败', description: apiErrorMessage(err), color: 'error', icon: 'i-lucide-circle-alert' })
|
|
throw err
|
|
}
|
|
}
|
|
|
|
return {
|
|
get: <T>(url: string) => call<T>(url, 'GET'),
|
|
post: <T>(url: string, body?: unknown, opts?: { silent?: boolean }) => call<T>(url, 'POST', body ?? {}, opts),
|
|
put: <T>(url: string, body?: unknown) => call<T>(url, 'PUT', body ?? {}),
|
|
del: <T>(url: string) => call<T>(url, 'DELETE'),
|
|
upload: <T>(url: string, form: FormData) => call<T>(url, 'POST', form, { title: '上传失败' }),
|
|
toast
|
|
}
|
|
}
|