refactor: extract client side openai url

This commit is contained in:
Yidadaa 2023-05-03 23:25:17 +08:00
parent 48ebd74859
commit b1ea26467d
2 changed files with 10 additions and 4 deletions

View File

@ -65,10 +65,12 @@ function getHeaders() {
} }
export function requestOpenaiClient(path: string) { export function requestOpenaiClient(path: string) {
const openaiUrl = useAccessStore.getState().openaiUrl;
return (body: any, method = "POST") => return (body: any, method = "POST") =>
fetch("/api/openai/" + path, { fetch(openaiUrl + path, {
method, method,
body: body && JSON.stringify(body), body: body && JSON.stringify(body),
headers: getHeaders(),
}); });
} }
@ -163,7 +165,8 @@ export async function requestChatStream(
const reqTimeoutId = setTimeout(() => controller.abort(), TIME_OUT_MS); const reqTimeoutId = setTimeout(() => controller.abort(), TIME_OUT_MS);
try { try {
const res = await fetch("/api/openai/v1/chat/completions", { const openaiUrl = useAccessStore.getState().openaiUrl;
const res = await fetch(openaiUrl + "v1/chat/completions", {
method: "POST", method: "POST",
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",

View File

@ -8,6 +8,7 @@ export interface AccessControlStore {
token: string; token: string;
needCode: boolean; needCode: boolean;
openaiUrl: string;
updateToken: (_: string) => void; updateToken: (_: string) => void;
updateCode: (_: string) => void; updateCode: (_: string) => void;
@ -24,16 +25,18 @@ export const useAccessStore = create<AccessControlStore>()(
token: "", token: "",
accessCode: "", accessCode: "",
needCode: true, needCode: true,
openaiUrl: "/api/openai/",
enabledAccessControl() { enabledAccessControl() {
get().fetch(); get().fetch();
return get().needCode; return get().needCode;
}, },
updateCode(code: string) { updateCode(code: string) {
set((state) => ({ accessCode: code })); set(() => ({ accessCode: code }));
}, },
updateToken(token: string) { updateToken(token: string) {
set((state) => ({ token })); set(() => ({ token }));
}, },
isAuthorized() { isAuthorized() {
// has token or has code or disabled access control // has token or has code or disabled access control