@@ -12,7 +12,11 @@ import type { TestCaseResponse } from './cache'
1212import { useI18n } from ' vue-i18n'
1313import { JSONPath } from ' jsonpath-plus'
1414import { Codemirror } from ' vue-codemirror'
15+ import jsonlint from ' jsonlint-mod'
16+ import { useMagicKeys } from ' @vueuse/core'
1517
18+ const keys = useMagicKeys ()
19+ const keyAltS = keys [' Alt+S' ]
1620const { t } = useI18n ()
1721
1822const props = defineProps ({
@@ -26,14 +30,25 @@ let querySuggestedAPIs = NewSuggestedAPIsQuery(Cache.GetCurrentStore().name!, pr
2630const testResultActiveTab = ref (Cache .GetPreference ().responseActiveTab )
2731watch (testResultActiveTab , Cache .WatchResponseActiveTab )
2832
33+ watch (keyAltS , (v ) => {
34+ if (v ) {
35+ sendRequest ()
36+ }
37+ })
38+
2939const parameters = ref ([] as Pair [])
3040const requestLoading = ref (false )
3141const testResult = ref ({ header: [] as Pair [] } as TestResult )
3242const sendRequest = async () => {
3343 if (needUpdate .value ) {
34- await saveTestCase (false )
44+ await saveTestCase (false , runTestCase )
45+ needUpdate .value = false
46+ } else {
47+ runTestCase ()
3548 }
49+ }
3650
51+ const runTestCase = () => {
3752 requestLoading .value = true
3853 const name = props .name
3954 const suite = props .suite
@@ -280,14 +295,18 @@ watch(testCaseWithSuite, (after, before) => {
280295}, { deep: true })
281296
282297const saveLoading = ref (false )
283- function saveTestCase(tip : boolean = true ) {
298+ function saveTestCase(tip : boolean = true , callback : ( c : any ) => void ) {
284299 UIAPI .UpdateTestCase (testCaseWithSuite .value , (e ) => {
285300 if (tip ) {
286301 ElMessage ({
287302 message: ' Saved.' ,
288303 type: ' success'
289304 })
290305 }
306+
307+ if (callback ) {
308+ callback ()
309+ }
291310 }, UIAPI .ErrorTip , saveLoading )
292311}
293312
@@ -433,15 +452,21 @@ function bodyTypeChange(e: number) {
433452 }
434453}
435454
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 === ' ' ) {
438459 return
439460 }
440461
441462 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 = ' '
443468 } catch (e ) {
444- console . log ( e )
469+ lintingError . value = e . message
445470 }
446471}
447472
@@ -539,7 +564,7 @@ const duplicateTestCase = () => {
539564 v-model =" testCaseWithSuite.data.request.method"
540565 class =" m-2"
541566 placeholder =" Method"
542- size =" middle "
567+ size =" default "
543568 test-id =" case-editor-method"
544569 >
545570 <el-option
@@ -654,6 +679,11 @@ const duplicateTestCase = () => {
654679 </el-tab-pane >
655680
656681 <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 >
657687 <template #label >
658688 <el-badge :is-dot =" testCaseWithSuite.data.request.body !== ''" class =" item" >Body</el-badge >
659689 </template >
@@ -667,7 +697,7 @@ const duplicateTestCase = () => {
667697
668698 <div style =" flex-grow : 1 ;" >
669699 <Codemirror v-if =" bodyType === 3 || bodyType === 5"
670- @change =" jsonFormat"
700+ @blur =" jsonFormat(-1) "
671701 v-model =" testCaseWithSuite.data.request.body" />
672702 <el-table :data =" testCaseWithSuite.data.request.form" style =" width : 100% " v-if =" bodyType === 4" >
673703 <el-table-column label =" Key" width =" 180" >
@@ -684,6 +714,9 @@ const duplicateTestCase = () => {
684714 </el-table-column >
685715 </el-table >
686716 </div >
717+ <div v-if =" lintingError" style =" color : red ; margin-top : 10px ;" >
718+ {{ lintingError }}
719+ </div >
687720 </el-tab-pane >
688721
689722 <el-tab-pane label =" Expected" name =" expected" v-if =" props.kindName !== 'tRPC' && props.kindName !== 'gRPC'" >
@@ -777,7 +810,7 @@ const duplicateTestCase = () => {
777810 v-model =" currentCodeGenerator"
778811 class =" m-2"
779812 style =" padding-right : 10px ;"
780- size =" middle "
813+ size =" default "
781814 >
782815 <el-option
783816 v-for =" item in codeGenerators"
0 commit comments