@@ -26,15 +26,33 @@ type OriginalHandleRequestWithMiddleware = (
2626 loadContext : RouterContextProvider ,
2727) => Promise < unknown > ;
2828
29- type OriginalHandleRequest = OriginalHandleRequestWithoutMiddleware | OriginalHandleRequestWithMiddleware ;
30-
3129/**
3230 * Wraps the original handleRequest function to add Sentry instrumentation.
3331 *
3432 * @param originalHandle - The original handleRequest function to wrap
3533 * @returns A wrapped version of the handle request function with Sentry instrumentation
3634 */
37- 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 {
3856 return async function sentryInstrumentedHandleRequest (
3957 request : Request ,
4058 responseStatusCode : number ,
@@ -67,10 +85,39 @@ export function wrapSentryHandleRequest(originalHandle: OriginalHandleRequest):
6785 }
6886
6987 try {
70- 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+ }
71108 } finally {
72109 await flushIfServerless ( ) ;
73110 }
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+ }
74121 } ;
75122}
76123
0 commit comments