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,8 +30,8 @@ export async function requestOpenai(req: NextRequest) {
controller.abort(); controller.abort();
}, 10 * 60 * 1000); }, 10 * 60 * 1000);
try { const fetchUrl = `${baseUrl}/${openaiPath}`;
return await fetch(`${baseUrl}/${openaiPath}`, { const fetchOptions: RequestInit = {
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
Authorization: authValue, Authorization: authValue,
@ -43,13 +43,17 @@ export async function requestOpenai(req: NextRequest) {
method: req.method, method: req.method,
body: req.body, body: req.body,
signal: controller.signal, signal: controller.signal,
}); };
} catch (err: unknown) {
if (err instanceof Error && err.name === "AbortError") { try {
console.log("Fetch aborted"); const res = await fetch(fetchUrl, fetchOptions);
} else {
throw err; if (res.status === 401) {
// to prevent browser prompt for credentials
res.headers.delete("www-authenticate");
} }
return res;
} finally { } finally {
clearTimeout(timeoutId); clearTimeout(timeoutId);
} }