1- import { IS_PRODUCTION } from '@/utilsutils/isProduction '
2- import { getVSCodeAPI } from '@/utilsutils/vscodeapi '
1+ import { sendVSCodeMessage } from '@/hooks/vscode '
2+ import { isErr } from '@bus/result '
33
44declare global {
55 interface Window {
@@ -34,22 +34,9 @@ interface FetchOptions<B extends object = any> {
3434 params ?: Record < string , string | number | boolean | undefined | null >
3535}
3636
37- /**
38- * A result is a value that can be either an ok or an error
39- */
40- export type Result < T , E > = { ok : true ; value : T } | { ok : false ; error : E }
41-
42- /**
43- * returns true if the result is an error
44- */
45- export const isErr = < T , E > (
46- result : Result < T , E > ,
47- ) : result is { ok : false ; error : E } => {
48- return ! result . ok
49- }
50-
51- async function fetchAPIDevelopment < T = any , B extends object = any > (
37+ export async function fetchAPI < T = any , B extends object = any > (
5238 config : FetchOptions < B > ,
39+ _options ?: Partial < FetchOptionsWithSignal > ,
5340) : Promise < T & ResponseWithDetail > {
5441 // Generate a unique ID for this request
5542 // Create a promise that will resolve when we get a response with matching ID
@@ -77,73 +64,12 @@ async function fetchAPIDevelopment<T = any, B extends object = any>(
7764 // Add the listener
7865 window . addEventListener ( 'message' , messageHandler )
7966
80- // If we're in an iframe, we need to post the message to the parent window
81- if ( window . parent !== window ) {
82- window . parent . postMessage (
83- {
84- key : 'queryRequest' ,
85- payload : {
86- requestId,
87- url : config . url ,
88- params : config . params as any ,
89- data : config . data ,
90- method : config . method ,
91- } ,
92- } ,
93- '*' ,
94- )
95- return
96- }
97-
98- // Set a timeout to prevent hanging promises
99- setTimeout ( ( ) => {
100- window . removeEventListener ( 'message' , messageHandler )
101- reject ( new Error ( 'Query request timed out' ) )
102- } , 30000 ) // 30 second timeout
103- } )
104- }
105-
106- export async function fetchAPIProduction < T = any , B extends object = any > (
107- config : FetchOptions < B > ,
108- ) : Promise < T & ResponseWithDetail > {
109- // Generate a unique ID for this request
110- const requestId = `query_${ Date . now ( ) } _${ Math . random ( ) . toString ( 36 ) . substr ( 2 , 9 ) } `
111- // Create a promise that will resolve when we get a response with matching ID
112- return new Promise ( ( resolve , reject ) => {
113- // Set up listener for the response
114- const messageHandler = ( event : MessageEvent ) => {
115- if (
116- event . data &&
117- event . data . key === 'query_response' &&
118- event . data . payload &&
119- event . data . payload . requestId === requestId
120- ) {
121- // Remove the listener once we get our response
122- window . removeEventListener ( 'message' , messageHandler )
123-
124- if ( event . data . payload . error ) {
125- reject ( new Error ( event . data . payload . error ) )
126- } else {
127- resolve ( event . data . payload . data )
128- }
129- }
130- }
131-
132- // Add the listener
133- window . addEventListener ( 'message' , messageHandler )
134- console . log ( 'added listener to window' )
135-
136- // Send the message to VSCode with the query parameters and request ID
137- // @ts -ignore
138- const vscode = getVSCodeAPI ( )
139- vscode . postMessage ( {
140- key : 'query_request' ,
141- payload : {
142- requestId,
143- queryKey : config . url ,
144- // @ts -ignore
145- queryParams : config . params as any ,
146- } ,
67+ sendVSCodeMessage ( 'queryRequest' , {
68+ requestId,
69+ url : config . url ,
70+ params : config . params as any ,
71+ data : config . data ,
72+ method : config . method ,
14773 } )
14874
14975 // Set a timeout to prevent hanging promises
@@ -153,13 +79,3 @@ export async function fetchAPIProduction<T = any, B extends object = any>(
15379 } , 30000 ) // 30 second timeout
15480 } )
15581}
156-
157- export function fetchAPI < T = any , B extends object = any > (
158- config : FetchOptions < B > ,
159- _options ?: Partial < FetchOptionsWithSignal > ,
160- ) : Promise < T & ResponseWithDetail > {
161- if ( IS_PRODUCTION ) {
162- return fetchAPIProduction ( config )
163- }
164- return fetchAPIDevelopment ( config )
165- }
0 commit comments