@@ -20,26 +20,9 @@ export async function fetch<T>(endpoint: RequestInfo, init: RequestInit & { noti
2020 const response = await window . fetch ( endpoint , config ) ;
2121
2222 if ( response . ok ) {
23- // First, clone the response to avoid consuming the body multiple times
24- const clonedResponse = response . clone ( ) ;
25- try {
26- return await clonedResponse . json ( ) ;
27- } catch ( jsonError ) {
28- // If JSON parsing fails, return empty object
29- console . warn ( 'Failed to parse JSON response:' , jsonError ) ;
30- return { } as T ;
31- }
23+ return await response . json ( ) . catch ( ( ) => ( { } ) ) ;
3224 } else {
33- // Clone the response before reading text to avoid consumption issues
34- const clonedResponse = response . clone ( ) ;
35- let errorMessage = response . status . toString ( ) ;
36-
37- try {
38- errorMessage = await clonedResponse . text ( ) || errorMessage ;
39- } catch ( textError ) {
40- console . warn ( 'Failed to read error response text:' , textError ) ;
41- }
42-
25+ const errorMessage = ( await response . text ( ) ) || response . status . toString ( ) ;
4326 console . error ( `HTTP ${ response . status } : ${ errorMessage } ` , response ) ;
4427 if ( notifyOnError || notifyOnError === undefined ) notifyError ( errorMessage ) ;
4528 return Promise . reject ( new Error ( errorMessage ) ) ;
@@ -83,19 +66,7 @@ async function get<T>(path: string, config?: RequestInit & { notifyOnError?: boo
8366}
8467
8568async function post < T , U > ( path : string , body ?: T , config ?: RequestInit & { notifyOnError ?: boolean } ) : Promise < U > {
86- const defaultHeaders = {
87- 'Content-Type' : 'application/json' ,
88- } ;
89-
90- const init = {
91- method : "POST" ,
92- body : body ? JSON . stringify ( body ) : undefined ,
93- headers : {
94- ...defaultHeaders ,
95- ...( config ?. headers || { } )
96- } ,
97- ...config
98- } ;
69+ const init = { method : "POST" , body : JSON . stringify ( body ) , ...config } ;
9970 return fetch < U > ( path , init ) ;
10071}
10172
@@ -149,4 +120,4 @@ export function parseHttpException(ex: any, t: (key: string) => string): string[
149120 return [ t ( "common.forbidden" ) ] ;
150121 }
151122 return [ t ( "common.error" ) ] ;
152- }
123+ }
0 commit comments