1+ import { IS_PRODUCTION } from '@/utilsutils/isProduction'
2+ import { getVSCodeAPI } from '@/utilsutils/vscodeapi'
13import { tableFromIPC } from 'apache-arrow'
24
35declare global {
@@ -6,16 +8,16 @@ declare global {
68 }
79}
810
9- export interface ResponseWithDetail {
11+ interface ResponseWithDetail {
1012 ok : boolean
1113 detail ?: string
1214}
1315
14- export interface FetchOptionsWithSignal {
16+ interface FetchOptionsWithSignal {
1517 signal ?: AbortSignal
1618}
1719
18- export interface FetchOptions < B extends object = any > {
20+ interface FetchOptions < B extends object = any > {
1921 url : string
2022 method : 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'
2123 data ?: B
@@ -33,7 +35,7 @@ export interface FetchOptions<B extends object = any> {
3335 params ?: Record < string , string | number | boolean | undefined | null >
3436}
3537
36- export async function fetchAPI < T = any , B extends object = any > (
38+ async function fetchAPIDevelopment < T = any , B extends object = any > (
3739 config : FetchOptions < B > ,
3840 options ?: Partial < FetchOptionsWithSignal > ,
3941) : Promise < T & ResponseWithDetail > {
@@ -122,7 +124,7 @@ function toRequestBody(obj: unknown): BodyInit {
122124 }
123125}
124126
125- export function getUrlWithPrefix ( url : string = '/' ) : string {
127+ function getUrlWithPrefix ( url : string = '/' ) : string {
126128 let urlWithPrefix = `${
127129 window . __BASE_URL__ ?? '/'
128130 } /${ url } `
@@ -134,4 +136,73 @@ export function getUrlWithPrefix(url: string = '/'): string {
134136 return urlWithPrefix
135137}
136138
137- export default fetchAPI
139+ export async function fetchAPIProduction < T = any , B extends object = any > (
140+ config : FetchOptions < B > ,
141+ options ?: Partial < FetchOptionsWithSignal > ,
142+ ) : Promise < T & ResponseWithDetail > {
143+ console . log ( 'queryFn' , config , options )
144+ getVSCodeAPI ( ) . postMessage ( {
145+ type : 'fetchAPI' ,
146+ config,
147+ options,
148+ } )
149+
150+ // Generate a unique ID for this request
151+ const requestId = `query_${ Date . now ( ) } _${ Math . random ( ) . toString ( 36 ) . substr ( 2 , 9 ) } ` ;
152+ // Create a promise that will resolve when we get a response with matching ID
153+ return new Promise ( ( resolve , reject ) => {
154+ // Set up listener for the response
155+ console . log ( "setting up listener for response" )
156+ const messageHandler = ( event : MessageEvent ) => {
157+ if (
158+ event . data &&
159+ event . data . key === 'vscode_response' &&
160+ event . data . payload &&
161+ event . data . payload . requestId === requestId
162+ ) {
163+ // Remove the listener once we get our response
164+ window . removeEventListener ( 'message' , messageHandler ) ;
165+
166+ if ( event . data . payload . error ) {
167+ reject ( new Error ( event . data . payload . error ) ) ;
168+ } else {
169+ resolve ( event . data . payload . data ) ;
170+ }
171+ }
172+ } ;
173+
174+ // Add the listener
175+ window . addEventListener ( 'message' , messageHandler ) ;
176+
177+ // Send the message to VSCode with the query parameters and request ID
178+ // @ts -ignore
179+ const vscode = acquireVsCodeApi ( )
180+ vscode . postMessage ( {
181+ key : 'query_request' ,
182+ payload : {
183+ requestId,
184+ queryKey : config . url ,
185+ // @ts -ignore
186+ queryParams : config . params as any
187+ }
188+ } ) ;
189+ console . log ( "sent message to vscode" )
190+
191+ // Set a timeout to prevent hanging promises
192+ setTimeout ( ( ) => {
193+ window . removeEventListener ( 'message' , messageHandler ) ;
194+ reject ( new Error ( 'Query request timed out' ) ) ;
195+ } , 30000 ) ; // 30 second timeout
196+ } ) ;
197+ }
198+
199+
200+ export function fetchAPI < T = any , B extends object = any > (
201+ config : FetchOptions < B > ,
202+ options ?: Partial < FetchOptionsWithSignal > ,
203+ ) : Promise < T & ResponseWithDetail > {
204+ if ( IS_PRODUCTION ) {
205+ return fetchAPIProduction ( config , options )
206+ }
207+ return fetchAPIDevelopment ( config , options )
208+ }
0 commit comments