generateImg.ts 1.1 KB
import { getServerSideConfig } from "@/app/config/server";
import { NextRequest, NextResponse } from "next/server";

export async function handle(
  req: NextRequest,
  { params }: { params: { path: string[] } },
) {
  const config = getServerSideConfig();
  const baseUrl = config.baseUrl;
  const subPath = params.path.join("/");
  // if(subPath==='createApiToken'){const reqUrl = `${baseUrl}/api/user/${subPath}`;}
  const reqUrl = `${baseUrl}/v1/images/${subPath}`;
  const apiKey = config.apiKey;
  try {
    const headers = new Headers({
      Authorization: `Bearer ${apiKey}`,
      "Content-Type": "application/json",
    });
    const body = await new Response(req.body).text();
    const prompt = JSON.parse(body);
    const response = await fetch(reqUrl, {
      headers,
      method: "POST",
      body: JSON.stringify({
        model: "dall-e-3",
        prompt: prompt,
        n: 1,
        size: "1024x1024",
      }),
    });
    const data = await response.json();
    return NextResponse.json({
      data: data,
      status: 200,
    });
  } catch {
    return NextResponse.json({
      data: "处理请求时出错",
      status: 500,
    });
  }
}