Skip to content

Commit 8fd812d

Browse files
authored
Merge branch 'develop' into genai-msg-limit
2 parents e03f0b8 + 9b584f2 commit 8fd812d

File tree

2 files changed

+80
-12
lines changed

2 files changed

+80
-12
lines changed

packages/react-router/src/server/createSentryHandleRequest.tsx

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,22 @@ export interface SentryHandleRequestOptions {
5353
botRegex?: RegExp;
5454
}
5555

56+
type HandleRequestWithoutMiddleware = (
57+
request: Request,
58+
responseStatusCode: number,
59+
responseHeaders: Headers,
60+
routerContext: EntryContext,
61+
loadContext: AppLoadContext,
62+
) => Promise<unknown>;
63+
64+
type HandleRequestWithMiddleware = (
65+
request: Request,
66+
responseStatusCode: number,
67+
responseHeaders: Headers,
68+
routerContext: EntryContext,
69+
loadContext: RouterContextProvider,
70+
) => Promise<unknown>;
71+
5672
/**
5773
* A complete Sentry-instrumented handleRequest implementation that handles both
5874
* route parametrization and trace meta tag injection.
@@ -62,13 +78,7 @@ export interface SentryHandleRequestOptions {
6278
*/
6379
export function createSentryHandleRequest(
6480
options: SentryHandleRequestOptions,
65-
): (
66-
request: Request,
67-
responseStatusCode: number,
68-
responseHeaders: Headers,
69-
routerContext: EntryContext,
70-
loadContext: AppLoadContext | RouterContextProvider,
71-
) => Promise<unknown> {
81+
): HandleRequestWithoutMiddleware & HandleRequestWithMiddleware {
7282
const {
7383
streamTimeout = 10000,
7484
renderToPipeableStream,
@@ -135,5 +145,6 @@ export function createSentryHandleRequest(
135145
};
136146

137147
// Wrap the handle request function for request parametrization
138-
return wrapSentryHandleRequest(handleRequest);
148+
return wrapSentryHandleRequest(handleRequest as HandleRequestWithoutMiddleware) as HandleRequestWithoutMiddleware &
149+
HandleRequestWithMiddleware;
139150
}

packages/react-router/src/server/wrapSentryHandleRequest.ts

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,20 @@ import {
1010
} from '@sentry/core';
1111
import 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

Comments
 (0)