@@ -847,6 +847,17 @@ export interface AsyncRequestOptions extends RequestOptions {
847847 body : { async_handle : string ; } & Required < RequestOptions [ "body" ] > ;
848848}
849849
850+ export function DEBUG ( ...args : any [ ] ) {
851+ if (
852+ typeof process !== "undefined" &&
853+ typeof process . env !== "undefined" &&
854+ process . env . DEBUG === "true"
855+ ) {
856+ console . log ( "[DEBUG]" , ...args ) ;
857+ }
858+ }
859+
860+
850861/**
851862 * A client for interacting with the Pipedream Connect API on the server-side.
852863 */
@@ -905,6 +916,8 @@ export abstract class BaseClient {
905916 ...fetchOpts
906917 } = opts ;
907918
919+
920+
908921 const url = new URL ( `${ baseURL } ${ path } ` ) ;
909922
910923 if ( params ) {
@@ -958,10 +971,13 @@ export abstract class BaseClient {
958971 ) {
959972 requestOptions . body = processedBody ;
960973 }
974+ DEBUG ( "makeRequest" )
975+ DEBUG ( "url: " , url . toString ( ) )
976+ DEBUG ( "requestOptions: " , requestOptions )
961977
962978 const response : Response = await fetch ( url . toString ( ) , requestOptions ) ;
963979
964- if ( ! response . ok ) {
980+ /* if (!response.ok) {
965981 const errorBody = await response.text();
966982 throw new Error(
967983 `HTTP error! status: ${response.status}, body: ${errorBody}`,
@@ -974,7 +990,27 @@ export abstract class BaseClient {
974990 return (await response.json()) as T;
975991 }
976992
977- return ( await response . text ( ) ) as unknown as T ;
993+ return (await response.text()) as unknown as T;*/
994+ const rawBody = await response . text ( ) ;
995+ DEBUG ( "Response status:" , response . status ) ;
996+ DEBUG ( "Response body:" , rawBody ) ;
997+
998+ if ( ! response . ok ) {
999+ throw new Error ( `HTTP error! status: ${ response . status } , body: ${ rawBody } ` ) ;
1000+ }
1001+
1002+ const contentType = response . headers . get ( "Content-Type" ) ;
1003+ if ( contentType && contentType . includes ( "application/json" ) ) {
1004+ try {
1005+ const json = JSON . parse ( rawBody ) ;
1006+ DEBUG ( "Parsed JSON:" , json ) ;
1007+ return json as T ;
1008+ } catch ( err ) {
1009+ DEBUG ( "Failed to parse JSON, returning raw body as fallback." ) ;
1010+ }
1011+ }
1012+
1013+ return rawBody as unknown as T ;
9781014 }
9791015
9801016 protected abstract authHeaders ( ) : string | Promise < string > ;
0 commit comments