feat: prevent browser to invoke basic auth popup

This commit is contained in:
Yidadaa 2023-05-21 00:06:28 +08:00
parent 600b1814a1
commit c2b36cdffa

View File

@ -30,26 +30,30 @@ export async function requestOpenai(req: NextRequest) {
controller.abort(); controller.abort();
}, 10 * 60 * 1000); }, 10 * 60 * 1000);
const fetchUrl = `${baseUrl}/${openaiPath}`;
const fetchOptions: RequestInit = {
headers: {
"Content-Type": "application/json",
Authorization: authValue,
...(process.env.OPENAI_ORG_ID && {
"OpenAI-Organization": process.env.OPENAI_ORG_ID,
}),
},
cache: "no-store",
method: req.method,
body: req.body,
signal: controller.signal,
};
try { try {
return await fetch(`${baseUrl}/${openaiPath}`, { const res = await fetch(fetchUrl, fetchOptions);
headers: {
"Content-Type": "application/json", if (res.status === 401) {
Authorization: authValue, // to prevent browser prompt for credentials
...(process.env.OPENAI_ORG_ID && { res.headers.delete("www-authenticate");
"OpenAI-Organization": process.env.OPENAI_ORG_ID,
}),
},
cache: "no-store",
method: req.method,
body: req.body,
signal: controller.signal,
});
} catch (err: unknown) {
if (err instanceof Error && err.name === "AbortError") {
console.log("Fetch aborted");
} else {
throw err;
} }
return res;
} finally { } finally {
clearTimeout(timeoutId); clearTimeout(timeoutId);
} }