Browse Source

Refactor Api Common [Server Side] [Console Log]

- [+] refactor(common.ts): remove unnecessary console.log for [Org ID] in requestOpenai function
- [+] refactor(common.ts): conditionally delete OpenAI-Organization header from response if [Org ID] is not set up in ENV
H0llyW00dzZ 2 năm trước cách đây
mục cha
commit
36e9c6ac4d
1 tập tin đã thay đổi với 17 bổ sung4 xóa
  1. 17 4
      app/api/common.ts

+ 17 - 4
app/api/common.ts

@@ -30,10 +30,6 @@ export async function requestOpenai(req: NextRequest) {
 
   console.log("[Proxy] ", path);
   console.log("[Base Url]", baseUrl);
-  // this fix [Org ID] undefined in server side if not using custom point
-  if (serverConfig.openaiOrgId !== undefined) {
-    console.log("[Org ID]", serverConfig.openaiOrgId);
-  }
 
   const timeoutId = setTimeout(
     () => {
@@ -103,12 +99,29 @@ export async function requestOpenai(req: NextRequest) {
   try {
     const res = await fetch(fetchUrl, fetchOptions);
 
+  // Extract the OpenAI-Organization header from the response
+  const openaiOrganizationHeader = res.headers.get("OpenAI-Organization");
+
+  // Check if serverConfig.openaiOrgId is defined
+  if (serverConfig.openaiOrgId !== undefined) {
+    // If openaiOrganizationHeader is present, log it; otherwise, log that the header is not present
+    console.log("[Org ID]", openaiOrganizationHeader);
+  } else {
+    console.log("[Org ID] is not set up.");
+  }
+
     // to prevent browser prompt for credentials
     const newHeaders = new Headers(res.headers);
     newHeaders.delete("www-authenticate");
     // to disable nginx buffering
     newHeaders.set("X-Accel-Buffering", "no");
 
+    // Conditionally delete the OpenAI-Organization header from the response if [Org ID] is undefined (not setup in ENV)
+    // Also This one is to prevent the header from being sent to the client
+    if (!serverConfig.openaiOrgId) {
+      newHeaders.delete("OpenAI-Organization");
+    }
+
     return new Response(res.body, {
       status: res.status,
       statusText: res.statusText,