@@ -12,7 +12,11 @@ import type { TestCaseResponse } from './cache'
12
12
import { useI18n } from ' vue-i18n'
13
13
import { JSONPath } from ' jsonpath-plus'
14
14
import { Codemirror } from ' vue-codemirror'
15
+ import jsonlint from ' jsonlint-mod'
16
+ import { useMagicKeys } from ' @vueuse/core'
15
17
18
+ const keys = useMagicKeys ()
19
+ const keyAltS = keys [' Alt+S' ]
16
20
const { t } = useI18n ()
17
21
18
22
const props = defineProps ({
@@ -26,14 +30,25 @@ let querySuggestedAPIs = NewSuggestedAPIsQuery(Cache.GetCurrentStore().name!, pr
26
30
const testResultActiveTab = ref (Cache .GetPreference ().responseActiveTab )
27
31
watch (testResultActiveTab , Cache .WatchResponseActiveTab )
28
32
33
+ watch (keyAltS , (v ) => {
34
+ if (v ) {
35
+ sendRequest ()
36
+ }
37
+ })
38
+
29
39
const parameters = ref ([] as Pair [])
30
40
const requestLoading = ref (false )
31
41
const testResult = ref ({ header: [] as Pair [] } as TestResult )
32
42
const sendRequest = async () => {
33
43
if (needUpdate .value ) {
34
- await saveTestCase (false )
44
+ await saveTestCase (false , runTestCase )
45
+ needUpdate .value = false
46
+ } else {
47
+ runTestCase ()
35
48
}
49
+ }
36
50
51
+ const runTestCase = () => {
37
52
requestLoading .value = true
38
53
const name = props .name
39
54
const suite = props .suite
@@ -280,14 +295,18 @@ watch(testCaseWithSuite, (after, before) => {
280
295
}, { deep: true })
281
296
282
297
const saveLoading = ref (false )
283
- function saveTestCase(tip : boolean = true ) {
298
+ function saveTestCase(tip : boolean = true , callback : ( c : any ) => void ) {
284
299
UIAPI .UpdateTestCase (testCaseWithSuite .value , (e ) => {
285
300
if (tip ) {
286
301
ElMessage ({
287
302
message: ' Saved.' ,
288
303
type: ' success'
289
304
})
290
305
}
306
+
307
+ if (callback ) {
308
+ callback ()
309
+ }
291
310
}, UIAPI .ErrorTip , saveLoading )
292
311
}
293
312
@@ -433,15 +452,21 @@ function bodyTypeChange(e: number) {
433
452
}
434
453
}
435
454
436
- function jsonFormat() {
437
- if (bodyType .value !== 5 ) {
455
+ const lintingError = ref (' ' )
456
+ function jsonFormat(space ) {
457
+ const jsonText = testCaseWithSuite .value .data .request .body
458
+ if (bodyType .value !== 5 || jsonText === ' ' ) {
438
459
return
439
460
}
440
461
441
462
try {
442
- testCaseWithSuite .value .data .request .body = JSON .stringify (JSON .parse (testCaseWithSuite .value .data .request .body ), null , 4 )
463
+ const jsonObj = jsonlint .parse (jsonText )
464
+ if (space >= 0 ) {
465
+ testCaseWithSuite .value .data .request .body = JSON .stringify (jsonObj , null , space )
466
+ }
467
+ lintingError .value = ' '
443
468
} catch (e ) {
444
- console . log ( e )
469
+ lintingError . value = e . message
445
470
}
446
471
}
447
472
@@ -539,7 +564,7 @@ const duplicateTestCase = () => {
539
564
v-model =" testCaseWithSuite.data.request.method"
540
565
class =" m-2"
541
566
placeholder =" Method"
542
- size =" middle "
567
+ size =" default "
543
568
test-id =" case-editor-method"
544
569
>
545
570
<el-option
@@ -654,6 +679,11 @@ const duplicateTestCase = () => {
654
679
</el-tab-pane >
655
680
656
681
<el-tab-pane name =" body" >
682
+ <span style =" margin-right : 10px ; padding-right : 5px ;" >
683
+ <el-button type =" primary" @click =" jsonFormat(4)" >Beautify</el-button >
684
+ <el-button type =" primary" @click =" jsonFormat(0)" >Minify</el-button >
685
+ <el-text class =" mx-1" >Choose the body format</el-text >
686
+ </span >
657
687
<template #label >
658
688
<el-badge :is-dot =" testCaseWithSuite.data.request.body !== ''" class =" item" >Body</el-badge >
659
689
</template >
@@ -667,7 +697,7 @@ const duplicateTestCase = () => {
667
697
668
698
<div style =" flex-grow : 1 ;" >
669
699
<Codemirror v-if =" bodyType === 3 || bodyType === 5"
670
- @change =" jsonFormat"
700
+ @blur =" jsonFormat(-1) "
671
701
v-model =" testCaseWithSuite.data.request.body" />
672
702
<el-table :data =" testCaseWithSuite.data.request.form" style =" width : 100% " v-if =" bodyType === 4" >
673
703
<el-table-column label =" Key" width =" 180" >
@@ -684,6 +714,9 @@ const duplicateTestCase = () => {
684
714
</el-table-column >
685
715
</el-table >
686
716
</div >
717
+ <div v-if =" lintingError" style =" color : red ; margin-top : 10px ;" >
718
+ {{ lintingError }}
719
+ </div >
687
720
</el-tab-pane >
688
721
689
722
<el-tab-pane label =" Expected" name =" expected" v-if =" props.kindName !== 'tRPC' && props.kindName !== 'gRPC'" >
@@ -777,7 +810,7 @@ const duplicateTestCase = () => {
777
810
v-model =" currentCodeGenerator"
778
811
class =" m-2"
779
812
style =" padding-right : 10px ;"
780
- size =" middle "
813
+ size =" default "
781
814
>
782
815
<el-option
783
816
v-for =" item in codeGenerators"
0 commit comments