feat: #1640 support free gpt endpoint

This commit is contained in:
Yidadaa 2023-05-19 23:53:27 +08:00
parent e5329dc28a
commit 203ac0970d
3 changed files with 7 additions and 4 deletions

View File

@ -2,6 +2,7 @@ import { NextRequest } from "next/server";
import { getServerSideConfig } from "../config/server";
import md5 from "spark-md5";
import { ACCESS_CODE_PREFIX } from "../constant";
import { OPENAI_URL } from "./common";
function getIP(req: NextRequest) {
let ip = req.ip ?? req.headers.get("x-real-ip");
@ -55,7 +56,7 @@ export function auth(req: NextRequest) {
} else {
console.log("[Auth] admin did not provide an api key");
return {
error: true,
error: serverConfig.baseUrl?.includes(OPENAI_URL),
msg: "admin did not provide an api key",
};
}

View File

@ -1,6 +1,6 @@
import { NextRequest } from "next/server";
const OPENAI_URL = "api.openai.com";
export const OPENAI_URL = "api.openai.com";
const DEFAULT_PROTOCOL = "https";
const PROTOCOL = process.env.PROTOCOL ?? DEFAULT_PROTOCOL;
const BASE_URL = process.env.BASE_URL ?? OPENAI_URL;
@ -45,8 +45,8 @@ export async function requestOpenai(req: NextRequest) {
signal: controller.signal,
});
} catch (err: unknown) {
if (err instanceof Error && err.name === 'AbortError') {
console.log('Fetch aborted');
if (err instanceof Error && err.name === "AbortError") {
console.log("Fetch aborted");
} else {
throw err;
}

View File

@ -5,6 +5,7 @@ declare global {
interface ProcessEnv {
OPENAI_API_KEY?: string;
CODE?: string;
BASE_URL?: string;
PROXY_URL?: string;
VERCEL?: string;
HIDE_USER_API_KEY?: string; // disable user's api key input
@ -38,6 +39,7 @@ export const getServerSideConfig = () => {
code: process.env.CODE,
codes: ACCESS_CODES,
needCode: ACCESS_CODES.size > 0,
baseUrl: process.env.BASE_URL,
proxyUrl: process.env.PROXY_URL,
isVercel: !!process.env.VERCEL,
hideUserApiKey: !!process.env.HIDE_USER_API_KEY,