@@ -30,26 +30,30 @@ export async function requestOpenai(req: NextRequest) {
30
30
controller . abort ( ) ;
31
31
} , 10 * 60 * 1000 ) ;
32
32
33
+ const fetchUrl = `${ baseUrl } /${ openaiPath } ` ;
34
+ const fetchOptions : RequestInit = {
35
+ headers : {
36
+ "Content-Type" : "application/json" ,
37
+ Authorization : authValue ,
38
+ ...( process . env . OPENAI_ORG_ID && {
39
+ "OpenAI-Organization" : process . env . OPENAI_ORG_ID ,
40
+ } ) ,
41
+ } ,
42
+ cache : "no-store" ,
43
+ method : req . method ,
44
+ body : req . body ,
45
+ signal : controller . signal ,
46
+ } ;
47
+
33
48
try {
34
- return await fetch ( `${ baseUrl } /${ openaiPath } ` , {
35
- headers : {
36
- "Content-Type" : "application/json" ,
37
- Authorization : authValue ,
38
- ...( process . env . OPENAI_ORG_ID && {
39
- "OpenAI-Organization" : process . env . OPENAI_ORG_ID ,
40
- } ) ,
41
- } ,
42
- cache : "no-store" ,
43
- method : req . method ,
44
- body : req . body ,
45
- signal : controller . signal ,
46
- } ) ;
47
- } catch ( err : unknown ) {
48
- if ( err instanceof Error && err . name === "AbortError" ) {
49
- console . log ( "Fetch aborted" ) ;
50
- } else {
51
- throw err ;
49
+ const res = await fetch ( fetchUrl , fetchOptions ) ;
50
+
51
+ if ( res . status === 401 ) {
52
+ // to prevent browser prompt for credentials
53
+ res . headers . delete ( "www-authenticate" ) ;
52
54
}
55
+
56
+ return res ;
53
57
} finally {
54
58
clearTimeout ( timeoutId ) ;
55
59
}
0 commit comments