@@ -10,12 +10,20 @@ import {
1010}  from  '@sentry/core' ; 
1111import  type  {  AppLoadContext ,  EntryContext ,  RouterContextProvider  }  from  'react-router' ; 
1212
13- type  OriginalHandleRequest  =  ( 
13+ type  OriginalHandleRequestWithoutMiddleware  =  ( 
1414  request : Request , 
1515  responseStatusCode : number , 
1616  responseHeaders : Headers , 
1717  routerContext : EntryContext , 
18-   loadContext : AppLoadContext  |  RouterContextProvider , 
18+   loadContext : AppLoadContext , 
19+ )  =>  Promise < unknown > ; 
20+ 
21+ type  OriginalHandleRequestWithMiddleware  =  ( 
22+   request : Request , 
23+   responseStatusCode : number , 
24+   responseHeaders : Headers , 
25+   routerContext : EntryContext , 
26+   loadContext : RouterContextProvider , 
1927)  =>  Promise < unknown > ; 
2028
2129/** 
@@ -24,7 +32,27 @@ type OriginalHandleRequest = (
2432 * @param  originalHandle - The original handleRequest function to wrap 
2533 * @returns  A wrapped version of the handle request function with Sentry instrumentation 
2634 */ 
27- export  function  wrapSentryHandleRequest ( originalHandle : OriginalHandleRequest ) : OriginalHandleRequest  { 
35+ export  function  wrapSentryHandleRequest ( 
36+   originalHandle : OriginalHandleRequestWithoutMiddleware , 
37+ ) : OriginalHandleRequestWithoutMiddleware ; 
38+ /** 
39+  * Wraps the original handleRequest function to add Sentry instrumentation. 
40+  * 
41+  * @param  originalHandle - The original handleRequest function to wrap 
42+  * @returns  A wrapped version of the handle request function with Sentry instrumentation 
43+  */ 
44+ export  function  wrapSentryHandleRequest ( 
45+   originalHandle : OriginalHandleRequestWithMiddleware , 
46+ ) : OriginalHandleRequestWithMiddleware ; 
47+ /** 
48+  * Wraps the original handleRequest function to add Sentry instrumentation. 
49+  * 
50+  * @param  originalHandle - The original handleRequest function to wrap 
51+  * @returns  A wrapped version of the handle request function with Sentry instrumentation 
52+  */ 
53+ export  function  wrapSentryHandleRequest ( 
54+   originalHandle : OriginalHandleRequestWithoutMiddleware  |  OriginalHandleRequestWithMiddleware , 
55+ ) : OriginalHandleRequestWithoutMiddleware  |  OriginalHandleRequestWithMiddleware  { 
2856  return  async  function  sentryInstrumentedHandleRequest ( 
2957    request : Request , 
3058    responseStatusCode : number , 
@@ -57,10 +85,39 @@ export function wrapSentryHandleRequest(originalHandle: OriginalHandleRequest):
5785    } 
5886
5987    try  { 
60-       return  await  originalHandle ( request ,  responseStatusCode ,  responseHeaders ,  routerContext ,  loadContext ) ; 
88+       // Type guard to call the correct overload based on loadContext type 
89+       if  ( isRouterContextProvider ( loadContext ) )  { 
90+         // loadContext is RouterContextProvider 
91+         return  await  ( originalHandle  as  OriginalHandleRequestWithMiddleware ) ( 
92+           request , 
93+           responseStatusCode , 
94+           responseHeaders , 
95+           routerContext , 
96+           loadContext , 
97+         ) ; 
98+       }  else  { 
99+         // loadContext is AppLoadContext 
100+         return  await  ( originalHandle  as  OriginalHandleRequestWithoutMiddleware ) ( 
101+           request , 
102+           responseStatusCode , 
103+           responseHeaders , 
104+           routerContext , 
105+           loadContext , 
106+         ) ; 
107+       } 
61108    }  finally  { 
62109      await  flushIfServerless ( ) ; 
63110    } 
111+ 
112+     /** 
113+      * Helper type guard to determine if the context is a RouterContextProvider. 
114+      * 
115+      * @param  ctx - The context to check 
116+      * @returns  True if the context is a RouterContextProvider 
117+      */ 
118+     function  isRouterContextProvider ( ctx : AppLoadContext  |  RouterContextProvider ) : ctx  is RouterContextProvider  { 
119+       return  typeof  ( ctx  as  RouterContextProvider ) ?. get  ===  'function' ; 
120+     } 
64121  } ; 
65122} 
66123
0 commit comments