更新安全配置项目
This commit is contained in:
@@ -1,68 +1,68 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { batchTranslateWithAI, translateWithAI } from "@/lib/ai-translate";
|
||||
import {NextRequest, NextResponse} from "next/server";
|
||||
import {batchTranslateWithAI, translateWithAI} from "@/lib/ai-translate";
|
||||
|
||||
// 单个翻译
|
||||
export async function GET(request: NextRequest) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const word = searchParams.get("word");
|
||||
const {searchParams} = new URL(request.url);
|
||||
const word = searchParams.get("word");
|
||||
|
||||
if (!word) {
|
||||
return NextResponse.json({ error: "缺少 word 参数" }, { status: 400 });
|
||||
}
|
||||
|
||||
try {
|
||||
const translation = await translateWithAI(word);
|
||||
|
||||
if (!translation) {
|
||||
return NextResponse.json({ error: "翻译失败" }, { status: 500 });
|
||||
if (!word) {
|
||||
return NextResponse.json({error: "缺少 word 参数"}, {status: 400});
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
word,
|
||||
english: translation,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("AI translation error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: String(error) },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
try {
|
||||
const translation = await translateWithAI(word);
|
||||
|
||||
if (!translation) {
|
||||
return NextResponse.json({error: "翻译失败"}, {status: 500});
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
word,
|
||||
english: translation,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("AI translation error:", error);
|
||||
return NextResponse.json(
|
||||
{error: String(error)},
|
||||
{status: 500}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 批量翻译
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { words } = body as { words: string[] };
|
||||
try {
|
||||
const body = await request.json();
|
||||
const {words} = body as { words: string[] };
|
||||
|
||||
if (!Array.isArray(words) || words.length === 0) {
|
||||
return NextResponse.json({ error: "缺少 words 参数(数组)" }, { status: 400 });
|
||||
if (!Array.isArray(words) || words.length === 0) {
|
||||
return NextResponse.json({error: "缺少 words 参数(数组)"}, {status: 400});
|
||||
}
|
||||
|
||||
// 检查 API Key
|
||||
if (!process.env.AI_API_KEY) {
|
||||
return NextResponse.json({error: "AI 配置未完成,请先在设置中配置 AI"}, {status: 500});
|
||||
}
|
||||
|
||||
const results = await batchTranslateWithAI(words);
|
||||
|
||||
// 转换为响应格式
|
||||
const translations: Record<string, string | null> = {};
|
||||
for (const word of words) {
|
||||
translations[word] = results.get(word) || null;
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
translations,
|
||||
total: words.length,
|
||||
success: results.size,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("AI batch translation error:", error);
|
||||
return NextResponse.json(
|
||||
{error: String(error)},
|
||||
{status: 500}
|
||||
);
|
||||
}
|
||||
|
||||
// 检查 API Key
|
||||
if (!process.env.AI_API_KEY) {
|
||||
return NextResponse.json({ error: "AI 配置未完成,请先在设置中配置 AI" }, { status: 500 });
|
||||
}
|
||||
|
||||
const results = await batchTranslateWithAI(words);
|
||||
|
||||
// 转换为响应格式
|
||||
const translations: Record<string, string | null> = {};
|
||||
for (const word of words) {
|
||||
translations[word] = results.get(word) || null;
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
translations,
|
||||
total: words.length,
|
||||
success: results.size,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("AI batch translation error:", error);
|
||||
return NextResponse.json(
|
||||
{ error: String(error) },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
+35
-35
@@ -1,48 +1,48 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { getTranslation, getTranslations } from "@/lib/database";
|
||||
import {NextRequest, NextResponse} from "next/server";
|
||||
import {getTranslation, getTranslations} from "@/lib/database";
|
||||
|
||||
// 单个翻译查询
|
||||
export async function GET(request: NextRequest) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const chineseName = searchParams.get("name");
|
||||
const {searchParams} = new URL(request.url);
|
||||
const chineseName = searchParams.get("name");
|
||||
|
||||
if (!chineseName) {
|
||||
return NextResponse.json({ error: "缺少 name 参数" }, { status: 400 });
|
||||
}
|
||||
if (!chineseName) {
|
||||
return NextResponse.json({error: "缺少 name 参数"}, {status: 400});
|
||||
}
|
||||
|
||||
const translation = await getTranslation(chineseName);
|
||||
const translation = await getTranslation(chineseName);
|
||||
|
||||
return NextResponse.json({
|
||||
chinese: chineseName,
|
||||
english: translation,
|
||||
found: translation !== null,
|
||||
});
|
||||
return NextResponse.json({
|
||||
chinese: chineseName,
|
||||
english: translation,
|
||||
found: translation !== null,
|
||||
});
|
||||
}
|
||||
|
||||
// 批量翻译查询
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { names } = body as { names: string[] };
|
||||
try {
|
||||
const body = await request.json();
|
||||
const {names} = body as { names: string[] };
|
||||
|
||||
if (!names || !Array.isArray(names)) {
|
||||
return NextResponse.json({ error: "缺少 names 参数(数组)" }, { status: 400 });
|
||||
if (!names || !Array.isArray(names)) {
|
||||
return NextResponse.json({error: "缺少 names 参数(数组)"}, {status: 400});
|
||||
}
|
||||
|
||||
const translations = await getTranslations(names);
|
||||
|
||||
const result: Record<string, string | null> = {};
|
||||
for (const name of names) {
|
||||
result[name] = translations.get(name) || null;
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
translations: result,
|
||||
total: names.length,
|
||||
found: translations.size,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Translation API error:", error);
|
||||
return NextResponse.json({error: "请求格式错误"}, {status: 400});
|
||||
}
|
||||
|
||||
const translations = await getTranslations(names);
|
||||
|
||||
const result: Record<string, string | null> = {};
|
||||
for (const name of names) {
|
||||
result[name] = translations.get(name) || null;
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
translations: result,
|
||||
total: names.length,
|
||||
found: translations.size,
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Translation API error:", error);
|
||||
return NextResponse.json({ error: "请求格式错误" }, { status: 400 });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user