generateImg.ts
1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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,
});
}
}