Skip to content

Commit 6f88c1a

Browse files
committed
Add DEBUG mode
1 parent cd04a13 commit 6f88c1a

File tree

5 files changed

+227
-102
lines changed

5 files changed

+227
-102
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@
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",
48+
"@types/node": "^20.17.6",
4749
"@typescript-eslint/eslint-plugin": "^8",
4850
"@typescript-eslint/parser": "^8",
4951
"eslint": "^8",
@@ -59,7 +61,6 @@
5961
"husky": "^7.0.4",
6062
"jest": "^29.7.0",
6163
"lint-staged": "^12.3.4",
62-
"@next/eslint-plugin-next": "^14.2.5",
6364
"pnpm": "9.14.2",
6465
"putout": ">=36",
6566
"renamer": "^4.0.0",
@@ -77,7 +78,6 @@
7778
"@actions/core": "^1.10.0",
7879
"@pipedream/platform": "^1.5.1",
7980
"@sentry/node": "^7.7.0",
80-
"@types/node": "^20.17.6",
8181
"crypto": "^1.0.1",
8282
"linkup-sdk": "^1.0.3",
8383
"uuid": "^8.3.2",

packages/sdk/src/shared/index.ts

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

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

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