@@ -852,6 +852,59 @@ export interface AsyncRequestOptions extends RequestOptions {
852852 body : { async_handle : string ; } & Required < RequestOptions [ "body" ] > ;
853853}
854854
855+ const SENSITIVE_KEYS = [
856+ "token" ,
857+ "password" ,
858+ "secret" ,
859+ "apiKey" ,
860+ "authorization" ,
861+ "auth" ,
862+ "key" ,
863+ "access_token" ,
864+ ] ;
865+
866+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
867+ function sanitize ( value : any , seen = new WeakSet ( ) ) : any {
868+ if ( value === null || value === undefined ) return value ;
869+
870+ if ( typeof value === "object" ) {
871+ if ( seen . has ( value ) ) return "[CIRCULAR]" ;
872+ seen . add ( value ) ;
873+
874+ if ( Array . isArray ( value ) ) {
875+ return value . map ( ( v ) => sanitize ( v , seen ) ) ;
876+ }
877+
878+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
879+ const sanitizedObj : Record < string , any > = { } ;
880+ for ( const [
881+ k ,
882+ v ,
883+ ] of Object . entries ( value ) ) {
884+ const isSensitiveKey = SENSITIVE_KEYS . some ( ( sensitiveKey ) =>
885+ k . toLowerCase ( ) . includes ( sensitiveKey . toLowerCase ( ) ) ) ;
886+ sanitizedObj [ k ] = isSensitiveKey
887+ ? "[REDACTED]"
888+ : sanitize ( v , seen ) ;
889+ }
890+ return sanitizedObj ;
891+ }
892+
893+ return value ; // numbers, booleans, functions, etc.
894+ }
895+
896+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
897+ export function DEBUG ( ...args : any [ ] ) {
898+ if (
899+ typeof process !== "undefined" &&
900+ typeof process . env !== "undefined" &&
901+ process . env . PD_SDK_DEBUG === "true"
902+ ) {
903+ const safeArgs = args . map ( ( arg ) => sanitize ( arg ) ) ;
904+ console . log ( "[PD_SDK_DEBUG]" , ...safeArgs ) ;
905+ }
906+ }
907+
855908/**
856909 * A client for interacting with the Pipedream Connect API on the server-side.
857910 */
@@ -966,20 +1019,24 @@ export abstract class BaseClient {
9661019
9671020 const response : Response = await fetch ( url . toString ( ) , requestOptions ) ;
9681021
1022+ const rawBody = await response . text ( ) ;
1023+
9691024 if ( ! response . ok ) {
970- const errorBody = await response . text ( ) ;
971- throw new Error (
972- `HTTP error! status: ${ response . status } , body: ${ errorBody } ` ,
973- ) ;
1025+ throw new Error ( `HTTP error! status: ${ response . status } , body: ${ rawBody } ` ) ;
9741026 }
9751027
976- // Attempt to parse JSON, fall back to raw text if it fails
1028+ DEBUG ( response . status , url . toString ( ) , requestOptions , rawBody )
9771029 const contentType = response . headers . get ( "Content-Type" ) ;
9781030 if ( contentType && contentType . includes ( "application/json" ) ) {
979- return ( await response . json ( ) ) as T ;
1031+ try {
1032+ const json = JSON . parse ( rawBody ) ;
1033+ return json as T ;
1034+ } catch ( err ) {
1035+ DEBUG ( "Couldn't parse json, falling back to raw" , err )
1036+ }
9801037 }
9811038
982- return ( await response . text ( ) ) as unknown as T ;
1039+ return rawBody as unknown as T ;
9831040 }
9841041
9851042 protected abstract authHeaders ( ) : string | Promise < string > ;
0 commit comments