@@ -78,7 +78,7 @@ export function createLoaderApiRoute<
78
78
const authenticationResult = await authenticateApiRequest ( request , { allowJWT } ) ;
79
79
80
80
if ( ! authenticationResult ) {
81
- return wrapResponse (
81
+ return await wrapResponse (
82
82
request ,
83
83
json ( { error : "Invalid or Missing API key" } , { status : 401 } ) ,
84
84
corsStrategy !== "none"
@@ -89,7 +89,7 @@ export function createLoaderApiRoute<
89
89
if ( paramsSchema ) {
90
90
const parsed = paramsSchema . safeParse ( params ) ;
91
91
if ( ! parsed . success ) {
92
- return wrapResponse (
92
+ return await wrapResponse (
93
93
request ,
94
94
json (
95
95
{ error : "Params Error" , details : fromZodError ( parsed . error ) . details } ,
@@ -106,7 +106,7 @@ export function createLoaderApiRoute<
106
106
const searchParams = Object . fromEntries ( new URL ( request . url ) . searchParams ) ;
107
107
const parsed = searchParamsSchema . safeParse ( searchParams ) ;
108
108
if ( ! parsed . success ) {
109
- return wrapResponse (
109
+ return await wrapResponse (
110
110
request ,
111
111
json (
112
112
{ error : "Query Error" , details : fromZodError ( parsed . error ) . details } ,
@@ -123,7 +123,7 @@ export function createLoaderApiRoute<
123
123
const rawHeaders = Object . fromEntries ( request . headers ) ;
124
124
const headers = headersSchema . safeParse ( rawHeaders ) ;
125
125
if ( ! headers . success ) {
126
- return wrapResponse (
126
+ return await wrapResponse (
127
127
request ,
128
128
json (
129
129
{ error : "Headers Error" , details : fromZodError ( headers . error ) . details } ,
@@ -154,7 +154,7 @@ export function createLoaderApiRoute<
154
154
) ;
155
155
156
156
if ( ! authorizationResult . authorized ) {
157
- return wrapResponse (
157
+ return await wrapResponse (
158
158
request ,
159
159
json (
160
160
{
@@ -177,16 +177,22 @@ export function createLoaderApiRoute<
177
177
authentication : authenticationResult ,
178
178
request,
179
179
} ) ;
180
- return wrapResponse ( request , result , corsStrategy !== "none" ) ;
180
+ return await wrapResponse ( request , result , corsStrategy !== "none" ) ;
181
181
} catch ( error ) {
182
- if ( error instanceof Response ) {
183
- return wrapResponse ( request , error , corsStrategy !== "none" ) ;
182
+ try {
183
+ if ( error instanceof Response ) {
184
+ return await wrapResponse ( request , error , corsStrategy !== "none" ) ;
185
+ }
186
+ return await wrapResponse (
187
+ request ,
188
+ json ( { error : "Internal Server Error" } , { status : 500 } ) ,
189
+ corsStrategy !== "none"
190
+ ) ;
191
+ } catch ( innerError ) {
192
+ logger . error ( "[apiBuilder] Failed to handle error" , { error, innerError } ) ;
193
+
194
+ return json ( { error : "Internal Server Error" } , { status : 500 } ) ;
184
195
}
185
- return wrapResponse (
186
- request ,
187
- json ( { error : "Internal Server Error" } , { status : 500 } ) ,
188
- corsStrategy !== "none"
189
- ) ;
190
196
}
191
197
} ;
192
198
}
@@ -240,7 +246,7 @@ export function createLoaderPATApiRoute<
240
246
const authenticationResult = await authenticateApiRequestWithPersonalAccessToken ( request ) ;
241
247
242
248
if ( ! authenticationResult ) {
243
- return wrapResponse (
249
+ return await wrapResponse (
244
250
request ,
245
251
json ( { error : "Invalid or Missing API key" } , { status : 401 } ) ,
246
252
corsStrategy !== "none"
@@ -251,7 +257,7 @@ export function createLoaderPATApiRoute<
251
257
if ( paramsSchema ) {
252
258
const parsed = paramsSchema . safeParse ( params ) ;
253
259
if ( ! parsed . success ) {
254
- return wrapResponse (
260
+ return await wrapResponse (
255
261
request ,
256
262
json (
257
263
{ error : "Params Error" , details : fromZodError ( parsed . error ) . details } ,
@@ -268,7 +274,7 @@ export function createLoaderPATApiRoute<
268
274
const searchParams = Object . fromEntries ( new URL ( request . url ) . searchParams ) ;
269
275
const parsed = searchParamsSchema . safeParse ( searchParams ) ;
270
276
if ( ! parsed . success ) {
271
- return wrapResponse (
277
+ return await wrapResponse (
272
278
request ,
273
279
json (
274
280
{ error : "Query Error" , details : fromZodError ( parsed . error ) . details } ,
@@ -285,7 +291,7 @@ export function createLoaderPATApiRoute<
285
291
const rawHeaders = Object . fromEntries ( request . headers ) ;
286
292
const headers = headersSchema . safeParse ( rawHeaders ) ;
287
293
if ( ! headers . success ) {
288
- return wrapResponse (
294
+ return await wrapResponse (
289
295
request ,
290
296
json (
291
297
{ error : "Headers Error" , details : fromZodError ( headers . error ) . details } ,
@@ -304,17 +310,22 @@ export function createLoaderPATApiRoute<
304
310
authentication : authenticationResult ,
305
311
request,
306
312
} ) ;
307
- return wrapResponse ( request , result , corsStrategy !== "none" ) ;
313
+ return await wrapResponse ( request , result , corsStrategy !== "none" ) ;
308
314
} catch ( error ) {
309
- console . error ( "Error in API route:" , error ) ;
310
- if ( error instanceof Response ) {
311
- return wrapResponse ( request , error , corsStrategy !== "none" ) ;
315
+ try {
316
+ if ( error instanceof Response ) {
317
+ return await wrapResponse ( request , error , corsStrategy !== "none" ) ;
318
+ }
319
+ return await wrapResponse (
320
+ request ,
321
+ json ( { error : "Internal Server Error" } , { status : 500 } ) ,
322
+ corsStrategy !== "none"
323
+ ) ;
324
+ } catch ( innerError ) {
325
+ logger . error ( "[apiBuilder] Failed to handle error" , { error, innerError } ) ;
326
+
327
+ return json ( { error : "Internal Server Error" } , { status : 500 } ) ;
312
328
}
313
- return wrapResponse (
314
- request ,
315
- json ( { error : "Internal Server Error" } , { status : 500 } ) ,
316
- corsStrategy !== "none"
317
- ) ;
318
329
}
319
330
} ;
320
331
}
@@ -388,7 +399,7 @@ export function createActionApiRoute<
388
399
const authenticationResult = await authenticateApiRequest ( request , { allowJWT } ) ;
389
400
390
401
if ( ! authenticationResult ) {
391
- return wrapResponse (
402
+ return await wrapResponse (
392
403
request ,
393
404
json ( { error : "Invalid or Missing API key" } , { status : 401 } ) ,
394
405
corsStrategy !== "none"
@@ -407,7 +418,7 @@ export function createActionApiRoute<
407
418
if ( paramsSchema ) {
408
419
const parsed = paramsSchema . safeParse ( params ) ;
409
420
if ( ! parsed . success ) {
410
- return wrapResponse (
421
+ return await wrapResponse (
411
422
request ,
412
423
json (
413
424
{ error : "Params Error" , details : fromZodError ( parsed . error ) . details } ,
@@ -424,7 +435,7 @@ export function createActionApiRoute<
424
435
const searchParams = Object . fromEntries ( new URL ( request . url ) . searchParams ) ;
425
436
const parsed = searchParamsSchema . safeParse ( searchParams ) ;
426
437
if ( ! parsed . success ) {
427
- return wrapResponse (
438
+ return await wrapResponse (
428
439
request ,
429
440
json (
430
441
{ error : "Query Error" , details : fromZodError ( parsed . error ) . details } ,
@@ -441,7 +452,7 @@ export function createActionApiRoute<
441
452
const rawHeaders = Object . fromEntries ( request . headers ) ;
442
453
const headers = headersSchema . safeParse ( rawHeaders ) ;
443
454
if ( ! headers . success ) {
444
- return wrapResponse (
455
+ return await wrapResponse (
445
456
request ,
446
457
json (
447
458
{ error : "Headers Error" , details : fromZodError ( headers . error ) . details } ,
@@ -457,7 +468,7 @@ export function createActionApiRoute<
457
468
if ( bodySchema ) {
458
469
const rawBody = await request . text ( ) ;
459
470
if ( rawBody . length === 0 ) {
460
- return wrapResponse (
471
+ return await wrapResponse (
461
472
request ,
462
473
json ( { error : "Request body is empty" } , { status : 400 } ) ,
463
474
corsStrategy !== "none"
@@ -467,7 +478,7 @@ export function createActionApiRoute<
467
478
const rawParsedJson = safeJsonParse ( rawBody ) ;
468
479
469
480
if ( ! rawParsedJson ) {
470
- return wrapResponse (
481
+ return await wrapResponse (
471
482
request ,
472
483
json ( { error : "Invalid JSON" } , { status : 400 } ) ,
473
484
corsStrategy !== "none"
@@ -476,7 +487,7 @@ export function createActionApiRoute<
476
487
477
488
const body = bodySchema . safeParse ( rawParsedJson ) ;
478
489
if ( ! body . success ) {
479
- return wrapResponse (
490
+ return await wrapResponse (
480
491
request ,
481
492
json ( { error : fromZodError ( body . error ) . toString ( ) } , { status : 400 } ) ,
482
493
corsStrategy !== "none"
@@ -497,7 +508,7 @@ export function createActionApiRoute<
497
508
} ) ;
498
509
499
510
if ( ! checkAuthorization ( authenticationResult , action , $resource , superScopes ) ) {
500
- return wrapResponse (
511
+ return await wrapResponse (
501
512
request ,
502
513
json ( { error : "Unauthorized" } , { status : 403 } ) ,
503
514
corsStrategy !== "none"
@@ -513,24 +524,36 @@ export function createActionApiRoute<
513
524
authentication : authenticationResult ,
514
525
request,
515
526
} ) ;
516
- return wrapResponse ( request , result , corsStrategy !== "none" ) ;
527
+ return await wrapResponse ( request , result , corsStrategy !== "none" ) ;
517
528
} catch ( error ) {
518
- if ( error instanceof Response ) {
519
- return wrapResponse ( request , error , corsStrategy !== "none" ) ;
529
+ try {
530
+ if ( error instanceof Response ) {
531
+ return await wrapResponse ( request , error , corsStrategy !== "none" ) ;
532
+ }
533
+ return await wrapResponse (
534
+ request ,
535
+ json ( { error : "Internal Server Error" } , { status : 500 } ) ,
536
+ corsStrategy !== "none"
537
+ ) ;
538
+ } catch ( innerError ) {
539
+ logger . error ( "[apiBuilder] Failed to handle error" , { error, innerError } ) ;
540
+
541
+ return json ( { error : "Internal Server Error" } , { status : 500 } ) ;
520
542
}
521
- return wrapResponse (
522
- request ,
523
- json ( { error : "Internal Server Error" } , { status : 500 } ) ,
524
- corsStrategy !== "none"
525
- ) ;
526
543
}
527
544
}
528
545
529
546
return { loader, action } ;
530
547
}
531
548
532
- function wrapResponse ( request : Request , response : Response , useCors : boolean ) {
549
+ async function wrapResponse (
550
+ request : Request ,
551
+ response : Response ,
552
+ useCors : boolean
553
+ ) : Promise < Response > {
533
554
return useCors
534
- ? apiCors ( request , response , { exposedHeaders : [ "x-trigger-jwt" , "x-trigger-jwt-claims" ] } )
555
+ ? await apiCors ( request , response , {
556
+ exposedHeaders : [ "x-trigger-jwt" , "x-trigger-jwt-claims" ] ,
557
+ } )
535
558
: response ;
536
559
}
0 commit comments