Skip to content

Commit 368a976

Browse files
authored
Merge pull request #595 from Cloud-Code-AI/vs-code-dev
UI changes to API request
2 parents ae681d7 + 93c5680 commit 368a976

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

extensions/vscode/src/apiRequest/apiRequestView.ts

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,17 @@ export class ApiRequestView {
278278
overflow-y: auto;
279279
border-left: 1px solid var(--vscode-panel-border);
280280
}
281+
/* Add these new styles for JSON formatting */
282+
.json-formatter {
283+
font-family: monospace;
284+
font-size: 14px;
285+
line-height: 1.4;
286+
}
287+
.json-formatter .json-key { color: #7c7cbe; }
288+
.json-formatter .json-string { color: #6a8759; }
289+
.json-formatter .json-number { color: #6897bb; }
290+
.json-formatter .json-boolean { color: #cc7832; }
291+
.json-formatter .json-null { color: #cc7832; }
281292
</style>
282293
</head>
283294
<body>
@@ -506,16 +517,49 @@ export class ApiRequestView {
506517
});
507518
});
508519
509-
// Receive response
520+
// Add this new function to format JSON
521+
function formatJSON(json) {
522+
if (typeof json !== 'string') {
523+
json = JSON.stringify(json, null, 2);
524+
}
525+
return json.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
526+
.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
527+
let cls = 'json-number';
528+
if (/^"/.test(match)) {
529+
if (/:$/.test(match)) {
530+
cls = 'json-key';
531+
} else {
532+
cls = 'json-string';
533+
}
534+
} else if (/true|false/.test(match)) {
535+
cls = 'json-boolean';
536+
} else if (/null/.test(match)) {
537+
cls = 'json-null';
538+
}
539+
return '<span class="' + cls + '">' + match + '</span>';
540+
});
541+
}
542+
543+
// Modify the receive response event listener
510544
window.addEventListener('message', event => {
511545
const message = event.data;
512546
switch (message.command) {
513547
case 'receiveResponse':
514548
document.getElementById('response-status').textContent = \`Status: \${message.response.status}\`;
515549
document.getElementById('response-time').textContent = \`Time: \${message.time} ms\`;
516550
document.getElementById('response-size').textContent = \`Size: \${message.size} Bytes\`;
517-
document.getElementById('response').textContent = message.response.body;
518-
document.getElementById('response-headers').textContent = JSON.stringify(message.response.headers, null, 2);
551+
552+
// Format the response body if it's JSON
553+
let formattedBody = message.response.body;
554+
try {
555+
const jsonBody = JSON.parse(message.response.body);
556+
formattedBody = formatJSON(JSON.stringify(jsonBody, null, 2));
557+
} catch (e) {
558+
// If parsing fails, it's not JSON, so we'll display it as is
559+
}
560+
document.getElementById('response').innerHTML = \`<pre class="json-formatter">\${formattedBody}</pre>\`;
561+
562+
document.getElementById('response-headers').innerHTML = \`<pre class="json-formatter">\${formatJSON(JSON.stringify(message.response.headers, null, 2))}</pre>\`;
519563
break;
520564
}
521565
});

0 commit comments

Comments
 (0)