Skip to content

Commit ecb9960

Browse files
authored
add sdk debug mode (#16306)
* Add DEBUG mode * pnpm-lock * -dev * changelog and version bump * cleanup * linting * lock * ugh, bad merge
1 parent cdca53c commit ecb9960

File tree

6 files changed

+230
-76
lines changed

6 files changed

+230
-76
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"devDependencies": {
4242
"@eslint/eslintrc": "^3.2.0",
4343
"@eslint/js": "^9.15.0",
44+
"@next/eslint-plugin-next": "^14.2.5",
4445
"@pipedream/types": "0.1.4",
4546
"@tsconfig/node14": "^1.0.1",
4647
"@types/jest": "^27.4.1",
@@ -59,7 +60,6 @@
5960
"husky": "^7.0.4",
6061
"jest": "^29.7.0",
6162
"lint-staged": "^12.3.4",
62-
"@next/eslint-plugin-next": "^14.2.5",
6363
"pnpm": "9.14.2",
6464
"putout": ">=36",
6565
"renamer": "^4.0.0",

packages/sdk/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
# Changelog
44

5+
## [1.5.2] - 2025-04-15
6+
7+
### Added
8+
9+
- PD_SDK_DEBUG env var. Set it to true to enable debugging of Pipedream Connect
10+
API requests. Simple sanitization is performed to prevent sensitive field leakage
11+
but use caution.
12+
513
## [1.5.1] - 2025-04-15
614

715
### Added

packages/sdk/src/shared/index.ts

Lines changed: 64 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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>;

packages/sdk/src/shared/shims.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare const process: {
2+
env?: {
3+
[key: string]: string | undefined;
4+
};
5+
} | undefined;

packages/sdk/tsconfig.browser.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"allowJs": true
1515
},
1616
"include": [
17+
"src/shared/shims.d.ts",
1718
"src/browser/**/*"
1819
],
1920
"exclude": [

0 commit comments

Comments
 (0)