11import type { TreeViewNode } from 'reactive-vscode'
22import type { ApiDetail , MockApiData } from '../types'
3+ import { parse , stringify } from 'comment-json'
34import { createSingletonComposable , executeCommand , ref , useCommand , useTreeView } from 'reactive-vscode'
45import { TreeItemCollapsibleState , Uri , window , workspace } from 'vscode'
56import { config } from '../config'
@@ -151,6 +152,21 @@ export const useMockTreeView = createSingletonComposable(async () => {
151152 } )
152153 } )
153154
155+ useCommand ( commands . aiGenerateMock , async ( event ) => {
156+ if ( ! event . treeItem || ! event . treeItem . mockItem ) {
157+ logger . error ( 'No mock item found in the event.' )
158+ return
159+ }
160+
161+ const mockItem = event . treeItem . mockItem as MockApiData
162+ const [ projectId , catId , interfaceId ] = mockItem . key . split ( '/' )
163+ await ofetch ( `${ crabuApiBaseUrl } /mock/template/ai/${ projectId } /${ catId } /${ interfaceId } ` , {
164+ method : 'POST' ,
165+ } )
166+
167+ window . showInformationMessage ( '正在使用AI生成Mock数据,请稍后...' )
168+ } )
169+
154170 useCommand ( commands . compareWithLatestVersion , async ( event ) => {
155171 if ( ! event . treeItem || ! event . treeItem . mockItem ) {
156172 logger . error ( 'No mock item found in the event.' )
@@ -162,25 +178,25 @@ export const useMockTreeView = createSingletonComposable(async () => {
162178 const oldDetail = await ofetch < ApiDetail > ( `${ crabuApiBaseUrl } /interface/local/json/${ mockItem . key } ` )
163179 const newDetail = await ofetch < ApiDetail > ( `${ crabuApiBaseUrl } /interface/json/${ projectId } /${ interfaceId } ` )
164180
165- oldDetail . req_body = JSON . parse ( oldDetail . req_body )
166- oldDetail . res_body = JSON . parse ( oldDetail . res_body )
167- newDetail . req_body = JSON . parse ( newDetail . req_body )
168- newDetail . res_body = JSON . parse ( newDetail . res_body )
181+ oldDetail . req_body = parse ( oldDetail . req_body as string )
182+ oldDetail . res_body = parse ( oldDetail . res_body as string )
183+ newDetail . req_body = parse ( newDetail . req_body as string )
184+ newDetail . res_body = parse ( newDetail . res_body as string )
169185
170186 workspace . registerTextDocumentContentProvider ( crabuDiffOldScheme , {
171187 provideTextDocumentContent : ( ) => {
172- return JSON . stringify ( oldDetail , null , 2 )
188+ return stringify ( oldDetail , null , 2 )
173189 } ,
174190 } )
175191
176192 workspace . registerTextDocumentContentProvider ( crabuDiffNewScheme , {
177193 provideTextDocumentContent : ( ) => {
178- return JSON . stringify ( newDetail , null , 2 )
194+ return stringify ( newDetail , null , 2 )
179195 } ,
180196 } )
181197
182- const oldUri = Uri . parse ( `${ crabuDiffOldScheme } :${ mockItem . label } .json ` )
183- const newUri = Uri . parse ( `${ crabuDiffNewScheme } :${ mockItem . label } .json ` )
198+ const oldUri = Uri . parse ( `${ crabuDiffOldScheme } :${ mockItem . label } .jsonc ` )
199+ const newUri = Uri . parse ( `${ crabuDiffNewScheme } :${ mockItem . label } .jsonc ` )
184200
185201 executeCommand ( 'vscode.diff' , oldUri , newUri , `检查变更:${ mockItem . label } ` )
186202 } )
0 commit comments