@@ -129,21 +129,47 @@ class CoCreateLazyLoader {
129
129
headers = { ...headers , ...override . headers } ; // Correct idea for merging
130
130
}
131
131
132
- let body = formatRequestBody ( data [ name ] ) ;
132
+ // let body = formatRequestBody(data[name]);
133
133
134
- let options = { method, headers, body, timeout } ;
134
+ let formatType = data . formatType || "json" ;
135
+ const timeout = 10000 ; // Set default timeout in ms (e.g., 10 seconds)
136
+ let options = { method, headers, timeout } ;
135
137
136
- const response = await makeHttpRequest ( url , options ) ;
137
- data [ name ] = parseResponse ( response ) ;
138
+ // Only add body for methods that support it (not GET or HEAD)
139
+ if ( ! [ "GET" , "HEAD" ] . includes ( method ) ) {
140
+ let { body } = this . formatRequestBody ( data [ name ] , formatType ) ;
141
+ options . body = body ;
142
+ }
143
+ // For GET/HEAD, do not create or send a body; all params should be in the URL
144
+
145
+ const response = await this . makeHttpRequest ( url , options ) ;
146
+
147
+ // If the response is not ok, makeHttpRequest will throw and be caught below.
148
+ // If you want to include more info in the error, you can log or attach response details here.
149
+
150
+ data [ name ] = await response . json ( ) ;
138
151
139
152
this . wsManager . send ( data ) ;
140
153
} catch ( error ) {
154
+ // Add more detail to the error for debugging 404s
141
155
data . error = error . message ;
156
+ if ( error . response ) {
157
+ data . status = error . response . status ;
158
+ data . statusText = error . response . statusText ;
159
+ data . responseData = error . response . data ;
160
+ }
142
161
if ( data . req ) {
143
162
data . res . writeHead ( 400 , {
144
- "Content-Type" : "text/plain "
163
+ "Content-Type" : "application/json "
145
164
} ) ;
146
- data . res . end ( `Lazyload Error: ${ error . message } ` ) ;
165
+ data . res . end (
166
+ JSON . stringify ( {
167
+ error : data . error ,
168
+ status : data . status ,
169
+ statusText : data . statusText ,
170
+ responseData : data . responseData
171
+ } )
172
+ ) ;
147
173
}
148
174
if ( data . socket ) {
149
175
this . wsManager . send ( data ) ;
@@ -255,13 +281,12 @@ class CoCreateLazyLoader {
255
281
* @throws {Error } If the request fails or returns a non-ok status.
256
282
*/
257
283
async makeHttpRequest ( url , options ) {
258
- if ( ! this . server . AbortController ) {
259
- console . log ( "makeHttpRequest test" ) ;
260
- return { } ;
284
+ let controller , timeoutId ;
285
+ if ( this . server . AbortController ) {
286
+ controller = new this . server . AbortController ( ) ;
287
+ timeoutId = setTimeout ( ( ) => controller . abort ( ) , options . timeout ) ;
288
+ options . signal = controller . signal ;
261
289
}
262
- const controller = new this . server . AbortController ( ) ;
263
- const timeoutId = setTimeout ( ( ) => controller . abort ( ) , options . timeout ) ;
264
- options . signal = controller . signal ;
265
290
266
291
// Remove Content-Type header if there's no body (relevant for GET, DELETE etc.)
267
292
if (
@@ -272,46 +297,32 @@ class CoCreateLazyLoader {
272
297
delete options . headers [ "Content-Type" ] ;
273
298
}
274
299
300
+ const fetchFn = this . server . fetch || global . fetch ;
301
+ if ( typeof fetchFn !== "function" ) {
302
+ throw new Error ( "No fetch implementation available." ) ;
303
+ }
304
+
275
305
try {
276
- const response = await this . server . fetch ( url , options ) ;
277
- clearTimeout ( timeoutId ) ; // Request finished, clear timeout
306
+ const response = await fetchFn ( url , options ) ;
307
+ if ( timeoutId ) clearTimeout ( timeoutId ) ;
278
308
279
309
if ( ! response . ok ) {
280
- // status >= 200 && status < 300
310
+ const text = await response . text ( ) ;
281
311
const error = new Error (
282
312
`HTTP error! Status: ${ response . status } ${ response . statusText } `
283
313
) ;
284
- // Attach structured response info to the error
285
314
error . response = {
286
315
status : response . status ,
287
316
statusText : response . statusText ,
288
317
headers : Object . fromEntries ( response . headers . entries ( ) ) ,
289
- data : parseResponse ( response ) // Include parsed error body
318
+ data : text
290
319
} ;
291
320
throw error ;
292
321
}
293
322
return response ;
294
323
} catch ( error ) {
295
- clearTimeout ( timeoutId ) ;
296
- if ( error . name === "AbortError" ) {
297
- console . error (
298
- `Request timed out after ${ options . timeout } ms: ${ options . method } ${ url } `
299
- ) ;
300
- throw new Error (
301
- `Request Timeout: API call exceeded ${ options . timeout } ms`
302
- ) ;
303
- }
304
-
305
- // If it already has response info (from !response.ok), rethrow it
306
- if ( error . response ) {
307
- throw error ;
308
- }
309
- // Otherwise, wrap other errors (network, DNS, etc.)
310
- console . error (
311
- `Network/Request Error: ${ options . method } ${ url } ` ,
312
- error
313
- ) ;
314
- throw new Error ( `Network/Request Error: ${ error . message } ` ) ;
324
+ if ( timeoutId ) clearTimeout ( timeoutId ) ;
325
+ throw error ;
315
326
}
316
327
}
317
328
0 commit comments