common.ts 631 B

12345678910111213141516171819202122
  1. import { NextRequest } from "next/server";
  2. const OPENAI_URL = "api.openai.com";
  3. const DEFAULT_PROTOCOL = "https";
  4. const PROTOCOL = process.env.PROTOCOL ?? DEFAULT_PROTOCOL;
  5. const BASE_URL = process.env.BASE_URL ?? OPENAI_URL;
  6. export async function requestOpenai(req: NextRequest) {
  7. const apiKey = req.headers.get("token");
  8. const openaiPath = req.headers.get("path");
  9. console.log("[Proxy] ", openaiPath);
  10. return fetch(`${PROTOCOL}://${BASE_URL}/${openaiPath}`, {
  11. headers: {
  12. "Content-Type": "application/json",
  13. Authorization: `Bearer ${apiKey}`,
  14. },
  15. method: req.method,
  16. body: req.body,
  17. });
  18. }