123 lines
3.2 KiB
TypeScript
123 lines
3.2 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
||
import { createApiKey, disableApiKey, listApiKeys } from '@/lib/api-key';
|
||
import { getPublicProjectDb, verifySuperAdmin } from '@/lib/project-auth';
|
||
|
||
export async function GET(
|
||
request: NextRequest,
|
||
{ params }: { params: Promise<{ projectCode: string }> }
|
||
) {
|
||
const authResult = await verifySuperAdmin();
|
||
if (!authResult.success) {
|
||
return authResult.response;
|
||
}
|
||
|
||
try {
|
||
const { projectCode } = await params;
|
||
const projectDbResult = await getPublicProjectDb(projectCode);
|
||
|
||
if (!projectDbResult.success) {
|
||
return projectDbResult.response;
|
||
}
|
||
|
||
const apiKeys = await listApiKeys(projectDbResult.prisma);
|
||
return NextResponse.json({ apiKeys });
|
||
} catch (error) {
|
||
console.error('获取项目 API Key 列表失败:', error);
|
||
return NextResponse.json({ error: '服务器错误' }, { status: 500 });
|
||
}
|
||
}
|
||
|
||
export async function POST(
|
||
request: NextRequest,
|
||
{ params }: { params: Promise<{ projectCode: string }> }
|
||
) {
|
||
const authResult = await verifySuperAdmin();
|
||
if (!authResult.success) {
|
||
return authResult.response;
|
||
}
|
||
|
||
try {
|
||
const { projectCode } = await params;
|
||
const projectDbResult = await getPublicProjectDb(projectCode);
|
||
|
||
if (!projectDbResult.success) {
|
||
return projectDbResult.response;
|
||
}
|
||
|
||
const { name } = await request.json();
|
||
|
||
if (!name || typeof name !== 'string' || name.trim().length === 0) {
|
||
return NextResponse.json(
|
||
{ success: false, error: 'API Key 名称不能为空' },
|
||
{ status: 400 }
|
||
);
|
||
}
|
||
|
||
const result = await createApiKey(name.trim(), projectDbResult.prisma);
|
||
|
||
if (!result.success) {
|
||
return NextResponse.json(
|
||
{ success: false, error: result.error || '创建失败' },
|
||
{ status: 500 }
|
||
);
|
||
}
|
||
|
||
return NextResponse.json({
|
||
success: true,
|
||
key: result.key,
|
||
message: '请立即保存此 API Key,它只会显示一次!',
|
||
});
|
||
} catch (error) {
|
||
console.error('创建项目 API Key 失败:', error);
|
||
return NextResponse.json(
|
||
{ success: false, error: '服务器错误' },
|
||
{ status: 500 }
|
||
);
|
||
}
|
||
}
|
||
|
||
export async function DELETE(
|
||
request: NextRequest,
|
||
{ params }: { params: Promise<{ projectCode: string }> }
|
||
) {
|
||
const authResult = await verifySuperAdmin();
|
||
if (!authResult.success) {
|
||
return authResult.response;
|
||
}
|
||
|
||
try {
|
||
const { projectCode } = await params;
|
||
const projectDbResult = await getPublicProjectDb(projectCode);
|
||
|
||
if (!projectDbResult.success) {
|
||
return projectDbResult.response;
|
||
}
|
||
|
||
const id = request.nextUrl.searchParams.get('id');
|
||
|
||
if (!id) {
|
||
return NextResponse.json(
|
||
{ success: false, error: 'API Key ID 不能为空' },
|
||
{ status: 400 }
|
||
);
|
||
}
|
||
|
||
const success = await disableApiKey(id, projectDbResult.prisma);
|
||
|
||
if (!success) {
|
||
return NextResponse.json(
|
||
{ success: false, error: '禁用失败' },
|
||
{ status: 500 }
|
||
);
|
||
}
|
||
|
||
return NextResponse.json({ success: true });
|
||
} catch (error) {
|
||
console.error('禁用项目 API Key 失败:', error);
|
||
return NextResponse.json(
|
||
{ success: false, error: '服务器错误' },
|
||
{ status: 500 }
|
||
);
|
||
}
|
||
}
|