route.ts 774 B

123456789101112131415161718192021222324252627282930313233
  1. import { NextRequest, NextResponse } from "next/server";
  2. import { requestOpenai } from "../common";
  3. async function makeRequest(req: NextRequest) {
  4. try {
  5. const api = await requestOpenai(req);
  6. const res = new NextResponse(api.body);
  7. res.headers.set("Content-Type", "application/json");
  8. res.headers.set("Cache-Control", "no-cache");
  9. return res;
  10. } catch (e) {
  11. console.error("[OpenAI] ", req.body, e);
  12. return NextResponse.json(
  13. {
  14. error: true,
  15. msg: JSON.stringify(e),
  16. },
  17. {
  18. status: 500,
  19. }
  20. );
  21. }
  22. }
  23. export async function POST(req: NextRequest) {
  24. return makeRequest(req);
  25. }
  26. export async function GET(req: NextRequest) {
  27. return makeRequest(req);
  28. }
  29. export const runtime = "experimental-edge";