Skip to content
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions app/client/src/pages/Editor/QueryEditor/QueryDebuggerTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,27 @@ interface QueryDebuggerTabsProps {
onRunClick: () => void;
showSchema?: boolean;
}
const parseResponseBody = (body: any): any[] => {
let parsedOutput: any;

if (typeof body === "string") {
try {
parsedOutput = JSON.parse(body);
} catch (e) {
return [{ response: body }];
}
} else {
parsedOutput = body;
}

if (Array.isArray(parsedOutput)) {
return parsedOutput;
} else if (parsedOutput.records && Array.isArray(parsedOutput.records)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can remove else from the line 69 also

return parsedOutput.records;
}
return [parsedOutput];

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor suggestion: Simplify JSON parsing and response handling.

The function parseResponseBody is intended to handle various data structures. However, the current implementation can be optimized by removing redundant else clauses as indicated by the static analysis. This will improve readability and reduce unnecessary nesting.

const parseResponseBody = (body: any): any[] => {
  let parsedOutput: any;
  if (typeof body === "string") {
    try {
      parsedOutput = JSON.parse(body);
    } catch (e) {
      return [{ response: body }];
    }
  } else {
    parsedOutput = body;
  }
  if (Array.isArray(parsedOutput)) {
    return parsedOutput;
  } else if (parsedOutput.records && Array.isArray(parsedOutput.records)) {
    return parsedOutput.records;
  }
  return [parsedOutput];
};
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const parseResponseBody = (body: any): any[] => {
let parsedOutput: any;
if (typeof body === "string") {
try {
parsedOutput = JSON.parse(body);
} catch (e) {
return [{ response: body }];
}
} else {
parsedOutput = body;
}
if (Array.isArray(parsedOutput)) {
return parsedOutput;
} else if (parsedOutput.records && Array.isArray(parsedOutput.records)) {
return parsedOutput.records;
}
return [parsedOutput];
const parseResponseBody = (body: any): any[] => {
let parsedOutput: any;
if (typeof body === "string") {
try {
parsedOutput = JSON.parse(body);
} catch (e) {
return [{ response: body }];
}
} else {
parsedOutput = body;
}
if (Array.isArray(parsedOutput)) {
return parsedOutput;
}
if (parsedOutput.records && Array.isArray(parsedOutput.records)) {
return parsedOutput.records;
}
return [parsedOutput];
};
Tools
Biome

[error] 69-71: This else clause can be omitted because previous branches break early. (lint/style/noUselessElse)

Unsafe fix: Omit the else clause.

};
Comment on lines 54 to 74
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor suggestion: Simplify JSON parsing and response handling.

The function parseResponseBody is intended to handle various data structures. However, the current implementation can be optimized by removing redundant else clauses as indicated by the static analysis. This will improve readability and reduce unnecessary nesting.

const parseResponseBody = (body: any): any[] => {
  let parsedOutput: any;
  if (typeof body === "string") {
    try {
      parsedOutput = JSON.parse(body);
    } catch (e) {
      return [{ response: body }];
    }
  } else {
    parsedOutput = body;
  }
  if (Array.isArray(parsedOutput)) {
    return parsedOutput;
  } else if (parsedOutput.records && Array.isArray(parsedOutput.records)) {
    return parsedOutput.records;
  }
  return [parsedOutput];
};
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const parseResponseBody = (body: any): any[] => {
let parsedOutput: any;
if (typeof body === "string") {
try {
parsedOutput = JSON.parse(body);
} catch (e) {
return [{ response: body }];
}
} else {
parsedOutput = body;
}
if (Array.isArray(parsedOutput)) {
return parsedOutput;
} else if (parsedOutput.records && Array.isArray(parsedOutput.records)) {
return parsedOutput.records;
} else {
return [parsedOutput];
}
};
const parseResponseBody = (body: any): any[] => {
let parsedOutput: any;
if (typeof body === "string") {
try {
parsedOutput = JSON.parse(body);
} catch (e) {
return [{ response: body }];
}
} else {
parsedOutput = body;
}
if (Array.isArray(parsedOutput)) {
return parsedOutput;
}
if (parsedOutput.records && Array.isArray(parsedOutput.records)) {
return parsedOutput.records;
}
return [parsedOutput];
};
Tools
Biome

[error] 69-73: This else clause can be omitted because previous branches break early. (lint/style/noUselessElse)

Unsafe fix: Omit the else clause.


[error] 71-73: This else clause can be omitted because previous branches break early. (lint/style/noUselessElse)


function QueryDebuggerTabs({
actionName,
Expand Down Expand Up @@ -152,7 +173,7 @@ function QueryDebuggerTabs({
if (isString(actionResponse.body)) {
try {
// Try to parse response as JSON array to be displayed in the Response tab
output = JSON.parse(actionResponse.body);
output = parseResponseBody(actionResponse.body);
} catch (e) {
// In case the string is not a JSON, wrap it in a response object
output = [
Expand All @@ -162,7 +183,7 @@ function QueryDebuggerTabs({
];
}
} else {
output = actionResponse.body as any;
output = parseResponseBody(actionResponse.body);
}
}

Expand Down