@@ -55,39 +55,56 @@ export class Client {
5555 * Gets the default HTTP client implementation
5656 */
5757 private getDefaultHttpClient ( ) : HttpClient {
58- // This would typically be implemented elsewhere, but we'll provide a basic implementation
58+ const handleResponse = async ( response : Response ) : Promise < any > => {
59+ if ( response . status === 204 ) {
60+ return null ;
61+ }
62+
63+ if ( ! response . ok ) {
64+ const text = await response . text ( ) ;
65+ throw new Error ( `HTTP error ${ response . status } : ${ text } ` ) ;
66+ }
67+
68+ const contentType = response . headers . get ( "Content-Type" ) ;
69+ if ( contentType && contentType . includes ( "application/json" ) ) {
70+ return await response . json ( ) ;
71+ } else {
72+ return await response . text ( ) ;
73+ }
74+ } ;
75+
5976 return {
6077 get : async < T > ( url : string , headers ?: Record < string , string > ) : Promise < T > => {
6178 const response = await fetch ( url , { method : 'GET' , headers } ) ;
62- return response . json ( ) ;
79+ return handleResponse ( response ) ;
6380 } ,
6481 post : async < T > ( url : string , body : any , headers ?: Record < string , string > ) : Promise < T > => {
6582 const response = await fetch ( url , {
6683 method : 'POST' ,
6784 headers,
6885 body : JSON . stringify ( body )
6986 } ) ;
70- return response . json ( ) ;
87+ return handleResponse ( response ) ;
7188 } ,
7289 put : async < T > ( url : string , body : any , headers ?: Record < string , string > ) : Promise < T > => {
7390 const response = await fetch ( url , {
7491 method : 'PUT' ,
7592 headers,
7693 body : JSON . stringify ( body )
7794 } ) ;
78- return response . json ( ) ;
95+ return handleResponse ( response ) ;
7996 } ,
8097 patch : async < T > ( url : string , body : any , headers ?: Record < string , string > ) : Promise < T > => {
8198 const response = await fetch ( url , {
8299 method : 'PATCH' ,
83100 headers,
84101 body : JSON . stringify ( body )
85102 } ) ;
86- return response . json ( ) ;
103+ return handleResponse ( response ) ;
87104 } ,
88105 delete : async < T > ( url : string , headers ?: Record < string , string > ) : Promise < T > => {
89106 const response = await fetch ( url , { method : 'DELETE' , headers } ) ;
90- return response . json ( ) ;
107+ return handleResponse ( response ) ;
91108 } ,
92109 } ;
93110 }
0 commit comments