Skip to content

Commit 0d25713

Browse files
authored
Merge pull request #164 from simonredfern/develop
If large json response don't highlight it
2 parents 469beb5 + ba03c59 commit 0d25713

2 files changed

Lines changed: 76 additions & 44 deletions

File tree

src/components/Menu.vue

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import {
3333
HEADER_LINKS_BACKGROUND_COLOR as headerLinksBackgroundColorSetting
3434
} from '../obp/style-setting'
3535
import { inject, ref, computed, onMounted } from 'vue'
36+
37+
const obpApiHost = ref(import.meta.env.VITE_OBP_API_HOST)
3638
import { getOBPBanks } from '../obp'
3739
import SvelteDropdown from './SvelteDropdown.vue'
3840
@@ -95,6 +97,7 @@ const handleLocale = (command: string) => {
9597
<span id="selected-endpoint-tags" class="endpoint-tags"></span>
9698
</el-col>
9799
<el-col :span="14" class="menu-right">
100+
<a :href="obpApiHost" class="api-host-label" target="_blank">{{ obpApiHost }}</a>
98101
<SvelteDropdown
99102
class="menu-right bank-selector"
100103
id="menu-nav-banks"
@@ -176,6 +179,14 @@ a:hover {
176179
text-decoration: underline;
177180
}
178181
182+
.api-host-label {
183+
font-size: 14px;
184+
font-family: 'Roboto';
185+
color: #7787a6;
186+
margin-right: 10px;
187+
vertical-align: middle;
188+
}
189+
179190
.bank-selector {
180191
display: inline-block;
181192
vertical-align: middle;

src/components/Preview.vue

Lines changed: 65 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const showConnectorMethods = ref(true)
5959
const isUserLogon = ref(true)
6060
const userEntitlements = ref([])
6161
const type = ref('')
62+
const isLoading = ref(false)
6263
const resourceDocs = inject(obpResourceDocsKey)
6364
const footNote = ref({
6465
operationId: '',
@@ -186,43 +187,48 @@ const setType = (method) => {
186187
}
187188
const submitRequest = async () => {
188189
if (url.value) {
189-
switch (method.value) {
190-
case 'POST': {
191-
highlightCode(
192-
await create(
193-
url.value,
194-
(() => {
195-
const rawBody = exampleRequestBody.value
196-
const maybeBody = typeof rawBody === 'string' ? rawBody.trim() : rawBody
197-
return maybeBody ? maybeBody : undefined
198-
})()
190+
isLoading.value = true
191+
try {
192+
switch (method.value) {
193+
case 'POST': {
194+
highlightCode(
195+
await create(
196+
url.value,
197+
(() => {
198+
const rawBody = exampleRequestBody.value
199+
const maybeBody = typeof rawBody === 'string' ? rawBody.trim() : rawBody
200+
return maybeBody ? maybeBody : undefined
201+
})()
202+
)
199203
)
200-
)
201-
break
202-
}
203-
case 'PUT': {
204-
highlightCode(
205-
await update(
206-
url.value,
207-
(() => {
208-
const rawBody = exampleRequestBody.value
209-
const maybeBody = typeof rawBody === 'string' ? rawBody.trim() : rawBody
210-
return maybeBody ? maybeBody : undefined
211-
})()
204+
break
205+
}
206+
case 'PUT': {
207+
highlightCode(
208+
await update(
209+
url.value,
210+
(() => {
211+
const rawBody = exampleRequestBody.value
212+
const maybeBody = typeof rawBody === 'string' ? rawBody.trim() : rawBody
213+
return maybeBody ? maybeBody : undefined
214+
})()
215+
)
212216
)
213-
)
214-
break
215-
}
216-
case 'DELETE': {
217-
highlightCode(await discard(url.value))
218-
break
219-
}
220-
default: {
221-
highlightCode(await get(url.value))
222-
break
217+
break
218+
}
219+
case 'DELETE': {
220+
highlightCode(await discard(url.value))
221+
break
222+
}
223+
default: {
224+
highlightCode(await get(url.value))
225+
break
226+
}
223227
}
228+
responseHeaderTitle.value = 'RESPONSE'
229+
} finally {
230+
isLoading.value = false
224231
}
225-
responseHeaderTitle.value = 'RESPONSE'
226232
} else {
227233
ElNotification({
228234
duration: elMessageDuration,
@@ -243,6 +249,11 @@ const parseDoubleEncodedJson = (obj: any): any => {
243249
244250
// If it's a string, try to parse it as JSON
245251
if (typeof obj === 'string') {
252+
// Skip strings that can't be JSON (must start with { [ or ")
253+
const trimmed = obj.trimStart()
254+
if (trimmed.length === 0 || (trimmed[0] !== '{' && trimmed[0] !== '[' && trimmed[0] !== '"')) {
255+
return obj
256+
}
246257
try {
247258
const parsed = JSON.parse(obj)
248259
// Recursively parse the result in case it's triple-encoded or more
@@ -279,20 +290,28 @@ const highlightCode = (json) => {
279290
return
280291
}
281292
293+
let dataToFormat
282294
if (json.error) {
283-
// Parse double-encoded JSON error messages to display them cleanly
284-
const errorObj = parseDoubleEncodedJson(json.error)
295+
dataToFormat = parseDoubleEncodedJson(json.error)
296+
} else {
297+
dataToFormat = parseDoubleEncodedJson(json)
298+
}
285299
286-
// Display the full OBP error object with proper formatting
287-
successResponseBody.value = hljs.lineNumbersValue(
288-
hljs.highlightAuto(JSON.stringify(errorObj, null, 4), ['JSON']).value
289-
)
300+
const jsonString = JSON.stringify(dataToFormat, null, 4)
301+
302+
// For large responses, show plain text without highlighting to avoid freezing the UI
303+
if (jsonString.length > 50000) {
304+
successResponseBody.value = jsonString.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
305+
return
306+
}
307+
308+
const highlighted = hljs.highlight(jsonString, { language: 'json' }).value
309+
const lineCount = jsonString.split('\n').length
310+
311+
if (lineCount > 500) {
312+
successResponseBody.value = highlighted
290313
} else {
291-
// Parse double-encoded JSON in successful responses too
292-
const parsedJson = parseDoubleEncodedJson(json)
293-
successResponseBody.value = hljs.lineNumbersValue(
294-
hljs.highlightAuto(JSON.stringify(parsedJson, null, 4), ['JSON']).value
295-
)
314+
successResponseBody.value = hljs.lineNumbersValue(highlighted)
296315
}
297316
}
298317
const submitSingleEntitlement = async (formRole: any, idx: number) => {
@@ -701,6 +720,7 @@ const onError = (error) => {
701720
/>
702721
<el-button
703722
:type="type"
723+
:loading="isLoading"
704724
id="search-button"
705725
@click="submit(requestFormRef, submitRequest)"
706726
>{{ method }}</el-button
@@ -858,6 +878,7 @@ span {
858878
pre {
859879
padding: 0px 30px 0px 30px;
860880
max-height: 340px;
881+
overflow: auto;
861882
background-color: #253047;
862883
font-size: 14px;
863884
margin: 0;

0 commit comments

Comments
 (0)