114 lines
2.9 KiB
TypeScript
114 lines
2.9 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
|
|
const API_BASE_URL = process.env.KIE_API_URL ?? "https://api.kie.ai";
|
|
const API_KEY = process.env.KIE_API_KEY ?? "";
|
|
|
|
function buildErrorResponse(message: string, status = 500) {
|
|
return NextResponse.json({ message }, { status });
|
|
}
|
|
|
|
async function parseResponse(response: Response) {
|
|
const raw = await response.text();
|
|
|
|
if (!raw) {
|
|
return {};
|
|
}
|
|
|
|
try {
|
|
return JSON.parse(raw);
|
|
} catch {
|
|
return { message: raw };
|
|
}
|
|
}
|
|
|
|
export async function POST(req: Request) {
|
|
if (!API_KEY) {
|
|
return buildErrorResponse("Kie.ai API key is not configured.");
|
|
}
|
|
|
|
let body: unknown;
|
|
|
|
try {
|
|
body = await req.json();
|
|
} catch {
|
|
return buildErrorResponse("Request body must be valid JSON.", 400);
|
|
}
|
|
|
|
if (!body || typeof body !== "object") {
|
|
return buildErrorResponse("Request body must be an object.", 400);
|
|
}
|
|
|
|
const { model = "google/nano-banana", callBackUrl, input } = body as {
|
|
model?: string;
|
|
callBackUrl?: string;
|
|
input?: Record<string, unknown>;
|
|
};
|
|
|
|
if (!input || typeof input !== "object") {
|
|
return buildErrorResponse("Input payload is required.", 400);
|
|
}
|
|
|
|
const prompt = input.prompt;
|
|
|
|
if (typeof prompt !== "string" || prompt.trim().length === 0) {
|
|
return buildErrorResponse("Input prompt is required.", 400);
|
|
}
|
|
|
|
const payload: Record<string, unknown> = { model, input };
|
|
|
|
if (callBackUrl) {
|
|
payload.callBackUrl = callBackUrl;
|
|
}
|
|
|
|
const upstreamResponse = await fetch(`${API_BASE_URL}/api/v1/jobs/createTask`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: `Bearer ${API_KEY}`
|
|
},
|
|
body: JSON.stringify(payload),
|
|
cache: "no-store"
|
|
});
|
|
|
|
const data = await parseResponse(upstreamResponse);
|
|
|
|
if (!upstreamResponse.ok) {
|
|
const message = typeof data?.message === "string" ? data.message : "Kie.ai API request failed.";
|
|
return buildErrorResponse(message, upstreamResponse.status);
|
|
}
|
|
|
|
return NextResponse.json(data, { status: upstreamResponse.status });
|
|
}
|
|
|
|
export async function GET(req: Request) {
|
|
if (!API_KEY) {
|
|
return buildErrorResponse("Kie.ai API key is not configured.");
|
|
}
|
|
|
|
const url = new URL(req.url);
|
|
const taskId = url.searchParams.get("taskId");
|
|
|
|
if (!taskId) {
|
|
return buildErrorResponse("taskId query parameter is required.", 400);
|
|
}
|
|
|
|
const upstreamResponse = await fetch(
|
|
`${API_BASE_URL}/api/v1/jobs/recordInfo?taskId=${encodeURIComponent(taskId)}`,
|
|
{
|
|
headers: {
|
|
Authorization: `Bearer ${API_KEY}`
|
|
},
|
|
cache: "no-store"
|
|
}
|
|
);
|
|
|
|
const data = await parseResponse(upstreamResponse);
|
|
|
|
if (!upstreamResponse.ok) {
|
|
const message = typeof data?.message === "string" ? data.message : "Failed to retrieve Kie.ai task status.";
|
|
return buildErrorResponse(message, upstreamResponse.status);
|
|
}
|
|
|
|
return NextResponse.json(data, { status: upstreamResponse.status });
|
|
}
|